diff --git a/app/receipt_test.go b/app/receipt_test.go index b0ea2f60da..a76912f269 100644 --- a/app/receipt_test.go +++ b/app/receipt_test.go @@ -2,6 +2,7 @@ package app_test import ( "crypto/sha256" + "embed" "encoding/hex" "fmt" "math/big" @@ -20,6 +21,7 @@ import ( "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + pcommon "github.com/sei-protocol/sei-chain/precompiles/common" "github.com/sei-protocol/sei-chain/precompiles/wasmd" testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper" evmtypes "github.com/sei-protocol/sei-chain/x/evm/types" @@ -28,6 +30,9 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) +//go:embed wasm_abi.json +var f embed.FS + func TestEvmEventsForCw20(t *testing.T) { k := testkeeper.EVMTestApp.EvmKeeper wasmKeeper := k.WasmKeeper() @@ -74,8 +79,7 @@ func TestEvmEventsForCw20(t *testing.T) { require.True(t, found) // calling from wasmd precompile - abi, err := wasmd.GetABI() - require.Nil(t, err) + abi := pcommon.MustGetABI(f, "wasm_abi.json") emptyCoins, err := sdk.NewCoins().MarshalJSON() require.Nil(t, err) data, err := abi.Pack("execute", contractAddr.String(), payload, emptyCoins) @@ -189,8 +193,7 @@ func TestEvmEventsForCw721(t *testing.T) { require.True(t, found) // calling from wasmd precompile - abi, err := wasmd.GetABI() - require.Nil(t, err) + abi := pcommon.MustGetABI(f, "wasm_abi.json") emptyCoins, err := sdk.NewCoins().MarshalJSON() require.Nil(t, err) payload = []byte(fmt.Sprintf("{\"mint\":{\"token_id\":\"2\",\"owner\":\"%s\"}}", creator.String())) diff --git a/app/wasm_abi.json b/app/wasm_abi.json new file mode 100644 index 0000000000..c5e5b1b3f6 --- /dev/null +++ b/app/wasm_abi.json @@ -0,0 +1 @@ +[{"inputs":[{"internalType":"string","name":"contractAddress","type":"string"},{"internalType":"bytes","name":"msg","type":"bytes"},{"internalType":"bytes","name":"coins","type":"bytes"}],"name":"execute","outputs":[{"internalType":"bytes","name":"response","type":"bytes"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"string","name":"contractAddress","type":"string"},{"internalType":"bytes","name":"msg","type":"bytes"},{"internalType":"bytes","name":"coins","type":"bytes"}],"internalType":"struct IWasmd.ExecuteMsg[]","name":"executeMsgs","type":"tuple[]"}],"name":"execute_batch","outputs":[{"internalType":"bytes[]","name":"responses","type":"bytes[]"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint64","name":"codeID","type":"uint64"},{"internalType":"string","name":"admin","type":"string"},{"internalType":"bytes","name":"msg","type":"bytes"},{"internalType":"string","name":"label","type":"string"},{"internalType":"bytes","name":"coins","type":"bytes"}],"name":"instantiate","outputs":[{"internalType":"string","name":"contractAddr","type":"string"},{"internalType":"bytes","name":"data","type":"bytes"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"contractAddress","type":"string"},{"internalType":"bytes","name":"req","type":"bytes"}],"name":"query","outputs":[{"internalType":"bytes","name":"response","type":"bytes"}],"stateMutability":"view","type":"function"}] \ No newline at end of file diff --git a/precompiles/addr/addr.go b/precompiles/addr/addr.go index cd35c5ec5a..f232f18c8c 100644 --- a/precompiles/addr/addr.go +++ b/precompiles/addr/addr.go @@ -1,7 +1,6 @@ package addr import ( - "bytes" "embed" "fmt" "math/big" @@ -38,15 +37,7 @@ type PrecompileExecutor struct { } func NewPrecompile(evmKeeper pcommon.EVMKeeper) (*pcommon.Precompile, error) { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - return nil, fmt.Errorf("error loading the addr ABI %s", err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - return nil, err - } + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ evmKeeper: evmKeeper, diff --git a/precompiles/bank/bank.go b/precompiles/bank/bank.go index 84f9f607f5..efc137090f 100644 --- a/precompiles/bank/bank.go +++ b/precompiles/bank/bank.go @@ -1,7 +1,6 @@ package bank import ( - "bytes" "embed" "errors" "fmt" @@ -37,19 +36,6 @@ const ( //go:embed abi.json var f embed.FS -func GetABI() abi.ABI { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - panic(err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - panic(err) - } - return newAbi -} - type PrecompileExecutor struct { accountKeeper pcommon.AccountKeeper bankKeeper pcommon.BankKeeper @@ -72,7 +58,7 @@ type CoinBalance struct { } func NewPrecompile(bankKeeper pcommon.BankKeeper, evmKeeper pcommon.EVMKeeper, accountKeeper pcommon.AccountKeeper) (*pcommon.Precompile, error) { - newAbi := GetABI() + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ bankKeeper: bankKeeper, evmKeeper: evmKeeper, diff --git a/precompiles/bank/bank_test.go b/precompiles/bank/bank_test.go index d586de3525..107cbc69e5 100644 --- a/precompiles/bank/bank_test.go +++ b/precompiles/bank/bank_test.go @@ -1,6 +1,7 @@ package bank_test import ( + "embed" "encoding/hex" "fmt" "math/big" @@ -16,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "github.com/sei-protocol/sei-chain/precompiles/bank" + pcommon "github.com/sei-protocol/sei-chain/precompiles/common" testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper" "github.com/sei-protocol/sei-chain/x/evm/ante" "github.com/sei-protocol/sei-chain/x/evm/keeper" @@ -26,6 +28,9 @@ import ( tmtypes "github.com/tendermint/tendermint/proto/tendermint/types" ) +//go:embed abi.json +var f embed.FS + type mockTx struct { msgs []sdk.Msg signers []sdk.AccAddress @@ -99,7 +104,7 @@ func TestRun(t *testing.T) { // Send native 10_000_000_000_100, split into 10 usei 100wei // Test payable with eth LegacyTx - abi := bank.GetABI() + abi := pcommon.MustGetABI(f, "abi.json") argsNative, err := abi.Pack(bank.SendNativeMethod, seiAddr.String()) require.Nil(t, err) require.Nil(t, err) diff --git a/precompiles/common/precompiles.go b/precompiles/common/precompiles.go index cc1217893c..3692e74963 100644 --- a/precompiles/common/precompiles.go +++ b/precompiles/common/precompiles.go @@ -1,6 +1,8 @@ package common import ( + "bytes" + "embed" "errors" "fmt" "math/big" @@ -248,3 +250,16 @@ func DefaultGasCost(input []byte, isTransaction bool) uint64 { return storetypes.KVGasConfig().ReadCostFlat + (storetypes.KVGasConfig().ReadCostPerByte * uint64(len(input))) } + +func MustGetABI(f embed.FS, filename string) abi.ABI { + abiBz, err := f.ReadFile(filename) + if err != nil { + panic(err) + } + + newAbi, err := abi.JSON(bytes.NewReader(abiBz)) + if err != nil { + panic(err) + } + return newAbi +} diff --git a/precompiles/distribution/distribution.go b/precompiles/distribution/distribution.go index 539edcc9d8..d24c63efc0 100644 --- a/precompiles/distribution/distribution.go +++ b/precompiles/distribution/distribution.go @@ -1,7 +1,6 @@ package distribution import ( - "bytes" "embed" "errors" "fmt" @@ -34,19 +33,6 @@ const ( //go:embed abi.json var f embed.FS -func GetABI() abi.ABI { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - panic(err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - panic(err) - } - return newAbi -} - type PrecompileExecutor struct { distrKeeper pcommon.DistributionKeeper evmKeeper pcommon.EVMKeeper @@ -59,7 +45,7 @@ type PrecompileExecutor struct { } func NewPrecompile(distrKeeper pcommon.DistributionKeeper, evmKeeper pcommon.EVMKeeper) (*pcommon.DynamicGasPrecompile, error) { - newAbi := GetABI() + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ distrKeeper: distrKeeper, @@ -113,10 +99,6 @@ func (p PrecompileExecutor) EVMKeeper() pcommon.EVMKeeper { return p.evmKeeper } -func (p PrecompileExecutor) GetName() string { - return "distribution" -} - func (p PrecompileExecutor) setWithdrawAddress(ctx sdk.Context, method *abi.Method, caller common.Address, args []interface{}, value *big.Int) (ret []byte, remainingGas uint64, rerr error) { defer func() { if err := recover(); err != nil { diff --git a/precompiles/distribution/distribution_test.go b/precompiles/distribution/distribution_test.go index 3b68d1a487..cc7f0c770d 100644 --- a/precompiles/distribution/distribution_test.go +++ b/precompiles/distribution/distribution_test.go @@ -2,13 +2,15 @@ package distribution_test import ( "context" + "embed" "encoding/hex" - distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" "math/big" "reflect" "testing" "time" + distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types" + "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" crptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -36,6 +38,10 @@ import ( tmtypes "github.com/tendermint/tendermint/proto/tendermint/types" ) +//go:embed abi.json +//go:embed staking_abi.json +var f embed.FS + func TestWithdraw(t *testing.T) { testApp := testkeeper.EVMTestApp ctx := testApp.NewContext(false, tmtypes.Header{}).WithBlockHeight(2) @@ -47,7 +53,7 @@ func TestWithdraw(t *testing.T) { val := setupValidator(t, ctx, testApp, stakingtypes.Unbonded, valPub1) // delegate - abi := staking.GetABI() + abi := pcommon.MustGetABI(f, "staking_abi.json") args, err := abi.Pack("delegate", val.String()) require.Nil(t, err) @@ -95,7 +101,7 @@ func TestWithdraw(t *testing.T) { // set withdraw addr withdrawSeiAddr, withdrawAddr := testkeeper.MockAddressPair() k.SetAddressMapping(ctx, withdrawSeiAddr, withdrawAddr) - abi = distribution.GetABI() + abi = pcommon.MustGetABI(f, "abi.json") args, err = abi.Pack("setWithdrawAddress", withdrawAddr) require.Nil(t, err) addr = common.HexToAddress(distribution.DistrAddress) @@ -161,7 +167,7 @@ func TestWithdrawMultipleDelegationRewards(t *testing.T) { getValidator(t, ctx, testApp), getValidator(t, ctx, testApp)} - abi := staking.GetABI() + abi := pcommon.MustGetABI(f, "staking_abi.json") privKey := testkeeper.MockPrivateKey() addr := common.HexToAddress(staking.StakingAddress) chainID := k.ChainID(ctx) @@ -241,7 +247,7 @@ func setWithdrawAddressAndWithdraw( ) { withdrawSeiAddr, withdrawAddr := testkeeper.MockAddressPair() k.SetAddressMapping(ctx, withdrawSeiAddr, withdrawAddr) - abi := distribution.GetABI() + abi := pcommon.MustGetABI(f, "abi.json") args, err := abi.Pack("setWithdrawAddress", withdrawAddr) require.Nil(t, err) addr = common.HexToAddress(distribution.DistrAddress) diff --git a/precompiles/distribution/staking_abi.json b/precompiles/distribution/staking_abi.json new file mode 100755 index 0000000000..0be519e4d8 --- /dev/null +++ b/precompiles/distribution/staking_abi.json @@ -0,0 +1 @@ +[{"inputs":[{"internalType":"string","name":"valAddress","type":"string"}],"name":"delegate","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"srcAddress","type":"string"},{"internalType":"string","name":"dstAddress","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"redelegate","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"valAddress","type":"string"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"undelegate","outputs":[{"internalType":"bool","name":"success","type":"bool"}],"stateMutability":"nonpayable","type":"function"}] \ No newline at end of file diff --git a/precompiles/gov/gov.go b/precompiles/gov/gov.go index 802bcd3e1e..e3be07b761 100644 --- a/precompiles/gov/gov.go +++ b/precompiles/gov/gov.go @@ -29,19 +29,6 @@ const ( //go:embed abi.json var f embed.FS -func GetABI() abi.ABI { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - panic(err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - panic(err) - } - return newAbi -} - type PrecompileExecutor struct { govKeeper pcommon.GovKeeper evmKeeper pcommon.EVMKeeper @@ -53,7 +40,7 @@ type PrecompileExecutor struct { } func NewPrecompile(govKeeper pcommon.GovKeeper, evmKeeper pcommon.EVMKeeper, bankKeeper pcommon.BankKeeper) (*pcommon.Precompile, error) { - newAbi := GetABI() + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ govKeeper: govKeeper, diff --git a/precompiles/gov/gov_test.go b/precompiles/gov/gov_test.go index 43cca676b3..2c44fa81f4 100644 --- a/precompiles/gov/gov_test.go +++ b/precompiles/gov/gov_test.go @@ -1,6 +1,7 @@ package gov_test import ( + "embed" "encoding/hex" "math/big" "testing" @@ -13,6 +14,7 @@ import ( "github.com/stretchr/testify/require" tmtypes "github.com/tendermint/tendermint/proto/tendermint/types" + pcommon "github.com/sei-protocol/sei-chain/precompiles/common" "github.com/sei-protocol/sei-chain/precompiles/gov" testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper" "github.com/sei-protocol/sei-chain/x/evm/ante" @@ -21,6 +23,9 @@ import ( "github.com/sei-protocol/sei-chain/x/evm/types/ethtx" ) +//go:embed abi.json +var f embed.FS + func TestGovPrecompile(t *testing.T) { testApp := testkeeper.EVMTestApp ctx := testApp.NewContext(false, tmtypes.Header{}).WithBlockHeight(2) @@ -34,7 +39,7 @@ func TestGovPrecompile(t *testing.T) { testApp.GovKeeper.ActivateVotingPeriod(ctx, proposal2) k := &testApp.EvmKeeper - abi := gov.GetABI() + abi := pcommon.MustGetABI(f, "abi.json") type args struct { method string diff --git a/precompiles/ibc/ibc.go b/precompiles/ibc/ibc.go index f353b39f1d..160ab8cdb9 100644 --- a/precompiles/ibc/ibc.go +++ b/precompiles/ibc/ibc.go @@ -1,7 +1,6 @@ package ibc import ( - "bytes" "embed" "errors" "fmt" @@ -34,19 +33,6 @@ const ( //go:embed abi.json var f embed.FS -func GetABI() abi.ABI { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - panic(err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - panic(err) - } - return newAbi -} - type PrecompileExecutor struct { transferKeeper pcommon.TransferKeeper evmKeeper pcommon.EVMKeeper @@ -64,7 +50,7 @@ func NewPrecompile( clientKeeper pcommon.ClientKeeper, connectionKeeper pcommon.ConnectionKeeper, channelKeeper pcommon.ChannelKeeper) (*pcommon.DynamicGasPrecompile, error) { - newAbi := GetABI() + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ transferKeeper: transferKeeper, diff --git a/precompiles/json/json.go b/precompiles/json/json.go index eceab916bd..2536eb6847 100644 --- a/precompiles/json/json.go +++ b/precompiles/json/json.go @@ -1,7 +1,6 @@ package json import ( - "bytes" "embed" gjson "encoding/json" "errors" @@ -37,24 +36,8 @@ type PrecompileExecutor struct { ExtractAsUint256ID []byte } -func ABI() (*abi.ABI, error) { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - return nil, fmt.Errorf("error loading the json ABI %s", err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - return nil, err - } - return &newAbi, nil -} - func NewPrecompile() (*pcommon.Precompile, error) { - newAbi, err := ABI() - if err != nil { - return nil, err - } + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{} @@ -69,7 +52,7 @@ func NewPrecompile() (*pcommon.Precompile, error) { } } - return pcommon.NewPrecompile(*newAbi, p, common.HexToAddress(JSONAddress), "json"), nil + return pcommon.NewPrecompile(newAbi, p, common.HexToAddress(JSONAddress), "json"), nil } // RequiredGas returns the required bare minimum gas to execute the precompile. diff --git a/precompiles/oracle/oracle.go b/precompiles/oracle/oracle.go index 663f992a10..548fc063ed 100644 --- a/precompiles/oracle/oracle.go +++ b/precompiles/oracle/oracle.go @@ -1,7 +1,6 @@ package oracle import ( - "bytes" "embed" "math/big" @@ -28,19 +27,6 @@ const ( //go:embed abi.json var f embed.FS -func GetABI() abi.ABI { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - panic(err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - panic(err) - } - return newAbi -} - type PrecompileExecutor struct { evmKeeper pcommon.EVMKeeper oracleKeeper pcommon.OracleKeeper @@ -68,7 +54,7 @@ type OracleTwap struct { } func NewPrecompile(oracleKeeper pcommon.OracleKeeper, evmKeeper pcommon.EVMKeeper) (*pcommon.Precompile, error) { - newAbi := GetABI() + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ evmKeeper: evmKeeper, diff --git a/precompiles/pointer/pointer.go b/precompiles/pointer/pointer.go index 088d547fb3..8efe8386fe 100644 --- a/precompiles/pointer/pointer.go +++ b/precompiles/pointer/pointer.go @@ -1,7 +1,6 @@ package pointer import ( - "bytes" "embed" "encoding/json" "errors" @@ -41,24 +40,8 @@ type PrecompileExecutor struct { AddCW721PointerID []byte } -func ABI() (*ethabi.ABI, error) { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - return nil, fmt.Errorf("error loading the pointer ABI %s", err) - } - - newAbi, err := ethabi.JSON(bytes.NewReader(abiBz)) - if err != nil { - return nil, err - } - return &newAbi, nil -} - func NewPrecompile(evmKeeper pcommon.EVMKeeper, bankKeeper pcommon.BankKeeper, wasmdKeeper pcommon.WasmdViewKeeper) (*pcommon.DynamicGasPrecompile, error) { - newAbi, err := ABI() - if err != nil { - return nil, err - } + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ evmKeeper: evmKeeper, @@ -77,7 +60,7 @@ func NewPrecompile(evmKeeper pcommon.EVMKeeper, bankKeeper pcommon.BankKeeper, w } } - return pcommon.NewDynamicGasPrecompile(*newAbi, p, common.HexToAddress(PointerAddress), PrecompileName), nil + return pcommon.NewDynamicGasPrecompile(newAbi, p, common.HexToAddress(PointerAddress), PrecompileName), nil } func (p PrecompileExecutor) Execute(ctx sdk.Context, method *ethabi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool, evm *vm.EVM, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { diff --git a/precompiles/pointerview/pointerview.go b/precompiles/pointerview/pointerview.go index 912c6ac94d..4a55c204a4 100644 --- a/precompiles/pointerview/pointerview.go +++ b/precompiles/pointerview/pointerview.go @@ -1,7 +1,6 @@ package pointerview import ( - "bytes" "embed" "fmt" "math/big" @@ -35,15 +34,7 @@ type PrecompileExecutor struct { } func NewPrecompile(evmKeeper pcommon.EVMKeeper) (*pcommon.Precompile, error) { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - return nil, fmt.Errorf("error loading the pointer ABI %s", err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - return nil, err - } + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ evmKeeper: evmKeeper, @@ -69,6 +60,9 @@ func (p PrecompileExecutor) RequiredGas([]byte, *abi.Method) uint64 { } func (p PrecompileExecutor) Execute(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool, evm *vm.EVM) (ret []byte, err error) { + if err := pcommon.ValidateNonPayable(value); err != nil { + return nil, err + } switch method.Name { case GetNativePointer: return p.GetNative(ctx, method, args) diff --git a/precompiles/staking/staking.go b/precompiles/staking/staking.go index 4701d238ad..0e3e0629ac 100644 --- a/precompiles/staking/staking.go +++ b/precompiles/staking/staking.go @@ -30,19 +30,6 @@ const ( //go:embed abi.json var f embed.FS -func GetABI() abi.ABI { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - panic(err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - panic(err) - } - return newAbi -} - type PrecompileExecutor struct { stakingKeeper pcommon.StakingKeeper evmKeeper pcommon.EVMKeeper @@ -55,7 +42,7 @@ type PrecompileExecutor struct { } func NewPrecompile(stakingKeeper pcommon.StakingKeeper, evmKeeper pcommon.EVMKeeper, bankKeeper pcommon.BankKeeper) (*pcommon.Precompile, error) { - newAbi := GetABI() + newAbi := pcommon.MustGetABI(f, "abi.json") p := &PrecompileExecutor{ stakingKeeper: stakingKeeper, diff --git a/precompiles/staking/staking_test.go b/precompiles/staking/staking_test.go index 682dbcf2b3..a89b68d10b 100644 --- a/precompiles/staking/staking_test.go +++ b/precompiles/staking/staking_test.go @@ -1,6 +1,7 @@ package staking_test import ( + "embed" "encoding/hex" "math/big" "testing" @@ -16,6 +17,7 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/sei-protocol/sei-chain/app" + pcommon "github.com/sei-protocol/sei-chain/precompiles/common" "github.com/sei-protocol/sei-chain/precompiles/staking" testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper" "github.com/sei-protocol/sei-chain/x/evm/ante" @@ -27,6 +29,9 @@ import ( tmtypes "github.com/tendermint/tendermint/proto/tendermint/types" ) +//go:embed abi.json +var f embed.FS + func TestStaking(t *testing.T) { testApp := testkeeper.EVMTestApp ctx := testApp.NewContext(false, tmtypes.Header{}).WithBlockHeight(2) @@ -37,7 +42,7 @@ func TestStaking(t *testing.T) { val2 := setupValidator(t, ctx, testApp, stakingtypes.Unbonded, valPub2) // delegate - abi := staking.GetABI() + abi := pcommon.MustGetABI(f, "abi.json") args, err := abi.Pack("delegate", val.String()) require.Nil(t, err) @@ -146,7 +151,7 @@ func TestStakingError(t *testing.T) { val := setupValidator(t, ctx, testApp, stakingtypes.Unbonded, valPub1) val2 := setupValidator(t, ctx, testApp, stakingtypes.Unbonded, valPub2) - abi := staking.GetABI() + abi := pcommon.MustGetABI(f, "abi.json") args, err := abi.Pack("undelegate", val.String(), big.NewInt(100)) require.Nil(t, err) diff --git a/precompiles/wasmd/wasmd.go b/precompiles/wasmd/wasmd.go index da06a6b703..279132e8db 100644 --- a/precompiles/wasmd/wasmd.go +++ b/precompiles/wasmd/wasmd.go @@ -1,7 +1,6 @@ package wasmd import ( - "bytes" "embed" "encoding/json" "errors" @@ -52,24 +51,8 @@ type ExecuteMsg struct { Coins []byte `json:"coins"` } -func GetABI() (*abi.ABI, error) { - abiBz, err := f.ReadFile("abi.json") - if err != nil { - return nil, fmt.Errorf("error loading the wasmd ABI %s", err) - } - - newAbi, err := abi.JSON(bytes.NewReader(abiBz)) - if err != nil { - return nil, err - } - return &newAbi, err -} - func NewPrecompile(evmKeeper pcommon.EVMKeeper, wasmdKeeper pcommon.WasmdKeeper, wasmdViewKeeper pcommon.WasmdViewKeeper, bankKeeper pcommon.BankKeeper) (*pcommon.DynamicGasPrecompile, error) { - newAbi, err := GetABI() - if err != nil { - return nil, err - } + newAbi := pcommon.MustGetABI(f, "abi.json") executor := &PrecompileExecutor{ wasmdKeeper: wasmdKeeper, @@ -91,7 +74,7 @@ func NewPrecompile(evmKeeper pcommon.EVMKeeper, wasmdKeeper pcommon.WasmdKeeper, executor.QueryID = m.ID } } - return pcommon.NewDynamicGasPrecompile(*newAbi, executor, common.HexToAddress(WasmdAddress), "wasmd"), nil + return pcommon.NewDynamicGasPrecompile(newAbi, executor, common.HexToAddress(WasmdAddress), "wasmd"), nil } func (p PrecompileExecutor) Execute(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool, evm *vm.EVM, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) { diff --git a/x/evm/integration_test.go b/x/evm/integration_test.go index a5cb180a06..577fbcb46b 100644 --- a/x/evm/integration_test.go +++ b/x/evm/integration_test.go @@ -3,6 +3,7 @@ package evm_test import ( "bytes" "crypto/sha256" + "embed" "encoding/hex" "encoding/json" "fmt" @@ -16,6 +17,7 @@ import ( "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" + pcommon "github.com/sei-protocol/sei-chain/precompiles/common" "github.com/sei-protocol/sei-chain/precompiles/pointer" testkeeper "github.com/sei-protocol/sei-chain/testutil/keeper" "github.com/sei-protocol/sei-chain/x/evm/artifacts/cw721" @@ -26,6 +28,9 @@ import ( abci "github.com/tendermint/tendermint/abci/types" ) +//go:embed pointer_abi.json +var f embed.FS + func TestERC2981PointerToCW2981(t *testing.T) { k := testkeeper.EVMTestApp.EvmKeeper ctx := testkeeper.EVMTestApp.GetContextForDeliverTx([]byte{}).WithBlockTime(time.Now()) @@ -65,8 +70,7 @@ func TestERC2981PointerToCW2981(t *testing.T) { testPrivHex := hex.EncodeToString(privKey.Bytes()) key, _ := crypto.HexToECDSA(testPrivHex) to := common.HexToAddress(pointer.PointerAddress) - abi, err := pointer.ABI() - require.Nil(t, err) + abi := pcommon.MustGetABI(f, "pointer_abi.json") data, err := abi.Pack("addCW721Pointer", cw2981Addr.String()) require.Nil(t, err) txData := ethtypes.LegacyTx{ @@ -98,9 +102,9 @@ func TestERC2981PointerToCW2981(t *testing.T) { require.True(t, exists) require.NotEmpty(t, pointerAddr) // call pointer to get royalty info - abi, err = cw721.Cw721MetaData.GetAbi() + cw721abi, err := cw721.Cw721MetaData.GetAbi() require.Nil(t, err) - data, err = abi.Pack("royaltyInfo", big.NewInt(1), big.NewInt(1000)) + data, err = cw721abi.Pack("royaltyInfo", big.NewInt(1), big.NewInt(1000)) require.Nil(t, err) txData = ethtypes.LegacyTx{ Nonce: 1, @@ -126,7 +130,7 @@ func TestERC2981PointerToCW2981(t *testing.T) { require.Nil(t, typedTxData.Unmarshal(res.Data)) typedMsgData := types.MsgEVMTransactionResponse{} require.Nil(t, typedMsgData.Unmarshal(typedTxData.Data[0].Data)) - ret, err := abi.Unpack("royaltyInfo", typedMsgData.ReturnData) + ret, err := cw721abi.Unpack("royaltyInfo", typedMsgData.ReturnData) require.Nil(t, err) require.Equal(t, big.NewInt(10), ret[1].(*big.Int)) require.Equal(t, adminEvmAddr.Hex(), ret[0].(common.Address).Hex()) diff --git a/x/evm/keeper/code.go b/x/evm/keeper/code.go index 059f8a3da8..ac69c4a1a9 100644 --- a/x/evm/keeper/code.go +++ b/x/evm/keeper/code.go @@ -31,7 +31,9 @@ func (k *Keeper) SetCode(ctx sdk.Context, addr common.Address, code []byte) { h := crypto.Keccak256Hash(code) k.PrefixStore(ctx, types.CodeHashKeyPrefix).Set(addr[:], h[:]) // set association with direct cast Sei address for the contract address - k.SetAddressMapping(ctx, k.GetSeiAddressOrDefault(ctx, addr), addr) + if _, ok := k.GetSeiAddress(ctx, addr); !ok { + k.SetAddressMapping(ctx, k.GetSeiAddressOrDefault(ctx, addr), addr) + } } func (k *Keeper) GetCodeHash(ctx sdk.Context, addr common.Address) common.Hash { diff --git a/x/evm/pointer_abi.json b/x/evm/pointer_abi.json new file mode 100644 index 0000000000..8fb8238da0 --- /dev/null +++ b/x/evm/pointer_abi.json @@ -0,0 +1 @@ +[{"inputs":[{"internalType":"string","name":"cwAddr","type":"string"}],"name":"addCW20Pointer","outputs":[{"internalType":"address","name":"ret","type":"address"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"cwAddr","type":"string"}],"name":"addCW721Pointer","outputs":[{"internalType":"address","name":"ret","type":"address"}],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"string","name":"token","type":"string"}],"name":"addNativePointer","outputs":[{"internalType":"address","name":"ret","type":"address"}],"stateMutability":"payable","type":"function"}] \ No newline at end of file