Skip to content

Commit

Permalink
Merge pull request #65 from oraichain/feat/orai-osmosis-ict
Browse files Browse the repository at this point in the history
Feat/orai-disable-force-transfer-tokenfactory
  • Loading branch information
ledanghuy1811 authored Jan 7, 2025
2 parents 3bddd33 + d271763 commit 67d483a
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 39 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/interchaintest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,20 @@ jobs:
uses: actions/checkout@v4

- run: make ictest-wasm-gasless
env:
BRANCH_CI: "latest"
ictest-tf-force-transfer-ibc:
runs-on: ubuntu-latest
needs: build-and-push-image # This job must run after build and push image
steps:
- name: Set up Go 1.22
uses: actions/setup-go@v4
with:
go-version: '1.22.7'

- name: checkout code
uses: actions/checkout@v4

- run: make ictest-tf-force-transfer-ibc
env:
BRANCH_CI: "latest"
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ ictest-tf-set-metadata:
ictest-ibchooks:
cd tests/interchaintest && go test -race -v -run TestIbcHooks .

# Executes ibc orai osmosis with token factory force transfer tests via interchaintest
ictest-tf-force-transfer-ibc:
cd tests/interchaintest && go test -race -v -run TestTokenFactoryForceTransferWithIbc .

# Executes wasm gas less tests via interchaintest
ictest-wasm-gasless:
cd tests/interchaintest && go test -race -v -run TestWasmGasLessContract .
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ var (

EnabledCapabilities = []string{
tokenfactorytypes.EnableBurnFrom,
tokenfactorytypes.EnableForceTransfer,
// tokenfactorytypes.EnableForceTransfer,
tokenfactorytypes.EnableSetMetadata,
}
)
Expand Down
67 changes: 67 additions & 0 deletions scripts/tests-0.50.4/test-tokenfactory-force-transfer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash

set -ux

CHAIN_ID=${CHAIN_ID:-testing}
USER=${USER:-"validator1"}
NODE_HOME=${NODE_HOME:-"$PWD/.oraid"}
ARGS="--from $USER --chain-id $CHAIN_ID -y --keyring-backend test --gas auto --gas-adjustment 1.5 -b sync --home $NODE_HOME"
HIDE_LOGS="/dev/null"

# prepare a new contract for gasless
fee_params=$(oraid query tokenfactory params --output json | jq '.params.denom_creation_fee[0].denom')
if ! [[ $fee_params =~ "orai" ]]; then
echo "Tokenfactory force transfer tests failed. The tokenfactory fee params is not orai"
exit 1
fi

# try creating a new denom
denom_name="usd"
oraid tx tokenfactory create-denom $denom_name $ARGS >$HIDE_LOGS

# try querying list denoms afterwards
# need to sleep 1s
sleep 1
user_address=$(oraid keys show $USER --home $NODE_HOME --keyring-backend test -a)
first_denom=$(oraid query tokenfactory denoms-from-creator $user_address --output json | jq '.denoms[0]' | tr -d '"')
echo "first denom: $first_denom"

if ! [[ $first_denom =~ "factory/$user_address/$denom_name" ]]; then
echo "Tokenfactory force transfer tests failed. The tokenfactory denom does not match the created denom"
exit 1
fi

admin=$(oraid query tokenfactory denom-authority-metadata $first_denom --output json | jq '.authority_metadata.admin')
echo "admin: $admin"

if ! [[ $admin =~ $user_address ]]; then
echo "Tokenfactory force transfer tests failed. The tokenfactory admin does not match the creator"
exit 1
fi

sleep 2
# try to mint token
oraid tx tokenfactory mint 10000$first_denom $ARGS >$HIDE_LOGS

# query balance after mint
# need sleep 1s
sleep 2
tokenfactory_balance=$(oraid query bank balance $user_address $first_denom --output json | jq '.balance.amount | tonumber')
if [[ $tokenfactory_balance -ne 10000 ]]; then
echo "Tokenfactory force transfer failed. The tokenfactory balance does not increase after mint"
exit 1
fi

# try to force transfer token to another address
oraid tx tokenfactory force-transfer 10$first_denom $user_address orai1cknd27x0244595pp7a5c9sdekl3ywl52x62ssn $ARGS &>$HIDE_LOGS

# query balance after force trasnfer
# need sleep 2s
sleep 2
tokenfactory_balance=$(oraid query bank balance $user_address $first_denom --output json | jq '.balance.amount | tonumber')
if ! [[ $tokenfactory_balance =~ 10000 ]]; then
echo "Tokenfactory force transfer failed. The tokenfactory balance decreases after force transfer"
exit 1
fi

echo "Tokenfactory force transfer tests passed!"
6 changes: 3 additions & 3 deletions tests/interchaintest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ module github.com/oraichain/wasmd/tests/interchaintest
go 1.23.3

