Truffle QuickStart

This page will take you through the basics of creating a alaya truffle project and deploying a smart contract to a blockchain.

Creating a project

To use most alaya truffle commands, you need to run them against an existing alaya truffle project. So the first step is to create a alaya truffle project.

You can create a bare project template, but for those just getting started, you can use alaya-truffle Boxes, which are example applications and project templates. We’ll use the MetaCoin box, which creates a token that can be transferred between accounts:

  1. Create a new directory for your alaya truffle project:

    mkdir MetaCoin
    cd MetaCoin
    
  2. Download (“unbox”) the MetaCoin box:

    alaya-truffle unbox metacoin
    

Note

You can use the alaya-truffle unbox <box-name> command to download any of the other alaya truffle Boxes.

Note

To create a bare alaya truffle project with no smart contracts included, use alaya-truffle init.

Once this operation is completed, you’ll now have a project structure with the following items:

Exploring the project

Note

This page is just a quickstart, so we’re not going to go into much detail here. Please see the rest of the alaya truffle documentation to learn more.

  1. Open the contracts/MetaCoin.sol file in a text editor. This is a smart contract (written in Solidity) that creates a MetaCoin token. Note that this also references another Solidity file contracts/ConvertLib.sol in the same directory.
  2. Open the contracts/Migrations.sol file. This is a separate Solidity file that manages and updates the status of your deployed smart contract. This file comes with every alaya truffle project, and is usually not edited.
  3. Open the migrations/1_initial_migration.js file. This file is the migration (deployment) script for the Migrations contract found in the Migrations.sol file.
  4. Open the migrations/2_deploy_contracts.js file. This file is the migration script for the MetaCoin contract. (Migration scripts are run in order, so the file beginning with 2 will be run after the file beginning with 1.)
  5. Open the test/TestMetacoin.sol file. This is a test file written in Solidity which ensures that your contract is working as expected.
  6. Open the test/metacoin.js file. This is a test file written in JavaScript which performs a similar function to the Solidity test above.
  7. Open the truffle-config.js file. This is the alaya truffle configuration file, for setting network information and other project-related settings. The file is blank, but this is okay, as we’ll be using a alaya truffle command that has some defaults built-in.

Testing

  1. On a terminal, run the Solidity test:

    alaya-truffle test ./test/TestMetacoin.sol
    

    You will see the following output

     TestMetacoin
       √ testInitialBalanceUsingDeployedContract (71ms)
       √ testInitialBalanceWithNewMetaCoin (59ms)
    
     2 passing (794ms)
    

Note

If you’re on Windows and encountering problems running this command, please see the documentation on resolving naming conflicts on Windows.

These tree tests were run against the contract, with descriptions displayed on what the tests are supposed to do.

  1. Run the JavaScript test:

    alaya-truffle test ./test/metacoin.js
    

    You will see the following output

      Contract: MetaCoin
        √ should put 10000 MetaCoin in the first account
        √ should call a function that depends on a linked library (40ms)
        √ should send coin correctly (129ms)
    
      3 passing (255ms)
    

Compiling

  1. Compile the smart contracts:

    alaya-truffle compile
    

    You will see the following output:

    Compiling .\contracts\ConvertLib.sol...
    Compiling .\contracts\MetaCoin.sol...
    Compiling .\contracts\Migrations.sol...
    
    Writing artifacts to .\build\contracts
    

Migrating

To deploy our smart contracts, we’re going to need to connect to a blockchain. alaya truffle has a built-in personal blockchain that can be used for testing. This blockchain is local to your system and does not interact with the main PlatON network.

You can create this blockchain and interact with it using scripts/node/start.sh shell scripts.

  1. Run alaya truffle migrate:

    alaya-truffle migrate
    

我们可以看到下面的信息:

Starting migrations...
======================
> Network name:    'develop'
> Network id:      4447
> Block gas limit: 6721975

