From a57fe4efbc62a6526ca437671c36411284fcba74 Mon Sep 17 00:00:00 2001 From: Murad Karammaev Date: Mon, 30 Jan 2023 06:57:04 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20example=20contracts=20fee:=20`stake`=20?= =?UTF-8?q?=E2=86=92=20`untrn`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contracts/ibc_transfer/examples/schema.rs | 2 +- contracts/ibc_transfer/src/contract.rs | 31 +--- contracts/ibc_transfer/src/lib.rs | 1 + contracts/ibc_transfer/src/msg.rs | 20 +++ .../neutron_interchain_txs/src/contract.rs | 8 +- packages/neutron-sdk/schema/neutron_msg.json | 4 + scripts/test_ibc_transfer.sh | 109 +++++++++----- scripts/test_interchain_txs.sh | 141 ++++++++++++------ scripts/test_kv_query.sh | 98 +++++++----- scripts/test_tx_query.sh | 113 +++++++++----- 10 files changed, 343 insertions(+), 184 deletions(-) create mode 100644 contracts/ibc_transfer/src/msg.rs diff --git a/contracts/ibc_transfer/examples/schema.rs b/contracts/ibc_transfer/examples/schema.rs index 7bb145ef..919e4033 100644 --- a/contracts/ibc_transfer/examples/schema.rs +++ b/contracts/ibc_transfer/examples/schema.rs @@ -3,7 +3,7 @@ use std::fs::create_dir_all; use cosmwasm_schema::{export_schema, remove_schemas, schema_for}; -use ibc_transfer::contract::{ExecuteMsg, InstantiateMsg}; +use ibc_transfer::msg::{ExecuteMsg, InstantiateMsg}; fn main() { let mut out_dir = current_dir().unwrap(); diff --git a/contracts/ibc_transfer/src/contract.rs b/contracts/ibc_transfer/src/contract.rs index 4127ee14..c7df96d5 100644 --- a/contracts/ibc_transfer/src/contract.rs +++ b/contracts/ibc_transfer/src/contract.rs @@ -11,9 +11,12 @@ use neutron_sdk::{ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use crate::state::{ - read_reply_payload, read_sudo_payload, save_reply_payload, save_sudo_payload, - IBC_SUDO_ID_RANGE_END, IBC_SUDO_ID_RANGE_START, +use crate::{ + msg::{ExecuteMsg, InstantiateMsg, MigrateMsg}, + state::{ + read_reply_payload, read_sudo_payload, save_reply_payload, save_sudo_payload, + IBC_SUDO_ID_RANGE_END, IBC_SUDO_ID_RANGE_START, + }, }; // Default timeout for IbcTransfer is 10000000 blocks @@ -22,9 +25,6 @@ const DEFAULT_TIMEOUT_HEIGHT: u64 = 10000000; const CONTRACT_NAME: &str = concat!("crates.io:neutron-sdk__", env!("CARGO_PKG_NAME")); const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] -pub struct InstantiateMsg {} - #[entry_point] pub fn instantiate( deps: DepsMut, @@ -36,18 +36,6 @@ pub fn instantiate( Ok(Response::default()) } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] -#[serde(rename_all = "snake_case")] -pub enum ExecuteMsg { - Send { - channel: String, - to: String, - denom: String, - amount: u128, - timeout_height: Option, - }, -} - #[entry_point] pub fn execute( deps: DepsMut, @@ -159,8 +147,8 @@ fn execute_send( // See more info here: https://docs.neutron.org/neutron/feerefunder/overview let fee = IbcFee { recv_fee: vec![], - ack_fee: vec![Coin::new(1000u128, "stake")], - timeout_fee: vec![Coin::new(1000u128, "stake")], + ack_fee: vec![Coin::new(2000u128, "untrn")], + timeout_fee: vec![Coin::new(2000u128, "untrn")], }; let coin1 = coin(amount, denom.clone()); let msg1 = NeutronMsg::IbcTransfer { @@ -279,9 +267,6 @@ fn sudo_response(deps: DepsMut, req: RequestPacket, data: Binary) -> StdResult StdResult { deps.api.debug("WASMDEBUG: migrate"); diff --git a/contracts/ibc_transfer/src/lib.rs b/contracts/ibc_transfer/src/lib.rs index 8ba43d98..aae1b3fe 100644 --- a/contracts/ibc_transfer/src/lib.rs +++ b/contracts/ibc_transfer/src/lib.rs @@ -1,4 +1,5 @@ #![warn(clippy::unwrap_used, clippy::expect_used)] pub mod contract; +pub mod msg; pub mod state; diff --git a/contracts/ibc_transfer/src/msg.rs b/contracts/ibc_transfer/src/msg.rs new file mode 100644 index 00000000..08145854 --- /dev/null +++ b/contracts/ibc_transfer/src/msg.rs @@ -0,0 +1,20 @@ +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct InstantiateMsg {} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum ExecuteMsg { + Send { + channel: String, + to: String, + denom: String, + amount: u128, + timeout_height: Option, + }, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct MigrateMsg {} diff --git a/contracts/neutron_interchain_txs/src/contract.rs b/contracts/neutron_interchain_txs/src/contract.rs index 6acc6554..6d2b3e59 100644 --- a/contracts/neutron_interchain_txs/src/contract.rs +++ b/contracts/neutron_interchain_txs/src/contract.rs @@ -203,8 +203,8 @@ fn execute_delegate( // See more info here: https://docs.neutron.org/neutron/feerefunder/overview let fee = IbcFee { recv_fee: vec![], - ack_fee: vec![CosmosCoin::new(1000u128, "stake")], - timeout_fee: vec![CosmosCoin::new(1000u128, "stake")], + ack_fee: vec![CosmosCoin::new(2000u128, "untrn")], + timeout_fee: vec![CosmosCoin::new(2000u128, "untrn")], }; let (delegator, connection_id) = get_ica(deps.as_ref(), &env, &interchain_account_id)?; let delegate_msg = MsgDelegate { @@ -263,8 +263,8 @@ fn execute_undelegate( // See more info here: https://docs.neutron.org/neutron/feerefunder/overview let fee = IbcFee { recv_fee: vec![], - ack_fee: vec![CosmosCoin::new(1000u128, "stake")], - timeout_fee: vec![CosmosCoin::new(1000u128, "stake")], + ack_fee: vec![CosmosCoin::new(2000u128, "untrn")], + timeout_fee: vec![CosmosCoin::new(2000u128, "untrn")], }; let (delegator, connection_id) = get_ica(deps.as_ref(), &env, &interchain_account_id)?; let delegate_msg = MsgUndelegate { diff --git a/packages/neutron-sdk/schema/neutron_msg.json b/packages/neutron-sdk/schema/neutron_msg.json index 04056531..54956b2e 100644 --- a/packages/neutron-sdk/schema/neutron_msg.json +++ b/packages/neutron-sdk/schema/neutron_msg.json @@ -363,6 +363,7 @@ } }, "IbcFee": { + "description": "IbcFee defines struct for fees that refund the relayer for `SudoMsg` messages submission. Unused fee kind will be returned back to message sender. Please refer to these links for more information: IBC transaction structure - https://docs.neutron.org/neutron/interchain-txs/messages/#msgsubmittx General mechanics of fee payments - https://docs.neutron.org/neutron/feerefunder/overview/#general-mechanics", "type": "object", "required": [ "ack_fee", @@ -371,18 +372,21 @@ ], "properties": { "ack_fee": { + "description": "*ack_fee** is an amount of coins to refund relayer for submitting ack message for a particular IBC packet.", "type": "array", "items": { "$ref": "#/definitions/Coin" } }, "recv_fee": { + "description": "**recv_fee** currently is used for compatibility with ICS-29 interface only and must be set to zero (i.e. 0untrn), because Neutron's fee module can't refund relayer for submission of Recv IBC packets due to compatibility with target chains.", "type": "array", "items": { "$ref": "#/definitions/Coin" } }, "timeout_fee": { + "description": "*timeout_fee** amount of coins to refund relayer for submitting timeout message for a particular IBC packet.", "type": "array", "items": { "$ref": "#/definitions/Coin" diff --git a/scripts/test_ibc_transfer.sh b/scripts/test_ibc_transfer.sh index b3eb37ee..59bc27dd 100755 --- a/scripts/test_ibc_transfer.sh +++ b/scripts/test_ibc_transfer.sh @@ -1,37 +1,72 @@ -CONTRACT=../artifacts/ibc_transfer.wasm -CHAINID=test-1 -NEUTRON_DIR=${NEUTRON_DIR:-../../neutron} -HOME=${NEUTRON_DIR}/data/test-1/ -HOME2=${NEUTRON_DIR}/data/test-2/ -KEY=demowallet1 -ADMIN=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 -BIN=neutrond - -RES=$(${BIN} tx wasm store ${CONTRACT} --from ${KEY} --gas 50000000 --chain-id ${CHAINID} --broadcast-mode=block --gas-prices 0.0025stake -y --output json --keyring-backend test --home ${HOME} --node tcp://127.0.0.1:16657) -TRANSFER_CONTRACT_CODE_ID=$(echo $RES | jq -r '.logs[0].events[1].attributes[0].value') -echo $RES -echo $TRANSFER_CONTRACT_CODE_ID - -INIT_TRANSFER_CONTRACT='{}' - -RES=$(${BIN} tx wasm instantiate $TRANSFER_CONTRACT_CODE_ID "$INIT_TRANSFER_CONTRACT" --from ${KEY} --admin ${ADMIN} -y --chain-id ${CHAINID} --output json --broadcast-mode=block --label "init" --keyring-backend test --gas-prices 0.0025stake --home ${HOME} --node tcp://127.0.0.1:16657) -echo $RES -TRANSFER_CONTRACT_ADDRESS=$(echo $RES | jq -r '.logs[0].events[0].attributes[0].value') -echo $TRANSFER_CONTRACT_ADDRESS - -${BIN} tx bank send demowallet1 ${TRANSFER_CONTRACT_ADDRESS} 10000stake --chain-id ${CHAINID} --home ${HOME} --node tcp://localhost:16657 --keyring-backend test -y --gas-prices 0.0025stake --broadcast-mode=block - - -echo "Tranfer coins from test-1 to test-2" -RES=$(${BIN} tx wasm execute $TRANSFER_CONTRACT_ADDRESS \ - '{"send":{"to":"cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs","amount":"1000", "denom": "stake", "channel": "channel-0"}}' \ - --from ${KEY} -y \ - --chain-id ${CHAINID} \ - --output json \ - --broadcast-mode=block \ - --gas-prices 0.0025stake \ - --gas 1000000 \ - --keyring-backend test \ - --home ${HOME} \ - --node tcp://127.0.0.1:16657) -echo $RES | jq \ No newline at end of file +#!/usr/bin/env bash + +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' + +CONTRACT_PATH="../artifacts/ibc_transfer.wasm" +CHAIN_ID="test-1" +NEUTRON_DIR="${NEUTRON_DIR:-../../neutron}" +HOME="$NEUTRON_DIR/data/test-1/" +KEY="demowallet1" +ADMIN="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +BIN="neutrond" +GAIA_BIN="gaiad" +NODE="tcp://127.0.0.1:16657" + +code_id="$("$BIN" tx wasm store "$CONTRACT_PATH" \ + --from "$KEY" -y --chain-id "$CHAIN_ID" \ + --gas 50000000 --gas-prices 0.0025untrn \ + --broadcast-mode=block --keyring-backend=test \ + --output json --home "$HOME" --node "$NODE" \ + | jq -r '.logs[0].events[] | select(.type == "store_code").attributes[] | select(.key == "code_id").value')" +echo "Code ID: $code_id" + +contract_address="$("$BIN" tx wasm instantiate "$code_id" '{}' \ + --from ${KEY} --admin ${ADMIN} -y --chain-id "$CHAIN_ID" \ + --output json --broadcast-mode=block --label "init" \ + --keyring-backend=test --gas-prices 0.0025untrn \ + --home "$HOME" --node "$NODE" \ + | jq -r '.logs[0].events[] | select(.type == "instantiate").attributes[] | select(.key == "_contract_address").value')" +echo "Contract address: $contract_address" + +tx_result="$("$BIN" tx bank send demowallet1 "$contract_address" 20000untrn \ + -y --chain-id "$CHAIN_ID" --home "$HOME" --node "$NODE" \ + --keyring-backend=test --gas-prices 0.0025untrn --output json \ + --broadcast-mode=block)" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to send money to contract: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Sent money to contract to pay fees" + +msg='{"send":{ + "to": "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs", + "amount": "1000", + "denom": "untrn", + "channel": "channel-0" +}}' +tx_result="$("$BIN" tx wasm execute "$contract_address" "$msg" \ + --from ${KEY} -y --chain-id ${CHAIN_ID} --output json \ + --broadcast-mode=block --gas-prices 0.0025untrn --gas 1000000 \ + --keyring-backend test --home "$HOME" --node "$NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to execute contract: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi + +echo "Waiting 20 seconds for IBC transfer to complete…" +# shellcheck disable=SC2034 +for i in $(seq 20); do + sleep 1 + echo -n . +done +echo " done" + +echo +echo "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs should have 3000untrn now:" +"$GAIA_BIN" query bank balances "cosmos17dtl0mjt3t77kpuhg2edqzjpszulwhgzuj9ljs" \ + --node tcp://localhost:26657 --output json | jq '.balances' + +echo +echo "If you see more than 3000untrn, you have already run this test several times before" diff --git a/scripts/test_interchain_txs.sh b/scripts/test_interchain_txs.sh index 5af5e304..5d663ebf 100755 --- a/scripts/test_interchain_txs.sh +++ b/scripts/test_interchain_txs.sh @@ -1,53 +1,110 @@ -BIN=neutrond -GAIA_BIN=gaiad +#!/usr/bin/env bash -CONTRACT=../artifacts/neutron_interchain_txs.wasm +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' -CHAIN_ID_1=test-1 -CHAIN_ID_2=test-2 +BIN="neutrond" +GAIA_BIN="gaiad" +CONTRACT_PATH="../artifacts/neutron_interchain_txs.wasm" +CHAIN_ID_1="test-1" +CHAIN_ID_2="test-2" +NEUTRON_DIR="${NEUTRON_DIR:-../../neutron}" +HOME_1="${NEUTRON_DIR}/data/test-1/" +HOME_2="${NEUTRON_DIR}/data/test-2/" +ADDRESS_1="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +ADDRESS_2="cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw" +ADMIN="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +VALIDATOR="cosmosvaloper1qnk2n4nlkpw9xfqntladh74w6ujtulwnmxnh3k" +NEUTRON_NODE="tcp://127.0.0.1:16657" +GAIA_NODE="tcp://127.0.0.1:26657" -NEUTRON_DIR=${NEUTRON_DIR:-../../neutron} -HOME_1=${NEUTRON_DIR}/data/test-1/ -HOME_2=${NEUTRON_DIR}/data/test-2/ +code_id="$("$BIN" tx wasm store "$CONTRACT_PATH" \ + --from "$ADDRESS_1" --gas 50000000 --chain-id "$CHAIN_ID_1" \ + --broadcast-mode=block --gas-prices 0.0025untrn -y \ + --output json --keyring-backend=test --home "$HOME_1" \ + --node "$NEUTRON_NODE" \ + | jq -r '.logs[0].events[] | select(.type == "store_code").attributes[] | select(.key == "code_id").value')" +echo "Code ID: $code_id" -ADDRESS_1=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 -ADDRESS_2=cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw -ADMIN=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 +contract_address=$("$BIN" tx wasm instantiate "$code_id" '{}' \ + --from "$ADDRESS_1" --admin "$ADMIN" -y --chain-id "$CHAIN_ID_1" \ + --output json --broadcast-mode=block --label "init" \ + --keyring-backend=test --gas-prices 0.0025untrn --gas auto \ + --gas-adjustment 1.4 --home "$HOME_1" \ + --node "$NEUTRON_NODE" 2>/dev/null \ + | jq -r '.logs[0].events[] | select(.type == "instantiate").attributes[] | select(.key == "_contract_address").value') +echo "Contract address: $contract_address" -VAL2=cosmos1qnk2n4nlkpw9xfqntladh74w6ujtulwn7j8za9 +tx_result="$("$BIN" tx bank send demowallet1 "$contract_address" 100000untrn \ + --chain-id "$CHAIN_ID_1" --home "$HOME_1" --node "$NEUTRON_NODE" \ + --keyring-backend=test -y --gas-prices 0.0025untrn \ + --broadcast-mode=block --output json)" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to send money to contract: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Sent money to contract to pay fees" -# Upload the txs contract -RES=$(${BIN} tx wasm store ${CONTRACT} --from ${ADDRESS_1} --gas 50000000 --chain-id ${CHAIN_ID_1} --broadcast-mode=block --gas-prices 0.0025stake -y --output json --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -CONTRACT_CODE_ID=$(echo $RES | jq -r '.logs[0].events[1].attributes[0].value') -echo $RES -echo $CONTRACT_CODE_ID +msg='{"register":{ + "connection_id": "connection-0", + "interchain_account_id": "test" +}}' +tx_result="$("$BIN" tx wasm execute "$contract_address" "$msg" \ + --from "$ADDRESS_1" -y --chain-id "$CHAIN_ID_1" --output json \ + --broadcast-mode=block --gas-prices 0.0025untrn --gas 1000000 \ + --keyring-backend=test --home "$HOME_1" --node "$NEUTRON_NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to register interchain account: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Waiting 60 seconds for interchain account (sometimes it takes a lot of time)…" +# shellcheck disable=SC2034 +for i in $(seq 60); do + sleep 1 + echo -n . +done +echo " done" -# Instantiate the contract -INIT_CONTRACT='{}' -echo "Instantiate" -RES=$(${BIN} tx wasm instantiate $CONTRACT_CODE_ID "$INIT_CONTRACT" --from ${ADDRESS_1} --admin ${ADMIN} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --label "init" --keyring-backend test --gas-prices 0.0025stake --gas auto --gas-adjustment 1.4 --home ${HOME_1} --node tcp://127.0.0.1:16657) -CONTRACT_ADDRESS=$(echo $RES | jq -r '.logs[0].events[0].attributes[0].value') -echo $CONTRACT_ADDRESS +# FIXME: why do we even perform a query like this, can't we do `neutrond query wasm smart blah-blah`? +query='{"interchain_account_address_from_contract":{"interchain_account_id":"test"}}' +query_b64_urlenc="$(echo -n "$query" | base64 | tr -d '\n' | jq -sRr '@uri')" +url="http://127.0.0.1:1316/wasm/contract/$contract_address/smart/$query_b64_urlenc?encoding=base64" +ica_address=$(curl -s "$url" | jq -r '.result.smart' | base64 -d | jq -r '.[0]') +echo "ICA address: $ica_address" -${BIN} tx bank send demowallet1 ${CONTRACT_ADDRESS} 100000stake --chain-id ${CHAIN_ID_1} --home ${HOME_1} --node tcp://localhost:16657 --keyring-backend test -y --gas-prices 0.0025stake --broadcast-mode=block +tx_result=$("$GAIA_BIN" tx bank send "$ADDRESS_2" "$ica_address" 50000stake \ + --chain-id "$CHAIN_ID_2" --broadcast-mode=block --gas-prices 0.0025stake \ + -y --output json --keyring-backend=test --home "$HOME_2" --node "$GAIA_NODE") +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to send money to ICA: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Sent money to ICA" -#Register interchain account -RES=$(${BIN} tx wasm execute $CONTRACT_ADDRESS "{\"register\": {\"connection_id\": \"connection-0\", \"interchain_account_id\": \"test\"}}" --from ${ADDRESS_1} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --gas-prices 0.0025stake --gas 1000000 --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -echo $RES -sleep 20 +msg="$(printf '{"delegate":{ + "interchain_account_id": "test", + "validator": "%s", + "amount": "2000", + "denom": "stake" +}}' "$VALIDATOR")" +tx_result="$("$BIN" tx wasm execute "$contract_address" "$msg" \ + --from "$ADDRESS_1" -y --chain-id "$CHAIN_ID_1" --output json \ + --broadcast-mode=block --gas-prices 0.0025untrn --gas 1000000 \ + --keyring-backend=test --home "$HOME_1" --node "$NEUTRON_NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to execute contract: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi -RES=$(curl http://127.0.0.1:1316/wasm/contract/$CONTRACT_ADDRESS/smart/eyJpbnRlcmNoYWluX2FjY291bnRfYWRkcmVzc19mcm9tX2NvbnRyYWN0Ijp7ImludGVyY2hhaW5fYWNjb3VudF9pZCI6InRlc3QifX0\=?encoding\=base64 | jq -r ".result.smart") -echo $RES -ICA_ADDRESS=$(echo $RES | base64 --decode | jq -r ".[0]") -echo $ICA_ADDRESS +echo "Waiting 20 seconds for interchain transaction to complete…" +# shellcheck disable=SC2034 +for i in $(seq 20); do + sleep 1 + echo -n . +done +echo " done" -#Send some money to ICA -RES=$(${GAIA_BIN} tx bank send ${ADDRESS_2} ${ICA_ADDRESS} 10000stake --chain-id ${CHAIN_ID_2} --broadcast-mode=block --gas-prices 0.0025stake -y --output json --keyring-backend test --home ${HOME_2} --node tcp://127.0.0.1:26657) -echo $RES - -#Delegate -RES=$(${BIN} tx wasm execute $CONTRACT_ADDRESS "{\"delegate\": {\"interchain_account_id\": \"test\", \"validator\": \"${VAL2}\", \"amount\":\"5000\",\"denom\":\"stake\"}}" --from ${ADDRESS_1} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --gas-prices 0.0025stake --gas 1000000 --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -echo $RES - -sleep 7; -curl http://127.0.0.1:1317/staking/delegators/$ICA_ADDRESS/delegations \ No newline at end of file +echo +echo "This delegation is performed using interchain tx module" +curl -s "http://127.0.0.1:1317/staking/delegators/$ica_address/delegations" | jq '.result' diff --git a/scripts/test_kv_query.sh b/scripts/test_kv_query.sh index 258036a6..4b18209e 100755 --- a/scripts/test_kv_query.sh +++ b/scripts/test_kv_query.sh @@ -1,46 +1,68 @@ -BIN=neutrond +#!/usr/bin/env bash -CONTRACT=../artifacts/neutron_interchain_queries.wasm +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' -CHAIN_ID_1=test-1 -CHAIN_ID_2=test-2 +BIN="neutrond" +CONTRACT_PATH="../artifacts/neutron_interchain_queries.wasm" +CHAIN_ID_1="test-1" +NEUTRON_DIR="${NEUTRON_DIR:-../../neutron}" +HOME_1="${NEUTRON_DIR}/data/test-1/" +ADDRESS_1="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +ADMIN="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +NODE="tcp://127.0.0.1:16657" -NEUTRON_DIR=${NEUTRON_DIR:-../../neutron} -HOME_1=${NEUTRON_DIR}/data/test-1/ -HOME_2=${NEUTRON_DIR}/data/test-2/ +code_id="$("$BIN" tx wasm store "$CONTRACT_PATH" \ + --from "$ADDRESS_1" --gas 50000000 --chain-id "$CHAIN_ID_1" \ + --broadcast-mode=block --gas-prices 0.0025untrn -y \ + --output json --keyring-backend=test --home "$HOME_1" \ + --node "$NODE" \ + | jq -r '.logs[0].events[] | select(.type == "store_code").attributes[] | select(.key == "code_id").value')" +echo "Code ID: $code_id" -ADDRESS_1=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 -ADDRESS_2=cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw -ADMIN=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 +contract_address="$("$BIN" tx wasm instantiate "$code_id" '{}' \ + --from "$ADDRESS_1" --admin "$ADMIN" -y --chain-id "$CHAIN_ID_1" \ + --output json --broadcast-mode=block --label "init" --node "$NODE" \ + --keyring-backend=test --gas-prices 0.0025untrn --home "$HOME_1" \ + | jq -r '.logs[0].events[] | select(.type == "instantiate").attributes[] | select(.key == "_contract_address").value')" +echo "Contract address: $contract_address" -VAL2=cosmos1qnk2n4nlkpw9xfqntladh74w6ujtulwn7j8za9 +tx_result="$("$BIN" tx bank send "$ADDRESS_1" "$contract_address" 10000000untrn \ + -y --chain-id "$CHAIN_ID_1" --output json --broadcast-mode=block \ + --gas-prices 0.0025untrn --gas 300000 --keyring-backend=test \ + --home "$HOME_1" --node "$NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to send money to contract: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Sent money to contract to pay for deposit" -# Upload the queries contract -echo "Upload the queries contract" -RES=$(${BIN} tx wasm store ${CONTRACT} --from ${ADDRESS_1} --gas 50000000 --chain-id ${CHAIN_ID_1} --broadcast-mode=block --gas-prices 0.0025stake -y --output json --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -QUERIES_CONTRACT_CODE_ID=$(echo $RES | jq -r '.logs[0].events[1].attributes[0].value') -echo $RES -echo $QUERIES_CONTRACT_CODE_ID +msg='{"register_bank_total_supply_query":{ + "connection_id": "connection-0", + "denoms": ["stake"], + "update_period": 5 +}}' +tx_result="$("$BIN" tx wasm execute "$contract_address" "$msg" \ + --from "$ADDRESS_1" -y --chain-id "$CHAIN_ID_1" --output json \ + --broadcast-mode=block --gas-prices 0.0025untrn --gas 1000000 \ + --keyring-backend=test --home "$HOME_1" --node "$NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to register ICQ: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +query_id="$(echo "$tx_result" | jq -r '.logs[0].events[] | select(.type == "neutron").attributes[] | select(.key == "query_id").value')" +echo "Registered total supply ICQ with query ID: $query_id" -# Instantiate the queries contract -echo "Instantiate the queries contract" -INIT_QUERIES_CONTRACT='{}' +echo "Waiting 10 seconds for ICQ result to arrive…" +# shellcheck disable=SC2034 +for i in $(seq 10); do + sleep 1 + echo -n . +done +echo " done" -RES=$(${BIN} tx wasm instantiate $QUERIES_CONTRACT_CODE_ID "$INIT_QUERIES_CONTRACT" --from ${ADDRESS_1} --admin ${ADMIN} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --label "init" --keyring-backend test --gas-prices 0.0025stake --home ${HOME_1} --node tcp://127.0.0.1:16657) -echo $RES -QUERIES_CONTRACT_ADDRESS=$(echo $RES | jq -r '.logs[0].events[0].attributes[0].value') -echo $QUERIES_CONTRACT_ADDRESS - -# Send coins from USERNAME_1 to QUERIES_CONTRACT_ADDRESS to perform register_interchain_query message -echo "Send coins from ${ADDRESS_1} to ${QUERIES_CONTRACT_ADDRESS} to perform register_interchain_query message" -echo $(${BIN} tx bank send ${ADDRESS_1} ${QUERIES_CONTRACT_ADDRESS} 10000000stake -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --gas-prices 0.0025stake --gas 300000 --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) - -# Register a query for a total supply of stake token -echo "Register a query for total supply" -RES=$(${BIN} tx wasm execute $QUERIES_CONTRACT_ADDRESS "{\"register_bank_total_supply_query\": {\"connection_id\": \"connection-0\", \"denoms\": [\"stake\"], \"update_period\": 5}}" --from ${ADDRESS_1} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --gas-prices 0.0025stake --gas 1000000 --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -echo $RES | jq - -sleep 5 - -echo "KV query response:" -${BIN} query wasm contract-state smart ${QUERIES_CONTRACT_ADDRESS} '{"bank_total_supply": {"query_id": 1}}' --node tcp://127.0.0.1:16657 \ No newline at end of file +echo +echo "KV query total supply response:" +query="$(printf '{"bank_total_supply": {"query_id": %s}}' "$query_id")" +"$BIN" query wasm contract-state smart "$contract_address" "$query" --node "$NODE" --output json | jq diff --git a/scripts/test_tx_query.sh b/scripts/test_tx_query.sh index e74c1425..69bd97f6 100755 --- a/scripts/test_tx_query.sh +++ b/scripts/test_tx_query.sh @@ -1,49 +1,84 @@ -BIN=neutrond -GAIA_BIN=gaiad +#!/usr/bin/env bash -CONTRACT=../artifacts/neutron_interchain_queries.wasm +# http://redsymbol.net/articles/unofficial-bash-strict-mode/ +set -euo pipefail +IFS=$'\n\t' -CHAIN_ID_1=test-1 -CHAIN_ID_2=test-2 +BIN="neutrond" +GAIA_BIN="gaiad" +CONTRACT_PATH="../artifacts/neutron_interchain_queries.wasm" +CHAIN_ID_1="test-1" +CHAIN_ID_2="test-2" +NEUTRON_DIR="${NEUTRON_DIR:-../../neutron}" +HOME_1="${NEUTRON_DIR}/data/test-1/" +HOME_2="${NEUTRON_DIR}/data/test-2/" +ADDRESS_1="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +ADDRESS_2="cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw" +ADMIN="neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2" +VALIDATOR="cosmos1qnk2n4nlkpw9xfqntladh74w6ujtulwn7j8za9" +NEUTRON_NODE="tcp://127.0.0.1:16657" +GAIA_NODE="tcp://127.0.0.1:26657" -NEUTRON_DIR=${NEUTRON_DIR:-../../neutron} -HOME_1=${NEUTRON_DIR}/data/test-1/ -HOME_2=${NEUTRON_DIR}/data/test-2/ +code_id="$("$BIN" tx wasm store "$CONTRACT_PATH" \ + --from "$ADDRESS_1" --gas 50000000 --chain-id "$CHAIN_ID_1" \ + --broadcast-mode=block --gas-prices 0.0025untrn -y \ + --output json --keyring-backend=test --home "$HOME_1" \ + --node "$NEUTRON_NODE" \ + | jq -r '.logs[0].events[] | select(.type == "store_code").attributes[] | select(.key == "code_id").value')" +echo "Code ID: $code_id" -ADDRESS_1=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 -ADDRESS_2=cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw -ADMIN=neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2 +contract_address="$("$BIN" tx wasm instantiate "$code_id" '{}' \ + --from "$ADDRESS_1" --admin "$ADMIN" -y --chain-id "$CHAIN_ID_1" \ + --output json --broadcast-mode=block --label "init" --keyring-backend=test \ + --gas-prices 0.0025untrn --home "$HOME_1" --node "$NEUTRON_NODE" \ + | jq -r '.logs[0].events[] | select(.type == "instantiate").attributes[] | select(.key == "_contract_address").value')" +echo "Contract address: $contract_address" -VAL2=cosmos1qnk2n4nlkpw9xfqntladh74w6ujtulwn7j8za9 +tx_result="$("$BIN" tx bank send "$ADDRESS_1" "$contract_address" 10000000untrn \ + -y --chain-id "$CHAIN_ID_1" --output json --broadcast-mode=block \ + --gas-prices 0.0025untrn --gas 300000 --keyring-backend=test \ + --home "$HOME_1" --node "$NEUTRON_NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to send money to contract: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Sent money to contract to pay for deposit" -# Upload the queries contract -echo "Upload the queries contract" -RES=$(${BIN} tx wasm store ${CONTRACT} --from ${ADDRESS_1} --gas 50000000 --chain-id ${CHAIN_ID_1} --broadcast-mode=block --gas-prices 0.0025stake -y --output json --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -QUERIES_CONTRACT_CODE_ID=$(echo $RES | jq -r '.logs[0].events[1].attributes[0].value') -echo $RES -echo $QUERIES_CONTRACT_CODE_ID +msg="$(printf '{"register_transfers_query": { + "connection_id": "connection-0", + "recipient": "%s", + "update_period": 5, + "min_height": 1 +}}' "$VALIDATOR")" +tx_result="$("$BIN" tx wasm execute "$contract_address" "$msg" \ + --from "$ADDRESS_1" -y --chain-id "$CHAIN_ID_1" --output json \ + --broadcast-mode=block --gas-prices 0.0025untrn --gas 1000000 \ + --keyring-backend=test --home "$HOME_1" --node "$NEUTRON_NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to register ICQ: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Registered transfers ICQ" -# Instantiate the queries contract -echo "Instantiate the queries contract" -INIT_QUERIES_CONTRACT='{}' +tx_result="$("$GAIA_BIN" tx bank send "$ADDRESS_2" "$VALIDATOR" 1000stake \ + --gas 50000000 --gas-adjustment 1.4 --output json -y \ + --gas-prices 0.5stake --broadcast-mode=block --chain-id "$CHAIN_ID_2" \ + --keyring-backend=test --home "$HOME_2" --node "$GAIA_NODE")" +code="$(echo "$tx_result" | jq '.code')" +if [[ ! "$code" -eq 0 ]]; then + echo "Failed to transfer funds to trigger TX ICQ: $(echo "$tx_result" | jq '.raw_log')" && exit 1 +fi +echo "Triggered TX ICQ via transferring funds to watched address" -RES=$(${BIN} tx wasm instantiate $QUERIES_CONTRACT_CODE_ID "$INIT_QUERIES_CONTRACT" --from ${ADDRESS_1} --admin ${ADMIN} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --label "init" --keyring-backend test --gas-prices 0.0025stake --home ${HOME_1} --node tcp://127.0.0.1:16657) -echo $RES -QUERIES_CONTRACT_ADDRESS=$(echo $RES | jq -r '.logs[0].events[0].attributes[0].value') -echo $QUERIES_CONTRACT_ADDRESS - -# Send coins from USERNAME_1 to QUERIES_CONTRACT_ADDRESS to perform register_interchain_query message -echo "Send coins from ${ADDRESS_1} to ${QUERIES_CONTRACT_ADDRESS} to perform register_interchain_query message" -echo $(${BIN} tx bank send ${ADDRESS_1} ${QUERIES_CONTRACT_ADDRESS} 10000000stake -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --gas-prices 0.0025stake --gas 300000 --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) - -# Register a query for Send transactions -RES=$(${BIN} tx wasm execute $QUERIES_CONTRACT_ADDRESS "{\"register_transfers_query\": {\"connection_id\": \"connection-0\", \"recipient\": \"${VAL2}\", \"update_period\": 5, \"min_height\": 1}}" --from ${ADDRESS_1} -y --chain-id ${CHAIN_ID_1} --output json --broadcast-mode=block --gas-prices 0.0025stake --gas 1000000 --keyring-backend test --home ${HOME_1} --node tcp://127.0.0.1:16657) -echo $RES - -RES=$(${GAIA_BIN} tx bank send ${ADDRESS_2} ${VAL2} 1000stake --from ${ADDRESS_2} --gas 50000000 --gas-adjustment 1.4 --gas-prices 0.5stake --broadcast-mode block --chain-id ${CHAIN_ID_2} --keyring-backend test --home ${HOME_2} --node tcp://127.0.0.1:26657 -y) -echo $RES - -sleep 5 +echo "Waiting 10 seconds for ICQ result to arrive…" +# shellcheck disable=SC2034 +for i in $(seq 10); do + sleep 1 + echo -n . +done +echo " done" +echo echo "TX query response:" -${BIN} query wasm contract-state smart ${QUERIES_CONTRACT_ADDRESS} "{\"get_recipient_txs\": {\"recipient\": \"${VAL2}\"}}" --node tcp://127.0.0.1:16657 \ No newline at end of file +query="$(printf '{"get_recipient_txs": {"recipient": "%s"}}' "$VALIDATOR")" +"$BIN" query wasm contract-state smart "$contract_address" "$query" --node "$NEUTRON_NODE" --output json | jq