diff --git a/tests/crosschain_suite.go b/tests/crosschain_suite.go index 3cb7660d..72f90d3e 100644 --- a/tests/crosschain_suite.go +++ b/tests/crosschain_suite.go @@ -12,11 +12,12 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" - gethcommon "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" ethcrypto "github.com/ethereum/go-ethereum/crypto" "github.com/functionx/fx-core/v8/client" + "github.com/functionx/fx-core/v8/contract" "github.com/functionx/fx-core/v8/testutil/helpers" fxtypes "github.com/functionx/fx-core/v8/types" "github.com/functionx/fx-core/v8/x/crosschain/precompile" @@ -33,6 +34,8 @@ type CrosschainTestSuite struct { externalPrivKey *ecdsa.PrivateKey privKey cryptotypes.PrivKey executeClaimPrivKey cryptotypes.PrivKey + + crosschainAddr common.Address } func NewCrosschainWithTestSuite(chainName string, ts *TestSuite) CrosschainTestSuite { @@ -48,6 +51,7 @@ func NewCrosschainWithTestSuite(chainName string, ts *TestSuite) CrosschainTestS externalPrivKey: externalPrivKey, privKey: helpers.NewEthPrivKey(), executeClaimPrivKey: helpers.NewEthPrivKey(), + crosschainAddr: common.HexToAddress(contract.CrosschainAddress), } } @@ -80,8 +84,8 @@ func (suite *CrosschainTestSuite) ExecuteClaimAccAddress() sdk.AccAddress { return suite.executeClaimPrivKey.PubKey().Address().Bytes() } -func (suite *CrosschainTestSuite) HexAddress() gethcommon.Address { - return gethcommon.BytesToAddress(suite.privKey.PubKey().Address()) +func (suite *CrosschainTestSuite) HexAddress() common.Address { + return common.BytesToAddress(suite.privKey.PubKey().Address()) } func (suite *CrosschainTestSuite) HexAddressString() string { @@ -450,7 +454,7 @@ func (suite *CrosschainTestSuite) AddBridgeToken(md banktypes.Metadata) (string, } } -func (suite *CrosschainTestSuite) FormatAddress(address gethcommon.Address) string { +func (suite *CrosschainTestSuite) FormatAddress(address common.Address) string { return crosschaintypes.ExternalAddrToStr(suite.chainName, address.Bytes()) } @@ -524,8 +528,7 @@ func (suite *CrosschainTestSuite) ExecuteClaim() *ethtypes.Transaction { }) suite.Require().NoError(err) - address := crosschaintypes.GetAddress() - ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), suite.executeClaimPrivKey, &address, nil, pack) + ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), suite.executeClaimPrivKey, &suite.crosschainAddr, nil, pack) suite.Require().NoError(err) suite.SendTransaction(ethTx) @@ -560,3 +563,26 @@ func (suite *CrosschainTestSuite) UpdateParams(opts ...func(params *crosschainty } return suite.BroadcastProposalTx2([]sdk.Msg{msg}, "UpdateParams", "UpdateParams") } + +func (suite *CrosschainTestSuite) Crosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { + privateKey := suite.privKey + crosschainContract := suite.crosschainAddr + suite.ApproveERC20(privateKey, token, crosschainContract, big.NewInt(0).Add(amount, fee)) + + beforeBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) + pack, err := precompile.NewCrosschainMethod(nil).PackInput(crosschaintypes.CrosschainArgs{ + Token: token, + Receipt: recipient, + Amount: amount, + Fee: fee, + Target: fxtypes.MustStrToByte32(target), + Memo: "", + }) + suite.Require().NoError(err) + ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &crosschainContract, nil, pack) + suite.Require().NoError(err, target) + suite.SendTransaction(ethTx) + afterBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) + suite.Require().True(new(big.Int).Sub(beforeBalanceOf, afterBalanceOf).Cmp(new(big.Int).Add(amount, fee)) == 0) + return ethTx +} diff --git a/tests/precompile_suite.go b/tests/precompile_suite.go deleted file mode 100644 index 7c87e126..00000000 --- a/tests/precompile_suite.go +++ /dev/null @@ -1,51 +0,0 @@ -package tests - -import ( - "math/big" - - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/common" - ethtypes "github.com/ethereum/go-ethereum/core/types" - - "github.com/functionx/fx-core/v8/client" - "github.com/functionx/fx-core/v8/testutil/helpers" - fxtypes "github.com/functionx/fx-core/v8/types" - crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" -) - -type PrecompileTestSuite struct { - EvmTestSuite - privKey cryptotypes.PrivKey -} - -func NewPrecompileTestSuite(ts *TestSuite) PrecompileTestSuite { - return PrecompileTestSuite{ - EvmTestSuite: NewEvmTestSuite(ts), - privKey: helpers.NewEthPrivKey(), - } -} - -func (suite *PrecompileTestSuite) AccAddress() sdk.AccAddress { - return sdk.AccAddress(suite.privKey.PubKey().Address()) -} - -func (suite *PrecompileTestSuite) HexAddress() common.Address { - return common.BytesToAddress(suite.privKey.PubKey().Address()) -} - -func (suite *PrecompileTestSuite) Crosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { - privateKey := suite.privKey - crosschainContract := crosschaintypes.GetAddress() - suite.ApproveERC20(privateKey, token, crosschainContract, big.NewInt(0).Add(amount, fee)) - - beforeBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) - pack, err := crosschaintypes.GetABI().Pack("crossChain", token, recipient, amount, fee, fxtypes.MustStrToByte32(target), "") - suite.Require().NoError(err) - ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &crosschainContract, nil, pack) - suite.Require().NoError(err, target) - suite.SendTransaction(ethTx) - afterBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) - suite.Require().True(new(big.Int).Sub(beforeBalanceOf, afterBalanceOf).Cmp(new(big.Int).Add(amount, fee)) == 0) - return ethTx -} diff --git a/x/crosschain/precompile/bridge_call.go b/x/crosschain/precompile/bridge_call.go index b407c6a9..e29d925c 100644 --- a/x/crosschain/precompile/bridge_call.go +++ b/x/crosschain/precompile/bridge_call.go @@ -10,6 +10,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" + fxcontract "github.com/functionx/fx-core/v8/contract" fxtypes "github.com/functionx/fx-core/v8/types" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" evmtypes "github.com/functionx/fx-core/v8/x/evm/types" @@ -24,8 +25,8 @@ type BridgeCallMethod struct { func NewBridgeCallMethod(keeper *Keeper) *BridgeCallMethod { return &BridgeCallMethod{ Keeper: keeper, - Method: crosschaintypes.GetABI().Methods["bridgeCall"], - Event: crosschaintypes.GetABI().Events["BridgeCallEvent"], + Method: crosschainABI.Methods["bridgeCall"], + Event: crosschainABI.Events["BridgeCallEvent"], } } @@ -61,7 +62,7 @@ func (m *BridgeCallMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro originTokenAmount := sdkmath.ZeroInt() if value.Sign() > 0 { baseCoin := sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(value)) - if err = m.bankKeeper.SendCoins(ctx, crosschaintypes.GetAddress().Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil { + if err = m.bankKeeper.SendCoins(ctx, crosschainAddress.Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil { return err } baseCoins = append(baseCoins, baseCoin) @@ -93,7 +94,7 @@ func (m *BridgeCallMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, crosschainAddress, data, topic) result, err = m.PackOutput(eventNonce) return err diff --git a/x/crosschain/precompile/bridge_coin_amount.go b/x/crosschain/precompile/bridge_coin_amount.go index 37ad2328..fb169467 100644 --- a/x/crosschain/precompile/bridge_coin_amount.go +++ b/x/crosschain/precompile/bridge_coin_amount.go @@ -21,7 +21,7 @@ type BridgeCoinAmountMethod struct { func NewBridgeCoinAmountMethod(keeper *Keeper) *BridgeCoinAmountMethod { return &BridgeCoinAmountMethod{ Keeper: keeper, - Method: crosschaintypes.GetABI().Methods["bridgeCoinAmount"], + Method: crosschainABI.Methods["bridgeCoinAmount"], } } diff --git a/x/crosschain/precompile/contract.go b/x/crosschain/precompile/contract.go index f121e617..2454e8c9 100644 --- a/x/crosschain/precompile/contract.go +++ b/x/crosschain/precompile/contract.go @@ -5,14 +5,17 @@ import ( "errors" "github.com/ethereum/go-ethereum/common" - ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" "github.com/functionx/fx-core/v8/contract" - crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" evmtypes "github.com/functionx/fx-core/v8/x/evm/types" ) +var ( + crosschainABI = contract.MustABIJson(contract.ICrosschainMetaData.ABI) + crosschainAddress = common.HexToAddress(contract.CrosschainAddress) +) + type Contract struct { methods []contract.PrecompileMethod govKeeper GovKeeper @@ -42,7 +45,7 @@ func NewPrecompiledContract( } func (c *Contract) Address() common.Address { - return crosschaintypes.GetAddress() + return crosschainAddress } func (c *Contract) IsStateful() bool { @@ -86,12 +89,3 @@ func (c *Contract) Run(evm *vm.EVM, vmContract *vm.Contract, readonly bool) (ret } return contract.PackRetErrV2(errors.New("unknown method")) } - -func EmitEvent(evm *vm.EVM, data []byte, topics []common.Hash) { - evm.StateDB.AddLog(ðtypes.Log{ - Address: crosschaintypes.GetAddress(), - Topics: topics, - Data: data, - BlockNumber: evm.Context.BlockNumber.Uint64(), - }) -} diff --git a/x/crosschain/precompile/contract_test.go b/x/crosschain/precompile/contract_test.go index db82f10c..70c1514e 100644 --- a/x/crosschain/precompile/contract_test.go +++ b/x/crosschain/precompile/contract_test.go @@ -41,7 +41,7 @@ type PrecompileTestSuite struct { helpers.BaseSuite // signer *helpers.Signer - // crosschain common.Address + crosschainAddr common.Address } func TestPrecompileTestSuite(t *testing.T) { @@ -57,7 +57,9 @@ func (suite *PrecompileTestSuite) SetupTest() { // crosschainContract, err := suite.App.EvmKeeper.DeployContract(suite.Ctx, suite.signer.Address(), contract.MustABIJson(testscontract.CrosschainTestMetaData.ABI), contract.MustDecodeHex(testscontract.CrosschainTestMetaData.Bin)) // suite.Require().NoError(err) - // suite.crosschain = crosschainContract + // suite.crosschainAddrTest = crosschainContract + + suite.crosschainAddr = common.HexToAddress(contract.CrosschainAddress) } func (suite *PrecompileTestSuite) SetupSubTest() { diff --git a/x/crosschain/precompile/crosschain.go b/x/crosschain/precompile/crosschain.go index ff7026ad..78d3d6f8 100644 --- a/x/crosschain/precompile/crosschain.go +++ b/x/crosschain/precompile/crosschain.go @@ -27,8 +27,8 @@ type CrosschainMethod struct { func NewCrosschainMethod(keeper *Keeper) *CrosschainMethod { return &CrosschainMethod{ Keeper: keeper, - Method: crosschaintypes.GetABI().Methods["crossChain"], - Event: crosschaintypes.GetABI().Events["CrossChain"], + Method: crosschainABI.Methods["crossChain"], + Event: crosschainABI.Events["CrossChain"], } } @@ -80,7 +80,7 @@ func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro } baseCoin = sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(totalAmount)) - if err = m.bankKeeper.SendCoins(ctx, crosschaintypes.GetAddress().Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil { + if err = m.bankKeeper.SendCoins(ctx, crosschainAddress.Bytes(), sender.Bytes(), sdk.NewCoins(baseCoin)); err != nil { return err } } else { @@ -101,7 +101,7 @@ func (m *CrosschainMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, crosschainAddress, data, topic) return nil }); err != nil { diff --git a/x/crosschain/precompile/execute_claim.go b/x/crosschain/precompile/execute_claim.go index c6fbb300..03edf714 100644 --- a/x/crosschain/precompile/execute_claim.go +++ b/x/crosschain/precompile/execute_claim.go @@ -9,12 +9,12 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" - "github.com/functionx/fx-core/v8/contract" + fxcontract "github.com/functionx/fx-core/v8/contract" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" evmtypes "github.com/functionx/fx-core/v8/x/evm/types" ) -var _ contract.PrecompileMethod = (*ExecuteClaimMethod)(nil) +var _ fxcontract.PrecompileMethod = (*ExecuteClaimMethod)(nil) type ExecuteClaimMethod struct { *Keeper @@ -25,8 +25,8 @@ type ExecuteClaimMethod struct { func NewExecuteClaimMethod(keeper *Keeper) *ExecuteClaimMethod { return &ExecuteClaimMethod{ Keeper: keeper, - Method: crosschaintypes.GetABI().Methods["executeClaim"], - Event: crosschaintypes.GetABI().Events["ExecuteClaimEvent"], + Method: crosschainABI.Methods["executeClaim"], + Event: crosschainABI.Events["ExecuteClaimEvent"], } } @@ -65,7 +65,7 @@ func (m *ExecuteClaimMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, er if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, crosschainAddress, data, topic) return nil }); err != nil { return nil, err diff --git a/x/crosschain/precompile/has_oracle.go b/x/crosschain/precompile/has_oracle.go index c052a48b..295ca725 100644 --- a/x/crosschain/precompile/has_oracle.go +++ b/x/crosschain/precompile/has_oracle.go @@ -18,7 +18,7 @@ type HasOracleMethod struct { func NewHasOracleMethod(keeper *Keeper) *HasOracleMethod { return &HasOracleMethod{ Keeper: keeper, - Method: crosschaintypes.GetABI().Methods["hasOracle"], + Method: crosschainABI.Methods["hasOracle"], } } diff --git a/x/crosschain/precompile/has_oracle_test.go b/x/crosschain/precompile/has_oracle_test.go index 4b49a372..b61a3aff 100644 --- a/x/crosschain/precompile/has_oracle_test.go +++ b/x/crosschain/precompile/has_oracle_test.go @@ -68,9 +68,7 @@ func (suite *PrecompileTestSuite) TestHasOracle() { packData, err := hasOracle.PackInput(args) suite.Require().NoError(err) - contractAddr := crosschaintypes.GetAddress() - - res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &contractAddr, nil, packData, false) + res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &suite.crosschainAddr, nil, packData, false) if err != nil { suite.Require().EqualError(err, expectErr.Error()) return diff --git a/x/crosschain/precompile/is_oracle_online.go b/x/crosschain/precompile/is_oracle_online.go index 0b023d62..a431743f 100644 --- a/x/crosschain/precompile/is_oracle_online.go +++ b/x/crosschain/precompile/is_oracle_online.go @@ -18,7 +18,7 @@ type IsOracleOnlineMethod struct { func NewIsOracleOnlineMethod(keeper *Keeper) *IsOracleOnlineMethod { return &IsOracleOnlineMethod{ Keeper: keeper, - Method: crosschaintypes.GetABI().Methods["isOracleOnline"], + Method: crosschainABI.Methods["isOracleOnline"], } } diff --git a/x/crosschain/precompile/is_oracle_online_test.go b/x/crosschain/precompile/is_oracle_online_test.go index a37dc804..22ac36ac 100644 --- a/x/crosschain/precompile/is_oracle_online_test.go +++ b/x/crosschain/precompile/is_oracle_online_test.go @@ -80,9 +80,7 @@ func (suite *PrecompileTestSuite) TestIsOracleOnline() { packData, err := hasOracle.PackInput(args) suite.Require().NoError(err) - contractAddr := crosschaintypes.GetAddress() - - res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &contractAddr, nil, packData, false) + res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, signer.Address(), &suite.crosschainAddr, nil, packData, false) if err != nil { suite.Require().EqualError(err, expectErr.Error()) return diff --git a/x/crosschain/types/contract.go b/x/crosschain/types/contract.go index 734bbdcd..d8f718d6 100644 --- a/x/crosschain/types/contract.go +++ b/x/crosschain/types/contract.go @@ -4,25 +4,11 @@ import ( "errors" "math/big" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/functionx/fx-core/v8/contract" ) -var ( - crosschainAddress = common.HexToAddress(contract.CrosschainAddress) - crosschainABI = contract.MustABIJson(contract.ICrosschainMetaData.ABI) -) - -func GetAddress() common.Address { - return crosschainAddress -} - -func GetABI() abi.ABI { - return crosschainABI -} - type BridgeCoinAmountArgs struct { Token common.Address `abi:"_token"` Target [32]byte `abi:"_target"`