require (
cosmossdk.io/math v1.3.0
cosmossdk.io/math v1.4.0
github.com/CosmWasm/wasmd v0.50.0
github.com/cometbft/cometbft v0.38.12
github.com/cosmos/cosmos-sdk v0.50.10
github.com/cosmos/ibc-go/v8 v8.4.0
github.com/docker/docker v24.0.9+incompatible
github.com/icza/dyno v0.0.0-20220812133438-f0b6f8a18845
github.com/strangelove-ventures/interchaintest/v8 v8.3.0
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -36,7 +38,6 @@ require (
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/ChainSafe/go-schnorrkel/1 v0.0.0-00010101000000-000000000000 // indirect
github.com/ComposableFi/go-subkey/v2 v2.0.0-tm03420 // indirect
github.com/CosmWasm/wasmd v0.50.0 // indirect
github.com/CosmWasm/wasmvm/v2 v2.1.3 // indirect
github.com/DataDog/datadog-go v4.8.3+incompatible // indirect
github.com/DataDog/zstd v1.5.5 // indirect
Expand Down Expand Up @@ -84,7 +85,6 @@ require (
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.9+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions tests/interchaintest/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0=
cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U=
cosmossdk.io/log v1.4.1 h1:wKdjfDRbDyZRuWa8M+9nuvpVYxrEOwbD/CA8hvhU8QM=
cosmossdk.io/log v1.4.1/go.mod h1:k08v0Pyq+gCP6phvdI6RCGhLf/r425UT6Rk/m+o74rU=
cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
cosmossdk.io/math v1.4.0 h1:XbgExXFnXmF/CccPPEto40gOO7FpWu9yWNAZPN3nkNQ=
cosmossdk.io/math v1.4.0/go.mod h1:O5PkD4apz2jZs4zqFdTr16e1dcaQCc5z6lkEnrrppuk=
cosmossdk.io/store v1.1.1 h1:NA3PioJtWDVU7cHHeyvdva5J/ggyLDkyH0hGHl2804Y=
cosmossdk.io/store v1.1.1/go.mod h1:8DwVTz83/2PSI366FERGbWSH7hL6sB7HbYp8bqksNwM=
cosmossdk.io/x/circuit v0.1.1 h1:KPJCnLChWrxD4jLwUiuQaf5mFD/1m7Omyo7oooefBVQ=
Expand Down
47 changes: 47 additions & 0 deletions tests/interchaintest/helpers/tokenfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,37 @@ func TxTokenFactoryCreateDenom(
return denom, txHash
}

func TxTokenFactoryMintToken(
t *testing.T,
ctx context.Context,
chain *cosmos.CosmosChain,
user ibc.Wallet,
denom string,
amount uint64,
) string {
tn := chain.GetNode()
txHash, err := tn.TokenFactoryMintDenom(ctx, user.KeyName(), denom, amount)
require.NoError(t, err)

return txHash
}

func TxTokenFactoryForceTransfer(
t *testing.T,
ctx context.Context,
chain *cosmos.CosmosChain,
user ibc.Wallet,
denom string,
amount uint64,
fromAddr string,
toAddr string,
) (string, error) {
tn := chain.GetNode()
txHash, err := tn.TokenFactoryForceTransferDenom(ctx, user.KeyName(), denom, amount, fromAddr, toAddr)

return txHash, err
}

func TxTokenFactoryModifyMetadata(
t *testing.T,
ctx context.Context,
Expand Down Expand Up @@ -161,3 +192,19 @@ func QueryDenomAuthorityMetadata(t *testing.T,

return res.AuthorityMetadata.Admin, nil
}

func QueryBalance(
t *testing.T,
ctx context.Context,
chain *cosmos.CosmosChain,
denom string,
userAddress string,
) (uint64, error) {
tn := chain.GetNode()
balance, err := tn.Chain.GetBalance(ctx, userAddress, denom)
if err != nil {
return 0, err
}

return balance.Uint64(), nil
}
4 changes: 2 additions & 2 deletions tests/interchaintest/ibc_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func TestIbcHooks(t *testing.T) {
}

t.Parallel()
chains := CreateChains(t, 1, 1)
chains := CreateChains(t, 1, 1, []string{"orai", "gaia"})
orai, gaia := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)
ic, r, ctx, _, eRep, _ := BuildInitialChain(t, chains)
ic, r, ctx, _, eRep, _ := BuildInitialChain(t, chains, pathOraiGaia)
t.Cleanup(func() {
_ = ic.Close()
})
Expand Down
130 changes: 130 additions & 0 deletions tests/interchaintest/orai_osmo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package interchaintest

import (
"context"
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
transfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
"github.com/oraichain/wasmd/tests/interchaintest/helpers"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"

"github.com/stretchr/testify/require"
)

// TestStartOrai is a basic test to assert that spinning up a Orai network with 1 validator works properly.
func TestTokenFactoryForceTransferWithIbc(t *testing.T) {
if testing.Short() {
t.Skip()
}

t.Parallel()

ctx := context.Background()

chains := CreateChains(t, 1, 1, []string{"orai", "osmosis"})
orai, osmo := chains[0].(*cosmos.CosmosChain), chains[1].(*cosmos.CosmosChain)

// Create relayer factory to utilize the go-relayer
ic, r, ctx, _, eRep, _ := BuildInitialChain(t, chains, pathOraiOsmo)
t.Cleanup(func() {
_ = ic.Close()
})

// Start the relayer
require.NoError(t, r.StartRelayer(ctx, eRep, pathOraiOsmo))
t.Cleanup(
func() {
err := r.StopRelayer(ctx, eRep)
if err != nil {
panic(fmt.Errorf("an error occurred while stopping the relayer: %s", err))
}
},
)

channel, err := ibc.GetTransferChannel(ctx, r, eRep, orai.Config().ChainID, osmo.Config().ChainID)
require.NoError(t, err)

users := CreateTestingUser(t, ctx, t.Name(), genesisWalletAmount, chains...)
// Get our Bech32 encoded user addresses
oraiUser, osmoUser := users[0], users[1]

oraiUserAddress := sdk.MustBech32ifyAddressBytes(orai.Config().Bech32Prefix, oraiUser.Address())
osmoUserAddr := sdk.MustBech32ifyAddressBytes(osmo.Config().Bech32Prefix, osmoUser.Address())

_ = oraiUserAddress
_ = osmoUserAddr
gas := uint64(100_000_000)

// create new token factory denom
expectedDenom, _ := helpers.TxTokenFactoryCreateDenom(t, ctx, orai, oraiUser, "orai-usd", gas)
denomCreated, err := helpers.QueryDenomsFromCreator(t, ctx, orai, oraiUserAddress)
require.NoError(t, err)
require.Contains(t, denomCreated, expectedDenom)

// mint token
tokenToMint := uint64(100_000_000_000)
_ = helpers.TxTokenFactoryMintToken(t, ctx, orai, oraiUser, expectedDenom, tokenToMint)
oraiUserBalance, err := helpers.QueryBalance(t, ctx, orai, expectedDenom, oraiUserAddress)
require.NoError(t, err)
require.Equal(t, tokenToMint, oraiUserBalance)

// get escrowed address
addr := types.GetEscrowAddress(channel.PortID, channel.ChannelID)
escrowedAddress := sdk.MustBech32ifyAddressBytes(orai.Config().Bech32Prefix, addr.Bytes())

// balance before transfer ibc must be 0
escrowedBalance, err := helpers.QueryBalance(t, ctx, orai, expectedDenom, escrowedAddress)
require.NoError(t, err)
require.Equal(t, escrowedBalance, uint64(0))

// ibc denom when transfer orai to osmosis
// transfer/channel-0/factory/orai14zqwen0pqj7s6drrkwaqwded7ajrq5czyw7fhq/orai-usd
oraiDenom := transfertypes.GetPrefixedDenom(channel.Counterparty.PortID, channel.Counterparty.ChannelID, expectedDenom)
// ibc/F859A4CC5A5EA6533657F6A83F7C11A479A13DBDC53F68135CDA95B0F12E5892
oraiIBCDenom := transfertypes.ParseDenomTrace(oraiDenom).IBCDenom()

// osmosis user balance before transfer ibc must be 0
userOsmosisBalance, err := helpers.QueryBalance(t, ctx, osmo, oraiIBCDenom, osmoUserAddr)
require.NoError(t, err)
require.Equal(t, userOsmosisBalance, uint64(0))

// try to transfer token factory to osmosis
transfer := ibc.WalletAmount{
Address: osmoUserAddr,
Denom: expectedDenom,
Amount: amountToSend,
}
transferTx, err := orai.SendIBCTransfer(ctx, channel.Counterparty.ChannelID, oraiUserAddress, transfer, ibc.TransferOptions{})
require.NoError(t, err)

// waiting for ACK -> transfer successfully
oraiHeight, err := orai.Height(ctx)
require.NoError(t, err)
_, err = testutil.PollForAck(ctx, orai, oraiHeight-5, oraiHeight+25, transferTx.Packet)
require.NoError(t, err)

// balance after transfer ibc must be equalt amount to send
escrowedBalance, err = helpers.QueryBalance(t, ctx, orai, expectedDenom, escrowedAddress)
fmt.Println("escrowed balance: ", escrowedBalance)
require.NoError(t, err)
require.Equal(t, escrowedBalance, uint64(amountToSend.Int64()))

// osmosis user balance after transfer ibc must be equal amount to send
userOsmosisBalance, err = helpers.QueryBalance(t, ctx, osmo, oraiIBCDenom, osmoUserAddr)
require.NoError(t, err)
require.Equal(t, userOsmosisBalance, uint64(amountToSend.Int64()))

// try to force transfer tokenfactory from escrowed address
_, err = helpers.TxTokenFactoryForceTransfer(t, ctx, orai, oraiUser, expectedDenom, uint64(amountToSend.Int64()), escrowedAddress, oraiUserAddress)
require.Error(t, err)

escrowedBalance, err = helpers.QueryBalance(t, ctx, orai, expectedDenom, escrowedAddress)
fmt.Println("escrowed balance: ", escrowedBalance)
require.NoError(t, err)
require.Equal(t, escrowedBalance, uint64(amountToSend.Int64()))
}
Loading

0 comments on commit 67d483a

Please sign in to comment.