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

test: add bridge call unit test #951

Merged
merged 1 commit into from
Jan 24, 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
169 changes: 125 additions & 44 deletions precompiles/crosschain/bridge_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pundiai/fx-core/v8/precompiles/crosschain"
"github.com/pundiai/fx-core/v8/testutil/helpers"
fxtypes "github.com/pundiai/fx-core/v8/types"
crosschaintypes "github.com/pundiai/fx-core/v8/x/crosschain/types"
erc20types "github.com/pundiai/fx-core/v8/x/erc20/types"
ethtypes "github.com/pundiai/fx-core/v8/x/eth/types"
)
Expand Down Expand Up @@ -179,71 +180,151 @@ func TestContract_BridgeCall_NewBridgeCallEvent(t *testing.T) {
assert.EqualValues(t, expectTopic, topicNew)
}

func (suite *CrosschainPrecompileTestSuite) TestContract_BridgeCall() {
symbol := "USDT"
func (suite *CrosschainPrecompileTestSuite) TestContract_BridgeCall_NativeCoin() {
symbol := helpers.NewRandSymbol()
suite.AddBridgeToken(symbol, true)

baseDenom := strings.ToLower(symbol)
erc20token, err := suite.App.Erc20Keeper.GetERC20Token(suite.Ctx, baseDenom)
suite.Require().NoError(err)
suite.Quote(baseDenom)

erc20Contract := suite.GetERC20Token(baseDenom).GetERC20Contract()
suite.erc20TokenSuite.WithContract(erc20Contract)

amount := sdkmath.NewInt(100)
suite.DepositBridgeToken(erc20token, amount)

_, err = suite.GetERC20TokenKeeper().Approve(
suite.Ctx, erc20token.GetERC20Contract(), suite.signer.Address(), suite.crosschainAddr, big.NewInt(2))
suite.Require().NoError(err)

txResponse := suite.BridgeCall(suite.Ctx, nil, suite.signer.Address(),
contract.BridgeCallArgs{
DstChain: suite.chainName,
Refund: suite.signer.Address(),
Tokens: []common.Address{erc20token.GetERC20Contract()},
Amounts: []*big.Int{big.NewInt(2)},
To: suite.signer.Address(),
Data: nil,
QuoteId: big.NewInt(0),
GasLimit: big.NewInt(0),
Memo: nil,
})
suite.AddNativeCoinToEVM(baseDenom, amount)

feeAmount := big.NewInt(1)
transferAmount := big.NewInt(2)

approveAmount := transferAmount
if !suite.IsCallPrecompile() {
approveAmount = big.NewInt(0).Add(transferAmount, feeAmount)
}
suite.erc20TokenSuite.Approve(suite.Ctx, suite.crosschainAddr, approveAmount)

txResponse := suite.BridgeCall(suite.Ctx, nil,
suite.signer.Address(), suite.NewBridgeCallArgs(erc20Contract, transferAmount))
suite.NotNil(txResponse)
suite.GreaterOrEqual(len(txResponse.Logs), 3)

balance, err := suite.GetERC20TokenKeeper().
BalanceOf(suite.Ctx, erc20token.GetERC20Contract(), suite.signer.Address())
suite.Require().NoError(err)
suite.Equal(big.NewInt(98), balance)
balance := suite.erc20TokenSuite.BalanceOf(suite.Ctx, suite.signer.Address())
suite.Equal(big.NewInt(97), balance)

baseCoin := sdk.NewCoin(baseDenom, sdkmath.NewInt(98))
suite.AssertBalance(authtypes.NewModuleAddress(erc20types.ModuleName), baseCoin)

bridgeToken, err := suite.App.Erc20Keeper.GetBridgeToken(suite.Ctx, suite.chainName, baseDenom)
suite.Require().NoError(err)
bridgeFeeColBalance := suite.erc20TokenSuite.BalanceOf(suite.Ctx,
common.BytesToAddress(authtypes.NewModuleAddress(crosschaintypes.BridgeFeeCollectorName)))
suite.Equal(feeAmount, bridgeFeeColBalance)

bridgeToken := suite.GetBridgeToken(baseDenom)
bridgeCoin := sdk.NewCoin(bridgeToken.BridgeDenom(), sdkmath.NewInt(98))
suite.AssertBalance(authtypes.NewModuleAddress(crosschaintypes.ModuleName), bridgeCoin)
}

func (suite *CrosschainPrecompileTestSuite) TestContract_BridgeCall_NativeERC20() {
symbol := helpers.NewRandSymbol()

erc20TokenAddr := suite.erc20TokenSuite.DeployERC20Token(suite.Ctx, symbol)
suite.AddBridgeToken(erc20TokenAddr.String(), false)

baseDenom := strings.ToLower(symbol)
suite.Quote(baseDenom)

amount := sdkmath.NewInt(100)
suite.AddNativeERC20ToEVM(baseDenom, amount)

feeAmount := big.NewInt(1)
transferAmount := big.NewInt(2)

approveAmount := transferAmount
if !suite.IsCallPrecompile() {
approveAmount = big.NewInt(0).Add(transferAmount, feeAmount)
}
suite.erc20TokenSuite.Approve(suite.Ctx, suite.crosschainAddr, approveAmount)

txResponse := suite.BridgeCall(suite.Ctx, nil,
suite.signer.Address(), suite.NewBridgeCallArgs(erc20TokenAddr, transferAmount),
)
suite.NotNil(txResponse)
suite.GreaterOrEqual(len(txResponse.Logs), 3)

balance := suite.erc20TokenSuite.BalanceOf(suite.Ctx, suite.signer.Address())
suite.Equal(big.NewInt(97), balance)

baseCoin := sdk.NewCoin(baseDenom, sdkmath.NewInt(0))
suite.AssertBalance(authtypes.NewModuleAddress(erc20types.ModuleName), baseCoin)

bridgeFeeColBalance := suite.erc20TokenSuite.BalanceOf(suite.Ctx,
common.BytesToAddress(authtypes.NewModuleAddress(crosschaintypes.BridgeFeeCollectorName)))
suite.Equal(feeAmount, bridgeFeeColBalance)

bridgeToken := suite.GetBridgeToken(baseDenom)
bridgeCoin := sdk.NewCoin(bridgeToken.BridgeDenom(), sdkmath.NewInt(2))
suite.AssertBalance(authtypes.NewModuleAddress(suite.chainName), bridgeCoin)
}

func (suite *CrosschainPrecompileTestSuite) TestContract_BridgeCall_IBCToken() {
symbol := helpers.NewRandSymbol()

suite.AddBridgeToken(symbol, true, true)

baseDenom := strings.ToLower(symbol)
suite.Quote(baseDenom)

erc20Contract := suite.GetERC20Token(baseDenom).GetERC20Contract()
suite.erc20TokenSuite.WithContract(erc20Contract)

amount := sdkmath.NewInt(100)
suite.AddNativeCoinToEVM(baseDenom, amount, true)

feeAmount := big.NewInt(1)
transferAmount := big.NewInt(2)

approveAmount := transferAmount
if !suite.IsCallPrecompile() {
approveAmount = big.NewInt(0).Add(transferAmount, feeAmount)
}
suite.erc20TokenSuite.Approve(suite.Ctx, suite.crosschainAddr, approveAmount)

txResponse := suite.BridgeCall(suite.Ctx, nil,
suite.signer.Address(), suite.NewBridgeCallArgs(erc20Contract, transferAmount),
)
suite.NotNil(txResponse)
suite.GreaterOrEqual(len(txResponse.Logs), 3)

balance := suite.erc20TokenSuite.BalanceOf(suite.Ctx, suite.signer.Address())
suite.Equal(big.NewInt(97), balance)

baseCoin := sdk.NewCoin(baseDenom, sdkmath.NewInt(98))
suite.AssertBalance(authtypes.NewModuleAddress(erc20types.ModuleName), baseCoin)

bridgeFeeColBalance := suite.erc20TokenSuite.BalanceOf(suite.Ctx,
common.BytesToAddress(authtypes.NewModuleAddress(crosschaintypes.BridgeFeeCollectorName)))
suite.Equal(feeAmount, bridgeFeeColBalance)

bridgeToken := suite.GetBridgeToken(baseDenom)
bridgeCoin := sdk.NewCoin(bridgeToken.BridgeDenom(), sdkmath.NewInt(2))
suite.AssertBalance(authtypes.NewModuleAddress(suite.chainName), bridgeCoin)
}

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

suite.Quote(fxtypes.DefaultDenom)

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

value := big.NewInt(2)
txResponse := suite.BridgeCall(suite.Ctx, value, suite.signer.Address(),
contract.BridgeCallArgs{
DstChain: suite.chainName,
Refund: suite.signer.Address(),
Tokens: []common.Address{},
Amounts: []*big.Int{},
To: suite.signer.Address(),
Data: nil,
QuoteId: big.NewInt(0),
GasLimit: big.NewInt(0),
Memo: nil,
})
if !suite.IsCallPrecompile() {
value = big.NewInt(3) // add fee
}
txResponse := suite.BridgeCall(suite.Ctx, value,
suite.signer.Address(), suite.NewBridgeCallArgs(common.Address{}, nil))
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)
suite.AssertBalance(suite.signer.AccAddress(), balance.Sub(helpers.NewStakingCoin(3, 0))...)
suite.AssertBalance(authtypes.NewModuleAddress(crosschaintypes.BridgeFeeCollectorName), helpers.NewStakingCoin(1, 0))
suite.AssertBalance(authtypes.NewModuleAddress(ethtypes.ModuleName), helpers.NewStakingCoin(2, 0))
}
78 changes: 63 additions & 15 deletions precompiles/crosschain/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ type CrosschainPrecompileTestSuite struct {
chainName string

helpers.CrosschainPrecompileSuite
bridgeFeeSuite helpers.BridgeFeeSuite
bridgeFeeSuite helpers.BridgeFeeSuite
erc20TokenSuite helpers.ERC20TokenSuite
}

