Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/remove-force-transfer-capapility #63

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 tests via interchaintest
ictest-orai-osmo:
cd tests/interchaintest && go test -race -v -run TestOraiOsmoIbc .

# 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!"
48 changes: 48 additions & 0 deletions tests/interchaintest/helpers/tokenfactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,38 @@ 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 {
tn := chain.GetNode()
txHash, err := tn.TokenFactoryForceTransferDenom(ctx, user.KeyName(), denom, amount, fromAddr, toAddr)
require.NoError(t, err)

return txHash
}

func TxTokenFactoryModifyMetadata(
t *testing.T,
ctx context.Context,
Expand Down Expand Up @@ -161,3 +193,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
}
128 changes: 128 additions & 0 deletions tests/interchaintest/orai_osmo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
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 TestOraiOsmoIbc(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
_ = helpers.TxTokenFactoryForceTransfer(t, ctx, orai, oraiUser, expectedDenom, uint64(amountToSend.Int64()), escrowedAddress, oraiUserAddress)
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
Loading