Skip to content

Commit

Permalink
fix: example contracts fee: stakeuntrn
Browse files Browse the repository at this point in the history
  • Loading branch information
foxpy committed Mar 20, 2023
1 parent e2d4b9d commit a57fe4e
Show file tree
Hide file tree
Showing 10 changed files with 343 additions and 184 deletions.
2 changes: 1 addition & 1 deletion contracts/ibc_transfer/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
31 changes: 8 additions & 23 deletions contracts/ibc_transfer/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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<u64>,
},
}

#[entry_point]
pub fn execute(
deps: DepsMut,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -279,9 +267,6 @@ fn sudo_response(deps: DepsMut, req: RequestPacket, data: Binary) -> StdResult<R
// but it costs an extra gas, so its on you how to use the storage
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MigrateMsg {}

#[entry_point]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
deps.api.debug("WASMDEBUG: migrate");
Expand Down
1 change: 1 addition & 0 deletions contracts/ibc_transfer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![warn(clippy::unwrap_used, clippy::expect_used)]

pub mod contract;
pub mod msg;
pub mod state;
20 changes: 20 additions & 0 deletions contracts/ibc_transfer/src/msg.rs
Original file line number Diff line number Diff line change
@@ -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<u64>,
},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct MigrateMsg {}
8 changes: 4 additions & 4 deletions contracts/neutron_interchain_txs/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions packages/neutron-sdk/schema/neutron_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
109 changes: 72 additions & 37 deletions scripts/test_ibc_transfer.sh
Original file line number Diff line number Diff line change
@@ -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
#!/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"
Loading

0 comments on commit a57fe4e

Please sign in to comment.