func TestCrosschainPrecompileTestSuite(t *testing.T) {
Expand Down Expand Up @@ -59,6 +60,7 @@ func (suite *CrosschainPrecompileTestSuite) SetupTest() {

suite.CrosschainPrecompileSuite = helpers.NewCrosschainPrecompileSuite(suite.Require(), suite.signer, suite.App.EvmKeeper, suite.crosschainAddr)
suite.bridgeFeeSuite = helpers.NewBridgeFeeSuite(suite.Require(), suite.App.EvmKeeper)
suite.erc20TokenSuite = helpers.NewERC20Suite(suite.Require(), suite.signer, suite.App.EvmKeeper)

chainNames := fxtypes.GetSupportChains()
suite.chainName = chainNames[tmrand.Intn(len(chainNames))]
Expand All @@ -82,10 +84,6 @@ func (suite *CrosschainPrecompileTestSuite) IsCallPrecompile() bool {
return suite.crosschainAddr.String() == contract.CrosschainAddress
}

func (suite *CrosschainPrecompileTestSuite) GetERC20TokenKeeper() contract.ERC20TokenKeeper {
return contract.NewERC20TokenKeeper(suite.App.EvmKeeper)
}

func (suite *CrosschainPrecompileTestSuite) SetOracle(online bool) crosschaintypes.Oracle {
oracle := crosschaintypes.Oracle{
OracleAddress: helpers.GenAccAddress().String(),
Expand All @@ -104,28 +102,78 @@ func (suite *CrosschainPrecompileTestSuite) SetOracle(online bool) crosschaintyp
return oracle
}

func (suite *CrosschainPrecompileTestSuite) AddBridgeToken(symbolOrAddr string, isNativeOrOrigin bool) {
func (suite *CrosschainPrecompileTestSuite) GetERC20Token(baseDenom string) *erc20types.ERC20Token {
erc20token, err := suite.App.Erc20Keeper.GetERC20Token(suite.Ctx, baseDenom)
suite.Require().NoError(err)
return &erc20token
}

func (suite *CrosschainPrecompileTestSuite) GetBridgeToken(baseDenom string) erc20types.BridgeToken {
bridgeToken, err := suite.App.Erc20Keeper.GetBridgeToken(suite.Ctx, suite.chainName, baseDenom)
suite.Require().NoError(err)
return bridgeToken
}

func (suite *CrosschainPrecompileTestSuite) AddBridgeToken(symbolOrAddr string, isNativeCoin bool, isIBC ...bool) {
keeper := suite.App.Erc20Keeper
var erc20Token erc20types.ERC20Token
var err error
if isNativeOrOrigin {
isNative := false
if isNativeCoin || symbolOrAddr == fxtypes.DefaultSymbol {
erc20Token, err = keeper.RegisterNativeCoin(suite.Ctx, symbolOrAddr, symbolOrAddr, 18)
} else {
isNative = true
erc20Token, err = keeper.RegisterNativeERC20(suite.Ctx, common.HexToAddress(symbolOrAddr))
}
suite.Require().NoError(err)
if symbolOrAddr == fxtypes.DefaultSymbol {
isNativeOrOrigin = false
if len(isIBC) > 0 && isIBC[0] {
isNative = true
}
err = keeper.AddBridgeToken(suite.Ctx, erc20Token.Denom, suite.chainName, helpers.GenExternalAddr(suite.chainName), isNativeOrOrigin)
suite.Require().NoError(err)
err = keeper.AddBridgeToken(suite.Ctx, erc20Token.Denom, suite.chainName, helpers.GenExternalAddr(suite.chainName), isNative)
suite.Require().NoError(err)
}

func (suite *CrosschainPrecompileTestSuite) DepositBridgeToken(erc20token erc20types.ERC20Token, amount sdkmath.Int) {
func (suite *CrosschainPrecompileTestSuite) AddNativeERC20ToEVM(baseDenom string, amount sdkmath.Int) {
minter := common.BytesToAddress(authtypes.NewModuleAddress(erc20types.ModuleName).Bytes())
_, err := suite.GetERC20TokenKeeper().Mint(suite.Ctx, erc20token.GetERC20Contract(), minter, suite.signer.Address(), big.NewInt(100))
suite.Require().NoError(err)
suite.MintTokenToModule(erc20types.ModuleName, sdk.NewCoin(erc20token.Denom, amount))
erc20token := suite.GetERC20Token(baseDenom)
if erc20token.IsNativeERC20() {
minter = suite.signer.Address()
}
suite.erc20TokenSuite.Mint(suite.Ctx, minter, suite.signer.Address(), amount.BigInt())
balance := suite.erc20TokenSuite.BalanceOf(suite.Ctx, suite.signer.Address())
suite.Equal(balance.String(), amount.BigInt().String())
}

func (suite *CrosschainPrecompileTestSuite) AddNativeCoinToEVM(baseDenom string, amount sdkmath.Int, isIBC ...bool) {
suite.AddNativeERC20ToEVM(baseDenom, amount)

suite.MintTokenToModule(erc20types.ModuleName, sdk.NewCoin(baseDenom, amount))

if len(isIBC) == 0 || !isIBC[0] {
bridgeToken := suite.GetBridgeToken(baseDenom)
suite.MintTokenToModule(crosschaintypes.ModuleName, sdk.NewCoin(bridgeToken.BridgeDenom(), amount))
}
}

func (suite *CrosschainPrecompileTestSuite) NewBridgeCallArgs(erc20Contract common.Address, amount *big.Int) contract.BridgeCallArgs {
args := contract.BridgeCallArgs{
DstChain: suite.chainName,
Refund: suite.signer.Address(),
Tokens: []common.Address{erc20Contract},
Amounts: []*big.Int{amount},
To: suite.signer.Address(),
Data: nil,
QuoteId: big.NewInt(1),
GasLimit: big.NewInt(0),
Memo: nil,
}
if contract.IsZeroEthAddress(erc20Contract) {
args.Tokens = []common.Address{}
}
if amount == nil {
args.Amounts = []*big.Int{}
}
return args
}

func (suite *CrosschainPrecompileTestSuite) Quote(denom string) {
Expand Down
Loading
Loading