确认系统是否已安装Rust:
rustc --version
cargo --version
如果没有,请通过下列命令安装:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# 将Rust加载到当前shell会话中
source "$HOME/.cargo/env"
确认是否已安装wasm32-unknown-unknown:
rustup target list --installed
# 或使用
rustup show
'installed targets'下应该显示'wasm32-unknown-unknown',如果没有,请通过下列命令安装:
rustup target add wasm32-unknown-unknown
1.3 接着安装cargo-generate和cargo-run-script:
确认是否已安装'cargo generate'和'cargo run-script':
cargo --list
命令列表中应显示'generate'和'run-script',如果没有,请通过下列命令安装:
cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
确认是否已安装injectived:
injectived version
如果没有,请通过下列命令安装:
wget https://github.com/InjectiveLabs/injective-chain-releases/releases/download/v1.15.0-1748457819/linux-amd64.zip
unzip linux-amd64.zip
sudo mv injectived peggo /usr/bin
sudo mv libwasmvm.x86_64.so /usr/lib
确认是否已安装yj:
yj --version
如果没有,请通过下列命令安装:
sudo curl -L https://github.com/sclevine/yj/releases/latest/download/yj-linux-amd64 -o /usr/local/bin/yj
sudo chmod +x /usr/local/bin/yj
创建Counter项目,并转到它的文件夹:
cargo generate --git https://github.com/CosmWasm/cw-template.git --name counter
cd counter/
现在你已经创建了Counter合约,确保在进行任何更改之前可以编译和运行它。 进入仓库并执行:
# 这将在./target/wasm32-unknown-unknown/release/YOUR_NAME_HERE.wasm中生成wasm构建
cargo wasm
# 这会运行单元测试并提供有用的回溯
RUST_BACKTRACE=1 cargo unit-test
# 自动生成json模式
cargo schema
运行rust-optimizer
:
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer:0.16.0
或者,如果你在arm64机器上,你应该使用为arm64构建的docker镜像:
docker run --rm -v "$(pwd)":/code \
--mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/optimizer-arm64:0.16.0
这会生成一个artifacts
目录,其中包含PROJECT_NAME.wasm
,以及checksums.txt
,包含wasm文件的Sha256哈希值。
injectived config set client chain-id injective-888
injectived config set client node https://k8s.testnet.tm.injective.network:443
通过检查账户状态验证RPC端口:
injectived q bank balances ACCOUNT_ADDRESS
确保账户余额能正确显示。
# 生成一个新账户
injectived keys add ACCOUNT_NAME
# 或使用现有账户
injectived keys unsafe-import-eth-key ACCOUNT_NAME $PRIVATE_KEY
验证账户:
injectived keys list
确保账户内有INJ测试币:
injectived q bank balances ACCOUNT_ADDRESS
# 在"injective-core-staging"容器内,或如果本地运行injectived,则在合约目录中
TXHASH=$(
yes $KEYRING_PASSWORD | injectived tx wasm store artifacts/counter.wasm \
--from=$(echo $ACCOUNT_ADDRESS) \
--chain-id="injective-888" \
--yes --fees=1000000000000000inj --gas=2000000 \
--node=https://testnet.sentry.tm.injective.network:443 \
| yj -yj | jq -r .txhash
)
echo "txhash: $TXHASH"
# 得到Code ID
CODE_ID=$(
injectived query tx $TXHASH --node=https://testnet.sentry.tm.injective.network:443 \
| yj -yj | jq -r '.events[] | select(.type == "store_code") | .attributes[] | select(.key == "code_id") | .value'
)
echo "code_id: $CODE_ID"
INIT='{"count":99}'
echo "instantiate: ${INIT}"
TXHASH=$(
yes $KEYRING_PASSWORD | injectived tx wasm instantiate $CODE_ID $INIT \
--label="CounterTestInstance" \
--from=$(echo $ACCOUNT_ADDRESS) \
--chain-id="injective-888" \
--yes --fees=1000000000000000inj \
--gas=2000000 \
--no-admin \
--node=https://testnet.sentry.tm.injective.network:443 \
| yj -yj | jq -r .txhash
)
echo "txhash: $TXHASH"
# 得到合约地址
CONTRACT=$(
injectived query tx $TXHASH --node=https://testnet.sentry.tm.injective.network:443 \
| yj -yj | jq ".events[]" -c | jq ".attributes[]" -c \
| grep contract_address | jq -r .value | head -n1
)
echo "contract: $CONTRACT"
injectived query wasm contract $CONTRACT --node=https://testnet.sentry.tm.injective.network:443
GET_COUNT_QUERY='{"get_count":{}}'
echo "query: ${GET_COUNT_QUERY}"
injectived query wasm contract-state smart $CONTRACT "$GET_COUNT_QUERY" \
--node=https://testnet.sentry.tm.injective.network:443 \
--output json | jq .
INCREMENT='{"increment":{}}'
echo "execute: ${INCREMENT}"
TXHASH=$(
yes $KEYRING_PASSWORD | injectived tx wasm execute $CONTRACT "$INCREMENT" --from=$(echo $ACCOUNT_ADDRESS) \
--chain-id="injective-888" \
--yes --fees=1000000000000000inj --gas=2000000 \
--node=https://testnet.sentry.tm.injective.network:443 \
--output json | jq -r .txhash
)
echo "txhash: $TXHASH"
再次获取合约计数以验证:
GET_COUNT_QUERY='{"get_count":{}}'
echo "query: ${GET_COUNT_QUERY}"
injectived query wasm contract-state smart $CONTRACT "$GET_COUNT_QUERY" \
--node=https://testnet.sentry.tm.injective.network:443 \
--output json | jq .