LocalTerra环境部署CW20Token记录

  • 蘭兹
  • 更新于 2022-03-30 16:45
  • 阅读 2857

在本地搭建LocalTerra节点时,有时会用到创建cw20Token来测试。LocalTerra中的cw20合约代码需要自己编译后上传。记录该过程。

在本地搭建LocalTerra节点时,有时会用到创建cw20Token来测试。LocalTerra中的cw20合约代码需要自己编译后上传。记录该过程。

1、clone cw-plus项目

git clone https://github.com/CosmWasm/cw-plus

CosmWasm提供了多个已通过测试验证的合约,包括白名单、多签、质押,以及符合ERC20规范的相关合约。

.
├── cw1-subkeys
├── cw1-whitelist
├── cw1155-base
├── cw20-atomic-swap
├── cw20-base
├── cw20-bonding
├── cw20-escrow
├── cw20-ics20
├── cw20-merkle-airdrop
├── cw20-staking
├── cw3-fixed-multisig
├── cw3-flex-multisig
├── cw4-group
├── cw4-stake
└── cw721-base

其中cw20-base即实现ERC20规范的合约项目。

由于当前最新版本(v0.12.0)编译优化后,上传代码提示

Wasm contract has unknown interface_version_* marker

因此选择的是v0.8.0版本代码进行编译的

git tag
...
v0.7.0
v0.8.0
v0.8.0-rc1
v0.8.0-rc2
v0.8.0-rc3
v0.8.1
...

git checkout v0.8.0

2、编译合约代码

项目根目录下运行

cw-plus % docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target 
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry 
cosmwasm/workspace-optimizer:0.11.3

会将编译优化后的wasm文件放置在项目根目录下的artifacts目录下

1.51.0-x86_64-unknown-linux-musl (default)
cargo 1.51.0 (43b129a20 2021-03-16)
Building artifacts in workspace ...
Found workspace member entries: ['packages/*', 'contracts/*']
Package directories: ['contracts/cw1-subkeys', 'contracts/cw1-whitelist', 'contracts/cw1155-base', 'contracts/cw20-atomic-swap', 'contracts/cw20-base', 'contracts/cw20-bonding', 'contracts/cw20-escrow', 'contracts/cw20-ics20', 'contracts/cw20-merkle-airdrop', 'contracts/cw20-staking', 'contracts/cw3-fixed-multisig', 'contracts/cw3-flex-multisig', 'contracts/cw4-group', 'contracts/cw4-stake', 'contracts/cw721-base', 'packages/controllers', 'packages/cw0', 'packages/cw1', 'packages/cw1155', 'packages/cw2', 'packages/cw20', 'packages/cw3', 'packages/cw4', 'packages/cw721', 'packages/multi-test', 'packages/storage-plus']
Contracts to be built: ['contracts/cw1-subkeys', 'contracts/cw1-whitelist', 'contracts/cw1155-base', 'contracts/cw20-atomic-swap', 'contracts/cw20-base', 'contracts/cw20-bonding', 'contracts/cw20-escrow', 'contracts/cw20-ics20', 'contracts/cw20-merkle-airdrop', 'contracts/cw20-staking', 'contracts/cw3-fixed-multisig', 'contracts/cw3-flex-multisig', 'contracts/cw4-group', 'contracts/cw4-stake', 'contracts/cw721-base']
Building contracts/cw1-subkeys ...
Compiling cw1-whitelist v0.8.0 (/code/contracts/cw1-whitelist)
Compiling cw1-subkeys v0.8.0 (/code/contracts/cw1-subkeys)
Finished release [optimized] target(s) in 19.91s
Building contracts/cw1-whitelist ...
Compiling cw1-whitelist v0.8.0 (/code/contracts/cw1-whitelist)
Finished release [optimized] target(s) in 11.05s
Building contracts/cw1155-base ...
Finished release [optimized] target(s) in 0.30s
Building contracts/cw20-atomic-swap ...
Finished release [optimized] target(s) in 0.33s
Building contracts/cw20-base ...
Compiling cw20-base v0.8.0 (/code/contracts/cw20-base)
Finished release [optimized] target(s) in 15.57s
Building contracts/cw20-bonding ...
Compiling cw20-base v0.8.0 (/code/contracts/cw20-base)
Compiling cw20-bonding v0.8.0 (/code/contracts/cw20-bonding)
Finished release [optimized] target(s) in 20.32s
Building contracts/cw20-escrow ...
Finished release [optimized] target(s) in 0.32s
Building contracts/cw20-ics20 ...
Finished release [optimized] target(s) in 0.30s
Building contracts/cw20-merkle-airdrop ...
Finished release [optimized] target(s) in 0.30s
Building contracts/cw20-staking ...
Compiling cw20-staking v0.8.0 (/code/contracts/cw20-staking)
Finished release [optimized] target(s) in 13.36s
Building contracts/cw3-fixed-multisig ...
Finished release [optimized] target(s) in 0.30s
Building contracts/cw3-flex-multisig ...
Finished release [optimized] target(s) in 0.30s
Building contracts/cw4-group ...
Finished release [optimized] target(s) in 0.31s
Building contracts/cw4-stake ...
Finished release [optimized] target(s) in 0.31s
Building contracts/cw721-base ...
Finished release [optimized] target(s) in 0.30s
Optimizing artifacts in workspace ...
Optimizing ../target/wasm32-unknown-unknown/release/cw1155_base.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw1_subkeys.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw1_whitelist.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_atomic_swap.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_base.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_bonding.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_escrow.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_ics20.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_merkle_airdrop.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw20_staking.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw3_fixed_multisig.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw3_flex_multisig.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw4_group.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw4_stake.wasm ...
Optimizing ../target/wasm32-unknown-unknown/release/cw721_base.wasm ...
Moving wasm files ...
Post-processing artifacts in workspace ...
done

