How to Compile and Deploy an ERC20 Contract on Ewasm
Second State provides open source infrastructure technologies for public and enterprise blockchains. The smart contract compiler toolchain and virtual machine runtimes are critical elements of the infrastructure.
Through its web-based BUIDL IDE, developers can easily access Second State smart contract programming languages, compilers, optimizers, and virtual machines. Even novice developers can write and publish decentralized applications (smart contract + web app) on any Ethereum-compatible blockchain in minutes. See examples on Ethereum Classic and CyberMiles blockchains.
What is SOLL?
During the 2019 Ethereum Foundation Devcon5 in Osaka, Japan, Second State demonstrated the alpha release of its SOLL compiler project. It is the world’s first LLVM-based toolchain that can compile Solidity smart contracts into WebAssembly bytecode and successfully deploy onto the official Ewasm (Ethereum flavored WebAssembly) testnet.
With LLVM support, SOLL could not only support multiple smart contract programming languages, such as Rust and C++, but also support various VMs, such as Ewasm and the EVM 1.x.
The SOLL Project is the bridge of general developers and the blockchain world. We invite all developers to try out the SOLL+Ewasm toolchain.
Talk is cheap. Show me the code
Next, we will take an ERC20 contract as an example. We will compile and deploy an ERC20 contract using the SOLL toolchain and deploy on the Ethereum Foundation Ewasm testnet. Let’s get started!
Connect MetaMask to ewasm testnet
Environment
$ docker pull secondstate/soll:demo
$ docker run -it --rm secondstate/soll:demo
Prepare Contract
# vim contract.sol
contract Token {
uint256 totalSupply;
mapping(address => uint256) balances;event Transfer(address indexed _from, address indexed _to, uint256 _value);constructor() {
totalSupply = 100000000;
balances[msg.sender] = totalSupply;
}function balanceOf(address account) view public returns (uint256) {
return balances[account];
}function transfer(address to, uint256 amount) public returns (bool) {
balances[msg.sender] = balances[msg.sender] - amount;
balances[to] = balances[to] + amount;
emit Transfer(msg.sender, to, amount);
return true;
}
}
Compile Contract
- Get ABI
# soll -action=EmitABI contract.sol [{“inputs”:[],”payable”:false,”stateMutability”:”nonpayable”,”type”:”constructor”},{“constant”:true,”inputs”:[{“name”:”account”,”type”:”address”}],”name”:”balanceOf”,”outputs”:[{“name”:””,”type”:”uint256"}],”payable”:false,”stateMutability”:”view”,”type”:”function”},{“constant”:false,”inputs”:[{“name”:”to”,”type”:”address”},{“name”:”amount”,”type”:”uint256"}],”name”:”transfer”,”outputs”:[{“name”:””,”type”:”bool”}],”payable”:false,”stateMutability”:”nonpayable”,”type”:”function”},{“anonymous”:false,”inputs”:[{“indexed”:true,”name”:”_from”,”type”:”address”},{“indexed”:true,”name”:”_to”,”type”:”address”},
- Get data
# soll contract.sol > contract.ll
# soll/utils/compile contract.ll# ls -l contract*.wasm
-rwxr-xr-x 1 root root 1610 Oct 1 08:53 contract.deploy.wasm
-rwxr-xr-x 1 root root 1483 Oct 1 08:53 contract.wasm
```* `*.deploy.wasm `is just like `--bin` and `*.wasm` is `--bin-runtime` in EVM world.
Get hex data about `contract.deploy.wasm`# xxd -p contract.deploy.wasm | tr -d $'\n'
0061736d0100000001270760027f7f0060017f006000017e60047e7f7f7f017f60037f7f7f0060000060057f7e7e7e7e00029d010708657468657265756d0c73746f7261676553746f7265000008657468657265756d0967657443616c6c6572000108657468657265756d0a6765744761734c656674000208657468657265756d0a63616c6c537461746963000308657468657265756d0e72657475726e44617461436f7079000408657468657265756d0b73746f726167654c6f6164000008657468657265756d0666696e69736800000304030506050405017001010105030100020615037f014180a3040b7f004180a3040b7f0041fe220b071102066d656d6f72790200046d61696e00090aa8080302000b8d030020002004423888200442388684200442288642808080808080c0ff0083842004421886428080808080e03f838420044208864280808080f01f8384200442088842808080f80f83842004421888428080fc07838420044228884280fe038384370300200041186a2001423886200142288642808080808080c0ff0083842001421886428080808080e03f838420014208864280808080f01f8384200142088842808080f80f83842001421888428080fc07838420014228884280fe03838420014238888437030020002002423886200242288642808080808080c0ff0083842002421886428080808080e03f838420024208864280808080f01f8384200242088842808080f80f83842002421888428080fc07838420024228884280fe03838420024238888437031020002003423886200342288642808080808080c0ff0083842003421886428080808080e03f838420034208864280808080f01f8384200342088842808080f80f83842003421888428080fc07838420034228884280fe0383842003423888843703080b930506017f017e037f017e017f027e230041f0026b22002400200041f0016a41186a4280808080d0a0fdf00037030020004190026a41186a42003703002000420037038002200042003703f801200042003703f001200042003703a0022000420037039802200042003703900220004190026a200041f0016a1000200041d8016a1001200041c0006a420020002903d80122014220862001422088200041d8016a41086a290300220142208684200041d8016a41106a3502004220862001422088841008200041a0016a41186a4200370300200041c8016a200041c0006a41086a290300370300200041d0016a200041c0006a41106a2903003e0200200042003703b001200042003703a801200042013703a001200020002903403703c00110022101200041d0026a41186a22024200370300200042808080103703e002200042003703d802200042003703d0022001200041d0026a200041a0016a413410031a200041b0026a410041201004200041b0026a41086a22032903002101200041b0026a41106a22042903002105200041b0026a41186a2206290300210720002903b002210820024200370300200042003703e002200042003703d802200042003703d002200041d0026a200041b0026a1005200041206a20002903b002200329030020042903002006290300100820004180016a41186a200737030020002005370390012000200137038801200020083703800120002000290320200041206a41086a290300200041206a41106a290300200041206a41186a2903001008200041e0006a41186a200041186a2903003703002000200041106a2903003703702000200041086a2903003703682000200029030037036020004180016a200041e0006a100041800841fe1a1006200041f0026a24000b0b861b01004180080bfe1a0061736d0100000001350960027f7f0060017f006000017e60047e7f7f7f017f60037f7f7f006000017f60077f7f7f7f7f7f7f0060000060057f7e7e7e7e0002f1010b08657468657265756d0c73746f7261676553746f7265000008657468657265756d0967657443616c6c6572000108657468657265756d0a6765744761734c656674000208657468657265756d0a63616c6c537461746963000308657468657265756d0e72657475726e44617461436f7079000408657468657265756d0b73746f726167654c6f6164000008657468657265756d0666696e697368000008657468657265756d0f67657443616c6c4461746153697a65000508657468657265756d0c63616c6c44617461436f7079000408657468657265756d036c6f67000608657468657265756d0672657665727400000304030708070405017001010105030100020615037f0141a088040b7f0041a088040b7f0041a0080b071102066d656d6f72790200046d61696e000d0ae3170302000b8d030020002004423888200442388684200442288642808080808080c0ff0083842004421886428080808080e03f838420044208864280808080f01f8384200442088842808080f80f83842004421888428080fc07838420044228884280fe038384370300200041186a2001423886200142288642808080808080c0ff0083842001421886428080808080e03f838420014208864280808080f01f8384200142088842808080f80f83842001421888428080fc07838420014228884280fe03838420014238888437030020002002423886200242288642808080808080c0ff0083842002421886428080808080e03f838420024208864280808080f01f8384200242088842808080f80f83842002421888428080fc07838420024228884280fe03838420024238888437031020002003423886200342288642808080808080c0ff0083842003421886428080808080e03f838420034208864280808080f01f8384200342088842808080f80f83842003421888428080fc07838420034228884280fe0383842003423888843703080bce1403057f0c7e027f230041f0086b2200210120002400200021020240100741034d0d00200041706a2200220324002000410041041008024002402000280200220041a98bf0dc7b460d00200041f0c08a8c03470d022003220041606a2203220424002003410441201008200141c8006a2003290300200041686a290300200041706a290300200041786a290300100c200141c8006a41106a29030021052001290348210620014190046a200141c8006a41086a29030037030020014198046a20053e0200200141e8036a41186a42003703002001200637038804200142003703f803200142003703f003200142013703e80310022105200141e8076a41186a22004200370300200142808080103703f807200142003703f007200142003703e8072005200141e8076a200141e8036a413410031a200141b0066a410041201004200141b0066a200141e8076a1005200141286a20012903e807200141e8076a41086a290300200141e8076a41106a2903002000290300100c200141086a2001290328200141286a41086a290300200141286a41106a290300200141286a41186a290300100c200141086a41086a2903002105200141086a41106a2903002106200141086a41186a2903002107200129030821082004220041606a22032400200041786a2007370300200041706a2006370300200041686a2005370300200320083703002003412010060c010b2003220041406a2203220424002003410441c0001008200141c8036a2003290300200041486a290300200041506a290300200041586a290300100c200141c8036a41106a2903002108200141c8036a41086a290300210920012903c803210a200141a8036a200041606a290300200041686a290300200041706a290300200041786a290300100c200141a8036a41186a290300210b200141a8036a41106a2903002106200141a8036a41086a290300210520012903a8032107200141b8086a100120014188036a420020012903b808220c422086200c422088200141b8086a41086a290300220c42208684200141b8086a41106a350200422086200c42208884100c200141e8036a41186a4200370300200141e8036a41286a20014188036a41086a290300370300200141e8036a41306a20014188036a41106a2903003e0200200142003703f803200142003703f003200142013703e8032001200129038803370388041002210c200141e8076a41186a22034200370300200142808080103703f807200142003703f007200142003703e807200c200141e8076a200141e8036a413410031a200141b0066a410041201004200141b0066a41086a290300210c200141b0066a41106a290300210d200141b0066a41186a2200290300210e20012903b006210f200141a0086a1001200141e8026a420020012903a00822104220862010422088200141a0086a41086a290300221042208684200141a0086a41106a350200422086201042208884100c20034200370300200141e8076a41286a200141e8026a41086a290300370300200141e8076a41306a200141e8026a41106a2903003e0200200142003703f807200142003703f007200142013703e807200120012903e802370388081002211020004200370300200142808080103703c006200142003703b806200142003703b0062010200141b0066a200141e8076a413410031a200141c8076a410041201004200141c8076a200141a8076a1005200141c8026a20012903a807200141a8076a41086a290300200141a8076a41106a290300200141a8076a41186a290300100c20014188076a41186a200e3703002001200d370398072001200c370390072001200f37038807200141a8026a20012903c802220d20077d200141c8026a41086a290300220c20057d200d2007542203ad7d200141c8026a41106a290300220d20067d220e2003200c200554200c2005511bad220c7d200141c8026a41186a290300200b7d200d200654ad7d200e200c54ad7d100c200141e8066a41186a200141a8026a41186a2903003703002001200141a8026a41106a2903003703f8062001200141a8026a41086a2903003703f006200120012903a8023703e80620014188076a200141e8066a1000200141b0066a41286a2009370300200141b0066a41306a20083e0200200042003703002001200a3703d006200142003703c006200142003703b806200142013703b0061002210c200141f8056a41186a2203420037030020014280808010370388062001420037038006200142003703f805200c200141f8056a200141b0066a413410031a200141d0086a410041201004200141d0086a41086a2211290300210c200141d0086a41106a2212290300210d200141d0086a41186a2200290300210e20012903d008210f200141f8056a41286a2009370300200141f8056a41306a20083e0200200342003703002001200a3703980620014200370388062001420037038006200142013703f8051002211020004200370300200142808080103703e008200142003703d808200142003703d0082010200141d0086a200141f8056a413410031a200141d8056a410041201004200141d8056a200141d0086a100520014188026a20012903d008201129030020122903002000290300100c200141b8056a41186a200e3703002001200d3703c8052001200c3703c0052001200f3703b805200141e8016a2007200129038802220d7c220e200520014188026a41086a290300220c7c200e200d542200ad7c220d200620014188026a41106a290300220f7c220e2000200d200c54200d200c511bad7c220c200b20014188026a41186a2903007c200e200f54ad7c200c200e54ad7c100c20014198056a41186a200141e8016a41186a2903003703002001200141e8016a41106a2903003703a8052001200141e8016a41086a2903003703a005200120012903e80137039805200141b8056a20014198056a100020014180056a1001200141c8016a4200200129038005220c422086200c42208820014180056a41086a290300220c4220868420014180056a41106a350200422086200c42208884100c200141a8016a20012903c801200141c8016a41086a290300200141c8016a41106a3502004200100c200141e0046a41186a200141a8016a41186a2903003703002001200141a8016a41106a2903003703f0042001200141a8016a41086a2903003703e804200120012903a8013703e00420014188016a200a2009200842ffffffff0f834200100c200141c0046a41186a20014188016a41186a290300370300200120014188016a41106a2903003703d004200120014188016a41086a2903003703c80420012001290388013703c004200141e8006a200720052006200b100c200141a0046a41186a200141e8006a41186a2903003703002001200141e8006a41106a2903003703b0042001200141e8006a41086a2903003703a804200120012903683703a004200141a0046a41204103418008200141e0046a200141c0046a410010092004220041606a22032400200041786a42808080808080808001370300200041706a4200370300200041686a4200370300200342003703002003412010060b20021a200141f0086a24000f0b41004100100a000b0b2701004180080b20ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
Deploy Contract
- Using ewasm testnet explorer
- Paste contract data (with prefix 0x)
- Leave destination and value blank (for contract deployment)
- Accept at MetaMask and the tx will show up at ewasm testnet explorer
- View tx detail at ewasm testnet explorer
- http://ewasm.ethereum.org/explorer/tx/0xeb63374f3ee569f04455a59817f13b3bcc05d127899a0707362ec5a84504062f
- View contract detail at ewasm testnet explorer
- http://ewasm.ethereum.org/explorer/account/0xe48Db28C3F4daA7230B774dC1E7954Fd901de627
Interact with Contract
- Compile contract & assign contract address
balanceOf
transfer
- Transfer 990 token from MetaMask account to account
0x0
- Check transfer tx at ewasm testnet explorer
- http://ewasm.ethereum.org/explorer/tx/0x862f6227c00e9964e283c5eac43998514147e3f8a8654f3ff062b902c51a8890
balanceOf
aftertransfer