Skip to content

Commit

Permalink
test: crosschain origin token (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 21, 2025
1 parent c4b423a commit 4346856
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 24 deletions.
10 changes: 5 additions & 5 deletions app/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/pundiai/fx-core/v8/app/keepers"
"github.com/pundiai/fx-core/v8/contract"
fxtypes "github.com/pundiai/fx-core/v8/types"
ethtypes "github.com/pundiai/fx-core/v8/x/eth/types"
)

func initChainer(ctx sdk.Context, keepers keepers.AppKeepers) error {
Expand All @@ -26,11 +25,12 @@ func initChainer(ctx sdk.Context, keepers keepers.AppKeepers) error {
return err
}

bridgeDenoms := []contract.BridgeDenoms{
{
ChainName: contract.MustStrToByte32(ethtypes.ModuleName),
bridgeDenoms := make([]contract.BridgeDenoms, 0)
for _, chainName := range fxtypes.GetSupportChains() {
bridgeDenoms = append(bridgeDenoms, contract.BridgeDenoms{
ChainName: contract.MustStrToByte32(chainName),
Denoms: []common.Hash{contract.MustStrToByte32(fxtypes.DefaultDenom)},
},
})
}

acc := keepers.AccountKeeper.GetModuleAddress(evmtypes.ModuleName)
Expand Down
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (suite *rpcTestSuite) TestClient_Tx() {
// 0. initAccount
// 1.fee_collector + 2.distribution + 3.bonded_tokens_pool + 4.not_bonded_tokens_pool + 5.gov + 6.mint + 7.autytypes.NewModuleAddress(crosschain)
// 8.evm 9.0x..1001 10.0x..1002 11.erc20 12.warp-token-contract
suite.Equal(authtypes.NewBaseAccount(toAddress, nil, uint64(21+i), 0), account)
suite.Equal(authtypes.NewBaseAccount(toAddress, nil, uint64(20+i), 0), account)
}

ethPrivKey := suite.GetPrivKeyByIndex(hd2.EthSecp256k1Type, 0)
Expand Down Expand Up @@ -224,7 +224,7 @@ func (suite *rpcTestSuite) TestClient_Tx() {

account, err := cli.QueryAccount(ethAddress.String())
suite.Require().NoError(err)
suite.Equal(authtypes.NewBaseAccount(ethAddress, nil, uint64(24), 0), account)
suite.Equal(authtypes.NewBaseAccount(ethAddress, nil, uint64(23), 0), account)
}

for i := 0; i < len(clients); i++ {
Expand Down
9 changes: 9 additions & 0 deletions contract/crosschain_precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,12 @@ func (k CrosschainPrecompileKeeper) ExecuteClaim(ctx context.Context, from commo
}
return unpackRetIsOk(k.abi, "executeClaim", res)
}

func (k CrosschainPrecompileKeeper) Crosschain(ctx context.Context, value *big.Int, from common.Address, args CrosschainArgs) (*evmtypes.MsgEthereumTxResponse, error) {
res, err := k.ApplyContract(ctx, from, k.contractAddr, value, k.abi, "crossChain",
args.Token, args.Receipt, args.Amount, args.Fee, args.Target, args.Memo)
if err != nil {
return nil, err
}
return unpackRetIsOk(k.abi, "crossChain", res)
}
9 changes: 6 additions & 3 deletions precompiles/crosschain/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,20 @@ func (suite *CrosschainPrecompileTestSuite) SetOracle(online bool) crosschaintyp
return oracle
}

func (suite *CrosschainPrecompileTestSuite) AddBridgeToken(symbolOrAddr string, isNative bool) common.Address {
func (suite *CrosschainPrecompileTestSuite) AddBridgeToken(symbolOrAddr string, isNativeOrOrigin bool) common.Address {
keeper := suite.App.Erc20Keeper
var erc20Token erc20types.ERC20Token
var err error
if isNative {
if isNativeOrOrigin {
erc20Token, err = keeper.RegisterNativeCoin(suite.Ctx, symbolOrAddr, symbolOrAddr, 18)
} else {
erc20Token, err = keeper.RegisterNativeERC20(suite.Ctx, common.HexToAddress(symbolOrAddr))
}
suite.Require().NoError(err)
err = keeper.AddBridgeToken(suite.Ctx, erc20Token.Denom, suite.chainName, erc20Token.Erc20Address, isNative)
if symbolOrAddr == fxtypes.DefaultSymbol {
isNativeOrOrigin = false
}
err = keeper.AddBridgeToken(suite.Ctx, erc20Token.Denom, suite.chainName, helpers.GenExternalAddr(suite.chainName), isNativeOrOrigin)
suite.Require().NoError(err)
return common.HexToAddress(erc20Token.Erc20Address)
}
33 changes: 33 additions & 0 deletions precompiles/crosschain/crosschain_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package crosschain_test

import (
"math/big"
"testing"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/pundiai/fx-core/v8/contract"
"github.com/pundiai/fx-core/v8/precompiles/crosschain"
"github.com/pundiai/fx-core/v8/testutil/helpers"
fxtypes "github.com/pundiai/fx-core/v8/types"
ethtypes "github.com/pundiai/fx-core/v8/x/eth/types"
)

func TestCrosschainABI(t *testing.T) {
Expand All @@ -16,3 +23,29 @@ func TestCrosschainABI(t *testing.T) {

require.Len(t, crosschainABI.Event.Inputs, 8)
}

func (suite *CrosschainPrecompileTestSuite) TestContract_Crosschain() {
suite.AddBridgeToken(fxtypes.DefaultSymbol, true)

suite.App.CrosschainKeepers.GetKeeper(suite.chainName).
SetLastObservedBlockHeight(suite.Ctx, 100, 100)

balance := suite.Balance(suite.signer.AccAddress())

txResponse := suite.Crosschain(suite.Ctx, big.NewInt(2), suite.signer.Address(),
contract.CrosschainArgs{
Token: common.Address{},
Receipt: helpers.GenExternalAddr(suite.chainName),
Amount: big.NewInt(1),
Fee: big.NewInt(1),
Target: contract.MustStrToByte32(suite.chainName),
Memo: "",
},
)
suite.NotNil(txResponse)
suite.Len(txResponse.Logs, 1)

transferCoin := helpers.NewStakingCoin(2, 0)
suite.AssertBalance(suite.signer.AccAddress(), balance.Sub(transferCoin)...)
suite.AssertBalance(authtypes.NewModuleAddress(ethtypes.ModuleName), transferCoin)
}
2 changes: 1 addition & 1 deletion testutil/helpers/base_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (s *BaseSuite) Balance(acc sdk.AccAddress) sdk.Coins {
func (s *BaseSuite) AssertBalance(addr sdk.AccAddress, expBal ...sdk.Coin) {
balances := s.App.BankKeeper.GetAllBalances(s.Ctx, addr)
for _, bal := range expBal {
s.Equal(bal.Amount, balances.AmountOf(bal.Denom), bal.Denom)
s.Equal(bal.Amount.String(), balances.AmountOf(bal.Denom).String(), bal.Denom)
}
}

Expand Down
6 changes: 6 additions & 0 deletions testutil/helpers/crosschain_precompile_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,9 @@ func (s CrosschainPrecompileSuite) ExecuteClaim(ctx context.Context, from common
s.requireError(err)
return res
}

func (s CrosschainPrecompileSuite) Crosschain(ctx context.Context, value *big.Int, from common.Address, args contract.CrosschainArgs) *evmtypes.MsgEthereumTxResponse {
res, err := s.CrosschainPrecompileKeeper.Crosschain(ctx, value, from, args)
s.requireError(err)
return res
}
8 changes: 3 additions & 5 deletions x/crosschain/keeper/bridge_call_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,10 @@ func (k Keeper) CrosschainBaseCoin(ctx sdk.Context, caller contract.Caller, from
if originToken {
return k.erc20Keeper.SetCache(ctx, types.NewIBCTransferKey(fxTarget.IBCChannel, sequence), amount.Amount)
}
} else {
if _, err := k.BuildOutgoingTxBatch(ctx, caller, from, receipt, amount, fee); err != nil {
return err
}
return nil
}
return nil
_, err := k.BuildOutgoingTxBatch(ctx, caller, from, receipt, amount, fee)
return err
}

func (k Keeper) IBCTransfer(ctx sdk.Context, from sdk.AccAddress, to string, amount sdk.Coin, channel, memo string) (uint64, error) {
Expand Down
2 changes: 1 addition & 1 deletion x/crosschain/keeper/outgoing_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (k Keeper) BuildOutgoingTxBatch(ctx sdk.Context, caller contract.Caller, se
bridgeFeeQuoteKeeper := contract.NewBridgeFeeQuoteKeeper(caller)
quoteInfos, err := bridgeFeeQuoteKeeper.GetDefaultOracleQuote(ctx, contract.MustStrToByte32(k.moduleName), contract.MustStrToByte32(fee.Denom))
if err != nil {
return 0, err
return 0, types.ErrInvalid.Wrapf("failed to get bridge fee quote: %s", err.Error())
}
var quoteInfo *contract.IBridgeFeeQuoteQuoteInfo
for _, quote := range quoteInfos {
Expand Down
7 changes: 0 additions & 7 deletions x/erc20/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

fxtypes "github.com/pundiai/fx-core/v8/types"
"github.com/pundiai/fx-core/v8/x/erc20/types"
)

Expand All @@ -19,12 +18,6 @@ func (k Keeper) InitGenesis(ctx sdk.Context, data types.GenesisState) error {
// NOTE: shouldn't occur
return sdkerrors.ErrNotFound.Wrapf("module account %s", types.ModuleName)
}

metadata := fxtypes.NewDefaultMetadata()
_, err := k.RegisterNativeCoin(ctx, metadata.Name, metadata.Symbol, fxtypes.DenomUnit)
if err != nil {
return sdkerrors.ErrLogic.Wrapf("failed to register native coin: %s", err.Error())
}
return nil
}

Expand Down

0 comments on commit 4346856

Please sign in to comment.