1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x3fd222279dad48583a3320decd0a2d12e82e728ba9a0f19bdaaff98c72a030a2
   > Blocks: 0            Seconds: 0
   > contract address:    atp1gsddntg6uyk0ykvzy3zv5rp5nx5krnudmn9qav
   > account:             atp1x6s4e7kvyqhdqk7lw3095h9xargxxd4hdse737
   > balance:             99.99445076
   > gas used:            277462
   > gas price:           20 gvon
   > value sent:          0 ATP
   > total cost:          0.00554924 ATP

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00554924 ATP

2_deploy_contracts.js
=====================

   Deploying 'ConvertLib'
   ----------------------
   > transaction hash:    0x97e8168f1c05fc40dd8ffc529b9a2bf45cc7c55b07b6b9a5a22173235ee247b6
   > Blocks: 0            Seconds: 0
   > contract address:    atp13yv6jtgdqjyuqfl55e5470hm358hw7vfv8w48f
   > account:             atp1x6s4e7kvyqhdqk7lw3095h9xargxxd4hdse737
   > balance:             99.9914458
   > gas used:            108240
   > gas price:           20 gvon
   > value sent:          0 ATP
   > total cost:          0.0021648 ATP

   Linking
   -------
   * Contract: MetaCoin <--> Library: ConvertLib (at address: atp1c5aajt5zkyzughfsdm2l7fvl4untv2hvlhpkzl)

   Deploying 'MetaCoin'
   --------------------
   > transaction hash:    0xee4994097c10e7314cc83adf899d67f51f22e08b920e95b6d3f75c5eb498bde4
   > Blocks: 0            Seconds: 0
   > contract address:    atp1c5aajt5zkyzughfsdm2l7fvl4untv2hvlhpkzl
   > account:             atp1x6s4e7kvyqhdqk7lw3095h9xargxxd4hdse737
   > balance:             99.98449716
   > gas used:            347432
   > gas price:           20 gvon
   > value sent:          0 ATP
   > total cost:          0.00694864 ATP

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00911344 ATP

Summary
=======
> Total deployments:   3
> Final cost:          0.01466268 ATP

This shows the transaction IDs and addresses of your deployed contracts. It also includes a cost summary and real-time status updates.

Note

Your transaction hashes, contract addresses, and accounts will be different from the above.

Note

To see how to interact with the contract, please skip to the next section.

Interacting with the contract

To interact with the contract, you can use the alaya truffle console. The alaya truffle console is similar to alaya truffle Develop, except it connects to an existing blockchain.

alaya-truffle console

You will see the following prompt:

truffle(development)>

Note

Console prompt: truffle (development)> The brackets refer to the currently connected network.

Interact with the contract using the console in the following ways:

As of alaya truffle v5, the console supports async/await functions, enabling much simpler interactions with the contract.

  • Begin by establishing both the deployed MetaCoin contract instance and the accounts created by either alaya truffle’s built-in blockchain:

    truffle(development)> let instance = await MetaCoin.deployed()
    truffle(development)> let accounts = await web3.platon.getAccounts()
    
  • Check the metacoin balance of the account that deployed the contract:

    truffle(development)> let balance = await instance.getBalance(accounts[0])
    truffle(development)> balance.toNumber()
    
  • Transfer some metacoin from one account to another:

    truffle(development)> instance.sendCoin(accounts[1], 500)
    
  • Check the balance of the account that received the metacoin:

    truffle(development)> let received = await instance.getBalance(accounts[1])
    truffle(development)> received.toNumber()
    
  • Check the balance of the account that sent the metacoin:

    truffle(development)> let newBalance = await instance.getBalance(accounts[0])
    truffle(development)> newBalance.toNumber()
    

Continue learning

This quickstart showed you the basics of the alaya truffle project lifecycle, but there is much more to learn. Please continue on with the rest of our documentation and especially our tutorials to learn more.