diff --git a/contract/precompile.go b/contract/precompile.go index 688bf65d..c146e833 100644 --- a/contract/precompile.go +++ b/contract/precompile.go @@ -6,6 +6,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/vm" ) @@ -16,6 +17,15 @@ type PrecompileMethod interface { Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) } +func EmitEvent(evm *vm.EVM, address common.Address, data []byte, topics []common.Hash) { + evm.StateDB.AddLog(ðtypes.Log{ + Address: address, + Topics: topics, + Data: data, + BlockNumber: evm.Context.BlockNumber.Uint64(), + }) +} + type ERC20Call struct { ERC20ABI evm *vm.EVM diff --git a/tests/staking_suite.go b/tests/staking_suite.go index 30766edf..5ea4d142 100644 --- a/tests/staking_suite.go +++ b/tests/staking_suite.go @@ -27,14 +27,16 @@ import ( type StakingSuite struct { Erc20TestSuite - grantKey cryptotypes.PrivKey + grantKey cryptotypes.PrivKey + stakingContract common.Address } func NewStakingSuite(ts *TestSuite) StakingSuite { key := helpers.NewEthPrivKey() return StakingSuite{ - Erc20TestSuite: NewErc20TestSuite(ts), - grantKey: key, + Erc20TestSuite: NewErc20TestSuite(ts), + grantKey: key, + stakingContract: common.HexToAddress(contract.StakingAddress), } } @@ -102,11 +104,10 @@ func (suite *StakingSuite) SetWithdrawAddressWithResponse(privKey cryptotypes.Pr } func (suite *StakingSuite) send(privateKey cryptotypes.PrivKey, value *big.Int, data []byte) *ethtypes.Receipt { - stakingContract := stakingprecompile.GetAddress() if value == nil { value = big.NewInt(0) } - transaction, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &stakingContract, value, data) + transaction, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &suite.stakingContract, value, data) suite.Require().NoError(err) return suite.SendTransaction(transaction) } @@ -157,11 +158,10 @@ func (suite *StakingSuite) WithdrawReward(privateKey cryptotypes.PrivKey, valAdd } func (suite *StakingSuite) Delegation(valAddr string, delAddr common.Address) (*big.Int, *big.Int) { - stakingContract := stakingprecompile.GetAddress() method := stakingprecompile.NewDelegationMethod(nil) pack, err := method.PackInput(fxstakingtypes.DelegationArgs{Validator: valAddr, Delegator: delAddr}) suite.Require().NoError(err) - output, err := suite.EthClient().CallContract(suite.ctx, ethereum.CallMsg{To: &stakingContract, Data: pack}, nil) + output, err := suite.EthClient().CallContract(suite.ctx, ethereum.CallMsg{To: &suite.stakingContract, Data: pack}, nil) suite.Require().NoError(err) shares, amount, err := method.UnpackOutput(output) suite.Require().NoError(err) @@ -169,11 +169,10 @@ func (suite *StakingSuite) Delegation(valAddr string, delAddr common.Address) (* } func (suite *StakingSuite) Rewards(valAddr string, delAddr common.Address) *big.Int { - stakingContract := stakingprecompile.GetAddress() method := stakingprecompile.NewDelegationRewardsMethod(nil) pack, err := method.PackInput(fxstakingtypes.DelegationRewardsArgs{Validator: valAddr, Delegator: delAddr}) suite.Require().NoError(err) - output, err := suite.EthClient().CallContract(suite.ctx, ethereum.CallMsg{To: &stakingContract, Data: pack}, nil) + output, err := suite.EthClient().CallContract(suite.ctx, ethereum.CallMsg{To: &suite.stakingContract, Data: pack}, nil) suite.Require().NoError(err) amount, err := method.UnpackOutput(output) suite.Require().NoError(err) @@ -236,11 +235,10 @@ func (suite *StakingSuite) ApproveShares(privateKey cryptotypes.PrivKey, valAddr } func (suite *StakingSuite) AllowanceShares(valAddr string, owner, spender common.Address) *big.Int { - stakingContract := stakingprecompile.GetAddress() method := stakingprecompile.NewAllowanceSharesMethod(nil) pack, err := method.PackInput(fxstakingtypes.AllowanceSharesArgs{Validator: valAddr, Owner: owner, Spender: spender}) suite.Require().NoError(err) - output, err := suite.EthClient().CallContract(suite.ctx, ethereum.CallMsg{To: &stakingContract, Data: pack}, nil) + output, err := suite.EthClient().CallContract(suite.ctx, ethereum.CallMsg{To: &suite.stakingContract, Data: pack}, nil) suite.Require().NoError(err) amount, err := method.UnpackOutput(output) suite.Require().NoError(err) @@ -250,7 +248,7 @@ func (suite *StakingSuite) AllowanceShares(valAddr string, owner, spender common func (suite *StakingSuite) LogReward(logs []*ethtypes.Log, valAddr string, addr common.Address) *big.Int { method := stakingprecompile.NewWithdrawMethod(nil) for _, log := range logs { - if log.Address == stakingprecompile.GetAddress() && + if log.Address == suite.stakingContract && log.Topics[0] == method.Event.ID && log.Topics[1] == addr.Hash() { unpack, err := method.Event.Inputs.NonIndexed().Unpack(log.Data) diff --git a/tests/staking_test.go b/tests/staking_test.go index 4e31729c..087d613d 100644 --- a/tests/staking_test.go +++ b/tests/staking_test.go @@ -90,7 +90,7 @@ func (suite *IntegrationTest) StakingContractTest() { withdrawMethod := stakingprecompile.NewWithdrawMethod(nil) for _, log := range receipt.Logs { - if log.Address == stakingprecompile.GetAddress() && log.Topics[0] == withdrawMethod.Event.ID { + if log.Address == suite.staking.stakingContract && log.Topics[0] == withdrawMethod.Event.ID { unpack, err := withdrawMethod.Event.Inputs.NonIndexed().Unpack(log.Data) suite.Require().NoError(err) reward := unpack[1].(*big.Int) diff --git a/x/staking/precompile/allowance_shares.go b/x/staking/precompile/allowance_shares.go index f245bc25..5674bce2 100644 --- a/x/staking/precompile/allowance_shares.go +++ b/x/staking/precompile/allowance_shares.go @@ -18,7 +18,7 @@ type AllowanceSharesMethod struct { func NewAllowanceSharesMethod(keeper *Keeper) *AllowanceSharesMethod { return &AllowanceSharesMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["allowanceShares"], + Method: stakingABI.Methods["allowanceShares"], } } diff --git a/x/staking/precompile/allowance_shares_test.go b/x/staking/precompile/allowance_shares_test.go index 443e490a..2495e37a 100644 --- a/x/staking/precompile/allowance_shares_test.go +++ b/x/staking/precompile/allowance_shares_test.go @@ -9,8 +9,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" @@ -24,7 +22,6 @@ func TestStakingAllowanceSharesABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestAllowanceShares() { - allowanceSharesMethod := precompile.NewAllowanceSharesMethod(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, owner, spender *helpers.Signer) (types.AllowanceSharesArgs, error) @@ -116,21 +113,19 @@ func (suite *PrecompileTestSuite) TestAllowanceShares() { args, errResult := tc.malleate(operator, owner, spender) - packData, err := allowanceSharesMethod.PackInput(args) + packData, err := suite.allowanceSharesMethod.PackInput(args) suite.Require().NoError(err) - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestAllowanceSharesName, args.Validator, args.Owner, args.Spender) - suite.Require().NoError(err) + stakingContract = suite.stakingTestAddr } res := suite.EthereumTx(owner, stakingContract, big.NewInt(0), packData) if tc.result { suite.Require().False(res.Failed(), res.VmError) - shares, err := allowanceSharesMethod.UnpackOutput(res.Ret) + shares, err := suite.allowanceSharesMethod.UnpackOutput(res.Ret) suite.Require().NoError(err) if shares.Cmp(big.NewInt(0)) != 0 { suite.Require().Equal(allowanceAmt.BigInt(), shares) diff --git a/x/staking/precompile/approve_shares.go b/x/staking/precompile/approve_shares.go index 8cb58cfd..75dfe680 100644 --- a/x/staking/precompile/approve_shares.go +++ b/x/staking/precompile/approve_shares.go @@ -24,8 +24,8 @@ type ApproveSharesMethod struct { func NewApproveSharesMethod(keeper *Keeper) *ApproveSharesMethod { return &ApproveSharesMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["approveShares"], - Event: fxstakingtypes.GetABI().Events["ApproveShares"], + Method: stakingABI.Methods["approveShares"], + Event: stakingABI.Events["ApproveShares"], } } @@ -56,7 +56,7 @@ func (m *ApproveSharesMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, e if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) return nil }); err != nil { diff --git a/x/staking/precompile/approve_shares_test.go b/x/staking/precompile/approve_shares_test.go index d690e5dc..b16e3604 100644 --- a/x/staking/precompile/approve_shares_test.go +++ b/x/staking/precompile/approve_shares_test.go @@ -10,8 +10,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/staking/precompile" fxstakingtypes "github.com/functionx/fx-core/v8/x/staking/types" @@ -27,7 +25,6 @@ func TestStakingApproveSharesABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestApproveShares() { - approveSharesMethod := precompile.NewApproveSharesMethod(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, spender *helpers.Signer, allowance sdkmath.Int) (fxstakingtypes.ApproveSharesArgs, error) @@ -115,17 +112,14 @@ func (suite *PrecompileTestSuite) TestApproveShares() { args, errResult := tc.malleate(operator, spender, allowanceAmt) - packData, err := approveSharesMethod.PackInput(args) + packData, err := suite.approveSharesMethod.PackInput(args) suite.Require().NoError(err) - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr sender := owner.Address() if strings.HasPrefix(tc.name, "contract") { - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestApproveSharesName, args.Validator, args.Spender, args.Shares) - suite.Require().NoError(err) - - stakingContract = suite.staking - sender = suite.staking + stakingContract = suite.stakingTestAddr + sender = suite.stakingTestAddr } allowance := suite.App.StakingKeeper.GetAllowance(suite.Ctx, operator, owner.AccAddress(), spender.AccAddress()) @@ -143,9 +137,9 @@ func (suite *PrecompileTestSuite) TestApproveShares() { existLog := false for _, log := range res.Logs { - if log.Topics[0] == approveSharesMethod.Event.ID.String() { - suite.Require().Equal(log.Address, precompile.GetAddress().String()) - event, err := approveSharesMethod.UnpackEvent(log.ToEthereum()) + if log.Topics[0] == suite.approveSharesMethod.Event.ID.String() { + suite.Require().Equal(log.Address, suite.stakingAddr.String()) + event, err := suite.approveSharesMethod.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.Owner, sender) suite.Require().Equal(event.Spender, spender.Address()) diff --git a/x/staking/precompile/contract.go b/x/staking/precompile/contract.go index 5903ed45..7d513c6b 100644 --- a/x/staking/precompile/contract.go +++ b/x/staking/precompile/contract.go @@ -7,13 +7,16 @@ import ( distrkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" "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" evmtypes "github.com/functionx/fx-core/v8/x/evm/types" fxstakingkeeper "github.com/functionx/fx-core/v8/x/staking/keeper" - fxstakingtypes "github.com/functionx/fx-core/v8/x/staking/types" +) + +var ( + stakingAddress = common.HexToAddress(contract.StakingAddress) + stakingABI = contract.MustABIJson(contract.IStakingMetaData.ABI) ) type Contract struct { @@ -39,11 +42,6 @@ func NewPrecompiledContract( slashingKeeper: slashingKeeper, } - delegateV2 := NewDelegateV2Method(keeper) - redelegateV2 := NewRedelegateV2Method(keeper) - undelegateV2 := NewUndelegateV2Method(keeper) - slashingInfo := NewSlashingInfoMethod(keeper) - validatorList := NewValidatorListMethod(keeper) return &Contract{ methods: []contract.PrecompileMethod{ NewAllowanceSharesMethod(keeper), @@ -55,19 +53,19 @@ func NewPrecompiledContract( NewTransferFromSharesMethod(keeper), NewWithdrawMethod(keeper), - delegateV2, - redelegateV2, - undelegateV2, + NewDelegateV2Method(keeper), + NewRedelegateV2Method(keeper), + NewUndelegateV2Method(keeper), - slashingInfo, - validatorList, + NewSlashingInfoMethod(keeper), + NewValidatorListMethod(keeper), }, govKeeper: govKeeper, } } func (c *Contract) Address() common.Address { - return fxstakingtypes.GetAddress() + return stakingAddress } func (c *Contract) IsStateful() bool { @@ -111,12 +109,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: fxstakingtypes.GetAddress(), - Topics: topics, - Data: data, - BlockNumber: evm.Context.BlockNumber.Uint64(), - }) -} diff --git a/x/staking/precompile/contract_test.go b/x/staking/precompile/contract_test.go index 56ad6eb9..36269b5f 100644 --- a/x/staking/precompile/contract_test.go +++ b/x/staking/precompile/contract_test.go @@ -1,14 +1,12 @@ package precompile_test import ( - "bytes" "math/big" "strings" "testing" "time" sdkmath "cosmossdk.io/math" - tmrand "github.com/cometbft/cometbft/libs/rand" "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" distributionkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper" @@ -29,28 +27,28 @@ import ( fxtypes "github.com/functionx/fx-core/v8/types" "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/testutil" -) - -const ( - TestDelegateV2Name = "delegateV2" - TestUndelegateV2Name = "undelegateV2" - TestRedelegateV2Name = "redelegateV2" - TestWithdrawName = "withdraw" - TestDelegationName = "delegation" - TestDelegationRewardsName = "delegationRewards" - TestAllowanceSharesName = "allowanceShares" - TestApproveSharesName = "approveShares" - TestTransferSharesName = "transferShares" - TestTransferFromSharesName = "transferFromShares" - TestSlashingInfoName = "slashingInfo" - TestValidatorListName = "validatorList" + fxstakingtypes "github.com/functionx/fx-core/v8/x/staking/types" ) type PrecompileTestSuite struct { helpers.BaseSuite testutil.StakingSuite - signer *helpers.Signer - staking common.Address + signer *helpers.Signer + stakingTestAddr common.Address + stakingAddr common.Address + + allowanceSharesMethod *precompile.AllowanceSharesMethod + approveSharesMethod *precompile.ApproveSharesMethod + delegateV2Method *precompile.DelegateV2Method + delegationMethod *precompile.DelegationMethod + delegationRewardsMethod *precompile.DelegationRewardsMethod + redelegateMethodV2 *precompile.RedelegateMethodV2 + // slashingInfoMethod *precompile.SlashingInfoMethod + transferSharesMethod *precompile.TransferShares + transferFromSharesMethod *precompile.TransferFromShares + undelegateV2Method *precompile.UndelegateV2Method + validatorListMethod *precompile.ValidatorListMethod + withdrawMethod *precompile.WithdrawMethod } func TestPrecompileTestSuite(t *testing.T) { @@ -74,11 +72,26 @@ func (suite *PrecompileTestSuite) SetupTest() { stakingContract, err := suite.App.EvmKeeper.DeployContract(suite.Ctx, suite.signer.Address(), contract.MustABIJson(testscontract.StakingTestMetaData.ABI), contract.MustDecodeHex(testscontract.StakingTestMetaData.Bin)) suite.Require().NoError(err) - suite.staking = stakingContract + suite.stakingTestAddr = stakingContract + + suite.stakingAddr = common.HexToAddress(contract.StakingAddress) suite.MintToken(suite.signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewInt(10000).Mul(sdkmath.NewInt(1e18)))) suite.StakingSuite.Init(suite.Require(), suite.Ctx, suite.App.StakingKeeper) + + suite.allowanceSharesMethod = precompile.NewAllowanceSharesMethod(nil) + suite.approveSharesMethod = precompile.NewApproveSharesMethod(nil) + suite.delegateV2Method = precompile.NewDelegateV2Method(nil) + suite.delegationMethod = precompile.NewDelegationMethod(nil) + suite.delegationRewardsMethod = precompile.NewDelegationRewardsMethod(nil) + suite.redelegateMethodV2 = precompile.NewRedelegateV2Method(nil) + // suite.slashingInfoMethod = precompile.NewSlashingInfoMethod(nil) + suite.transferSharesMethod = precompile.NewTransferSharesMethod(nil) + suite.transferFromSharesMethod = precompile.NewTransferFromSharesMethod(nil) + suite.undelegateV2Method = precompile.NewUndelegateV2Method(nil) + suite.validatorListMethod = precompile.NewValidatorListMethod(nil) + suite.withdrawMethod = precompile.NewWithdrawMethod(nil) } func (suite *PrecompileTestSuite) DistributionQueryClient(ctx sdk.Context) distributiontypes.QueryClient { @@ -193,73 +206,34 @@ func (suite *PrecompileTestSuite) undelegateToFromFunc(val sdk.ValAddress, from, suite.Require().NoError(err) } -func (suite *PrecompileTestSuite) packTransferRand(val sdk.ValAddress, contractAddr, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { - randShares := big.NewInt(0).Sub(shares, big.NewInt(0).Mul(big.NewInt(tmrand.Int63n(900)+100), big.NewInt(1e18))) - callABI := precompile.GetABI() - if bytes.Equal(contractAddr.Bytes(), suite.staking.Bytes()) { - callABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - } - pack, err := callABI.Pack(TestTransferSharesName, val.String(), to, randShares) - suite.Require().NoError(err) - return pack, randShares, nil -} - -func (suite *PrecompileTestSuite) packTransferAll(val sdk.ValAddress, contractAddr, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { - callABI := precompile.GetABI() - if bytes.Equal(contractAddr.Bytes(), suite.staking.Bytes()) { - callABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - } - pack, err := callABI.Pack(TestTransferSharesName, val.String(), to, shares) - suite.Require().NoError(err) - return pack, shares, nil -} - func (suite *PrecompileTestSuite) approveFunc(val sdk.ValAddress, owner, spender common.Address, allowance *big.Int) { suite.App.StakingKeeper.SetAllowance(suite.Ctx, val, owner.Bytes(), spender.Bytes(), allowance) } -func (suite *PrecompileTestSuite) packTransferFromRand(val sdk.ValAddress, spender, from, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { - randShares := big.NewInt(0).Sub(shares, big.NewInt(0).Mul(big.NewInt(tmrand.Int63n(900)+100), big.NewInt(1e18))) - suite.approveFunc(val, from, spender, randShares) - callABI := precompile.GetABI() - if spender == suite.staking { - callABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - } - pack, err := callABI.Pack(TestTransferFromSharesName, val.String(), from, to, randShares) +func (suite *PrecompileTestSuite) PrecompileStakingDelegation(val sdk.ValAddress, del common.Address) (*big.Int, *big.Int) { + input, err := suite.delegationMethod.PackInput(fxstakingtypes.DelegationArgs{ + Validator: val.String(), + Delegator: del, + }) suite.Require().NoError(err) - return pack, randShares, nil -} - -func (suite *PrecompileTestSuite) packTransferFromAll(val sdk.ValAddress, spender, from, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { - suite.approveFunc(val, from, spender, shares) - callABI := precompile.GetABI() - if spender == suite.staking { - callABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - } - pack, err := callABI.Pack(TestTransferFromSharesName, val.String(), from, to, shares) + res, err := suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, del, &suite.stakingAddr, nil, input, false) suite.Require().NoError(err) - return pack, shares, nil -} - -func (suite *PrecompileTestSuite) PrecompileStakingDelegation(val sdk.ValAddress, del common.Address) (*big.Int, *big.Int) { - var res struct { - Shares *big.Int `abi:"_shares"` - Amount *big.Int `abi:"_delegateAmount"` - } - err := suite.App.EvmKeeper.QueryContract(suite.Ctx, del, precompile.GetAddress(), precompile.GetABI(), - TestDelegationName, &res, val.String(), del) + shares, amount, err := suite.delegationMethod.UnpackOutput(res.Ret) suite.Require().NoError(err) - return res.Shares, res.Amount + return shares, amount } func (suite *PrecompileTestSuite) PrecompileStakingDelegateV2(signer *helpers.Signer, val sdk.ValAddress, amt *big.Int) *big.Int { suite.MintToken(signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, sdkmath.NewIntFromBigInt(amt))) - pack, err := precompile.GetABI().Pack(TestDelegateV2Name, val.String(), amt) + pack, err := suite.delegateV2Method.PackInput(fxstakingtypes.DelegateV2Args{ + Validator: val.String(), + Amount: amt, + }) suite.Require().NoError(err) _, amountBefore := suite.PrecompileStakingDelegation(val, signer.Address()) - res := suite.EthereumTx(signer, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer, suite.stakingAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) shares, amount := suite.PrecompileStakingDelegation(val, signer.Address()) @@ -269,10 +243,10 @@ func (suite *PrecompileTestSuite) PrecompileStakingDelegateV2(signer *helpers.Si func (suite *PrecompileTestSuite) PrecompileStakingWithdraw(signer *helpers.Signer, val sdk.ValAddress) *big.Int { balanceBefore := suite.App.EvmKeeper.GetEVMDenomBalance(suite.Ctx, signer.Address()) - pack, err := precompile.GetABI().Pack(TestWithdrawName, val.String()) + pack, err := suite.withdrawMethod.PackInput(fxstakingtypes.WithdrawArgs{Validator: val.String()}) suite.Require().NoError(err) - res := suite.EthereumTx(signer, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer, suite.stakingAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) balanceAfter := suite.App.EvmKeeper.GetEVMDenomBalance(suite.Ctx, signer.Address()) @@ -282,10 +256,14 @@ func (suite *PrecompileTestSuite) PrecompileStakingWithdraw(signer *helpers.Sign func (suite *PrecompileTestSuite) PrecompileStakingTransferShares(signer *helpers.Signer, val sdk.ValAddress, receipt common.Address, shares *big.Int) (*big.Int, *big.Int) { balanceBefore := suite.App.EvmKeeper.GetEVMDenomBalance(suite.Ctx, signer.Address()) - pack, err := precompile.GetABI().Pack(TestTransferSharesName, val.String(), receipt, shares) + pack, err := suite.transferSharesMethod.PackInput(fxstakingtypes.TransferSharesArgs{ + Validator: val.String(), + To: receipt, + Shares: shares, + }) suite.Require().NoError(err) - res := suite.EthereumTx(signer, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer, suite.stakingAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) signerShares, _ := suite.PrecompileStakingDelegation(val, signer.Address()) @@ -297,10 +275,13 @@ func (suite *PrecompileTestSuite) PrecompileStakingTransferShares(signer *helper func (suite *PrecompileTestSuite) PrecompileStakingUndelegateV2(signer *helpers.Signer, val sdk.ValAddress, shares *big.Int) *big.Int { balanceBefore := suite.App.EvmKeeper.GetEVMDenomBalance(suite.Ctx, signer.Address()) - pack, err := precompile.GetABI().Pack(TestUndelegateV2Name, val.String(), shares) + pack, err := suite.undelegateV2Method.PackInput(fxstakingtypes.UndelegateV2Args{ + Validator: val.String(), + Amount: shares, + }) suite.Require().NoError(err) - res := suite.EthereumTx(signer, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer, suite.stakingAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) balanceAfter := suite.App.EvmKeeper.GetEVMDenomBalance(suite.Ctx, signer.Address()) @@ -309,17 +290,26 @@ func (suite *PrecompileTestSuite) PrecompileStakingUndelegateV2(signer *helpers. } func (suite *PrecompileTestSuite) PrecompileStakingApproveShares(signer *helpers.Signer, val sdk.ValAddress, spender common.Address, shares *big.Int) { - pack, err := precompile.GetABI().Pack(TestApproveSharesName, val.String(), spender, shares) + pack, err := suite.approveSharesMethod.PackInput(fxstakingtypes.ApproveSharesArgs{ + Validator: val.String(), + Spender: spender, + Shares: shares, + }) suite.Require().NoError(err) - res := suite.EthereumTx(signer, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer, suite.stakingAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) } func (suite *PrecompileTestSuite) PrecompileStakingTransferFromShares(signer *helpers.Signer, val sdk.ValAddress, from, receipt common.Address, shares *big.Int) { - pack, err := precompile.GetABI().Pack(TestTransferFromSharesName, val.String(), from, receipt, shares) + pack, err := suite.transferFromSharesMethod.PackInput(fxstakingtypes.TransferFromSharesArgs{ + Validator: val.String(), + From: from, + To: receipt, + Shares: shares, + }) suite.Require().NoError(err) - res := suite.EthereumTx(signer, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer, suite.stakingAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) } @@ -360,7 +350,7 @@ func (suite *PrecompileTestSuite) CheckDelegateLogs(logs []*evmtypes.Log, delAdd existLog := false for _, log := range logs { if log.Topics[0] == delegateV2Method.Event.ID.String() { - suite.Require().Equal(log.Address, precompile.GetAddress().String()) + suite.Require().Equal(log.Address, suite.stakingAddr.String()) event, err := delegateV2Method.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) @@ -397,7 +387,7 @@ func (suite *PrecompileTestSuite) CheckRedelegateLogs(logs []*evmtypes.Log, delA existLog := false for _, log := range logs { if log.Topics[0] == redelegateV2Method.Event.ID.String() { - suite.Require().Equal(log.Address, precompile.GetAddress().String()) + suite.Require().Equal(log.Address, suite.stakingAddr.String()) event, err := redelegateV2Method.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.Sender, delAddr) @@ -441,7 +431,7 @@ func (suite *PrecompileTestSuite) CheckUndelegateLogs(logs []*evmtypes.Log, delA existLog := false for _, log := range logs { if log.Topics[0] == undelegateV2Method.Event.ID.String() { - suite.Require().Equal(log.Address, precompile.GetAddress().String()) + suite.Require().Equal(log.Address, suite.stakingAddr.String()) event, err := undelegateV2Method.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.Sender, delAddr) diff --git a/x/staking/precompile/delegate.go b/x/staking/precompile/delegate.go index 4ead1af5..66edf70f 100644 --- a/x/staking/precompile/delegate.go +++ b/x/staking/precompile/delegate.go @@ -25,8 +25,8 @@ type DelegateV2Method struct { func NewDelegateV2Method(keeper *Keeper) *DelegateV2Method { return &DelegateV2Method{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["delegateV2"], - Event: fxstakingtypes.GetABI().Events["DelegateV2"], + Method: stakingABI.Methods["delegateV2"], + Event: stakingABI.Events["DelegateV2"], } } @@ -63,7 +63,7 @@ func (m *DelegateV2Method) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) return nil }); err != nil { return nil, err diff --git a/x/staking/precompile/delegate_test.go b/x/staking/precompile/delegate_test.go index 56073e57..b2bef5b8 100644 --- a/x/staking/precompile/delegate_test.go +++ b/x/staking/precompile/delegate_test.go @@ -12,8 +12,6 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" fxtypes "github.com/functionx/fx-core/v8/types" "github.com/functionx/fx-core/v8/x/staking/precompile" @@ -21,7 +19,6 @@ import ( ) func (suite *PrecompileTestSuite) TestDelegate() { - delegateV2Method := precompile.NewDelegateV2Method(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, delAmount sdkmath.Int) (interface{}, *big.Int, error) @@ -81,11 +78,14 @@ func (suite *PrecompileTestSuite) TestDelegate() { malleate: func(val sdk.ValAddress, delAmount sdkmath.Int) (interface{}, *big.Int, error) { suite.MintToken(suite.signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, delAmount)) - suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.staking.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) - pack, err := contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestDelegateV2Name, val.String(), delAmount.BigInt()) + suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.stakingTestAddr.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val.String(), + Amount: delAmount.BigInt(), + }) suite.Require().NoError(err) - res := suite.EthereumTx(suite.signer, suite.staking, big.NewInt(0), pack) + res := suite.EthereumTx(suite.signer, suite.stakingTestAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) return types.DelegateV2Args{ @@ -112,24 +112,22 @@ func (suite *PrecompileTestSuite) TestDelegate() { delAmount := helpers.NewRandAmount() - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr var packData []byte var err error operator, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) suite.Require().NoError(err) args, value, errResult := tc.malleate(operator, delAmount) - packData, err = delegateV2Method.PackInput(args.(types.DelegateV2Args)) + packData, err = suite.delegateV2Method.PackInput(args.(types.DelegateV2Args)) suite.Require().NoError(err) delAddr := suite.signer.Address() if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - delAddr = suite.staking + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr - v2Args := args.(types.DelegateV2Args) - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestDelegateV2Name, v2Args.Validator, v2Args.Amount) - suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.staking.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) + suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.stakingTestAddr.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) suite.Require().NoError(err) } @@ -215,7 +213,6 @@ func TestStakingDelegateV2ABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestDelegateV2() { - delegateV2Method := precompile.NewDelegateV2Method(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, delAmount sdkmath.Int) (types.DelegateV2Args, error) @@ -274,11 +271,14 @@ func (suite *PrecompileTestSuite) TestDelegateV2() { malleate: func(val sdk.ValAddress, delAmount sdkmath.Int) (types.DelegateV2Args, error) { suite.MintToken(suite.signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, delAmount)) - suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.staking.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) - pack, err := contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestDelegateV2Name, val.String(), delAmount.BigInt()) + suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.stakingTestAddr.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val.String(), + Amount: delAmount.BigInt(), + }) suite.Require().NoError(err) - res := suite.EthereumTx(suite.signer, suite.staking, big.NewInt(0), pack) + res := suite.EthereumTx(suite.signer, suite.stakingTestAddr, big.NewInt(0), pack) suite.Require().False(res.Failed(), res.VmError) return types.DelegateV2Args{ @@ -308,20 +308,17 @@ func (suite *PrecompileTestSuite) TestDelegateV2() { args, errResult := tc.malleate(operator, delAmount) - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr delAddr := suite.signer.Address() suite.MintToken(suite.signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, delAmount)) - packData, err := delegateV2Method.PackInput(args) + packData, err := suite.delegateV2Method.PackInput(args) suite.Require().NoError(err) if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - delAddr = suite.staking - - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestDelegateV2Name, args.Validator, args.Amount) - suite.Require().NoError(err) + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr - suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.staking.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) + suite.Require().NoError(suite.App.BankKeeper.SendCoins(suite.Ctx, suite.signer.AccAddress(), suite.stakingTestAddr.Bytes(), sdk.NewCoins(sdk.NewCoin(fxtypes.DefaultDenom, delAmount)))) } delFound := true @@ -352,10 +349,10 @@ func (suite *PrecompileTestSuite) TestDelegateV2() { existLog := false for _, log := range res.Logs { - if log.Topics[0] == delegateV2Method.Event.ID.String() { - suite.Require().Equal(log.Address, precompile.GetAddress().String()) + if log.Topics[0] == suite.delegateV2Method.Event.ID.String() { + suite.Require().Equal(log.Address, suite.stakingAddr.String()) - event, err := delegateV2Method.UnpackEvent(log.ToEthereum()) + event, err := suite.delegateV2Method.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.Delegator, delAddr) suite.Require().Equal(event.Validator, val.GetOperator()) diff --git a/x/staking/precompile/delegation.go b/x/staking/precompile/delegation.go index fa784382..82f00293 100644 --- a/x/staking/precompile/delegation.go +++ b/x/staking/precompile/delegation.go @@ -20,7 +20,7 @@ type DelegationMethod struct { func NewDelegationMethod(keeper *Keeper) *DelegationMethod { return &DelegationMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["delegation"], + Method: stakingABI.Methods["delegation"], } } diff --git a/x/staking/precompile/delegation_rewards.go b/x/staking/precompile/delegation_rewards.go index 7cab5971..6e09295b 100644 --- a/x/staking/precompile/delegation_rewards.go +++ b/x/staking/precompile/delegation_rewards.go @@ -20,7 +20,7 @@ type DelegationRewardsMethod struct { func NewDelegationRewardsMethod(keeper *Keeper) *DelegationRewardsMethod { return &DelegationRewardsMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["delegationRewards"], + Method: stakingABI.Methods["delegationRewards"], } } diff --git a/x/staking/precompile/delegation_rewards_test.go b/x/staking/precompile/delegation_rewards_test.go index 8891b9b3..8ff84c28 100644 --- a/x/staking/precompile/delegation_rewards_test.go +++ b/x/staking/precompile/delegation_rewards_test.go @@ -13,8 +13,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" fxtypes "github.com/functionx/fx-core/v8/types" "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" @@ -28,7 +26,6 @@ func TestStakingDelegationRewardsABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestDelegationRewards() { - delegationRewardMethod := precompile.NewDelegationRewardsMethod(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, del common.Address) (types.DelegationRewardsArgs, error) @@ -109,21 +106,22 @@ func (suite *PrecompileTestSuite) TestDelegationRewards() { signer := suite.RandSigner() suite.MintToken(signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, delAmt)) - stakingContract := precompile.GetAddress() - stakingABI := precompile.GetABI() + stakingContract := suite.stakingAddr delAddr := signer.Address() value := big.NewInt(0) if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - stakingABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - delAddr = suite.staking + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr value = delAmt.BigInt() } operator0, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val0.GetOperator()) suite.Require().NoError(err) - pack, err := stakingABI.Pack(TestDelegateV2Name, val0.GetOperator(), delAmt.BigInt()) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val0.GetOperator(), + Amount: delAmt.BigInt(), + }) suite.Require().NoError(err) res := suite.EthereumTx(signer, stakingContract, value, pack) @@ -136,20 +134,16 @@ func (suite *PrecompileTestSuite) TestDelegationRewards() { evmDenom := suite.App.EvmKeeper.GetParams(suite.Ctx).EvmDenom args, errResult := tc.malleate(operator0, delAddr) - packData, err := delegationRewardMethod.PackInput(args) + packData, err := suite.delegationRewardsMethod.PackInput(args) suite.Require().NoError(err) - if strings.HasPrefix(tc.name, "contract") { - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestDelegationRewardsName, args.Validator, args.Delegator) - suite.Require().NoError(err) - } res, err = suite.App.EvmKeeper.CallEVMWithoutGas(suite.Ctx, suite.signer.Address(), &stakingContract, nil, packData, false) if tc.result { suite.Require().NoError(err) suite.Require().False(res.Failed(), res.VmError) - rewardsValue, err := stakingABI.Methods[TestDelegationRewardsName].Outputs.Unpack(res.Ret) + rewardsValue, err := suite.delegationRewardsMethod.UnpackOutput(res.Ret) suite.Require().NoError(err) - suite.Require().EqualValues(rewardsValue[0].(*big.Int).String(), resp.Rewards.AmountOf(evmDenom).TruncateInt().BigInt().String()) + suite.Require().EqualValues(rewardsValue.String(), resp.Rewards.AmountOf(evmDenom).TruncateInt().BigInt().String()) } else { suite.Error(res, errResult) } diff --git a/x/staking/precompile/delegation_test.go b/x/staking/precompile/delegation_test.go index ae61e3c6..1b534c21 100644 --- a/x/staking/precompile/delegation_test.go +++ b/x/staking/precompile/delegation_test.go @@ -10,8 +10,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" @@ -25,7 +23,6 @@ func TestStakingDelegationABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestDelegation() { - delegationMethod := precompile.NewDelegationMethod(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, del common.Address) (types.DelegationArgs, error) @@ -127,21 +124,22 @@ func (suite *PrecompileTestSuite) TestDelegation() { delAmount := helpers.NewRandAmount() - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr delAddr := suite.signer.Address() - stakingABI := precompile.GetABI() value := big.NewInt(0) if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - delAddr = suite.staking - stakingABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr value = delAmount.BigInt() } operator0, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val0.GetOperator()) suite.Require().NoError(err) - pack, err := stakingABI.Pack(TestDelegateV2Name, val0.GetOperator(), delAmount.BigInt()) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val0.GetOperator(), + Amount: delAmount.BigInt(), + }) suite.Require().NoError(err) res := suite.EthereumTx(suite.signer, stakingContract, value, pack) @@ -150,12 +148,8 @@ func (suite *PrecompileTestSuite) TestDelegation() { suite.Commit() args, errResult := tc.malleate(operator0, delAddr) - packData, err := delegationMethod.PackInput(args) + packData, err := suite.delegationMethod.PackInput(args) suite.Require().NoError(err) - if strings.HasPrefix(tc.name, "contract") { - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestDelegationName, args.Validator, args.Delegator) - suite.Require().NoError(err) - } delegation, err := suite.App.StakingKeeper.GetDelegation(suite.Ctx, delAddr.Bytes(), operator0) suite.Require().NoError(err) @@ -163,9 +157,9 @@ func (suite *PrecompileTestSuite) TestDelegation() { if tc.result { suite.Require().NoError(err) suite.Require().False(res.Failed(), res.VmError) - delValue, err := stakingABI.Methods[TestDelegationName].Outputs.Unpack(res.Ret) + delValue, _, err := suite.delegationMethod.UnpackOutput(res.Ret) suite.Require().NoError(err) - suite.Require().Equal(delegation.GetShares().TruncateInt().String(), delValue[0].(*big.Int).String()) + suite.Require().Equal(delegation.GetShares().TruncateInt().String(), delValue.String()) } else { suite.Error(res, errResult) } diff --git a/x/staking/precompile/keys.go b/x/staking/precompile/keys.go deleted file mode 100644 index d232c1af..00000000 --- a/x/staking/precompile/keys.go +++ /dev/null @@ -1,21 +0,0 @@ -package precompile - -import ( - "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" - - "github.com/functionx/fx-core/v8/contract" -) - -var ( - stakingAddress = common.HexToAddress(contract.StakingAddress) - stakingABI = contract.MustABIJson(contract.IStakingMetaData.ABI) -) - -func GetAddress() common.Address { - return stakingAddress -} - -func GetABI() abi.ABI { - return stakingABI -} diff --git a/x/staking/precompile/redelegate.go b/x/staking/precompile/redelegate.go index ef638aae..2f5ce89f 100644 --- a/x/staking/precompile/redelegate.go +++ b/x/staking/precompile/redelegate.go @@ -25,8 +25,8 @@ type RedelegateMethodV2 struct { func NewRedelegateV2Method(keeper *Keeper) *RedelegateMethodV2 { return &RedelegateMethodV2{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["redelegateV2"], - Event: fxstakingtypes.GetABI().Events["RedelegateV2"], + Method: stakingABI.Methods["redelegateV2"], + Event: stakingABI.Events["RedelegateV2"], } } @@ -66,7 +66,7 @@ func (m *RedelegateMethodV2) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, er if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) return nil }); err != nil { diff --git a/x/staking/precompile/redelegate_test.go b/x/staking/precompile/redelegate_test.go index 36090618..ddce00cd 100644 --- a/x/staking/precompile/redelegate_test.go +++ b/x/staking/precompile/redelegate_test.go @@ -9,17 +9,11 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" - "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" ) func (suite *PrecompileTestSuite) TestRedelegate() { - testABI := contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - redelegateV2Method := precompile.NewRedelegateV2Method(nil) - testCases := []struct { name string malleate func(valSrc, valDst sdk.ValAddress, shares sdkmath.LegacyDec, delAmount sdkmath.Int) (interface{}, error) @@ -82,14 +76,12 @@ func (suite *PrecompileTestSuite) TestRedelegate() { delAmt := helpers.NewRandAmount() - stakingContract := precompile.GetAddress() - stakingABI := precompile.GetABI() + stakingContract := suite.stakingAddr delAddr := suite.signer.Address() value := big.NewInt(0) if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - stakingABI = testABI - delAddr = suite.staking + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr value = delAmt.BigInt() } @@ -100,7 +92,10 @@ func (suite *PrecompileTestSuite) TestRedelegate() { suite.Require().NoError(err) // delegate to val0 - pack, err := stakingABI.Pack(TestDelegateV2Name, val0.GetOperator(), delAmt.BigInt()) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val0.GetOperator(), + Amount: delAmt.BigInt(), + }) suite.Require().NoError(err) res := suite.EthereumTx(suite.signer, stakingContract, value, pack) @@ -118,14 +113,9 @@ func (suite *PrecompileTestSuite) TestRedelegate() { var packData []byte args, errResult := tc.malleate(operator0, operator1, delegation0.Shares, delAmt) - packData, err = redelegateV2Method.PackInput(args.(types.RedelegateV2Args)) + packData, err = suite.redelegateMethodV2.PackInput(args.(types.RedelegateV2Args)) suite.Require().NoError(err) - if strings.HasPrefix(tc.name, "contract") { - argsV2 := args.(types.RedelegateV2Args) - packData, err = testABI.Pack(TestRedelegateV2Name, argsV2.ValidatorSrc, argsV2.ValidatorDst, argsV2.Amount) - suite.Require().NoError(err) - } res = suite.EthereumTx(suite.signer, stakingContract, big.NewInt(0), packData) if tc.result { diff --git a/x/staking/precompile/slashing_info.go b/x/staking/precompile/slashing_info.go index 6c4465a9..7de7a69e 100644 --- a/x/staking/precompile/slashing_info.go +++ b/x/staking/precompile/slashing_info.go @@ -18,7 +18,7 @@ type SlashingInfoMethod struct { func NewSlashingInfoMethod(keeper *Keeper) *SlashingInfoMethod { return &SlashingInfoMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["slashingInfo"], + Method: stakingABI.Methods["slashingInfo"], } } diff --git a/x/staking/precompile/slashing_info_test.go b/x/staking/precompile/slashing_info_test.go index 3e683ba4..1db2d3a0 100644 --- a/x/staking/precompile/slashing_info_test.go +++ b/x/staking/precompile/slashing_info_test.go @@ -9,8 +9,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" @@ -88,12 +86,10 @@ func (suite *PrecompileTestSuite) TestSlashingInfo() { packData, err := slashingInfoMethod.PackInput(args) suite.Require().NoError(err) - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestSlashingInfoName, args.Validator) - suite.Require().NoError(err) + stakingContract = suite.stakingTestAddr } res := suite.EthereumTx(owner, stakingContract, big.NewInt(0), packData) diff --git a/x/staking/precompile/transfer_shares.go b/x/staking/precompile/transfer_shares.go index 5d845a9c..965970cb 100644 --- a/x/staking/precompile/transfer_shares.go +++ b/x/staking/precompile/transfer_shares.go @@ -34,8 +34,8 @@ func NewTransferSharesMethod(keeper *Keeper) *TransferShares { return &TransferShares{ TransferShare: &TransferShare{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["transferShares"], - Event: fxstakingtypes.GetABI().Events["TransferShares"], + Method: stakingABI.Methods["transferShares"], + Event: stakingABI.Events["TransferShares"], }, } } @@ -61,7 +61,7 @@ func (m *TransferShares) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) valAddr := args.GetValidator() stateDB := evm.StateDB.(types.ExtStateDB) var result []byte - if err := stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error { + if err = stateDB.ExecuteNativeAction(contract.Address(), nil, func(ctx sdk.Context) error { token, reward, err := m.handlerTransferShares(ctx, evm, valAddr, contract.Caller(), args.To, args.Shares) if err != nil { return err @@ -104,8 +104,8 @@ func NewTransferFromSharesMethod(keeper *Keeper) *TransferFromShares { return &TransferFromShares{ TransferShare: &TransferShare{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["transferFromShares"], - Event: fxstakingtypes.GetABI().Events["TransferShares"], + Method: stakingABI.Methods["transferFromShares"], + Event: stakingABI.Events["TransferShares"], }, } } @@ -233,7 +233,7 @@ func (m *TransferShare) handlerTransferShares( if err != nil { return nil, nil, err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) // get to delegation toDel, err := m.stakingKeeper.GetDelegation(ctx, to.Bytes(), valAddr) @@ -260,7 +260,7 @@ func (m *TransferShare) handlerTransferShares( if err != nil { return nil, nil, err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) } // update from delegate, delete it if shares zero @@ -336,7 +336,7 @@ func (m *TransferShare) handlerTransferShares( if err != nil { return nil, nil, err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) return token.BigInt(), toRewardCoin.Amount.BigInt(), nil } diff --git a/x/staking/precompile/transfer_shares_test.go b/x/staking/precompile/transfer_shares_test.go index d4d61dcc..1c848b06 100644 --- a/x/staking/precompile/transfer_shares_test.go +++ b/x/staking/precompile/transfer_shares_test.go @@ -32,7 +32,6 @@ func TestStakingTransferSharesABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestTransferShares() { - transferSharesMethod := precompile.NewTransferSharesMethod(nil) testCases := []struct { name string pretransfer func(val sdk.ValAddress, from, to common.Address, delAmount sdkmath.Int) @@ -283,11 +282,11 @@ func (suite *PrecompileTestSuite) TestTransferShares() { fromSigner := suite.RandSigner() toSigner := suite.RandSigner() - contract := precompile.GetAddress() + contract := suite.stakingAddr delAddr := fromSigner.Address() if strings.HasPrefix(tc.name, "contract") { - contract = suite.staking - delAddr = suite.staking + contract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr } operator, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) @@ -355,9 +354,9 @@ func (suite *PrecompileTestSuite) TestTransferShares() { existLog := false for _, log := range res.Logs { - if log.Topics[0] == transferSharesMethod.Event.ID.String() { + if log.Topics[0] == suite.transferSharesMethod.Event.ID.String() { suite.Require().Len(log.Topics, 3) - event, err := transferSharesMethod.UnpackEvent(log.ToEthereum()) + event, err := suite.transferSharesMethod.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.From, delAddr) suite.Require().Equal(event.To, toSigner.Address()) @@ -374,6 +373,27 @@ func (suite *PrecompileTestSuite) TestTransferShares() { } } +func (suite *PrecompileTestSuite) packTransferRand(val sdk.ValAddress, contractAddr, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { + randShares := big.NewInt(0).Sub(shares, big.NewInt(0).Mul(big.NewInt(tmrand.Int63n(900)+100), big.NewInt(1e18))) + pack, err := suite.transferSharesMethod.PackInput(fxstakingtypes.TransferSharesArgs{ + Validator: val.String(), + To: to, + Shares: randShares, + }) + suite.Require().NoError(err) + return pack, randShares, nil +} + +func (suite *PrecompileTestSuite) packTransferAll(val sdk.ValAddress, contractAddr, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { + pack, err := suite.transferSharesMethod.PackInput(fxstakingtypes.TransferSharesArgs{ + Validator: val.String(), + To: to, + Shares: shares, + }) + suite.Require().NoError(err) + return pack, shares, nil +} + func TestStakingTransferFromSharesABI(t *testing.T) { transferFromSharesMethod := precompile.NewTransferFromSharesMethod(nil) @@ -382,7 +402,6 @@ func TestStakingTransferFromSharesABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestTransferFromShares() { - transferFromSharesMethod := precompile.NewTransferFromSharesMethod(nil) testCases := []struct { name string pretransfer func(val sdk.ValAddress, from, to common.Address, delAmount sdkmath.Int) @@ -636,12 +655,12 @@ func (suite *PrecompileTestSuite) TestTransferFromShares() { // from delegate, approve sender, sender send tx, transferFrom to toSigner // from delegate, approve contract, sender call contract, transferFrom to toSigner - contract := precompile.GetAddress() + contract := suite.stakingAddr delAddr := fromSigner.Address() spender := sender.Address() if strings.HasPrefix(tc.name, "contract") { - contract = suite.staking - spender = suite.staking + contract = suite.stakingTestAddr + spender = suite.stakingTestAddr } operator, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) @@ -726,9 +745,9 @@ func (suite *PrecompileTestSuite) TestTransferFromShares() { existLog := false for _, log := range res.Logs { - if log.Topics[0] == transferFromSharesMethod.Event.ID.String() { + if log.Topics[0] == suite.transferFromSharesMethod.Event.ID.String() { suite.Require().Len(log.Topics, 3) - event, err := transferFromSharesMethod.UnpackEvent(log.ToEthereum()) + event, err := suite.transferFromSharesMethod.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.From, delAddr) suite.Require().Equal(event.To, toSigner.Address()) @@ -745,6 +764,32 @@ func (suite *PrecompileTestSuite) TestTransferFromShares() { } } +func (suite *PrecompileTestSuite) packTransferFromRand(val sdk.ValAddress, spender, from, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { + randShares := big.NewInt(0).Sub(shares, big.NewInt(0).Mul(big.NewInt(tmrand.Int63n(900)+100), big.NewInt(1e18))) + suite.approveFunc(val, from, spender, randShares) + pack, err := suite.transferFromSharesMethod.PackInput(fxstakingtypes.TransferFromSharesArgs{ + Validator: val.String(), + From: from, + To: to, + Shares: randShares, + }) + suite.Require().NoError(err) + return pack, randShares, nil +} + +func (suite *PrecompileTestSuite) packTransferFromAll(val sdk.ValAddress, spender, from, to common.Address, shares *big.Int) ([]byte, *big.Int, []string) { + suite.approveFunc(val, from, spender, shares) + pack, err := suite.transferFromSharesMethod.PackInput(fxstakingtypes.TransferFromSharesArgs{ + Validator: val.String(), + From: from, + To: to, + Shares: shares, + }) + + suite.Require().NoError(err) + return pack, shares, nil +} + func (suite *PrecompileTestSuite) TestTransferSharesCompare() { val := suite.GetFirstValidator() delAmount := sdkmath.NewInt(int64(tmrand.Int() + 100)).Mul(sdkmath.NewInt(1e18)) @@ -1060,6 +1105,6 @@ func (suite *PrecompileTestSuite) TestTransferSharesRedelegate() { Shares: delegation.Shares.TruncateInt().BigInt(), }) suite.Require().NoError(err) - res := suite.EthereumTx(signer1, precompile.GetAddress(), big.NewInt(0), pack) + res := suite.EthereumTx(signer1, suite.stakingAddr, big.NewInt(0), pack) suite.Error(res, errors.New("from has receiving redelegation")) } diff --git a/x/staking/precompile/undelegate.go b/x/staking/precompile/undelegate.go index 5b16a4b0..c4ac3251 100644 --- a/x/staking/precompile/undelegate.go +++ b/x/staking/precompile/undelegate.go @@ -25,8 +25,8 @@ type UndelegateV2Method struct { func NewUndelegateV2Method(keeper *Keeper) *UndelegateV2Method { return &UndelegateV2Method{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["undelegateV2"], - Event: fxstakingtypes.GetABI().Events["UndelegateV2"], + Method: stakingABI.Methods["undelegateV2"], + Event: stakingABI.Events["UndelegateV2"], } } @@ -65,7 +65,7 @@ func (m *UndelegateV2Method) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, er if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) return nil }); err != nil { return nil, err diff --git a/x/staking/precompile/undelegate_test.go b/x/staking/precompile/undelegate_test.go index e7743cf9..7864812d 100644 --- a/x/staking/precompile/undelegate_test.go +++ b/x/staking/precompile/undelegate_test.go @@ -8,15 +8,11 @@ import ( sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" - "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" ) func (suite *PrecompileTestSuite) TestUndelegate() { - undelegateV2Method := precompile.NewUndelegateV2Method(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, shares sdkmath.LegacyDec, delAmt sdkmath.Int) (interface{}, error) @@ -73,21 +69,22 @@ func (suite *PrecompileTestSuite) TestUndelegate() { val := suite.GetFirstValidator() delAmt := helpers.NewRandAmount() - stakingContract := precompile.GetAddress() - stakingABI := precompile.GetABI() + stakingContract := suite.stakingAddr delAddr := suite.signer.Address() value := big.NewInt(0) if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - stakingABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - delAddr = suite.staking + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr value = delAmt.BigInt() } operator, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) suite.Require().NoError(err) - pack, err := stakingABI.Pack(TestDelegateV2Name, val.GetOperator(), delAmt.BigInt()) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val.GetOperator(), + Amount: delAmt.BigInt(), + }) suite.Require().NoError(err) res := suite.EthereumTx(suite.signer, stakingContract, value, pack) @@ -103,16 +100,9 @@ func (suite *PrecompileTestSuite) TestUndelegate() { var packData []byte args, errResult := tc.malleate(operator, delegation.Shares, delAmt) - packData, err = undelegateV2Method.PackInput(args.(types.UndelegateV2Args)) + packData, err = suite.undelegateV2Method.PackInput(args.(types.UndelegateV2Args)) suite.Require().NoError(err) - if strings.HasPrefix(tc.name, "contract") { - argsV2 := args.(types.UndelegateV2Args) - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestUndelegateV2Name, argsV2.Validator, argsV2.Amount) - - suite.Require().NoError(err) - } - res = suite.EthereumTx(suite.signer, stakingContract, big.NewInt(0), packData) if tc.result { diff --git a/x/staking/precompile/validator_list.go b/x/staking/precompile/validator_list.go index 167b77bf..0b0a91a4 100644 --- a/x/staking/precompile/validator_list.go +++ b/x/staking/precompile/validator_list.go @@ -25,7 +25,7 @@ type ValidatorList struct { func NewValidatorListMethod(keeper *Keeper) *ValidatorListMethod { return &ValidatorListMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["validatorList"], + Method: stakingABI.Methods["validatorList"], } } diff --git a/x/staking/precompile/validator_list_test.go b/x/staking/precompile/validator_list_test.go index bc881932..fa455d05 100644 --- a/x/staking/precompile/validator_list_test.go +++ b/x/staking/precompile/validator_list_test.go @@ -9,8 +9,6 @@ import ( "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" "github.com/functionx/fx-core/v8/x/staking/precompile" "github.com/functionx/fx-core/v8/x/staking/types" @@ -24,7 +22,6 @@ func TestValidatorListABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestValidatorList() { - validatroListMethod := precompile.NewValidatorListMethod(nil) testCases := []struct { name string malleate func() (types.ValidatorListArgs, error) @@ -92,21 +89,19 @@ func (suite *PrecompileTestSuite) TestValidatorList() { args, errResult := tc.malleate() - packData, err := validatroListMethod.PackInput(args) + packData, err := suite.validatorListMethod.PackInput(args) suite.Require().NoError(err) - stakingContract := precompile.GetAddress() + stakingContract := suite.stakingAddr if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestValidatorListName, args.SortBy) - suite.Require().NoError(err) + stakingContract = suite.stakingTestAddr } res := suite.EthereumTx(owner, stakingContract, big.NewInt(0), packData) if tc.result { suite.Require().False(res.Failed(), res.VmError) - valAddrs, err := validatroListMethod.UnpackOutput(res.Ret) + valAddrs, err := suite.validatorListMethod.UnpackOutput(res.Ret) suite.Require().NoError(err) valsByPower, err := suite.App.StakingKeeper.GetBondedValidatorsByPower(suite.Ctx) suite.Require().NoError(err) diff --git a/x/staking/precompile/withdraw.go b/x/staking/precompile/withdraw.go index 39c614ac..6a21908f 100644 --- a/x/staking/precompile/withdraw.go +++ b/x/staking/precompile/withdraw.go @@ -25,8 +25,8 @@ type WithdrawMethod struct { func NewWithdrawMethod(keeper *Keeper) *WithdrawMethod { return &WithdrawMethod{ Keeper: keeper, - Method: fxstakingtypes.GetABI().Methods["withdraw"], - Event: fxstakingtypes.GetABI().Events["Withdraw"], + Method: stakingABI.Methods["withdraw"], + Event: stakingABI.Events["Withdraw"], } } @@ -67,7 +67,7 @@ func (m *WithdrawMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) if err != nil { return err } - EmitEvent(evm, data, topic) + fxcontract.EmitEvent(evm, stakingAddress, data, topic) result, err = m.PackOutput(bigInt) return err @@ -102,12 +102,12 @@ func (m *WithdrawMethod) PackOutput(reward *big.Int) ([]byte, error) { return m.Method.Outputs.Pack(reward) } -func (m *WithdrawMethod) UnpackOutput(data []byte) (bool, error) { +func (m *WithdrawMethod) UnpackOutput(data []byte) (*big.Int, error) { amount, err := m.Method.Outputs.Unpack(data) if err != nil { - return false, err + return nil, err } - return amount[0].(bool), nil + return amount[0].(*big.Int), nil } func (m *WithdrawMethod) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingWithdraw, error) { diff --git a/x/staking/precompile/withdraw_test.go b/x/staking/precompile/withdraw_test.go index 99fca4a2..e758e24e 100644 --- a/x/staking/precompile/withdraw_test.go +++ b/x/staking/precompile/withdraw_test.go @@ -12,8 +12,6 @@ import ( distritypes "github.com/cosmos/cosmos-sdk/x/distribution/types" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/contract" - testscontract "github.com/functionx/fx-core/v8/tests/contract" "github.com/functionx/fx-core/v8/testutil/helpers" fxtypes "github.com/functionx/fx-core/v8/types" "github.com/functionx/fx-core/v8/x/staking/precompile" @@ -30,7 +28,6 @@ func TestStakingWithdrawABI(t *testing.T) { } func (suite *PrecompileTestSuite) TestWithdraw() { - withdrawMethod := precompile.NewWithdrawMethod(nil) testCases := []struct { name string malleate func(val sdk.ValAddress, shares sdkmath.LegacyDec) (types.WithdrawArgs, error) @@ -107,21 +104,22 @@ func (suite *PrecompileTestSuite) TestWithdraw() { signer := suite.RandSigner() suite.MintToken(signer.AccAddress(), sdk.NewCoin(fxtypes.DefaultDenom, delAmt)) - stakingContract := precompile.GetAddress() - stakingABI := precompile.GetABI() + stakingContract := suite.stakingAddr delAddr := signer.Address() value := big.NewInt(0) if strings.HasPrefix(tc.name, "contract") { - stakingContract = suite.staking - stakingABI = contract.MustABIJson(testscontract.StakingTestMetaData.ABI) - delAddr = suite.staking + stakingContract = suite.stakingTestAddr + delAddr = suite.stakingTestAddr value = delAmt.BigInt() } operator, err := suite.App.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator()) suite.Require().NoError(err) - pack, err := stakingABI.Pack(TestDelegateV2Name, val.GetOperator(), delAmt.BigInt()) + pack, err := suite.delegateV2Method.PackInput(types.DelegateV2Args{ + Validator: val.GetOperator(), + Amount: delAmt.BigInt(), + }) suite.Require().NoError(err) res := suite.EthereumTx(signer, stakingContract, value, pack) @@ -137,12 +135,8 @@ func (suite *PrecompileTestSuite) TestWithdraw() { delegation := suite.GetDelegation(delAddr.Bytes(), operator) args, errResult := tc.malleate(operator, delegation.Shares) - packData, err := withdrawMethod.PackInput(args) + packData, err := suite.withdrawMethod.PackInput(args) suite.Require().NoError(err) - if strings.HasPrefix(tc.name, "contract") { - packData, err = contract.MustABIJson(testscontract.StakingTestMetaData.ABI).Pack(TestWithdrawName, args.Validator) - suite.Require().NoError(err) - } res = suite.EthereumTx(signer, stakingContract, big.NewInt(0), packData) if tc.result { @@ -152,18 +146,17 @@ func (suite *PrecompileTestSuite) TestWithdraw() { suite.Require().NoError(err) suite.Require().Equal(totalAfter, totalBefore) - unpack, err := stakingABI.Unpack(TestWithdrawName, res.Ret) + reward, err := suite.withdrawMethod.UnpackOutput(res.Ret) suite.Require().NoError(err) - reward := unpack[0].(*big.Int) chainBalances := suite.App.BankKeeper.GetAllBalances(suite.Ctx, delAddr.Bytes()) suite.Require().True(chainBalances.AmountOf(fxtypes.DefaultDenom).Equal(sdkmath.NewIntFromBigInt(reward)), chainBalances.String()) existLog := false for _, log := range res.Logs { - if log.Topics[0] == withdrawMethod.Event.ID.String() { - suite.Require().Equal(log.Address, precompile.GetAddress().String()) + if log.Topics[0] == suite.withdrawMethod.Event.ID.String() { + suite.Require().Equal(log.Address, suite.stakingAddr.String()) - event, err := withdrawMethod.UnpackEvent(log.ToEthereum()) + event, err := suite.withdrawMethod.UnpackEvent(log.ToEthereum()) suite.Require().NoError(err) suite.Require().Equal(event.Sender, delAddr) suite.Require().Equal(event.Validator, val.GetOperator()) diff --git a/x/staking/types/contract.go b/x/staking/types/contract.go index ea74732d..d4a11bb6 100644 --- a/x/staking/types/contract.go +++ b/x/staking/types/contract.go @@ -6,15 +6,7 @@ import ( "math/big" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" - - "github.com/functionx/fx-core/v8/contract" -) - -var ( - stakingAddress = common.HexToAddress(contract.StakingAddress) - stakingABI = contract.MustABIJson(contract.IStakingMetaData.ABI) ) type ValidatorSortBy uint8 @@ -24,14 +16,6 @@ const ( ValidatorSortByMissed ) -func GetAddress() common.Address { - return stakingAddress -} - -func GetABI() abi.ABI { - return stakingABI -} - type AllowanceSharesArgs struct { Validator string `abi:"_val"` Owner common.Address `abi:"_owner"`