@@ -11,13 +11,14 @@ import (
1111
1212 "github.com/ethereum/go-ethereum/accounts/abi"
1313 "github.com/ethereum/go-ethereum/common"
14- "github.com/ethereum/go-ethereum/core/tracing"
1514 "github.com/ethereum/go-ethereum/core/vm"
1615
1716 cmn "github.com/cosmos/evm/precompiles/common"
1817 evmtypes "github.com/cosmos/evm/x/vm/types"
1918
2019 storetypes "cosmossdk.io/store/types"
20+
21+ sdk "github.com/cosmos/cosmos-sdk/types"
2122)
2223
2324const (
@@ -33,14 +34,27 @@ const (
3334
3435var _ vm.PrecompiledContract = & Precompile {}
3536
36- // Embed abi json file to the executable binary. Needed when importing as dependency.
37- //
38- //go:embed abi.json
39- var f embed.FS
37+ var (
38+ // Embed abi json file to the executable binary. Needed when importing as dependency.
39+ //
40+ //go:embed abi.json
41+ f embed.FS
42+ ABI abi.ABI
43+ )
44+
45+ func init () {
46+ var err error
47+ ABI , err = cmn .LoadABI (f , "abi.json" )
48+ if err != nil {
49+ panic (err )
50+ }
51+ }
4052
4153// Precompile defines the bank precompile
4254type Precompile struct {
4355 cmn.Precompile
56+
57+ abi.ABI
4458 bankKeeper cmn.BankKeeper
4559 erc20Keeper cmn.ERC20Keeper
4660}
@@ -50,28 +64,19 @@ type Precompile struct {
5064func NewPrecompile (
5165 bankKeeper cmn.BankKeeper ,
5266 erc20Keeper cmn.ERC20Keeper ,
53- ) (* Precompile , error ) {
54- newABI , err := cmn .LoadABI (f , "abi.json" )
55- if err != nil {
56- return nil , err
57- }
58-
67+ ) * Precompile {
5968 // NOTE: we set an empty gas configuration to avoid extra gas costs
6069 // during the run execution
61- p := & Precompile {
70+ return & Precompile {
6271 Precompile : cmn.Precompile {
63- ABI : newABI ,
6472 KvGasConfig : storetypes.GasConfig {},
6573 TransientKVGasConfig : storetypes.GasConfig {},
74+ ContractAddress : common .HexToAddress (evmtypes .BankPrecompileAddress ),
6675 },
76+ ABI : ABI ,
6777 bankKeeper : bankKeeper ,
6878 erc20Keeper : erc20Keeper ,
6979 }
70-
71- // SetAddress defines the address of the bank compile contract.
72- p .SetAddress (common .HexToAddress (evmtypes .BankPrecompileAddress ))
73-
74- return p , nil
7580}
7681
7782// RequiredGas calculates the precompiled contract's base gas rate.
@@ -101,40 +106,33 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
101106 return 0
102107}
103108
104- // Run executes the precompiled contract bank query methods defined in the ABI.
105- func (p Precompile ) Run (evm * vm.EVM , contract * vm.Contract , readOnly bool ) (bz []byte , err error ) {
106- ctx , _ , method , initialGas , args , err := p .RunSetup (evm , contract , readOnly , p .IsTransaction )
109+ func (p Precompile ) Run (evm * vm.EVM , contract * vm.Contract , readonly bool ) ([]byte , error ) {
110+ return p .RunNativeAction (evm , contract , func (ctx sdk.Context ) ([]byte , error ) {
111+ return p .Execute (ctx , contract , readonly )
112+ })
113+ }
114+
115+ // Execute executes the precompiled contract bank query methods defined in the ABI.
116+ func (p Precompile ) Execute (ctx sdk.Context , contract * vm.Contract , readOnly bool ) ([]byte , error ) {
117+ method , args , err := cmn .SetupABI (p .ABI , contract , readOnly , p .IsTransaction )
107118 if err != nil {
108119 return nil , err
109120 }
110121
111- // This handles any out of gas errors that may occur during the execution of a precompile query.
112- // It avoids panics and returns the out of gas error so the EVM can continue gracefully.
113- defer cmn .HandleGasError (ctx , contract , initialGas , & err )()
114-
122+ var bz []byte
115123 switch method .Name {
116124 // Bank queries
117125 case BalancesMethod :
118- bz , err = p .Balances (ctx , contract , method , args )
126+ bz , err = p .Balances (ctx , method , args )
119127 case TotalSupplyMethod :
120- bz , err = p .TotalSupply (ctx , contract , method , args )
128+ bz , err = p .TotalSupply (ctx , method , args )
121129 case SupplyOfMethod :
122- bz , err = p .SupplyOf (ctx , contract , method , args )
130+ bz , err = p .SupplyOf (ctx , method , args )
123131 default :
124132 return nil , fmt .Errorf (cmn .ErrUnknownMethod , method .Name )
125133 }
126134
127- if err != nil {
128- return nil , err
129- }
130-
131- cost := ctx .GasMeter ().GasConsumed () - initialGas
132-
133- if ! contract .UseGas (cost , nil , tracing .GasChangeCallPrecompiledContract ) {
134- return nil , vm .ErrOutOfGas
135- }
136-
137- return bz , nil
135+ return bz , err
138136}
139137
140138// IsTransaction checks if the given method name corresponds to a transaction or query.
0 commit comments