diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index 0a579cb5..e619cf7c 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -62,6 +62,7 @@ import ( feemarkettypes "github.com/evmos/ethermint/x/feemarket/types" "github.com/spf13/cast" + "github.com/functionx/fx-core/v8/contract" fxtypes "github.com/functionx/fx-core/v8/types" arbitrumtypes "github.com/functionx/fx-core/v8/x/arbitrum/types" avalanchetypes "github.com/functionx/fx-core/v8/x/avalanche/types" @@ -362,7 +363,7 @@ func NewAppKeeper( appKeepers.AccountKeeper, appKeepers.BankKeeper, appKeepers.EvmKeeper, - appKeepers.EvmKeeper, + contract.NewERC20TokenKeeper(appKeepers.EvmKeeper), authAddr, ) diff --git a/cmd/delegate.go b/cmd/delegate.go index 5ffc6825..de5e079d 100644 --- a/cmd/delegate.go +++ b/cmd/delegate.go @@ -141,16 +141,14 @@ func queryContractBalance(myApp *app.App, ctx sdk.Context, contractAddr, address if contract.IsZeroEthAddress(contractAddr) { return } - - var balanceRes struct{ Value *big.Int } - err := myApp.EvmKeeper.QueryContract(ctx, contractAddr, contractAddr, contract.GetFIP20().ABI, "balanceOf", &balanceRes, address) + balance, err := contract.NewERC20TokenKeeper(myApp.EvmKeeper).BalanceOf(ctx, contractAddr, address) if err != nil { panic(err) } - if balanceRes.Value.Cmp(big.NewInt(0)) == 0 { + if balance.Cmp(big.NewInt(0)) == 0 { return } - holders[address.Hex()] = sdkmath.NewIntFromBigInt(balanceRes.Value) + holders[address.Hex()] = sdkmath.NewIntFromBigInt(balance) } func buildApp(db dbm.DB, height int64) (*app.App, sdk.Context, error) { diff --git a/contract/eth_validation.go b/contract/address.go similarity index 100% rename from contract/eth_validation.go rename to contract/address.go diff --git a/contract/eth_validation_test.go b/contract/address_test.go similarity index 100% rename from contract/eth_validation_test.go rename to contract/address_test.go diff --git a/contract/contract.go b/contract/contract.go index dadecdd0..df67dba3 100644 --- a/contract/contract.go +++ b/contract/contract.go @@ -1,12 +1,15 @@ package contract import ( + "math/big" "strings" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" + evmtypes "github.com/evmos/ethermint/x/evm/types" ) const ( @@ -17,23 +20,24 @@ const ( CrosschainAddress = "0x0000000000000000000000000000000000001004" ) -var ( - initFIP20Code = MustDecodeHex("0x6080604052600436106101095760003560e01c806370a08231116100955780639dc29fac116100645780639dc29fac146102bc578063a9059cbb146102dc578063dd62ed3e146102fc578063de7ea79d14610342578063f2fde38b1461036257600080fd5b806370a0823114610234578063715018a61461026a5780638da5cb5b1461027f57806395d89b41146102a757600080fd5b8063313ce567116100dc578063313ce567146101a85780633659cfe6146101ca57806340c10f19146101ec5780634f1ef2861461020c57806352d1902d1461021f57600080fd5b806306fdde031461010e578063095ea7b31461013957806318160ddd1461016957806323b872dd14610188575b600080fd5b34801561011a57600080fd5b50610123610382565b60405161013091906113d1565b60405180910390f35b34801561014557600080fd5b50610159610154366004611420565b610414565b6040519015158152602001610130565b34801561017557600080fd5b5060cc545b604051908152602001610130565b34801561019457600080fd5b506101596101a336600461144a565b61046a565b3480156101b457600080fd5b5060cb5460405160ff9091168152602001610130565b3480156101d657600080fd5b506101ea6101e5366004611486565b610517565b005b3480156101f857600080fd5b506101ea610207366004611420565b6105f7565b6101ea61021a36600461152d565b61060d565b34801561022b57600080fd5b5061017a6106da565b34801561024057600080fd5b5061017a61024f366004611486565b6001600160a01b0316600090815260cd602052604090205490565b34801561027657600080fd5b506101ea61078d565b34801561028b57600080fd5b506097546040516001600160a01b039091168152602001610130565b3480156102b357600080fd5b506101236107a1565b3480156102c857600080fd5b506101ea6102d7366004611420565b6107b0565b3480156102e857600080fd5b506101596102f7366004611420565b6107c2565b34801561030857600080fd5b5061017a61031736600461158f565b6001600160a01b03918216600090815260ce6020908152604080832093909416825291909152205490565b34801561034e57600080fd5b506101ea61035d3660046115e2565b6107d8565b34801561036e57600080fd5b506101ea61037d366004611486565b610947565b606060c980546103919061166f565b80601f01602080910402602001604051908101604052809291908181526020018280546103bd9061166f565b801561040a5780601f106103df5761010080835404028352916020019161040a565b820191906000526020600020905b8154815290600101906020018083116103ed57829003601f168201915b5050505050905090565b60006104213384846109bd565b6040518281526001600160a01b0384169033907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259060200160405180910390a350600192915050565b6001600160a01b038316600090815260ce60209081526040808320338452909152812054828110156104ed5760405162461bcd60e51b815260206004820152602160248201527f7472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636044820152606560f81b60648201526084015b60405180910390fd5b61050185336104fc86856116c0565b6109bd565b61050c858585610a3f565b506001949350505050565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010011614156105605760405162461bcd60e51b81526004016104e4906116d7565b7f00000000000000000000000000000000000000000000000000000000000010016001600160a01b03166105a9600080516020611808833981519152546001600160a01b031690565b6001600160a01b0316146105cf5760405162461bcd60e51b81526004016104e490611723565b6105d881610bee565b604080516000808252602082019092526105f491839190610bf6565b50565b6105ff610d66565b6106098282610dc0565b5050565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010011614156106565760405162461bcd60e51b81526004016104e4906116d7565b7f00000000000000000000000000000000000000000000000000000000000010016001600160a01b031661069f600080516020611808833981519152546001600160a01b031690565b6001600160a01b0316146106c55760405162461bcd60e51b81526004016104e490611723565b6106ce82610bee565b61060982826001610bf6565b6000306001600160a01b037f0000000000000000000000000000000000000000000000000000000000001001161461077a5760405162461bcd60e51b815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c000000000000000060648201526084016104e4565b5060008051602061180883398151915290565b610795610d66565b61079f6000610e9f565b565b606060ca80546103919061166f565b6107b8610d66565b6106098282610ef1565b60006107cf338484610a3f565b50600192915050565b600054610100900460ff16158080156107f85750600054600160ff909116105b806108125750303b158015610812575060005460ff166001145b6108755760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084016104e4565b6000805460ff191660011790558015610898576000805461ff0019166101001790555b84516108ab9060c990602088019061130c565b5083516108bf9060ca90602087019061130c565b5060cb805460ff191660ff851617905560cf80546001600160a01b0319166001600160a01b0384161790556108f2611033565b6108fa611062565b8015610940576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050505050565b61094f610d66565b6001600160a01b0381166109b45760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016104e4565b6105f481610e9f565b6001600160a01b038316610a135760405162461bcd60e51b815260206004820152601d60248201527f617070726f76652066726f6d20746865207a65726f206164647265737300000060448201526064016104e4565b6001600160a01b03928316600090815260ce602090815260408083209490951682529290925291902055565b6001600160a01b038316610a955760405162461bcd60e51b815260206004820152601e60248201527f7472616e736665722066726f6d20746865207a65726f2061646472657373000060448201526064016104e4565b6001600160a01b038216610aeb5760405162461bcd60e51b815260206004820152601c60248201527f7472616e7366657220746f20746865207a65726f20616464726573730000000060448201526064016104e4565b6001600160a01b038316600090815260cd602052604090205481811015610b545760405162461bcd60e51b815260206004820152601f60248201527f7472616e7366657220616d6f756e7420657863656564732062616c616e63650060448201526064016104e4565b610b5e82826116c0565b6001600160a01b03808616600090815260cd60205260408082209390935590851681529081208054849290610b9490849061176f565b92505081905550826001600160a01b0316846001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610be091815260200190565b60405180910390a350505050565b6105f4610d66565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff1615610c2e57610c2983611089565b505050565b826001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa925050508015610c88575060408051601f3d908101601f19168201909252610c8591810190611787565b60015b610ceb5760405162461bcd60e51b815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201526d6f6e206973206e6f74205555505360901b60648201526084016104e4565b6000805160206118088339815191528114610d5a5760405162461bcd60e51b815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f786044820152681a58589b195555525160ba1b60648201526084016104e4565b50610c29838383611125565b6097546001600160a01b0316331461079f5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016104e4565b6001600160a01b038216610e165760405162461bcd60e51b815260206004820152601860248201527f6d696e7420746f20746865207a65726f2061646472657373000000000000000060448201526064016104e4565b8060cc6000828254610e28919061176f565b90915550506001600160a01b038216600090815260cd602052604081208054839290610e5590849061176f565b90915550506040518181526001600160a01b038316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a35050565b609780546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b6001600160a01b038216610f475760405162461bcd60e51b815260206004820152601a60248201527f6275726e2066726f6d20746865207a65726f206164647265737300000000000060448201526064016104e4565b6001600160a01b038216600090815260cd602052604090205481811015610fb05760405162461bcd60e51b815260206004820152601b60248201527f6275726e20616d6f756e7420657863656564732062616c616e6365000000000060448201526064016104e4565b610fba82826116c0565b6001600160a01b038416600090815260cd602052604081209190915560cc8054849290610fe89084906116c0565b90915550506040518281526000906001600160a01b038516907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a3505050565b600054610100900460ff1661105a5760405162461bcd60e51b81526004016104e4906117a0565b61079f611150565b600054610100900460ff1661079f5760405162461bcd60e51b81526004016104e4906117a0565b6001600160a01b0381163b6110f65760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201526c1bdd08184818dbdb9d1c9858dd609a1b60648201526084016104e4565b60008051602061180883398151915280546001600160a01b0319166001600160a01b0392909216919091179055565b61112e83611180565b60008251118061113b5750805b15610c295761114a83836111c0565b50505050565b600054610100900460ff166111775760405162461bcd60e51b81526004016104e4906117a0565b61079f33610e9f565b61118981611089565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b60606111e58383604051806060016040528060278152602001611828602791396111ec565b9392505050565b6060600080856001600160a01b03168560405161120991906117eb565b600060405180830381855af49150503d8060008114611244576040519150601f19603f3d011682016040523d82523d6000602084013e611249565b606091505b509150915061125a86838387611264565b9695505050505050565b606083156112d05782516112c9576001600160a01b0385163b6112c95760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016104e4565b50816112da565b6112da83836112e2565b949350505050565b8151156112f25781518083602001fd5b8060405162461bcd60e51b81526004016104e491906113d1565b8280546113189061166f565b90600052602060002090601f01602090048101928261133a5760008555611380565b82601f1061135357805160ff1916838001178555611380565b82800160010185558215611380579182015b82811115611380578251825591602001919060010190611365565b5061138c929150611390565b5090565b5b8082111561138c5760008155600101611391565b60005b838110156113c05781810151838201526020016113a8565b8381111561114a5750506000910152565b60208152600082518060208401526113f08160408501602087016113a5565b601f01601f19169190910160400192915050565b80356001600160a01b038116811461141b57600080fd5b919050565b6000806040838503121561143357600080fd5b61143c83611404565b946020939093013593505050565b60008060006060848603121561145f57600080fd5b61146884611404565b925061147660208501611404565b9150604084013590509250925092565b60006020828403121561149857600080fd5b6111e582611404565b634e487b7160e01b600052604160045260246000fd5b600067ffffffffffffffff808411156114d2576114d26114a1565b604051601f8501601f19908116603f011681019082821181831017156114fa576114fa6114a1565b8160405280935085815286868601111561151357600080fd5b858560208301376000602087830101525050509392505050565b6000806040838503121561154057600080fd5b61154983611404565b9150602083013567ffffffffffffffff81111561156557600080fd5b8301601f8101851361157657600080fd5b611585858235602084016114b7565b9150509250929050565b600080604083850312156115a257600080fd5b6115ab83611404565b91506115b960208401611404565b90509250929050565b600082601f8301126115d357600080fd5b6111e5838335602085016114b7565b600080600080608085870312156115f857600080fd5b843567ffffffffffffffff8082111561161057600080fd5b61161c888389016115c2565b9550602087013591508082111561163257600080fd5b5061163f878288016115c2565b935050604085013560ff8116811461165657600080fd5b915061166460608601611404565b905092959194509250565b600181811c9082168061168357607f821691505b602082108114156116a457634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b6000828210156116d2576116d26116aa565b500390565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b19195b1959d85d1958d85b1b60a21b606082015260800190565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b6163746976652070726f787960a01b606082015260800190565b60008219821115611782576117826116aa565b500190565b60006020828403121561179957600080fd5b5051919050565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b600082516117fd8184602087016113a5565b919091019291505056fe360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a2646970667358221220c24d636966a60df9636041ea92ff12a963fc67961fabb8564b1631506b7785e864736f6c634300080a0033") - initWFXCode = MustDecodeHex("0x6080604052600436106101395760003560e01c8063715018a6116100ab578063b86d52981161006f578063b86d529814610366578063d0e30db014610148578063dd62ed3e14610384578063de7ea79d146103ca578063f2fde38b146103ea578063f3fef3a31461040a57610148565b8063715018a6146102ca5780638da5cb5b146102df57806395d89b41146103115780639dc29fac14610326578063a9059cbb1461034657610148565b8063313ce567116100fd578063313ce5671461020a5780633659cfe61461022c57806340c10f191461024c5780634f1ef2861461026c57806352d1902d1461027f57806370a082311461029457610148565b806306fdde0314610150578063095ea7b31461017b57806318160ddd146101ab57806323b872dd146101ca5780632e1a7d4d146101ea57610148565b366101485761014661042a565b005b61014661042a565b34801561015c57600080fd5b5061016561046b565b60405161017291906115a9565b60405180910390f35b34801561018757600080fd5b5061019b6101963660046115f1565b6104fd565b6040519015158152602001610172565b3480156101b757600080fd5b5060cc545b604051908152602001610172565b3480156101d657600080fd5b5061019b6101e536600461161d565b610553565b3480156101f657600080fd5b5061014661020536600461165e565b610600565b34801561021657600080fd5b5060cb5460405160ff9091168152602001610172565b34801561023857600080fd5b50610146610247366004611677565b610671565b34801561025857600080fd5b506101466102673660046115f1565b610751565b61014661027a366004611720565b610767565b34801561028b57600080fd5b506101bc610834565b3480156102a057600080fd5b506101bc6102af366004611677565b6001600160a01b0316600090815260cd602052604090205490565b3480156102d657600080fd5b506101466108e7565b3480156102eb57600080fd5b506097546001600160a01b03165b6040516001600160a01b039091168152602001610172565b34801561031d57600080fd5b506101656108fb565b34801561033257600080fd5b506101466103413660046115f1565b61090a565b34801561035257600080fd5b5061019b6103613660046115f1565b61091c565b34801561037257600080fd5b5060cf546001600160a01b03166102f9565b34801561039057600080fd5b506101bc61039f366004611784565b6001600160a01b03918216600090815260ce6020908152604080832093909416825291909152205490565b3480156103d657600080fd5b506101466103e53660046117dd565b610932565b3480156103f657600080fd5b50610146610405366004611677565b610aa1565b34801561041657600080fd5b506101466104253660046115f1565b610b17565b6104343334610b9c565b60405134815233907fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c9060200160405180910390a2565b606060c9805461047a9061186c565b80601f01602080910402602001604051908101604052809291908181526020018280546104a69061186c565b80156104f35780601f106104c8576101008083540402835291602001916104f3565b820191906000526020600020905b8154815290600101906020018083116104d657829003601f168201915b5050505050905090565b600061050a338484610c74565b6040518281526001600160a01b0384169033907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259060200160405180910390a350600192915050565b6001600160a01b038316600090815260ce60209081526040808320338452909152812054828110156105d65760405162461bcd60e51b815260206004820152602160248201527f7472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636044820152606560f81b60648201526084015b60405180910390fd5b6105ea85336105e586856118bd565b610c74565b6105f5858585610cf6565b506001949350505050565b61060b335b82610ea5565b604051339082156108fc029083906000818181858888f19350505050158015610638573d6000803e3d6000fd5b5060405181815233907f884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a94243649060200160405180910390a250565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010021614156106ba5760405162461bcd60e51b81526004016105cd906118d4565b7f00000000000000000000000000000000000000000000000000000000000010026001600160a01b0316610703600080516020611a05833981519152546001600160a01b031690565b6001600160a01b0316146107295760405162461bcd60e51b81526004016105cd90611920565b61073281610fe7565b6040805160008082526020820190925261074e91839190610fef565b50565b61075961115f565b6107638282610b9c565b5050565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010021614156107b05760405162461bcd60e51b81526004016105cd906118d4565b7f00000000000000000000000000000000000000000000000000000000000010026001600160a01b03166107f9600080516020611a05833981519152546001600160a01b031690565b6001600160a01b03161461081f5760405162461bcd60e51b81526004016105cd90611920565b61082882610fe7565b61076382826001610fef565b6000306001600160a01b037f000000000000000000000000000000000000000000000000000000000000100216146108d45760405162461bcd60e51b815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c000000000000000060648201526084016105cd565b50600080516020611a0583398151915290565b6108ef61115f565b6108f960006111b9565b565b606060ca805461047a9061186c565b61091261115f565b6107638282610ea5565b6000610929338484610cf6565b50600192915050565b600054610100900460ff16158080156109525750600054600160ff909116105b8061096c5750303b15801561096c575060005460ff166001145b6109cf5760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084016105cd565b6000805460ff1916600117905580156109f2576000805461ff0019166101001790555b8451610a059060c99060208801906114e4565b508351610a199060ca9060208701906114e4565b5060cb805460ff191660ff851617905560cf80546001600160a01b0319166001600160a01b038416179055610a4c61120b565b610a5461123a565b8015610a9a576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050505050565b610aa961115f565b6001600160a01b038116610b0e5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016105cd565b61074e816111b9565b610b2033610605565b6040516001600160a01b0383169082156108fc029083906000818181858888f19350505050158015610b56573d6000803e3d6000fd5b506040518181526001600160a01b0383169033907f9b1bfa7fa9ee420a16e124f794c35ac9f90472acc99140eb2f6447c714cad8eb906020015b60405180910390a35050565b6001600160a01b038216610bf25760405162461bcd60e51b815260206004820152601860248201527f6d696e7420746f20746865207a65726f2061646472657373000000000000000060448201526064016105cd565b8060cc6000828254610c04919061196c565b90915550506001600160a01b038216600090815260cd602052604081208054839290610c3190849061196c565b90915550506040518181526001600160a01b038316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef90602001610b90565b6001600160a01b038316610cca5760405162461bcd60e51b815260206004820152601d60248201527f617070726f76652066726f6d20746865207a65726f206164647265737300000060448201526064016105cd565b6001600160a01b03928316600090815260ce602090815260408083209490951682529290925291902055565b6001600160a01b038316610d4c5760405162461bcd60e51b815260206004820152601e60248201527f7472616e736665722066726f6d20746865207a65726f2061646472657373000060448201526064016105cd565b6001600160a01b038216610da25760405162461bcd60e51b815260206004820152601c60248201527f7472616e7366657220746f20746865207a65726f20616464726573730000000060448201526064016105cd565b6001600160a01b038316600090815260cd602052604090205481811015610e0b5760405162461bcd60e51b815260206004820152601f60248201527f7472616e7366657220616d6f756e7420657863656564732062616c616e63650060448201526064016105cd565b610e1582826118bd565b6001600160a01b03808616600090815260cd60205260408082209390935590851681529081208054849290610e4b90849061196c565b92505081905550826001600160a01b0316846001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610e9791815260200190565b60405180910390a350505050565b6001600160a01b038216610efb5760405162461bcd60e51b815260206004820152601a60248201527f6275726e2066726f6d20746865207a65726f206164647265737300000000000060448201526064016105cd565b6001600160a01b038216600090815260cd602052604090205481811015610f645760405162461bcd60e51b815260206004820152601b60248201527f6275726e20616d6f756e7420657863656564732062616c616e6365000000000060448201526064016105cd565b610f6e82826118bd565b6001600160a01b038416600090815260cd602052604081209190915560cc8054849290610f9c9084906118bd565b90915550506040518281526000906001600160a01b038516907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a3505050565b61074e61115f565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff16156110275761102283611261565b505050565b826001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa925050508015611081575060408051601f3d908101601f1916820190925261107e91810190611984565b60015b6110e45760405162461bcd60e51b815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201526d6f6e206973206e6f74205555505360901b60648201526084016105cd565b600080516020611a0583398151915281146111535760405162461bcd60e51b815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f786044820152681a58589b195555525160ba1b60648201526084016105cd565b506110228383836112fd565b6097546001600160a01b031633146108f95760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016105cd565b609780546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b600054610100900460ff166112325760405162461bcd60e51b81526004016105cd9061199d565b6108f9611328565b600054610100900460ff166108f95760405162461bcd60e51b81526004016105cd9061199d565b6001600160a01b0381163b6112ce5760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201526c1bdd08184818dbdb9d1c9858dd609a1b60648201526084016105cd565b600080516020611a0583398151915280546001600160a01b0319166001600160a01b0392909216919091179055565b61130683611358565b6000825111806113135750805b15611022576113228383611398565b50505050565b600054610100900460ff1661134f5760405162461bcd60e51b81526004016105cd9061199d565b6108f9336111b9565b61136181611261565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b60606113bd8383604051806060016040528060278152602001611a25602791396113c4565b9392505050565b6060600080856001600160a01b0316856040516113e191906119e8565b600060405180830381855af49150503d806000811461141c576040519150601f19603f3d011682016040523d82523d6000602084013e611421565b606091505b50915091506114328683838761143c565b9695505050505050565b606083156114a85782516114a1576001600160a01b0385163b6114a15760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016105cd565b50816114b2565b6114b283836114ba565b949350505050565b8151156114ca5781518083602001fd5b8060405162461bcd60e51b81526004016105cd91906115a9565b8280546114f09061186c565b90600052602060002090601f0160209004810192826115125760008555611558565b82601f1061152b57805160ff1916838001178555611558565b82800160010185558215611558579182015b8281111561155857825182559160200191906001019061153d565b50611564929150611568565b5090565b5b808211156115645760008155600101611569565b60005b83811015611598578181015183820152602001611580565b838111156113225750506000910152565b60208152600082518060208401526115c881604085016020870161157d565b601f01601f19169190910160400192915050565b6001600160a01b038116811461074e57600080fd5b6000806040838503121561160457600080fd5b823561160f816115dc565b946020939093013593505050565b60008060006060848603121561163257600080fd5b833561163d816115dc565b9250602084013561164d816115dc565b929592945050506040919091013590565b60006020828403121561167057600080fd5b5035919050565b60006020828403121561168957600080fd5b81356113bd816115dc565b634e487b7160e01b600052604160045260246000fd5b600067ffffffffffffffff808411156116c5576116c5611694565b604051601f8501601f19908116603f011681019082821181831017156116ed576116ed611694565b8160405280935085815286868601111561170657600080fd5b858560208301376000602087830101525050509392505050565b6000806040838503121561173357600080fd5b823561173e816115dc565b9150602083013567ffffffffffffffff81111561175a57600080fd5b8301601f8101851361176b57600080fd5b61177a858235602084016116aa565b9150509250929050565b6000806040838503121561179757600080fd5b82356117a2816115dc565b915060208301356117b2816115dc565b809150509250929050565b600082601f8301126117ce57600080fd5b6113bd838335602085016116aa565b600080600080608085870312156117f357600080fd5b843567ffffffffffffffff8082111561180b57600080fd5b611817888389016117bd565b9550602087013591508082111561182d57600080fd5b5061183a878288016117bd565b935050604085013560ff8116811461185157600080fd5b91506060850135611861816115dc565b939692955090935050565b600181811c9082168061188057607f821691505b602082108114156118a157634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b6000828210156118cf576118cf6118a7565b500390565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b19195b1959d85d1958d85b1b60a21b606082015260800190565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b6163746976652070726f787960a01b606082015260800190565b6000821982111561197f5761197f6118a7565b500190565b60006020828403121561199657600080fd5b5051919050565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b600082516119fa81846020870161157d565b919091019291505056fe360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a26469706673582212203491f4b9433597e502b7d2b2ea2025687fa17dcc511ea87b77ea73cb9c0109d464736f6c634300080a0033") +const ( + DefaultGasCap uint64 = 30000000 ) +var typeString, _ = abi.NewType("string", "", nil) + var ( fip20Init = Contract{ Address: common.HexToAddress(FIP20LogicAddress), ABI: MustABIJson(FIP20UpgradableMetaData.ABI), Bin: MustDecodeHex(FIP20UpgradableMetaData.Bin), - Code: initFIP20Code, + Code: MustDecodeHex("0x6080604052600436106101095760003560e01c806370a08231116100955780639dc29fac116100645780639dc29fac146102bc578063a9059cbb146102dc578063dd62ed3e146102fc578063de7ea79d14610342578063f2fde38b1461036257600080fd5b806370a0823114610234578063715018a61461026a5780638da5cb5b1461027f57806395d89b41146102a757600080fd5b8063313ce567116100dc578063313ce567146101a85780633659cfe6146101ca57806340c10f19146101ec5780634f1ef2861461020c57806352d1902d1461021f57600080fd5b806306fdde031461010e578063095ea7b31461013957806318160ddd1461016957806323b872dd14610188575b600080fd5b34801561011a57600080fd5b50610123610382565b60405161013091906113d1565b60405180910390f35b34801561014557600080fd5b50610159610154366004611420565b610414565b6040519015158152602001610130565b34801561017557600080fd5b5060cc545b604051908152602001610130565b34801561019457600080fd5b506101596101a336600461144a565b61046a565b3480156101b457600080fd5b5060cb5460405160ff9091168152602001610130565b3480156101d657600080fd5b506101ea6101e5366004611486565b610517565b005b3480156101f857600080fd5b506101ea610207366004611420565b6105f7565b6101ea61021a36600461152d565b61060d565b34801561022b57600080fd5b5061017a6106da565b34801561024057600080fd5b5061017a61024f366004611486565b6001600160a01b0316600090815260cd602052604090205490565b34801561027657600080fd5b506101ea61078d565b34801561028b57600080fd5b506097546040516001600160a01b039091168152602001610130565b3480156102b357600080fd5b506101236107a1565b3480156102c857600080fd5b506101ea6102d7366004611420565b6107b0565b3480156102e857600080fd5b506101596102f7366004611420565b6107c2565b34801561030857600080fd5b5061017a61031736600461158f565b6001600160a01b03918216600090815260ce6020908152604080832093909416825291909152205490565b34801561034e57600080fd5b506101ea61035d3660046115e2565b6107d8565b34801561036e57600080fd5b506101ea61037d366004611486565b610947565b606060c980546103919061166f565b80601f01602080910402602001604051908101604052809291908181526020018280546103bd9061166f565b801561040a5780601f106103df5761010080835404028352916020019161040a565b820191906000526020600020905b8154815290600101906020018083116103ed57829003601f168201915b5050505050905090565b60006104213384846109bd565b6040518281526001600160a01b0384169033907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259060200160405180910390a350600192915050565b6001600160a01b038316600090815260ce60209081526040808320338452909152812054828110156104ed5760405162461bcd60e51b815260206004820152602160248201527f7472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636044820152606560f81b60648201526084015b60405180910390fd5b61050185336104fc86856116c0565b6109bd565b61050c858585610a3f565b506001949350505050565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010011614156105605760405162461bcd60e51b81526004016104e4906116d7565b7f00000000000000000000000000000000000000000000000000000000000010016001600160a01b03166105a9600080516020611808833981519152546001600160a01b031690565b6001600160a01b0316146105cf5760405162461bcd60e51b81526004016104e490611723565b6105d881610bee565b604080516000808252602082019092526105f491839190610bf6565b50565b6105ff610d66565b6106098282610dc0565b5050565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010011614156106565760405162461bcd60e51b81526004016104e4906116d7565b7f00000000000000000000000000000000000000000000000000000000000010016001600160a01b031661069f600080516020611808833981519152546001600160a01b031690565b6001600160a01b0316146106c55760405162461bcd60e51b81526004016104e490611723565b6106ce82610bee565b61060982826001610bf6565b6000306001600160a01b037f0000000000000000000000000000000000000000000000000000000000001001161461077a5760405162461bcd60e51b815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c000000000000000060648201526084016104e4565b5060008051602061180883398151915290565b610795610d66565b61079f6000610e9f565b565b606060ca80546103919061166f565b6107b8610d66565b6106098282610ef1565b60006107cf338484610a3f565b50600192915050565b600054610100900460ff16158080156107f85750600054600160ff909116105b806108125750303b158015610812575060005460ff166001145b6108755760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084016104e4565b6000805460ff191660011790558015610898576000805461ff0019166101001790555b84516108ab9060c990602088019061130c565b5083516108bf9060ca90602087019061130c565b5060cb805460ff191660ff851617905560cf80546001600160a01b0319166001600160a01b0384161790556108f2611033565b6108fa611062565b8015610940576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050505050565b61094f610d66565b6001600160a01b0381166109b45760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016104e4565b6105f481610e9f565b6001600160a01b038316610a135760405162461bcd60e51b815260206004820152601d60248201527f617070726f76652066726f6d20746865207a65726f206164647265737300000060448201526064016104e4565b6001600160a01b03928316600090815260ce602090815260408083209490951682529290925291902055565b6001600160a01b038316610a955760405162461bcd60e51b815260206004820152601e60248201527f7472616e736665722066726f6d20746865207a65726f2061646472657373000060448201526064016104e4565b6001600160a01b038216610aeb5760405162461bcd60e51b815260206004820152601c60248201527f7472616e7366657220746f20746865207a65726f20616464726573730000000060448201526064016104e4565b6001600160a01b038316600090815260cd602052604090205481811015610b545760405162461bcd60e51b815260206004820152601f60248201527f7472616e7366657220616d6f756e7420657863656564732062616c616e63650060448201526064016104e4565b610b5e82826116c0565b6001600160a01b03808616600090815260cd60205260408082209390935590851681529081208054849290610b9490849061176f565b92505081905550826001600160a01b0316846001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610be091815260200190565b60405180910390a350505050565b6105f4610d66565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff1615610c2e57610c2983611089565b505050565b826001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa925050508015610c88575060408051601f3d908101601f19168201909252610c8591810190611787565b60015b610ceb5760405162461bcd60e51b815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201526d6f6e206973206e6f74205555505360901b60648201526084016104e4565b6000805160206118088339815191528114610d5a5760405162461bcd60e51b815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f786044820152681a58589b195555525160ba1b60648201526084016104e4565b50610c29838383611125565b6097546001600160a01b0316331461079f5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016104e4565b6001600160a01b038216610e165760405162461bcd60e51b815260206004820152601860248201527f6d696e7420746f20746865207a65726f2061646472657373000000000000000060448201526064016104e4565b8060cc6000828254610e28919061176f565b90915550506001600160a01b038216600090815260cd602052604081208054839290610e5590849061176f565b90915550506040518181526001600160a01b038316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a35050565b609780546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b6001600160a01b038216610f475760405162461bcd60e51b815260206004820152601a60248201527f6275726e2066726f6d20746865207a65726f206164647265737300000000000060448201526064016104e4565b6001600160a01b038216600090815260cd602052604090205481811015610fb05760405162461bcd60e51b815260206004820152601b60248201527f6275726e20616d6f756e7420657863656564732062616c616e6365000000000060448201526064016104e4565b610fba82826116c0565b6001600160a01b038416600090815260cd602052604081209190915560cc8054849290610fe89084906116c0565b90915550506040518281526000906001600160a01b038516907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a3505050565b600054610100900460ff1661105a5760405162461bcd60e51b81526004016104e4906117a0565b61079f611150565b600054610100900460ff1661079f5760405162461bcd60e51b81526004016104e4906117a0565b6001600160a01b0381163b6110f65760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201526c1bdd08184818dbdb9d1c9858dd609a1b60648201526084016104e4565b60008051602061180883398151915280546001600160a01b0319166001600160a01b0392909216919091179055565b61112e83611180565b60008251118061113b5750805b15610c295761114a83836111c0565b50505050565b600054610100900460ff166111775760405162461bcd60e51b81526004016104e4906117a0565b61079f33610e9f565b61118981611089565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b60606111e58383604051806060016040528060278152602001611828602791396111ec565b9392505050565b6060600080856001600160a01b03168560405161120991906117eb565b600060405180830381855af49150503d8060008114611244576040519150601f19603f3d011682016040523d82523d6000602084013e611249565b606091505b509150915061125a86838387611264565b9695505050505050565b606083156112d05782516112c9576001600160a01b0385163b6112c95760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016104e4565b50816112da565b6112da83836112e2565b949350505050565b8151156112f25781518083602001fd5b8060405162461bcd60e51b81526004016104e491906113d1565b8280546113189061166f565b90600052602060002090601f01602090048101928261133a5760008555611380565b82601f1061135357805160ff1916838001178555611380565b82800160010185558215611380579182015b82811115611380578251825591602001919060010190611365565b5061138c929150611390565b5090565b5b8082111561138c5760008155600101611391565b60005b838110156113c05781810151838201526020016113a8565b8381111561114a5750506000910152565b60208152600082518060208401526113f08160408501602087016113a5565b601f01601f19169190910160400192915050565b80356001600160a01b038116811461141b57600080fd5b919050565b6000806040838503121561143357600080fd5b61143c83611404565b946020939093013593505050565b60008060006060848603121561145f57600080fd5b61146884611404565b925061147660208501611404565b9150604084013590509250925092565b60006020828403121561149857600080fd5b6111e582611404565b634e487b7160e01b600052604160045260246000fd5b600067ffffffffffffffff808411156114d2576114d26114a1565b604051601f8501601f19908116603f011681019082821181831017156114fa576114fa6114a1565b8160405280935085815286868601111561151357600080fd5b858560208301376000602087830101525050509392505050565b6000806040838503121561154057600080fd5b61154983611404565b9150602083013567ffffffffffffffff81111561156557600080fd5b8301601f8101851361157657600080fd5b611585858235602084016114b7565b9150509250929050565b600080604083850312156115a257600080fd5b6115ab83611404565b91506115b960208401611404565b90509250929050565b600082601f8301126115d357600080fd5b6111e5838335602085016114b7565b600080600080608085870312156115f857600080fd5b843567ffffffffffffffff8082111561161057600080fd5b61161c888389016115c2565b9550602087013591508082111561163257600080fd5b5061163f878288016115c2565b935050604085013560ff8116811461165657600080fd5b915061166460608601611404565b905092959194509250565b600181811c9082168061168357607f821691505b602082108114156116a457634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b6000828210156116d2576116d26116aa565b500390565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b19195b1959d85d1958d85b1b60a21b606082015260800190565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b6163746976652070726f787960a01b606082015260800190565b60008219821115611782576117826116aa565b500190565b60006020828403121561179957600080fd5b5051919050565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b600082516117fd8184602087016113a5565b919091019291505056fe360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a2646970667358221220c24d636966a60df9636041ea92ff12a963fc67961fabb8564b1631506b7785e864736f6c634300080a0033"), } wfxInit = Contract{ Address: common.HexToAddress(WFXLogicAddress), ABI: MustABIJson(WFXUpgradableMetaData.ABI), Bin: MustDecodeHex(WFXUpgradableMetaData.Bin), - Code: initWFXCode, + Code: MustDecodeHex("0x6080604052600436106101395760003560e01c8063715018a6116100ab578063b86d52981161006f578063b86d529814610366578063d0e30db014610148578063dd62ed3e14610384578063de7ea79d146103ca578063f2fde38b146103ea578063f3fef3a31461040a57610148565b8063715018a6146102ca5780638da5cb5b146102df57806395d89b41146103115780639dc29fac14610326578063a9059cbb1461034657610148565b8063313ce567116100fd578063313ce5671461020a5780633659cfe61461022c57806340c10f191461024c5780634f1ef2861461026c57806352d1902d1461027f57806370a082311461029457610148565b806306fdde0314610150578063095ea7b31461017b57806318160ddd146101ab57806323b872dd146101ca5780632e1a7d4d146101ea57610148565b366101485761014661042a565b005b61014661042a565b34801561015c57600080fd5b5061016561046b565b60405161017291906115a9565b60405180910390f35b34801561018757600080fd5b5061019b6101963660046115f1565b6104fd565b6040519015158152602001610172565b3480156101b757600080fd5b5060cc545b604051908152602001610172565b3480156101d657600080fd5b5061019b6101e536600461161d565b610553565b3480156101f657600080fd5b5061014661020536600461165e565b610600565b34801561021657600080fd5b5060cb5460405160ff9091168152602001610172565b34801561023857600080fd5b50610146610247366004611677565b610671565b34801561025857600080fd5b506101466102673660046115f1565b610751565b61014661027a366004611720565b610767565b34801561028b57600080fd5b506101bc610834565b3480156102a057600080fd5b506101bc6102af366004611677565b6001600160a01b0316600090815260cd602052604090205490565b3480156102d657600080fd5b506101466108e7565b3480156102eb57600080fd5b506097546001600160a01b03165b6040516001600160a01b039091168152602001610172565b34801561031d57600080fd5b506101656108fb565b34801561033257600080fd5b506101466103413660046115f1565b61090a565b34801561035257600080fd5b5061019b6103613660046115f1565b61091c565b34801561037257600080fd5b5060cf546001600160a01b03166102f9565b34801561039057600080fd5b506101bc61039f366004611784565b6001600160a01b03918216600090815260ce6020908152604080832093909416825291909152205490565b3480156103d657600080fd5b506101466103e53660046117dd565b610932565b3480156103f657600080fd5b50610146610405366004611677565b610aa1565b34801561041657600080fd5b506101466104253660046115f1565b610b17565b6104343334610b9c565b60405134815233907fe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c9060200160405180910390a2565b606060c9805461047a9061186c565b80601f01602080910402602001604051908101604052809291908181526020018280546104a69061186c565b80156104f35780601f106104c8576101008083540402835291602001916104f3565b820191906000526020600020905b8154815290600101906020018083116104d657829003601f168201915b5050505050905090565b600061050a338484610c74565b6040518281526001600160a01b0384169033907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259060200160405180910390a350600192915050565b6001600160a01b038316600090815260ce60209081526040808320338452909152812054828110156105d65760405162461bcd60e51b815260206004820152602160248201527f7472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636044820152606560f81b60648201526084015b60405180910390fd5b6105ea85336105e586856118bd565b610c74565b6105f5858585610cf6565b506001949350505050565b61060b335b82610ea5565b604051339082156108fc029083906000818181858888f19350505050158015610638573d6000803e3d6000fd5b5060405181815233907f884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a94243649060200160405180910390a250565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010021614156106ba5760405162461bcd60e51b81526004016105cd906118d4565b7f00000000000000000000000000000000000000000000000000000000000010026001600160a01b0316610703600080516020611a05833981519152546001600160a01b031690565b6001600160a01b0316146107295760405162461bcd60e51b81526004016105cd90611920565b61073281610fe7565b6040805160008082526020820190925261074e91839190610fef565b50565b61075961115f565b6107638282610b9c565b5050565b306001600160a01b037f00000000000000000000000000000000000000000000000000000000000010021614156107b05760405162461bcd60e51b81526004016105cd906118d4565b7f00000000000000000000000000000000000000000000000000000000000010026001600160a01b03166107f9600080516020611a05833981519152546001600160a01b031690565b6001600160a01b03161461081f5760405162461bcd60e51b81526004016105cd90611920565b61082882610fe7565b61076382826001610fef565b6000306001600160a01b037f000000000000000000000000000000000000000000000000000000000000100216146108d45760405162461bcd60e51b815260206004820152603860248201527f555550535570677261646561626c653a206d757374206e6f742062652063616c60448201527f6c6564207468726f7567682064656c656761746563616c6c000000000000000060648201526084016105cd565b50600080516020611a0583398151915290565b6108ef61115f565b6108f960006111b9565b565b606060ca805461047a9061186c565b61091261115f565b6107638282610ea5565b6000610929338484610cf6565b50600192915050565b600054610100900460ff16158080156109525750600054600160ff909116105b8061096c5750303b15801561096c575060005460ff166001145b6109cf5760405162461bcd60e51b815260206004820152602e60248201527f496e697469616c697a61626c653a20636f6e747261637420697320616c72656160448201526d191e481a5b9a5d1a585b1a5e995960921b60648201526084016105cd565b6000805460ff1916600117905580156109f2576000805461ff0019166101001790555b8451610a059060c99060208801906114e4565b508351610a199060ca9060208701906114e4565b5060cb805460ff191660ff851617905560cf80546001600160a01b0319166001600160a01b038416179055610a4c61120b565b610a5461123a565b8015610a9a576000805461ff0019169055604051600181527f7f26b83ff96e1f2b6a682f133852f6798a09c465da95921460cefb38474024989060200160405180910390a15b5050505050565b610aa961115f565b6001600160a01b038116610b0e5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b60648201526084016105cd565b61074e816111b9565b610b2033610605565b6040516001600160a01b0383169082156108fc029083906000818181858888f19350505050158015610b56573d6000803e3d6000fd5b506040518181526001600160a01b0383169033907f9b1bfa7fa9ee420a16e124f794c35ac9f90472acc99140eb2f6447c714cad8eb906020015b60405180910390a35050565b6001600160a01b038216610bf25760405162461bcd60e51b815260206004820152601860248201527f6d696e7420746f20746865207a65726f2061646472657373000000000000000060448201526064016105cd565b8060cc6000828254610c04919061196c565b90915550506001600160a01b038216600090815260cd602052604081208054839290610c3190849061196c565b90915550506040518181526001600160a01b038316906000907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef90602001610b90565b6001600160a01b038316610cca5760405162461bcd60e51b815260206004820152601d60248201527f617070726f76652066726f6d20746865207a65726f206164647265737300000060448201526064016105cd565b6001600160a01b03928316600090815260ce602090815260408083209490951682529290925291902055565b6001600160a01b038316610d4c5760405162461bcd60e51b815260206004820152601e60248201527f7472616e736665722066726f6d20746865207a65726f2061646472657373000060448201526064016105cd565b6001600160a01b038216610da25760405162461bcd60e51b815260206004820152601c60248201527f7472616e7366657220746f20746865207a65726f20616464726573730000000060448201526064016105cd565b6001600160a01b038316600090815260cd602052604090205481811015610e0b5760405162461bcd60e51b815260206004820152601f60248201527f7472616e7366657220616d6f756e7420657863656564732062616c616e63650060448201526064016105cd565b610e1582826118bd565b6001600160a01b03808616600090815260cd60205260408082209390935590851681529081208054849290610e4b90849061196c565b92505081905550826001600160a01b0316846001600160a01b03167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef84604051610e9791815260200190565b60405180910390a350505050565b6001600160a01b038216610efb5760405162461bcd60e51b815260206004820152601a60248201527f6275726e2066726f6d20746865207a65726f206164647265737300000000000060448201526064016105cd565b6001600160a01b038216600090815260cd602052604090205481811015610f645760405162461bcd60e51b815260206004820152601b60248201527f6275726e20616d6f756e7420657863656564732062616c616e6365000000000060448201526064016105cd565b610f6e82826118bd565b6001600160a01b038416600090815260cd602052604081209190915560cc8054849290610f9c9084906118bd565b90915550506040518281526000906001600160a01b038516907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9060200160405180910390a3505050565b61074e61115f565b7f4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd91435460ff16156110275761102283611261565b505050565b826001600160a01b03166352d1902d6040518163ffffffff1660e01b8152600401602060405180830381865afa925050508015611081575060408051601f3d908101601f1916820190925261107e91810190611984565b60015b6110e45760405162461bcd60e51b815260206004820152602e60248201527f45524331393637557067726164653a206e657720696d706c656d656e7461746960448201526d6f6e206973206e6f74205555505360901b60648201526084016105cd565b600080516020611a0583398151915281146111535760405162461bcd60e51b815260206004820152602960248201527f45524331393637557067726164653a20756e737570706f727465642070726f786044820152681a58589b195555525160ba1b60648201526084016105cd565b506110228383836112fd565b6097546001600160a01b031633146108f95760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657260448201526064016105cd565b609780546001600160a01b038381166001600160a01b0319831681179093556040519116919082907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b600054610100900460ff166112325760405162461bcd60e51b81526004016105cd9061199d565b6108f9611328565b600054610100900460ff166108f95760405162461bcd60e51b81526004016105cd9061199d565b6001600160a01b0381163b6112ce5760405162461bcd60e51b815260206004820152602d60248201527f455243313936373a206e657720696d706c656d656e746174696f6e206973206e60448201526c1bdd08184818dbdb9d1c9858dd609a1b60648201526084016105cd565b600080516020611a0583398151915280546001600160a01b0319166001600160a01b0392909216919091179055565b61130683611358565b6000825111806113135750805b15611022576113228383611398565b50505050565b600054610100900460ff1661134f5760405162461bcd60e51b81526004016105cd9061199d565b6108f9336111b9565b61136181611261565b6040516001600160a01b038216907fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b90600090a250565b60606113bd8383604051806060016040528060278152602001611a25602791396113c4565b9392505050565b6060600080856001600160a01b0316856040516113e191906119e8565b600060405180830381855af49150503d806000811461141c576040519150601f19603f3d011682016040523d82523d6000602084013e611421565b606091505b50915091506114328683838761143c565b9695505050505050565b606083156114a85782516114a1576001600160a01b0385163b6114a15760405162461bcd60e51b815260206004820152601d60248201527f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000060448201526064016105cd565b50816114b2565b6114b283836114ba565b949350505050565b8151156114ca5781518083602001fd5b8060405162461bcd60e51b81526004016105cd91906115a9565b8280546114f09061186c565b90600052602060002090601f0160209004810192826115125760008555611558565b82601f1061152b57805160ff1916838001178555611558565b82800160010185558215611558579182015b8281111561155857825182559160200191906001019061153d565b50611564929150611568565b5090565b5b808211156115645760008155600101611569565b60005b83811015611598578181015183820152602001611580565b838111156113225750506000910152565b60208152600082518060208401526115c881604085016020870161157d565b601f01601f19169190910160400192915050565b6001600160a01b038116811461074e57600080fd5b6000806040838503121561160457600080fd5b823561160f816115dc565b946020939093013593505050565b60008060006060848603121561163257600080fd5b833561163d816115dc565b9250602084013561164d816115dc565b929592945050506040919091013590565b60006020828403121561167057600080fd5b5035919050565b60006020828403121561168957600080fd5b81356113bd816115dc565b634e487b7160e01b600052604160045260246000fd5b600067ffffffffffffffff808411156116c5576116c5611694565b604051601f8501601f19908116603f011681019082821181831017156116ed576116ed611694565b8160405280935085815286868601111561170657600080fd5b858560208301376000602087830101525050509392505050565b6000806040838503121561173357600080fd5b823561173e816115dc565b9150602083013567ffffffffffffffff81111561175a57600080fd5b8301601f8101851361176b57600080fd5b61177a858235602084016116aa565b9150509250929050565b6000806040838503121561179757600080fd5b82356117a2816115dc565b915060208301356117b2816115dc565b809150509250929050565b600082601f8301126117ce57600080fd5b6113bd838335602085016116aa565b600080600080608085870312156117f357600080fd5b843567ffffffffffffffff8082111561180b57600080fd5b611817888389016117bd565b9550602087013591508082111561182d57600080fd5b5061183a878288016117bd565b935050604085013560ff8116811461185157600080fd5b91506060850135611861816115dc565b939692955090935050565b600181811c9082168061188057607f821691505b602082108114156118a157634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052601160045260246000fd5b6000828210156118cf576118cf6118a7565b500390565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b19195b1959d85d1958d85b1b60a21b606082015260800190565b6020808252602c908201527f46756e6374696f6e206d7573742062652063616c6c6564207468726f7567682060408201526b6163746976652070726f787960a01b606082015260800190565b6000821982111561197f5761197f6118a7565b500190565b60006020828403121561199657600080fd5b5051919050565b6020808252602b908201527f496e697469616c697a61626c653a20636f6e7472616374206973206e6f74206960408201526a6e697469616c697a696e6760a81b606082015260800190565b600082516119fa81846020870161157d565b919091019291505056fe360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc416464726573733a206c6f772d6c6576656c2064656c65676174652063616c6c206661696c6564a26469706673582212203491f4b9433597e502b7d2b2ea2025687fa17dcc511ea87b77ea73cb9c0109d464736f6c634300080a0033"), } erc1967Proxy = Contract{ Address: common.Address{}, @@ -42,12 +46,16 @@ var ( Code: []byte{}, } - fxBridgeABI = MustABIJson(IFxBridgeLogicMetaData.ABI) - - bridgeCallBridgeCallback = MustABIJson(IBridgeCallbackMetaData.ABI) - errorABI = MustABIJson(IErrorMetaData.ABI) + fxBridgeABI = MustABIJson(IFxBridgeLogicMetaData.ABI) + bridgeCallbackABI = MustABIJson(IBridgeCallbackMetaData.ABI) + errorABI = MustABIJson(IErrorMetaData.ABI) ) +type Caller interface { + QueryContract(ctx sdk.Context, from, contract common.Address, abi abi.ABI, method string, res interface{}, args ...interface{}) error + ApplyContract(ctx sdk.Context, from, contract common.Address, value *big.Int, abi abi.ABI, method string, constructorData ...interface{}) (*evmtypes.MsgEthereumTxResponse, error) +} + type Contract struct { Address common.Address ABI abi.ABI @@ -71,18 +79,6 @@ func GetERC1967Proxy() Contract { return erc1967Proxy } -func GetFxBridgeABI() abi.ABI { - return fxBridgeABI -} - -func GetBridgeCallBridgeCallback() abi.ABI { - return bridgeCallBridgeCallback -} - -func GetErrorABI() abi.ABI { - return errorABI -} - func MustDecodeHex(str string) []byte { bz, err := hexutil.Decode(str) if err != nil { @@ -98,3 +94,65 @@ func MustABIJson(str string) abi.ABI { } return j } + +func PackRetError(err error) ([]byte, error) { + pack, _ := abi.Arguments{{Type: typeString}}.Pack(err.Error()) + return pack, err +} + +func PackRetErrV2(err error) ([]byte, error) { + pack, _ := errorABI.Pack("Error", err.Error()) + return pack, err +} + +func PackBridgeCallback(sender, receiver common.Address, tokens []common.Address, amounts []*big.Int, data, memo []byte) ([]byte, error) { + return bridgeCallbackABI.Pack("bridgeCallback", + sender, + receiver, + tokens, + amounts, + data, + memo, + ) +} + +func PackOracleSetCheckpoint(gravityID, methodName [32]byte, nonce *big.Int, memberAddresses []common.Address, convertedPowers []*big.Int) ([]byte, error) { + return fxBridgeABI.Pack("oracleSetCheckpoint", + gravityID, + methodName, + nonce, + memberAddresses, + convertedPowers, + ) +} + +func PackSubmitBatchCheckpoint(gravityID, methodName [32]byte, amounts []*big.Int, destinations []common.Address, fees []*big.Int, batchNonce *big.Int, tokenContract common.Address, batchTimeout *big.Int, feeReceive common.Address) ([]byte, error) { + return fxBridgeABI.Pack("submitBatchCheckpoint", + gravityID, + methodName, + amounts, + destinations, + fees, + batchNonce, + tokenContract, + batchTimeout, + feeReceive, + ) +} + +func PackBridgeCallCheckpoint(gravityID, methodName [32]byte, sender, refund common.Address, tokens []common.Address, amounts []*big.Int, to common.Address, data, memo []byte, nonce, timeout, eventNonce *big.Int) ([]byte, error) { + return fxBridgeABI.Pack("bridgeCallCheckpoint", + gravityID, + methodName, + sender, + refund, + tokens, + amounts, + to, + data, + memo, + nonce, + timeout, + eventNonce, + ) +} diff --git a/x/crosschain/types/abi_test.go b/contract/contract_test.go similarity index 93% rename from x/crosschain/types/abi_test.go rename to contract/contract_test.go index 20df3bcf..b7beb824 100644 --- a/x/crosschain/types/abi_test.go +++ b/contract/contract_test.go @@ -1,4 +1,4 @@ -package types_test +package contract_test import ( "encoding/hex" @@ -8,7 +8,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/stretchr/testify/require" - "github.com/functionx/fx-core/v8/x/crosschain/types" + "github.com/functionx/fx-core/v8/contract" ) func TestPackBridgeCallback(t *testing.T) { @@ -52,7 +52,7 @@ func TestPackBridgeCallback(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result, err := types.PackBridgeCallback(tt.args.sender, tt.args.receiver, tt.args._tokens, tt.args._amounts, tt.args.data, tt.args.memo) + result, err := contract.PackBridgeCallback(tt.args.sender, tt.args.receiver, tt.args._tokens, tt.args._amounts, tt.args.data, tt.args.memo) if tt.err != nil { require.Error(t, err) require.EqualValues(t, tt.err.Error(), err.Error()) diff --git a/contract/erc20.go b/contract/erc20_abi.go similarity index 56% rename from contract/erc20.go rename to contract/erc20_abi.go index 1b0c4b23..1eed0e48 100644 --- a/contract/erc20.go +++ b/contract/erc20_abi.go @@ -1,27 +1,25 @@ package contract import ( - "errors" "fmt" "math/big" "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/vm" ) type ERC20ABI struct { - ABI abi.ABI + abi abi.ABI } func NewERC20ABI() ERC20ABI { return ERC20ABI{ - ABI: GetWFX().ABI, + abi: GetWFX().ABI, } } func (e ERC20ABI) PackName() (data []byte, err error) { - data, err = e.ABI.Pack("name") + data, err = e.abi.Pack("name") if err != nil { return nil, fmt.Errorf("pack name: %s", err.Error()) } @@ -30,14 +28,14 @@ func (e ERC20ABI) PackName() (data []byte, err error) { func (e ERC20ABI) UnpackName(ret []byte) (string, error) { var unpackedRet struct{ Value string } - if err := e.ABI.UnpackIntoInterface(&unpackedRet, "name", ret); err != nil { + if err := e.abi.UnpackIntoInterface(&unpackedRet, "name", ret); err != nil { return "", fmt.Errorf("unpack name: %s", err.Error()) } return unpackedRet.Value, nil } func (e ERC20ABI) PackSymbol() (data []byte, err error) { - data, err = e.ABI.Pack("symbol") + data, err = e.abi.Pack("symbol") if err != nil { return nil, fmt.Errorf("pack symbol: %s", err.Error()) } @@ -46,14 +44,14 @@ func (e ERC20ABI) PackSymbol() (data []byte, err error) { func (e ERC20ABI) UnpackSymbol(ret []byte) (string, error) { var unpackedRet struct{ Value string } - if err := e.ABI.UnpackIntoInterface(&unpackedRet, "symbol", ret); err != nil { + if err := e.abi.UnpackIntoInterface(&unpackedRet, "symbol", ret); err != nil { return "", fmt.Errorf("unpack symbol: %s", err.Error()) } return unpackedRet.Value, nil } func (e ERC20ABI) PackDecimals() (data []byte, err error) { - data, err = e.ABI.Pack("decimals") + data, err = e.abi.Pack("decimals") if err != nil { return nil, fmt.Errorf("pack decimals: %s", err.Error()) } @@ -62,14 +60,14 @@ func (e ERC20ABI) PackDecimals() (data []byte, err error) { func (e ERC20ABI) UnpackDecimals(ret []byte) (uint8, error) { var unpackedRet struct{ Value uint8 } - if err := e.ABI.UnpackIntoInterface(&unpackedRet, "decimals", ret); err != nil { + if err := e.abi.UnpackIntoInterface(&unpackedRet, "decimals", ret); err != nil { return 0, fmt.Errorf("unpack decimals: %s", err.Error()) } return unpackedRet.Value, nil } func (e ERC20ABI) PackBalanceOf(account common.Address) (data []byte, err error) { - data, err = e.ABI.Pack("balanceOf", account) + data, err = e.abi.Pack("balanceOf", account) if err != nil { return nil, fmt.Errorf("pack balanceOf: %s", err.Error()) } @@ -78,14 +76,14 @@ func (e ERC20ABI) PackBalanceOf(account common.Address) (data []byte, err error) func (e ERC20ABI) UnpackBalanceOf(ret []byte) (*big.Int, error) { var unpackedRet struct{ Value *big.Int } - if err := e.ABI.UnpackIntoInterface(&unpackedRet, "balanceOf", ret); err != nil { + if err := e.abi.UnpackIntoInterface(&unpackedRet, "balanceOf", ret); err != nil { return nil, fmt.Errorf("unpack balanceOf: %s", err.Error()) } return unpackedRet.Value, nil } func (e ERC20ABI) PackTotalSupply() (data []byte, err error) { - data, err = e.ABI.Pack("totalSupply") + data, err = e.abi.Pack("totalSupply") if err != nil { return nil, fmt.Errorf("pack totalSupply: %s", err.Error()) } @@ -94,14 +92,14 @@ func (e ERC20ABI) PackTotalSupply() (data []byte, err error) { func (e ERC20ABI) UnpackTotalSupply(ret []byte) (*big.Int, error) { var unpackedRet struct{ Value *big.Int } - if err := e.ABI.UnpackIntoInterface(&unpackedRet, "totalSupply", ret); err != nil { + if err := e.abi.UnpackIntoInterface(&unpackedRet, "totalSupply", ret); err != nil { return nil, fmt.Errorf("unpack totalSupply: %s", err.Error()) } return unpackedRet.Value, nil } func (e ERC20ABI) PackApprove(spender common.Address, amount *big.Int) (data []byte, err error) { - data, err = e.ABI.Pack("approve", spender, amount) + data, err = e.abi.Pack("approve", spender, amount) if err != nil { return nil, fmt.Errorf("pack approve: %s", err.Error()) } @@ -109,7 +107,7 @@ func (e ERC20ABI) PackApprove(spender common.Address, amount *big.Int) (data []b } func (e ERC20ABI) PackAllowance(owner, spender common.Address) (data []byte, err error) { - data, err = e.ABI.Pack("allowance", owner, spender) + data, err = e.abi.Pack("allowance", owner, spender) if err != nil { return nil, fmt.Errorf("pack allowance: %s", err.Error()) } @@ -117,7 +115,7 @@ func (e ERC20ABI) PackAllowance(owner, spender common.Address) (data []byte, err } func (e ERC20ABI) PackTransferFrom(sender, to common.Address, amount *big.Int) (data []byte, err error) { - data, err = e.ABI.Pack("transferFrom", sender, to, amount) + data, err = e.abi.Pack("transferFrom", sender, to, amount) if err != nil { return nil, fmt.Errorf("pack transferFrom: %s", err.Error()) } @@ -126,14 +124,14 @@ func (e ERC20ABI) PackTransferFrom(sender, to common.Address, amount *big.Int) ( func (e ERC20ABI) UnpackTransferFrom(ret []byte) (bool, error) { var unpackedRet struct{ Value bool } - if err := e.ABI.UnpackIntoInterface(&unpackedRet, "transferFrom", ret); err != nil { + if err := e.abi.UnpackIntoInterface(&unpackedRet, "transferFrom", ret); err != nil { return false, fmt.Errorf("unpack transferFrom: %s", err.Error()) } return unpackedRet.Value, nil } func (e ERC20ABI) PackTransfer(to common.Address, amount *big.Int) (data []byte, err error) { - data, err = e.ABI.Pack("transfer", to, amount) + data, err = e.abi.Pack("transfer", to, amount) if err != nil { return nil, fmt.Errorf("pack transfer: %s", err.Error()) } @@ -141,7 +139,7 @@ func (e ERC20ABI) PackTransfer(to common.Address, amount *big.Int) (data []byte, } func (e ERC20ABI) PackBurn(account common.Address, amount *big.Int) (data []byte, err error) { - data, err = e.ABI.Pack("burn", account, amount) + data, err = e.abi.Pack("burn", account, amount) if err != nil { return nil, fmt.Errorf("pack burn: %s", err.Error()) } @@ -149,90 +147,33 @@ func (e ERC20ABI) PackBurn(account common.Address, amount *big.Int) (data []byte } func (e ERC20ABI) PackMint(account common.Address, amount *big.Int) (data []byte, err error) { - data, err = e.ABI.Pack("mint", account, amount) + data, err = e.abi.Pack("mint", account, amount) if err != nil { return nil, fmt.Errorf("pack mint: %s", err.Error()) } return data, err } -type ERC20Call struct { - ERC20ABI - evm *vm.EVM - caller vm.AccountRef - contract common.Address - maxGas uint64 -} - -func NewERC20Call(evm *vm.EVM, caller, contract common.Address, maxGas uint64) *ERC20Call { - defMaxGas := DefaultGasCap - if maxGas > 0 { - defMaxGas = maxGas - } - return &ERC20Call{ - ERC20ABI: NewERC20ABI(), - evm: evm, - caller: vm.AccountRef(caller), - contract: contract, - maxGas: defMaxGas, - } -} - -func (e *ERC20Call) call(data []byte) (ret []byte, err error) { - ret, _, err = e.evm.Call(e.caller, e.contract, data, e.maxGas, big.NewInt(0)) - if err != nil { - return nil, err - } - return ret, err -} - -func (e *ERC20Call) staticCall(data []byte) (ret []byte, err error) { - ret, _, err = e.evm.StaticCall(e.caller, e.contract, data, e.maxGas) - if err != nil { - return nil, err - } - return ret, err -} - -func (e *ERC20Call) Burn(account common.Address, amount *big.Int) error { - data, err := e.ERC20ABI.PackBurn(account, amount) - if err != nil { - return err - } - _, err = e.call(data) +func (e ERC20ABI) PackDeposit() (data []byte, err error) { + data, err = e.abi.Pack("deposit") if err != nil { - return fmt.Errorf("call burn: %s", err.Error()) + return nil, fmt.Errorf("pack deposit: %s", err.Error()) } - return nil + return data, err } -func (e *ERC20Call) TransferFrom(from, to common.Address, amount *big.Int) error { - data, err := e.ERC20ABI.PackTransferFrom(from, to, amount) +func (e ERC20ABI) PackWithdraw(recipient common.Address, amount *big.Int) (data []byte, err error) { + data, err = e.abi.Pack("withdraw0", recipient, amount) if err != nil { - return err + return nil, fmt.Errorf("pack withdraw: %s", err.Error()) } - ret, err := e.call(data) - if err != nil { - return fmt.Errorf("call transferFrom: %s", err.Error()) - } - isSuccess, err := e.UnpackTransferFrom(ret) - if err != nil { - return err - } - if !isSuccess { - return errors.New("transferFrom failed") - } - return nil + return data, err } -func (e *ERC20Call) TotalSupply() (*big.Int, error) { - data, err := e.ERC20ABI.PackTotalSupply() +func (e ERC20ABI) PackTransferOwnership(newOwner common.Address) (data []byte, err error) { + data, err = e.abi.Pack("transferOwnership", newOwner) if err != nil { - return nil, err + return nil, fmt.Errorf("pack transferOwnership: %s", err.Error()) } - ret, err := e.staticCall(data) - if err != nil { - return nil, fmt.Errorf("StaticCall totalSupply: %s", err.Error()) - } - return e.ERC20ABI.UnpackTotalSupply(ret) + return data, err } diff --git a/contract/erc20_token.go b/contract/erc20_token.go new file mode 100644 index 00000000..89c227ee --- /dev/null +++ b/contract/erc20_token.go @@ -0,0 +1,97 @@ +package contract + +import ( + "context" + "math/big" + + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/common" + "github.com/evmos/ethermint/x/evm/types" +) + +type ERC20TokenKeeper struct { + Caller + abi abi.ABI + from common.Address +} + +func NewERC20TokenKeeper(caller Caller) ERC20TokenKeeper { + return ERC20TokenKeeper{ + Caller: caller, + abi: GetFIP20().ABI, + // evm module address + from: common.BytesToAddress(authtypes.NewModuleAddress(types.ModuleName).Bytes()), + } +} + +func (k ERC20TokenKeeper) Name(ctx context.Context, contractAddr common.Address) (string, error) { + var nameRes struct{ Value string } + if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, contractAddr, k.abi, "name", &nameRes); err != nil { + return "", err + } + return nameRes.Value, nil +} + +func (k ERC20TokenKeeper) Symbol(ctx context.Context, contractAddr common.Address) (string, error) { + var symbolRes struct{ Value string } + if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, contractAddr, k.abi, "symbol", &symbolRes); err != nil { + return "", err + } + return symbolRes.Value, nil +} + +func (k ERC20TokenKeeper) Decimals(ctx context.Context, contractAddr common.Address) (uint8, error) { + var decimalRes struct{ Value uint8 } + if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, contractAddr, k.abi, "decimals", &decimalRes); err != nil { + return 0, nil + } + return decimalRes.Value, nil +} + +func (k ERC20TokenKeeper) BalanceOf(ctx context.Context, contractAddr, addr common.Address) (*big.Int, error) { + var balanceRes struct { + Value *big.Int + } + if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, contractAddr, k.abi, "balanceOf", &balanceRes, addr); err != nil { + return big.NewInt(0), err + } + return balanceRes.Value, nil +} + +func (k ERC20TokenKeeper) TotalSupply(ctx context.Context, contractAddr common.Address) (*big.Int, error) { + var totalSupplyRes struct{ Value *big.Int } + if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.from, contractAddr, k.abi, "totalSupply", &totalSupplyRes); err != nil { + return nil, err + } + return totalSupplyRes.Value, nil +} + +func (k ERC20TokenKeeper) Mint(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error { + _, err := k.ApplyContract(sdk.UnwrapSDKContext(ctx), from, contractAddr, nil, k.abi, "mint", receiver, amount) + return err +} + +func (k ERC20TokenKeeper) Burn(ctx context.Context, contractAddr, from, account common.Address, amount *big.Int) error { + _, err := k.ApplyContract(sdk.UnwrapSDKContext(ctx), from, contractAddr, nil, k.abi, "burn", account, amount) + return err +} + +func (k ERC20TokenKeeper) Transfer(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error { + res, err := k.ApplyContract(sdk.UnwrapSDKContext(ctx), from, contractAddr, nil, k.abi, "transfer", receiver, amount) + if err != nil { + return err + } + + // Check unpackedRet execution + var unpackedRet struct{ Value bool } + if err = k.abi.UnpackIntoInterface(&unpackedRet, "transfer", res.Ret); err != nil { + return sdkerrors.ErrInvalidType.Wrapf("failed to unpack transfer: %s", err.Error()) + } + if !unpackedRet.Value { + return sdkerrors.ErrLogic.Wrap("failed to execute transfer") + } + return nil +} diff --git a/contract/precompile.go b/contract/precompile.go index 5b5f1434..688bf65d 100644 --- a/contract/precompile.go +++ b/contract/precompile.go @@ -1,6 +1,11 @@ package contract import ( + "errors" + "fmt" + "math/big" + + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/vm" ) @@ -10,3 +15,84 @@ type PrecompileMethod interface { IsReadonly() bool Run(evm *vm.EVM, contract *vm.Contract) ([]byte, error) } + +type ERC20Call struct { + ERC20ABI + evm *vm.EVM + caller vm.AccountRef + contract common.Address + maxGas uint64 +} + +func NewERC20Call(evm *vm.EVM, caller, contract common.Address, maxGas uint64) *ERC20Call { + defMaxGas := DefaultGasCap + if maxGas > 0 { + defMaxGas = maxGas + } + return &ERC20Call{ + ERC20ABI: NewERC20ABI(), + evm: evm, + caller: vm.AccountRef(caller), + contract: contract, + maxGas: defMaxGas, + } +} + +func (e *ERC20Call) call(data []byte) (ret []byte, err error) { + ret, _, err = e.evm.Call(e.caller, e.contract, data, e.maxGas, big.NewInt(0)) + if err != nil { + return nil, err + } + return ret, err +} + +func (e *ERC20Call) staticCall(data []byte) (ret []byte, err error) { + ret, _, err = e.evm.StaticCall(e.caller, e.contract, data, e.maxGas) + if err != nil { + return nil, err + } + return ret, err +} + +func (e *ERC20Call) Burn(account common.Address, amount *big.Int) error { + data, err := e.ERC20ABI.PackBurn(account, amount) + if err != nil { + return err + } + _, err = e.call(data) + if err != nil { + return fmt.Errorf("call burn: %s", err.Error()) + } + return nil +} + +func (e *ERC20Call) TransferFrom(from, to common.Address, amount *big.Int) error { + data, err := e.ERC20ABI.PackTransferFrom(from, to, amount) + if err != nil { + return err + } + ret, err := e.call(data) + if err != nil { + return fmt.Errorf("call transferFrom: %s", err.Error()) + } + isSuccess, err := e.UnpackTransferFrom(ret) + if err != nil { + return err + } + if !isSuccess { + return errors.New("transferFrom failed") + } + return nil +} + +func (e *ERC20Call) TotalSupply() (*big.Int, error) { + data, err := e.ERC20ABI.PackTotalSupply() + if err != nil { + return nil, err + } + ret, err := e.staticCall(data) + if err != nil { + return nil, fmt.Errorf("StaticCall totalSupply: %s", err.Error()) + } + return e.ERC20ABI.UnpackTotalSupply(ret) +} diff --git a/contract/types.go b/contract/types.go deleted file mode 100644 index 6d8cbc89..00000000 --- a/contract/types.go +++ /dev/null @@ -1,9 +0,0 @@ -package contract - -import "github.com/ethereum/go-ethereum/accounts/abi" - -const ( - DefaultGasCap uint64 = 30000000 -) - -var TypeString, _ = abi.NewType("string", "", nil) diff --git a/scripts/linter.sh b/scripts/linter.sh index de52b3eb..a294f7f6 100755 --- a/scripts/linter.sh +++ b/scripts/linter.sh @@ -5,8 +5,11 @@ set -eo pipefail patternLimits=( "nolint:20" "#nosec:5" - "CrossChain:4" + "CrossChain:3" "cross chain:0" + "GetERC1967Proxy:4" + "GetWFX:9" + "GetFIP20:12" ) if ! command -v rg &>/dev/null; then diff --git a/tests/evm_suite.go b/tests/evm_suite.go index 2b65628f..ec963aff 100644 --- a/tests/evm_suite.go +++ b/tests/evm_suite.go @@ -26,12 +26,14 @@ import ( type EvmTestSuite struct { *TestSuite + contract.ERC20ABI privKey cryptotypes.PrivKey } func NewEvmTestSuite(ts *TestSuite) EvmTestSuite { return EvmTestSuite{ TestSuite: ts, + ERC20ABI: contract.NewERC20ABI(), privKey: helpers.NewEthPrivKey(), } } @@ -143,7 +145,7 @@ func (suite *EvmTestSuite) Transfer(privateKey cryptotypes.PrivKey, recipient co func (suite *EvmTestSuite) WFXDeposit(privateKey cryptotypes.PrivKey, address common.Address, value *big.Int) *ethtypes.Transaction { suite.True(suite.Balance(common.BytesToAddress(privateKey.PubKey().Address().Bytes())).Cmp(value) >= 0) - pack, err := contract.GetWFX().ABI.Pack("deposit") + pack, err := suite.ERC20ABI.PackDeposit() suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &address, value, pack) @@ -158,7 +160,7 @@ func (suite *EvmTestSuite) WFXDeposit(privateKey cryptotypes.PrivKey, address co func (suite *EvmTestSuite) WFXWithdraw(privateKey cryptotypes.PrivKey, address, recipient common.Address, value *big.Int) *ethtypes.Transaction { suite.True(suite.TotalSupply(address).Cmp(value) >= 0) suite.True(suite.BalanceOf(address, common.BytesToAddress(privateKey.PubKey().Address().Bytes())).Cmp(value) >= 0) - pack, err := contract.GetWFX().ABI.Pack("withdraw0", recipient, value) + pack, err := suite.ERC20ABI.PackWithdraw(recipient, value) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &address, nil, pack) @@ -173,7 +175,7 @@ func (suite *EvmTestSuite) WFXWithdraw(privateKey cryptotypes.PrivKey, address, func (suite *EvmTestSuite) TransferERC20(privateKey cryptotypes.PrivKey, token, recipient common.Address, value *big.Int) *ethtypes.Transaction { suite.True(suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())).Cmp(value) >= 0) - pack, err := contract.GetFIP20().ABI.Pack("transfer", recipient, value) + pack, err := suite.ERC20ABI.PackTransfer(recipient, value) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) @@ -185,7 +187,7 @@ func (suite *EvmTestSuite) TransferERC20(privateKey cryptotypes.PrivKey, token, } func (suite *EvmTestSuite) ApproveERC20(privateKey cryptotypes.PrivKey, token, spender common.Address, value *big.Int) *ethtypes.Transaction { - pack, err := contract.GetFIP20().ABI.Pack("approve", spender, value) + pack, err := suite.ERC20ABI.PackApprove(spender, value) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) @@ -197,7 +199,7 @@ func (suite *EvmTestSuite) ApproveERC20(privateKey cryptotypes.PrivKey, token, s } func (suite *EvmTestSuite) TransferOwnership(privateKey cryptotypes.PrivKey, token, newOwner common.Address) *ethtypes.Transaction { - pack, err := contract.GetFIP20().ABI.Pack("transferOwnership", newOwner) + pack, err := suite.ERC20ABI.PackTransferOwnership(newOwner) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) @@ -209,7 +211,7 @@ func (suite *EvmTestSuite) TransferOwnership(privateKey cryptotypes.PrivKey, tok } func (suite *EvmTestSuite) TransferFromERC20(privateKey cryptotypes.PrivKey, token, sender, recipient common.Address, value *big.Int) *ethtypes.Transaction { - pack, err := contract.GetFIP20().ABI.Pack("transferFrom", sender, recipient, value) + pack, err := suite.ERC20ABI.PackTransferFrom(sender, recipient, value) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) @@ -221,7 +223,7 @@ func (suite *EvmTestSuite) TransferFromERC20(privateKey cryptotypes.PrivKey, tok } func (suite *EvmTestSuite) MintERC20(privateKey cryptotypes.PrivKey, token, account common.Address, value *big.Int) *ethtypes.Transaction { - pack, err := contract.GetFIP20().ABI.Pack("mint", account, value) + pack, err := suite.ERC20ABI.PackMint(account, value) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) suite.Require().NoError(err) @@ -236,7 +238,7 @@ func (suite *EvmTestSuite) BurnERC20(privateKey cryptotypes.PrivKey, token, acco suite.True(beforeBalance.Cmp(value) >= 0) beforeTotalSupply := suite.TotalSupply(token) suite.True(beforeTotalSupply.Cmp(value) >= 0) - pack, err := contract.GetFIP20().ABI.Pack("burn", account, value) + pack, err := suite.ERC20ABI.PackBurn(account, value) suite.Require().NoError(err) ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) suite.Require().NoError(err) @@ -332,9 +334,10 @@ func (suite *EvmTestSuite) DeployContract(privKey cryptotypes.PrivKey, contractB } func (suite *EvmTestSuite) DeployProxy(privateKey cryptotypes.PrivKey, logic common.Address, initData []byte) common.Address { - input, err := contract.GetERC1967Proxy().ABI.Pack("", logic, initData) + erc1967Proxy := contract.GetERC1967Proxy() + input, err := erc1967Proxy.ABI.Pack("", logic, initData) suite.Require().NoError(err) - tx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, nil, nil, append(contract.GetERC1967Proxy().Bin, input...)) + tx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, nil, nil, append(erc1967Proxy.Bin, input...)) suite.Require().NoError(err) suite.SendTransaction(tx) return crypto.CreateAddress(common.BytesToAddress(privateKey.PubKey().Address().Bytes()), tx.Nonce()) @@ -359,12 +362,13 @@ func (suite *EvmTestSuite) TxFee(hash common.Hash) *big.Int { } func (suite *EvmTestSuite) DeployERC20Contract(privKey cryptotypes.PrivKey) common.Address { - tx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privKey, nil, nil, contract.GetFIP20().Bin) + fip20 := contract.GetFIP20() + tx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privKey, nil, nil, fip20.Bin) suite.Require().NoError(err) suite.SendTransaction(tx) logic := crypto.CreateAddress(common.BytesToAddress(privKey.PubKey().Address().Bytes()), tx.Nonce()) proxy := suite.DeployProxy(privKey, logic, []byte{}) - pack, err := contract.GetFIP20().ABI.Pack("initialize", "Test ERC20", helpers.NewRandSymbol(), uint8(18), common.BytesToAddress(authtypes.NewModuleAddress(erc20types.ModuleName).Bytes())) + pack, err := fip20.ABI.Pack("initialize", "Test ERC20", helpers.NewRandSymbol(), uint8(18), common.BytesToAddress(authtypes.NewModuleAddress(erc20types.ModuleName).Bytes())) suite.Require().NoError(err) tx, err = client.BuildEthTransaction(suite.ctx, suite.EthClient(), privKey, &proxy, nil, pack) suite.Require().NoError(err) diff --git a/tests/evm_test.go b/tests/evm_test.go index 35f4a9b4..2a8d6467 100644 --- a/tests/evm_test.go +++ b/tests/evm_test.go @@ -9,7 +9,6 @@ import ( sdkmath "cosmossdk.io/math" tmrand "github.com/cometbft/cometbft/libs/rand" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" @@ -25,12 +24,13 @@ import ( func (suite *IntegrationTest) WFXTest() { suite.Send(suite.evm.AccAddress(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) - tx, err := client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, nil, nil, contract.GetWFX().Bin) + wfx := contract.GetWFX() + tx, err := client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, nil, nil, wfx.Bin) suite.Require().NoError(err) suite.evm.SendTransaction(tx) logic := crypto.CreateAddress(common.BytesToAddress(suite.evm.privKey.PubKey().Address().Bytes()), tx.Nonce()) - proxy := suite.deployProxy(suite.evm.privKey, logic, []byte{}) - pack, err := contract.GetWFX().ABI.Pack("initialize", "Wrapped Function X", "WFX", uint8(18), common.BytesToAddress([]byte(evmtypes.ModuleName))) + proxy := suite.evm.DeployProxy(suite.evm.privKey, logic, []byte{}) + pack, err := wfx.ABI.Pack("initialize", "Wrapped Function X", "WFX", uint8(18), common.BytesToAddress([]byte(evmtypes.ModuleName))) suite.Require().NoError(err) tx, err = client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, &proxy, nil, pack) suite.Require().NoError(err) @@ -48,16 +48,7 @@ func (suite *IntegrationTest) WFXTest() { func (suite *IntegrationTest) ERC20TokenTest() { suite.Send(suite.evm.AccAddress(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) - tx, err := client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, nil, nil, contract.GetFIP20().Bin) - suite.Require().NoError(err) - suite.evm.SendTransaction(tx) - logic := crypto.CreateAddress(common.BytesToAddress(suite.evm.privKey.PubKey().Address().Bytes()), tx.Nonce()) - proxy := suite.deployProxy(suite.evm.privKey, logic, []byte{}) - pack, err := contract.GetFIP20().ABI.Pack("initialize", "Test ERC20", "ERC20", uint8(18), common.BytesToAddress([]byte(evmtypes.ModuleName))) - suite.Require().NoError(err) - tx, err = client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, &proxy, nil, pack) - suite.Require().NoError(err) - suite.evm.SendTransaction(tx) + proxy := suite.evm.DeployERC20Contract(suite.evm.privKey) key := helpers.NewEthPrivKey() suite.Send(key.PubKey().Address().Bytes(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) @@ -87,7 +78,7 @@ func (suite *IntegrationTest) ERC721Test() { suite.Require().NoError(err) suite.evm.SendTransaction(tx) logic := crypto.CreateAddress(common.BytesToAddress(suite.evm.privKey.PubKey().Address().Bytes()), tx.Nonce()) - proxy := suite.deployProxy(suite.evm.privKey, logic, []byte{}) + proxy := suite.evm.DeployProxy(suite.evm.privKey, logic, []byte{}) pack, err := GetERC721().ABI.Pack("initialize") suite.Require().NoError(err) tx, err = client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, &proxy, nil, pack) @@ -119,15 +110,6 @@ func (suite *IntegrationTest) ERC721Test() { suite.evm.CheckBalanceOfERC721(proxy, common.BytesToAddress(key.PubKey().Address().Bytes()), big.NewInt(0)) } -func (suite *IntegrationTest) deployProxy(privateKey cryptotypes.PrivKey, logic common.Address, initData []byte) common.Address { - input, err := contract.GetERC1967Proxy().ABI.Pack("", logic, initData) - suite.Require().NoError(err) - tx, err := client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), privateKey, nil, nil, append(contract.GetERC1967Proxy().Bin, input...)) - suite.Require().NoError(err) - suite.evm.SendTransaction(tx) - return crypto.CreateAddress(common.BytesToAddress(privateKey.PubKey().Address().Bytes()), tx.Nonce()) -} - func (suite *IntegrationTest) EVMWeb3Test() { suite.Send(suite.evm.AccAddress(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) @@ -291,19 +273,11 @@ func (suite *IntegrationTest) EVMWeb3Test() { func (suite *IntegrationTest) CallContractTest() { suite.Send(suite.evm.AccAddress(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) - tx, err := client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, nil, nil, contract.GetFIP20().Bin) - suite.Require().NoError(err) - suite.evm.SendTransaction(tx) - logic := crypto.CreateAddress(common.BytesToAddress(suite.evm.privKey.PubKey().Address().Bytes()), tx.Nonce()) - proxy := suite.deployProxy(suite.evm.privKey, logic, []byte{}) - pack, err := contract.GetFIP20().ABI.Pack("initialize", "Test ERC20", "ERC20", uint8(18), common.BytesToAddress(suite.evm.privKey.PubKey().Address().Bytes())) - suite.Require().NoError(err) - tx, err = client.BuildEthTransaction(suite.ctx, suite.evm.EthClient(), suite.evm.privKey, &proxy, nil, pack) - suite.Require().NoError(err) - suite.evm.SendTransaction(tx) + proxy := suite.evm.DeployERC20Contract(suite.evm.privKey) + suite.evm.TransferOwnership(suite.evm.privKey, proxy, common.BytesToAddress(authtypes.NewModuleAddress(evmtypes.ModuleName))) amount := new(big.Int).Exp(big.NewInt(10), big.NewInt(20), nil) - args, err := contract.GetFIP20().ABI.Pack("mint", suite.evm.HexAddress(), amount) + args, err := suite.evm.ERC20ABI.PackMint(suite.evm.HexAddress(), amount) suite.Require().NoError(err) response, proposalId := suite.BroadcastProposalTx2([]sdk.Msg{&fxevmtypes.MsgCallContract{ Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), @@ -317,22 +291,24 @@ func (suite *IntegrationTest) CallContractTest() { func (suite *IntegrationTest) FIP20CodeCheckTest() { suite.Send(suite.evm.AccAddress(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) - fip20Addr, _ := suite.evm.DeployContract(suite.evm.privKey, contract.GetFIP20().Bin) + fip20 := contract.GetFIP20() + fip20Addr, _ := suite.evm.DeployContract(suite.evm.privKey, fip20.Bin) code, err := suite.evm.EthClient().CodeAt(suite.ctx, fip20Addr, nil) suite.Require().NoError(err) - suite.Equal(contract.GetFIP20().Code, code, fmt.Sprintf("fip20 deployed code: %s", common.Bytes2Hex(code))) + suite.Equal(fip20.Code, code, fmt.Sprintf("fip20 deployed code: %s", common.Bytes2Hex(code))) - deployedCode := bytes.ReplaceAll(code, contract.GetFIP20().Address.Bytes(), common.Address{}.Bytes()) + deployedCode := bytes.ReplaceAll(code, fip20.Address.Bytes(), common.Address{}.Bytes()) suite.True(strings.HasSuffix(contract.FIP20UpgradableMetaData.Bin, common.Bytes2Hex(deployedCode))) } func (suite *IntegrationTest) WFXCodeCheckTest() { suite.Send(suite.evm.AccAddress(), suite.NewCoin(sdkmath.NewInt(100).MulRaw(1e18))) - wfxAddr, _ := suite.evm.DeployContract(suite.evm.privKey, contract.GetWFX().Bin) + wfx := contract.GetWFX() + wfxAddr, _ := suite.evm.DeployContract(suite.evm.privKey, wfx.Bin) code, err := suite.evm.EthClient().CodeAt(suite.ctx, wfxAddr, nil) suite.Require().NoError(err) - suite.Equal(contract.GetWFX().Code, code, fmt.Sprintf("wfx deployed code: %s", common.Bytes2Hex(code))) + suite.Equal(wfx.Code, code, fmt.Sprintf("wfx deployed code: %s", common.Bytes2Hex(code))) - deployedCode := bytes.ReplaceAll(code, contract.GetWFX().Address.Bytes(), common.Address{}.Bytes()) + deployedCode := bytes.ReplaceAll(code, wfx.Address.Bytes(), common.Address{}.Bytes()) suite.True(strings.HasSuffix(contract.WFXUpgradableMetaData.Bin, common.Bytes2Hex(deployedCode))) } diff --git a/tests/precompile_suite.go b/tests/precompile_suite.go index f438db6f..7c87e126 100644 --- a/tests/precompile_suite.go +++ b/tests/precompile_suite.go @@ -9,7 +9,6 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/functionx/fx-core/v8/client" - "github.com/functionx/fx-core/v8/contract" "github.com/functionx/fx-core/v8/testutil/helpers" fxtypes "github.com/functionx/fx-core/v8/types" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" @@ -35,20 +34,7 @@ func (suite *PrecompileTestSuite) HexAddress() common.Address { return common.BytesToAddress(suite.privKey.PubKey().Address()) } -func (suite *PrecompileTestSuite) TransferCrosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { - privateKey := suite.privKey - beforeBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) - pack, err := contract.GetFIP20().ABI.Pack("transferCrossChain", recipient, amount, fee, fxtypes.MustStrToByte32(target)) - suite.Require().NoError(err) - ethTx, err := client.BuildEthTransaction(suite.ctx, suite.EthClient(), privateKey, &token, nil, pack) - suite.Require().NoError(err, target) - suite.SendTransaction(ethTx) - afterBalanceOf := suite.BalanceOf(token, common.BytesToAddress(privateKey.PubKey().Address().Bytes())) - suite.Require().True(new(big.Int).Sub(beforeBalanceOf, afterBalanceOf).Cmp(new(big.Int).Add(amount, fee)) == 0) - return ethTx -} - -func (suite *PrecompileTestSuite) CrosschainAndResponse(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { +func (suite *PrecompileTestSuite) Crosschain(token common.Address, recipient string, amount, fee *big.Int, target string) *ethtypes.Transaction { privateKey := suite.privKey crosschainContract := crosschaintypes.GetAddress() suite.ApproveERC20(privateKey, token, crosschainContract, big.NewInt(0).Add(amount, fee)) diff --git a/testutil/helpers/suite.go b/testutil/helpers/suite.go index 62eb2814..866d512d 100644 --- a/testutil/helpers/suite.go +++ b/testutil/helpers/suite.go @@ -28,6 +28,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/functionx/fx-core/v8/app" + fxevmkeeper "github.com/functionx/fx-core/v8/contract" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" ) @@ -38,6 +39,7 @@ type BaseSuite struct { ValAddr []sdk.ValAddress App *app.App Ctx sdk.Context + ERC20Token fxevmkeeper.ERC20TokenKeeper } func (s *BaseSuite) SetupTest() { @@ -55,6 +57,7 @@ func (s *BaseSuite) SetupTest() { s.App = setupWithGenesisValSet(s.T(), valSet, valAccounts, valBalances...) s.Ctx = s.App.GetContextForFinalizeBlock(nil) s.Ctx = s.Ctx.WithProposer(s.ValSet.Proposer.Address.Bytes()) + s.ERC20Token = fxevmkeeper.NewERC20TokenKeeper(s.App.EvmKeeper) } func (s *BaseSuite) AddTestSigner(amount ...int64) *Signer { @@ -180,7 +183,7 @@ func (s *BaseSuite) CheckAllBalance(addr sdk.AccAddress, expBal ...sdk.Coin) { } func (s *BaseSuite) CheckBalanceOf(contractAddr, address common.Address, expBal *big.Int) { - balanceOf, err := s.App.EvmKeeper.ERC20BalanceOf(s.Ctx, contractAddr, address) + balanceOf, err := s.ERC20Token.BalanceOf(s.Ctx, contractAddr, address) s.Require().NoError(err) s.Equal(expBal.String(), balanceOf.String()) } diff --git a/x/crosschain/keeper/bridge_call_in.go b/x/crosschain/keeper/bridge_call_in.go index 03fa5c0d..c457dcad 100644 --- a/x/crosschain/keeper/bridge_call_in.go +++ b/x/crosschain/keeper/bridge_call_in.go @@ -11,6 +11,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/hashicorp/go-metrics" + "github.com/functionx/fx-core/v8/contract" "github.com/functionx/fx-core/v8/x/crosschain/types" ) @@ -96,7 +97,7 @@ func (k Keeper) BridgeCallEvm(ctx sdk.Context, sender, refundAddr, to, receiverA callEvmSender = sender } else { var err error - args, err = types.PackBridgeCallback(sender, refundAddr, tokens, amounts, data, memo) + args, err = contract.PackBridgeCallback(sender, refundAddr, tokens, amounts, data, memo) if err != nil { return err } diff --git a/x/crosschain/precompile/contract.go b/x/crosschain/precompile/contract.go index be41e84e..f121e617 100644 --- a/x/crosschain/precompile/contract.go +++ b/x/crosschain/precompile/contract.go @@ -61,30 +61,30 @@ func (c *Contract) RequiredGas(input []byte) uint64 { return 0 } -func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) (ret []byte, err error) { - if len(contract.Input) <= 4 { - return evmtypes.PackRetErrV2(errors.New("invalid input")) +func (c *Contract) Run(evm *vm.EVM, vmContract *vm.Contract, readonly bool) (ret []byte, err error) { + if len(vmContract.Input) <= 4 { + return contract.PackRetErrV2(errors.New("invalid input")) } for _, method := range c.methods { - if bytes.Equal(method.GetMethodId(), contract.Input[:4]) { + if bytes.Equal(method.GetMethodId(), vmContract.Input[:4]) { if readonly && !method.IsReadonly() { - return evmtypes.PackRetErrV2(errors.New("write protection")) + return contract.PackRetErrV2(errors.New("write protection")) } stateDB := evm.StateDB.(evmtypes.ExtStateDB) if err = c.govKeeper.CheckDisabledPrecompiles(stateDB.Context(), c.Address(), method.GetMethodId()); err != nil { - return evmtypes.PackRetErrV2(err) + return contract.PackRetErrV2(err) } - ret, err = method.Run(evm, contract) + ret, err = method.Run(evm, vmContract) if err != nil { - return evmtypes.PackRetErrV2(err) + return contract.PackRetErrV2(err) } return ret, nil } } - return evmtypes.PackRetErrV2(errors.New("unknown method")) + return contract.PackRetErrV2(errors.New("unknown method")) } func EmitEvent(evm *vm.EVM, data []byte, topics []common.Hash) { diff --git a/x/crosschain/types/abi.go b/x/crosschain/types/abi.go deleted file mode 100644 index 2bf73a00..00000000 --- a/x/crosschain/types/abi.go +++ /dev/null @@ -1,27 +0,0 @@ -package types - -import ( - "math/big" - - "github.com/ethereum/go-ethereum/common" - - "github.com/functionx/fx-core/v8/contract" -) - -var MemoSendCallTo = common.HexToHash("0000000000000000000000000000000000000000000000000000000000010000") - -func IsMemoSendCallTo(memo []byte) bool { - return len(memo) == 32 && common.BytesToHash(memo) == MemoSendCallTo -} - -func PackBridgeCallback(sender, receiver common.Address, tokens []common.Address, amounts []*big.Int, data, memo []byte) ([]byte, error) { - args, err := contract.GetBridgeCallBridgeCallback().Pack("bridgeCallback", - sender, - receiver, - tokens, - amounts, - data, - memo, - ) - return args, err -} diff --git a/x/crosschain/types/memo.go b/x/crosschain/types/memo.go new file mode 100644 index 00000000..e5e90ff8 --- /dev/null +++ b/x/crosschain/types/memo.go @@ -0,0 +1,11 @@ +package types + +import ( + "github.com/ethereum/go-ethereum/common" +) + +var MemoSendCallTo = common.HexToHash("0000000000000000000000000000000000000000000000000000000000010000") + +func IsMemoSendCallTo(memo []byte) bool { + return len(memo) == 32 && common.BytesToHash(memo) == MemoSendCallTo +} diff --git a/x/crosschain/types/types.go b/x/crosschain/types/types.go index 39f5f6fb..03b89266 100644 --- a/x/crosschain/types/types.go +++ b/x/crosschain/types/types.go @@ -221,7 +221,7 @@ func (m *OracleSet) GetCheckpoint(gravityIDStr string) ([]byte, error) { // the word 'checkpoint' needs to be the same as the 'name' above in the checkpointAbiJson // but other than that it's a constant that has no impact on the output. This is because // it gets encoded as a function name which we must then discard. - packBytes, err := contract.GetFxBridgeABI().Pack("oracleSetCheckpoint", gravityID, checkpoint, big.NewInt(int64(m.Nonce)), memberAddresses, convertedPowers) + packBytes, err := contract.PackOracleSetCheckpoint(gravityID, checkpoint, big.NewInt(int64(m.Nonce)), memberAddresses, convertedPowers) // this should never happen outside of test since any case that could crash on encoding // should be filtered above. if err != nil { @@ -343,7 +343,7 @@ func (m *OutgoingTxBatch) GetCheckpoint(gravityIDString string) ([]byte, error) // the methodName needs to be the same as the 'name' above in the checkpointAbiJson // but other than that it's a constant that has no impact on the output. This is because // it gets encoded as a function name which we must then discard. - abiEncodedBatch, err := contract.GetFxBridgeABI().Pack("submitBatchCheckpoint", + abiEncodedBatch, err := contract.PackSubmitBatchCheckpoint( gravityID, batchMethodName, txAmounts, @@ -459,7 +459,7 @@ func (m *OutgoingBridgeCall) GetCheckpoint(gravityIDString string) ([]byte, erro // the methodName needs to be the same as the 'name' above in the checkpointAbiJson // but other than that it's a constant that has no impact on the output. This is because // it gets encoded as a function name which we must then discard. - abiEncodedBatch, err := contract.GetFxBridgeABI().Pack("bridgeCallCheckpoint", + abiEncodedBatch, err := contract.PackBridgeCallCheckpoint( gravityID, bridgeCallMethodName, gethcommon.HexToAddress(m.Sender), diff --git a/x/erc20/keeper/contract.go b/x/erc20/keeper/contract.go index be76e328..f75a8dbb 100644 --- a/x/erc20/keeper/contract.go +++ b/x/erc20/keeper/contract.go @@ -12,15 +12,15 @@ import ( ) func (k Keeper) ERC20BaseInfo(ctx context.Context, contractAddr common.Address) (name, symbol string, decimals uint8, err error) { - name, err = k.evmErc20Keeper.ERC20Name(ctx, contractAddr) + name, err = k.evmErc20Keeper.Name(ctx, contractAddr) if err != nil { return name, symbol, decimals, err } - symbol, err = k.evmErc20Keeper.ERC20Symbol(ctx, contractAddr) + symbol, err = k.evmErc20Keeper.Symbol(ctx, contractAddr) if err != nil { return name, symbol, decimals, err } - decimals, err = k.evmErc20Keeper.ERC20Decimals(ctx, contractAddr) + decimals, err = k.evmErc20Keeper.Decimals(ctx, contractAddr) if err != nil { return name, symbol, decimals, err } diff --git a/x/erc20/keeper/convert.go b/x/erc20/keeper/convert.go index 1f6e8ab5..3119c859 100644 --- a/x/erc20/keeper/convert.go +++ b/x/erc20/keeper/convert.go @@ -70,7 +70,7 @@ func (k Keeper) ConvertCoinNativeCoin(ctx context.Context, erc20Token types.ERC2 } erc20Contract := erc20Token.GetERC20Contract() - if err := k.evmErc20Keeper.ERC20Mint(ctx, erc20Contract, k.contractOwner, receiver, coin.Amount.BigInt()); err != nil { + if err := k.evmErc20Keeper.Mint(ctx, erc20Contract, k.contractOwner, receiver, coin.Amount.BigInt()); err != nil { return err } @@ -90,7 +90,7 @@ func (k Keeper) ConvertCoinNativeERC20(ctx context.Context, erc20Token types.ERC return err } - if err := k.evmErc20Keeper.ERC20Transfer(ctx, erc20Token.GetERC20Contract(), k.contractOwner, receiver, coin.Amount.BigInt()); err != nil { + if err := k.evmErc20Keeper.Transfer(ctx, erc20Token.GetERC20Contract(), k.contractOwner, receiver, coin.Amount.BigInt()); err != nil { return err } diff --git a/x/erc20/keeper/keeper.go b/x/erc20/keeper/keeper.go index 96bc1a60..269865fe 100644 --- a/x/erc20/keeper/keeper.go +++ b/x/erc20/keeper/keeper.go @@ -22,7 +22,7 @@ type Keeper struct { accountKeeper types.AccountKeeper bankKeeper types.BankKeeper evmKeeper types.EVMKeeper - evmErc20Keeper types.EvmERC20Keeper + evmErc20Keeper types.ERC20TokenKeeper contractOwner common.Address @@ -44,7 +44,7 @@ func NewKeeper( ak types.AccountKeeper, bk types.BankKeeper, evmKeeper types.EVMKeeper, - evmErc20Keeper types.EvmERC20Keeper, + evmErc20Keeper types.ERC20TokenKeeper, authority string, ) Keeper { moduleAddress := ak.GetModuleAddress(types.ModuleName) diff --git a/x/erc20/types/expected_keepers.go b/x/erc20/types/expected_keepers.go index 9d652fd2..873f8961 100644 --- a/x/erc20/types/expected_keepers.go +++ b/x/erc20/types/expected_keepers.go @@ -31,14 +31,14 @@ type BankKeeper interface { SetDenomMetaData(ctx context.Context, denomMetaData banktypes.Metadata) } -type EvmERC20Keeper interface { - ERC20Name(ctx context.Context, contractAddr common.Address) (string, error) - ERC20Symbol(ctx context.Context, contractAddr common.Address) (string, error) - ERC20Decimals(ctx context.Context, contractAddr common.Address) (uint8, error) - - ERC20Mint(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error - ERC20Burn(ctx context.Context, contractAddr, from, account common.Address, amount *big.Int) error - ERC20Transfer(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error +type ERC20TokenKeeper interface { + Name(ctx context.Context, contractAddr common.Address) (string, error) + Symbol(ctx context.Context, contractAddr common.Address) (string, error) + Decimals(ctx context.Context, contractAddr common.Address) (uint8, error) + + Mint(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error + Burn(ctx context.Context, contractAddr, from, account common.Address, amount *big.Int) error + Transfer(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error } // EVMKeeper defines the expected EVM keeper interface used on erc20 diff --git a/x/evm/keeper/erc20.go b/x/evm/keeper/erc20.go deleted file mode 100644 index cec1d4e8..00000000 --- a/x/evm/keeper/erc20.go +++ /dev/null @@ -1,83 +0,0 @@ -package keeper - -import ( - "context" - "math/big" - - sdk "github.com/cosmos/cosmos-sdk/types" - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" - "github.com/ethereum/go-ethereum/common" - - "github.com/functionx/fx-core/v8/contract" - "github.com/functionx/fx-core/v8/x/evm/types" -) - -func (k *Keeper) ERC20Name(ctx context.Context, contractAddr common.Address) (string, error) { - var nameRes struct{ Value string } - if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.module, contractAddr, contract.GetFIP20().ABI, "name", &nameRes); err != nil { - return "", err - } - return nameRes.Value, nil -} - -func (k *Keeper) ERC20Symbol(ctx context.Context, contractAddr common.Address) (string, error) { - var symbolRes struct{ Value string } - if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.module, contractAddr, contract.GetFIP20().ABI, "symbol", &symbolRes); err != nil { - return "", err - } - return symbolRes.Value, nil -} - -func (k *Keeper) ERC20Decimals(ctx context.Context, contractAddr common.Address) (uint8, error) { - var decimalRes struct{ Value uint8 } - if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.module, contractAddr, contract.GetFIP20().ABI, "decimals", &decimalRes); err != nil { - return 0, nil - } - return decimalRes.Value, nil -} - -func (k *Keeper) ERC20BalanceOf(ctx context.Context, contractAddr, addr common.Address) (*big.Int, error) { - var balanceRes struct { - Value *big.Int - } - if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.module, contractAddr, contract.GetFIP20().ABI, "balanceOf", &balanceRes, addr); err != nil { - return big.NewInt(0), err - } - return balanceRes.Value, nil -} - -func (k *Keeper) TotalSupply(ctx context.Context, contractAddr common.Address) (*big.Int, error) { - var totalSupplyRes struct{ Value *big.Int } - if err := k.QueryContract(sdk.UnwrapSDKContext(ctx), k.module, contractAddr, contract.GetFIP20().ABI, "totalSupply", &totalSupplyRes); err != nil { - return nil, err - } - return totalSupplyRes.Value, nil -} - -func (k *Keeper) ERC20Mint(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error { - _, err := k.ApplyContract(sdk.UnwrapSDKContext(ctx), from, contractAddr, nil, contract.GetFIP20().ABI, "mint", receiver, amount) - return err -} - -func (k *Keeper) ERC20Burn(ctx context.Context, contractAddr, from, account common.Address, amount *big.Int) error { - _, err := k.ApplyContract(sdk.UnwrapSDKContext(ctx), from, contractAddr, nil, contract.GetFIP20().ABI, "burn", account, amount) - return err -} - -func (k *Keeper) ERC20Transfer(ctx context.Context, contractAddr, from, receiver common.Address, amount *big.Int) error { - erc20ABI := contract.GetFIP20().ABI - res, err := k.ApplyContract(sdk.UnwrapSDKContext(ctx), from, contractAddr, nil, erc20ABI, "transfer", receiver, amount) - if err != nil { - return err - } - - // Check unpackedRet execution - var unpackedRet struct{ Value bool } - if err = erc20ABI.UnpackIntoInterface(&unpackedRet, "transfer", res.Ret); err != nil { - return types.ErrABIUnpack.Wrapf("failed to unpack transfer: %s", err.Error()) - } - if !unpackedRet.Value { - return sdkerrors.ErrLogic.Wrap("failed to execute transfer") - } - return nil -} diff --git a/x/evm/testutil/erc20.go b/x/evm/testutil/erc20.go index 1e072cde..e7437749 100644 --- a/x/evm/testutil/erc20.go +++ b/x/evm/testutil/erc20.go @@ -3,6 +3,7 @@ package testutil import ( "math/big" + "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/crypto" @@ -15,21 +16,23 @@ import ( type ERC20Suite struct { contract.ERC20ABI EVMSuite + abi abi.ABI } func NewERC20Suite(evmSuite EVMSuite) ERC20Suite { return ERC20Suite{ ERC20ABI: contract.NewERC20ABI(), EVMSuite: evmSuite, + abi: contract.GetWFX().ABI, } } func (s *ERC20Suite) Call(method string, res interface{}, args ...interface{}) { - s.EVMSuite.Call(s.ABI, method, res, args...) + s.EVMSuite.Call(s.abi, method, res, args...) } func (s *ERC20Suite) Send(method string, args ...interface{}) *evmtypes.MsgEthereumTxResponse { - return s.EVMSuite.Send(s.ABI, method, args...) + return s.EVMSuite.Send(s.abi, method, args...) } func (s *ERC20Suite) Deploy(symbol string) common.Address { @@ -129,7 +132,7 @@ func (s *ERC20Suite) Approve(spender common.Address, amount *big.Int, result boo } } -func (s *ERC20Suite) Transfer(recipient common.Address, amount *big.Int, result bool) { +func (s *ERC20Suite) Transfer(recipient common.Address, amount *big.Int, result bool) *evmtypes.MsgEthereumTxResponse { before := s.BalanceOf(s.signer.Address()) response := s.Send("transfer", recipient, amount) after := s.BalanceOf(s.signer.Address()) @@ -137,6 +140,7 @@ func (s *ERC20Suite) Transfer(recipient common.Address, amount *big.Int, result if result { s.Equal(after, new(big.Int).Sub(before, amount)) } + return response } func (s *ERC20Suite) TransferFrom(sender, recipient common.Address, amount *big.Int, result bool) { @@ -149,7 +153,7 @@ func (s *ERC20Suite) TransferFrom(sender, recipient common.Address, amount *big. } } -func (s *ERC20Suite) Mint(to common.Address, amount *big.Int, result bool) { +func (s *ERC20Suite) Mint(to common.Address, amount *big.Int, result bool) *evmtypes.MsgEthereumTxResponse { before := s.TotalSupply() response := s.Send("mint", to, amount) after := s.TotalSupply() @@ -157,6 +161,7 @@ func (s *ERC20Suite) Mint(to common.Address, amount *big.Int, result bool) { if result { s.Equal(after, new(big.Int).Add(before, amount)) } + return response } func (s *ERC20Suite) Burn(from common.Address, amount *big.Int, result bool) { @@ -200,7 +205,7 @@ func (s *ERC20Suite) Withdraw(to common.Address, amount *big.Int, result bool) { } func (s *ERC20Suite) Deposit(value *big.Int, result bool) { - data, err := s.ABI.Pack("deposit") + data, err := s.abi.Pack("deposit") s.NoError(err) msg := &core.Message{ diff --git a/x/evm/types/errors.go b/x/evm/types/errors.go index 6c315a0c..efcc477c 100644 --- a/x/evm/types/errors.go +++ b/x/evm/types/errors.go @@ -2,23 +2,10 @@ package types import ( errorsmod "cosmossdk.io/errors" - "github.com/ethereum/go-ethereum/accounts/abi" "github.com/evmos/ethermint/x/evm/types" - - "github.com/functionx/fx-core/v8/contract" ) var ( ErrABIPack = errorsmod.Register(types.ModuleName, 10001, "failed abi pack args") ErrABIUnpack = errorsmod.Register(types.ModuleName, 10002, "failed abi unpack data") ) - -func PackRetError(err error) ([]byte, error) { - pack, _ := abi.Arguments{{Type: contract.TypeString}}.Pack(err.Error()) - return pack, err -} - -func PackRetErrV2(err error) ([]byte, error) { - pack, _ := contract.GetErrorABI().Pack("Error", err.Error()) - return pack, err -} diff --git a/x/staking/precompile/contract.go b/x/staking/precompile/contract.go index 14dfe6b7..be17e707 100644 --- a/x/staking/precompile/contract.go +++ b/x/staking/precompile/contract.go @@ -86,30 +86,30 @@ func (c *Contract) RequiredGas(input []byte) uint64 { return 0 } -func (c *Contract) Run(evm *vm.EVM, contract *vm.Contract, readonly bool) (ret []byte, err error) { - if len(contract.Input) <= 4 { - return evmtypes.PackRetErrV2(errors.New("invalid input")) +func (c *Contract) Run(evm *vm.EVM, vmContract *vm.Contract, readonly bool) (ret []byte, err error) { + if len(vmContract.Input) <= 4 { + return contract.PackRetErrV2(errors.New("invalid input")) } for _, method := range c.methods { - if bytes.Equal(method.GetMethodId(), contract.Input[:4]) { + if bytes.Equal(method.GetMethodId(), vmContract.Input[:4]) { if readonly && !method.IsReadonly() { - return evmtypes.PackRetErrV2(errors.New("write protection")) + return contract.PackRetErrV2(errors.New("write protection")) } stateDB := evm.StateDB.(evmtypes.ExtStateDB) - if err = c.govKeeper.CheckDisabledPrecompiles(stateDB.Context(), c.Address(), contract.Input[:4]); err != nil { - return evmtypes.PackRetError(err) + if err = c.govKeeper.CheckDisabledPrecompiles(stateDB.Context(), c.Address(), vmContract.Input[:4]); err != nil { + return contract.PackRetError(err) } - ret, err = method.Run(evm, contract) + ret, err = method.Run(evm, vmContract) if err != nil { - return evmtypes.PackRetErrV2(err) + return contract.PackRetErrV2(err) } return ret, nil } } - return evmtypes.PackRetErrV2(errors.New("unknown method")) + return contract.PackRetErrV2(errors.New("unknown method")) } func EmitEvent(evm *vm.EVM, data []byte, topics []common.Hash) {