编译优化后的文件

.
├── checksums.txt
├── cw1155_base.wasm
├── cw1_subkeys.wasm
├── cw1_whitelist.wasm
├── cw20_atomic_swap.wasm
├── cw20_base.wasm
├── cw20_bonding.wasm
├── cw20_escrow.wasm
├── cw20_ics20.wasm
├── cw20_merkle_airdrop.wasm
├── cw20_staking.wasm
├── cw3_fixed_multisig.wasm
├── cw3_flex_multisig.wasm
├── cw4_group.wasm
├── cw4_stake.wasm
└── cw721_base.wasm

3、上传代码

cw-plus % terrad tx wasm store artifacts/cw20_base.wasm --from test1 --chain-id=localterra --gas=auto --fees=100000uluna --broadcast-mode=block

gas estimate: 1612883
{"body":{"messages":[{"@type":"/terra.wasm.v1beta1.MsgStoreCode","sender":"terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8","wasm_byte_code":"H4sIAAAAAAAA/+y9DZhdR3UgWH/357373uvbP7Jbasmpe1HgdXAPMjGvhewkXQ0t2THfIBO+rL0fM...IFNRQhOgEoKa5KvGf4QqH0nlp8YH/i+ArBCwFgeNf2UblJRhlJBBCgvwb2AjAMMhRmRI2ElVyo5CmhsAFBoUwJpK1L61ryryrWqAbt8VCBSyQwvo5NX+OQCxNAZ1nJ8lkeF7DQShUkJIaIiULEihBQu9Q4th76b/uniVpSEQRHoYIP0ltGQZbIEaHx8nhhJSIODfAQAA//8L2Bj8Q6AEAA=="}],"memo":"","timeout_height":"0","extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[],"fee":{"amount":[{"denom":"uluna","amount":"100000"}],"gas_limit":"1612883","payer":"","granter":""}},"signatures":[]}

confirm transaction before signing and broadcasting [y/N]: y
code: 0
codespace: ""
data: 0A260A202F74657272612E7761736D2E763162657461312E4D736753746F7265436F646512020802
events:

- attributes:
  - index: true
    key: c3BlbmRlcg==
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
  - index: true
    key: YW1vdW50
    value: MTAwMDAwdWx1bmE=
    type: coin_spent
- attributes:
  - index: true
    key: cmVjZWl2ZXI=
    value: dGVycmExN3hwZnZha20yYW1nOTYyeWxzNmY4NHoza2VsbDhjNWxrYWVxZmE=
  - index: true
    key: YW1vdW50
    value: MTAwMDAwdWx1bmE=
    type: coin_received
- attributes:
  - index: true
    key: cmVjaXBpZW50
    value: dGVycmExN3hwZnZha20yYW1nOTYyeWxzNmY4NHoza2VsbDhjNWxrYWVxZmE=
  - index: true
    key: c2VuZGVy
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
  - index: true
    key: YW1vdW50
    value: MTAwMDAwdWx1bmE=
    type: transfer
- attributes:
  - index: true
    key: c2VuZGVy
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
    type: message
- attributes:
  - index: true
    key: ZmVl
    value: MTAwMDAwdWx1bmE=
    type: tx
- attributes:
  - index: true
    key: YWNjX3NlcQ==
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDgvNzI0
    type: tx
- attributes:
  - index: true
    key: c2lnbmF0dXJl
    value: eTZaS1plRTZHTXlCM3BSL3BFWEE5M0llcmpUQXJlRkRobWhGRzBaVDhZY1k3LzZpWWdMdWV3UkVTNllIVk1naDB1Q3dacnZSK09kWDYyMjZrYm1kWXc9PQ==
    type: tx
- attributes:
  - index: true
    key: YWN0aW9u
    value: L3RlcnJhLndhc20udjFiZXRhMS5Nc2dTdG9yZUNvZGU=
    type: message
- attributes:
  - index: true
    key: c2VuZGVy
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
  - index: true
    key: Y29kZV9pZA==
    value: Mg==
    type: store_code
- attributes:
  - index: true
    key: bW9kdWxl
    value: d2FzbQ==
    type: message
    gas_used: "1611362"
    gas_wanted: "1612883"
    height: "3603"
    info: ""
    logs:
- events:
  - attributes:
    - key: action
      value: /terra.wasm.v1beta1.MsgStoreCode
    - key: module
      value: wasm
      type: message
  - attributes:
    - key: sender
      value: terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8
    - key: code_id
      value: "2"
      type: store_code
      log: ""
      msg_index: 0
      raw_log: '[{"events":[{"type":"message","attributes":[{"key":"action","value":"/terra.wasm.v1beta1.MsgStoreCode"},{"key":"module","value":"wasm"}]},{"type":"store_code","attributes":[{"key":"sender","value":"terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8"},{"key":"code_id","value":"2"}]}]}]'
      timestamp: ""
      tx: null
      txhash: 024D97F1FE62D117FC2E3817184030ADC3C772FB1C4951A158AD3946CCC5DD9F

上传成功,code_id为2记录了cw20合约代码。

4、部署合约

实例化CW20Token。

{
    "name": "yout_token_name",
    "symbol": "SYMBOL",
    "decimals": 6,
    "initial_balances": [
        {
            "address": "terraaddress0001asdfsdfbqwer...",
            "amount": "10000"
        },
        {
            "address": "terraaddress0002asdfsdfbqwer...",
            "amount": "10000"
        },
        ...
    ]
}

执行初始化命令。

% terrad tx wasm instantiate 2 '{"name":"Tether USD","symbol":"USDT","decimals":6,"initial_balances":[{"address":"terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v","amount":"1000000000"}]}' --from test1 --chain-id=localterra --fees=10000uluna --gas=auto --broadcast-mode=block

gas estimate: 289047
{"body":{"messages":[{"@type":"/terra.wasm.v1beta1.MsgInstantiateContract","sender":"terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8","admin":"","code_id":"2","init_msg":{"name":"Tether USD","symbol":"USDT","decimals":6,"initial_balances":[{"address":"terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v","amount":"1000000000"}]},"init_coins":[]}],"memo":"","timeout_height":"0","extension_options":[],"non_critical_extension_options":[]},"auth_info":{"signer_infos":[],"fee":{"amount":[{"denom":"uluna","amount":"10000"}],"gas_limit":"289047","payer":"","granter":""}},"signatures":[]}

confirm transaction before signing and broadcasting [y/N]: y
code: 0
codespace: ""
data: 0A5C0A2A2F74657272612E7761736D2E763162657461312E4D7367496E7374616E7469617465436F6E7472616374122E0A2C746572726131763765747336763538707736353373356B326E737764386B75396D64706D7137717A7A357178
events:

- attributes:
  - index: true
    key: c3BlbmRlcg==
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
  - index: true
    key: YW1vdW50
    value: MTAwMDB1bHVuYQ==
    type: coin_spent
- attributes:
  - index: true
    key: cmVjZWl2ZXI=
    value: dGVycmExN3hwZnZha20yYW1nOTYyeWxzNmY4NHoza2VsbDhjNWxrYWVxZmE=
  - index: true
    key: YW1vdW50
    value: MTAwMDB1bHVuYQ==
    type: coin_received
- attributes:
  - index: true
    key: cmVjaXBpZW50
    value: dGVycmExN3hwZnZha20yYW1nOTYyeWxzNmY4NHoza2VsbDhjNWxrYWVxZmE=
  - index: true
    key: c2VuZGVy
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
  - index: true
    key: YW1vdW50
    value: MTAwMDB1bHVuYQ==
    type: transfer
- attributes:
  - index: true
    key: c2VuZGVy
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
    type: message
- attributes:
  - index: true
    key: ZmVl
    value: MTAwMDB1bHVuYQ==
    type: tx
- attributes:
  - index: true
    key: YWNjX3NlcQ==
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDgvMTQyMQ==
    type: tx
- attributes:
  - index: true
    key: c2lnbmF0dXJl
    value: WDNGTytaSUNuVWxqSTJNNnhlazNaR1B1NTNFaDRqa1I0eE9EOURORVVMVTZtQlNwVXdtWnNGcUJKZTBRWUhKL0ROS0RBNUdBR1hqOXV0bFdVbk5iUkE9PQ==
    type: tx
- attributes:
  - index: true
    key: YWN0aW9u
    value: L3RlcnJhLndhc20udjFiZXRhMS5Nc2dJbnN0YW50aWF0ZUNvbnRyYWN0
    type: message
- attributes:
  - index: true
    key: Y3JlYXRvcg==
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
  - index: true
    key: YWRtaW4=
    value: ""
  - index: true
    key: Y29kZV9pZA==
    value: Mg==
  - index: true
    key: Y29udHJhY3RfYWRkcmVzcw==
    value: dGVycmExdjdldHM2djU4cHc2NTNzNWsybnN3ZDhrdTltZHBtcTdxeno1cXg=
    type: instantiate_contract
- attributes:
  - index: true
    key: bW9kdWxl
    value: d2FzbQ==
  - index: true
    key: c2VuZGVy
    value: dGVycmExZGNlZ3lyZWtsdHN3dnl5MHh5Njl5ZGd4bjl4OHgzMnpkdGFwZDg=
    type: message
    gas_used: "288496"
    gas_wanted: "289047"
    height: "7072"
    info: ""
    logs:
- events:
  - attributes:
    - key: creator
      value: terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8
    - key: admin
      value: ""
    - key: code_id
      value: "2"
    - key: contract_address
      value: terra1v7ets6v58pw653s5k2nswd8ku9mdpmq7qzz5qx
      type: instantiate_contract
  - attributes:
    - key: action
      value: /terra.wasm.v1beta1.MsgInstantiateContract
    - key: module
      value: wasm
    - key: sender
      value: terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8
      type: message
      log: ""
      msg_index: 0
      raw_log: '[{"events":[{"type":"instantiate_contract","attributes":[{"key":"creator","value":"terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8"},{"key":"admin"},{"key":"code_id","value":"2"},{"key":"contract_address","value":"terra1v7ets6v58pw653s5k2nswd8ku9mdpmq7qzz5qx"}]},{"type":"message","attributes":[{"key":"action","value":"/terra.wasm.v1beta1.MsgInstantiateContract"},{"key":"module","value":"wasm"},{"key":"sender","value":"terra1dcegyrekltswvyy0xy69ydgxn9x8x32zdtapd8"}]}]}]'
      timestamp: ""
      tx: null
      txhash: 33046CA5685B52C054B12EBD6AB9623D4BD357CB5F7DF2DEF05526DE837E92DA

image.png

部署完成。

点赞 1
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

1 条评论

请先 登录 后评论
蘭兹
蘭兹
0xC38C...609b
江湖只有他的大名,没有他的介绍。