-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pre-compilation get ERC20 token
- Loading branch information
Showing
13 changed files
with
286 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package crosschain | ||
|
||
import ( | ||
"github.com/ethereum/go-ethereum/accounts/abi" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/core/vm" | ||
|
||
"github.com/pundiai/fx-core/v8/contract" | ||
"github.com/pundiai/fx-core/v8/x/evm/types" | ||
) | ||
|
||
type GetERC20TokenMethod struct { | ||
*Keeper | ||
GetERC20TokenABI | ||
} | ||
|
||
func NewGetERC20TokenMethod(keeper *Keeper) *GetERC20TokenMethod { | ||
return &GetERC20TokenMethod{ | ||
Keeper: keeper, | ||
GetERC20TokenABI: NewGetERC20TokenABI(), | ||
} | ||
} | ||
|
||
func (m *GetERC20TokenMethod) GetMethodId() []byte { | ||
return m.Method.ID | ||
} | ||
|
||
func (m *GetERC20TokenMethod) RequiredGas() uint64 { | ||
return 1_000 | ||
} | ||
|
||
func (m *GetERC20TokenMethod) IsReadonly() bool { | ||
return true | ||
} | ||
|
||
func (m *GetERC20TokenMethod) Run(evm *vm.EVM, vmContract *vm.Contract) ([]byte, error) { | ||
args, err := m.UnpackInput(vmContract.Input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
stateDB := evm.StateDB.(types.ExtStateDB) | ||
denom := contract.Byte32ToString(args.Denom) | ||
erc20Token, err := m.Keeper.erc20Keeper.GetERC20Token(stateDB.Context(), denom) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return m.PackOutput(common.HexToAddress(erc20Token.GetErc20Address()), erc20Token.Enabled) | ||
} | ||
|
||
type GetERC20TokenABI struct { | ||
abi.Method | ||
} | ||
|
||
func NewGetERC20TokenABI() GetERC20TokenABI { | ||
return GetERC20TokenABI{ | ||
Method: crosschainABI.Methods["getERC20Token"], | ||
} | ||
} | ||
|
||
func (m GetERC20TokenABI) PackInput(args contract.GetERC20TokenArgs) ([]byte, error) { | ||
arguments, err := m.Method.Inputs.Pack(args.Denom) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return append(m.Method.ID, arguments...), nil | ||
} | ||
|
||
func (m GetERC20TokenABI) UnpackInput(data []byte) (*contract.GetERC20TokenArgs, error) { | ||
args := new(contract.GetERC20TokenArgs) | ||
err := types.ParseMethodArgs(m.Method, args, data[4:]) | ||
return args, err | ||
} | ||
|
||
func (m GetERC20TokenABI) PackOutput(tokenAddr common.Address, enable bool) ([]byte, error) { | ||
return m.Method.Outputs.Pack(tokenAddr, enable) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package crosschain_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/pundiai/fx-core/v8/contract" | ||
"github.com/pundiai/fx-core/v8/precompiles/crosschain" | ||
"github.com/pundiai/fx-core/v8/testutil/helpers" | ||
erc20types "github.com/pundiai/fx-core/v8/x/erc20/types" | ||
) | ||
|
||
func TestCrosschainGetERC20Tokwn(t *testing.T) { | ||
getERC20TokenABI := crosschain.NewGetERC20TokenMethod(nil) | ||
require.Len(t, getERC20TokenABI.Method.Inputs, 1) | ||
require.Len(t, getERC20TokenABI.Method.Outputs, 2) | ||
} | ||
|
||
func (suite *CrosschainPrecompileTestSuite) TestGetERC20Token() { | ||
testCases := []struct { | ||
name string | ||
malleate func() (contract.GetERC20TokenArgs, error) | ||
result bool | ||
}{ | ||
{ | ||
name: "erc20 token", | ||
malleate: func() (contract.GetERC20TokenArgs, error) { | ||
denom := "usdt" | ||
err := suite.App.Erc20Keeper.ERC20Token.Set(suite.Ctx, denom, erc20types.ERC20Token{ | ||
Erc20Address: helpers.GenHexAddress().Hex(), | ||
Denom: denom, | ||
Enabled: false, | ||
ContractOwner: erc20types.OWNER_MODULE, | ||
}) | ||
suite.Require().NoError(err) | ||
return contract.GetERC20TokenArgs{ | ||
Denom: contract.MustStrToByte32(denom), | ||
}, nil | ||
}, | ||
result: true, | ||
}, | ||
{ | ||
name: "denom not found", | ||
malleate: func() (contract.GetERC20TokenArgs, error) { | ||
return contract.GetERC20TokenArgs{ | ||
Denom: contract.MustStrToByte32("test"), | ||
}, fmt.Errorf("collections: not found: key 'test' of type github.com/cosmos/gogoproto/fx.erc20.v1.ERC20Token") | ||
}, | ||
result: false, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.name), func() { | ||
args, expectErr := tc.malleate() | ||
token, enable := suite.WithError(expectErr).GetERC20Token(suite.Ctx, args) | ||
if tc.result { | ||
erc20Token, err := suite.App.Erc20Keeper.GetERC20Token(suite.Ctx, contract.Byte32ToString(args.Denom)) | ||
suite.Require().NoError(err) | ||
suite.Require().Equal(common.HexToAddress(erc20Token.Erc20Address), token) | ||
suite.Require().Equal(erc20Token.Enabled, enable) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.