Compile and deploy smart contracts
In the following sections we focus on the example token contract included in the defi example contract archive. The contract utilizes several functions. The main functions are initialize, and transfer that allow you to perform the basic operations needed for a transfer. After deployment the contract actions can be called from the dashboard. When you perform an action it changes the contract state. If you inspect the contract you can see the serialized data showing the contract state.
If you want a developer environment without having to install the compiler yourself you can head over to our dApp playground to immediately start building your own contracts. The Dapp Playground uses the Testnet by default.
1) Compile a contract example
The token contract can be found in the example contract archive The following will compile it and generate an ABI for it:
cargo pbc build --release
Now you will find a .wasm-file and a .abi-file in:
/target/wasm32-unknown-unknown/release
.
2) Upload the contract to the blockchain
To deploy a smart contract you need an account, for a private deployment you can choose any key, since there is no gas, in a private deployment.
To deploy a contract you can use the browser that is connected to your private deployment.
Select the token.wasm
and the token.abi
.
The dashboard will then render a form for the initialization function. If you look at lib.rs
file
in your IDE, you will see that this matches the initialize function.
The other three actions will be available after successful deployment.
You can give your token contract any name and symbol to reflect the token you are creating a mint for. In the total_supply field you put the number of tokens you want minted for total supply of the contract from the moment of deployment. The decimals field indicates placement of decimal point in total supply. E.g. total supply: 1050 decimals: 3 will mint supply of 1.050 token.
It should look like this before deployment:
Successful deployment will look like this:
You are now ready to interact with the contract. You can now click Interact in the browser and start using the actions like mint and transfer of your tokens.
MPC contract compilation
The MPC-contracts consist of two main parts. The contract itself as well as a Multi Party Computation, (MPC). To compile a MPC contract run:
cargo pbc build --release
Note: This is the same command as for smart contracts.
The cargo pbc
command
looks for a path to the code defining the MPC in the manifest file. The path to the file
can be defined under package.metadata.zk
in the Cargo.toml
.
The cargo pbc
command compiles the contract as a MPC-contract instead of a normal smart contract.
The cargo pbc
command first compiles the on-chain part of the contract to get a WASM- and
ABI-file.
Afterwards it fetches the zk-compiler and compiles the zk-computation and links it with the
generated WASM-file to get a ZKWA-file.