Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(erc20): ERC-20 transactions #1993

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Features

- (p256) [#1922](https://github.com/evmos/evmos/pull/1922) [EIP-7212](https://eips.ethereum.org/EIPS/eip-7212) `secp256r1` curve precompile
- (erc20) [#1993](https://github.com/evmos/evmos/pull/1993) Add ERC-20 Precompile transactions.

### Improvements

Expand Down
82 changes: 82 additions & 0 deletions precompiles/erc20/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
package erc20

import (
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
)

const (
Expand All @@ -18,3 +24,79 @@ const (

// SendMsgURL defines the authorization type for MsgSend
var SendMsgURL = sdk.MsgTypeURL(&banktypes.MsgSend{})

// Transfer executes a direct transfer from the caller address to the
// destination address.
func (p Precompile) Transfer(
ctx sdk.Context,
contract *vm.Contract,
stateDB vm.StateDB,
method *abi.Method,
args []interface{},
) ([]byte, error) {
from := contract.CallerAddress
to, amount, err := ParseTransferArgs(args)
if err != nil {
return nil, err
}

return p.transfer(ctx, contract, stateDB, method, from, to, amount)
}

// TransferFrom executes a transfer on behalf of the specified from address in
// the call data to the destination address.
func (p Precompile) TransferFrom(
ctx sdk.Context,
contract *vm.Contract,
stateDB vm.StateDB,
method *abi.Method,
args []interface{},
) ([]byte, error) {
from, to, amount, err := ParseTransferFromArgs(args)
if err != nil {
return nil, err
}

return p.transfer(ctx, contract, stateDB, method, from, to, amount)
}

// transfer is a common function that handles transfers for the ERC-20 Transfer
// and TransferFrom methods. It executes a bank Send message if the spender is
// the sender of the transfer, otherwise it executes an authorization.
func (p Precompile) transfer(
ctx sdk.Context,
contract *vm.Contract,
stateDB vm.StateDB,
method *abi.Method,
from, to common.Address,
amount *big.Int,
) (data []byte, err error) {
coins := sdk.Coins{{Denom: p.tokenPair.Denom, Amount: sdk.NewIntFromBigInt(amount)}}

msg := banktypes.NewMsgSend(from.Bytes(), to.Bytes(), coins)

if err := msg.ValidateBasic(); err != nil {
return nil, err
}

sender := sdk.AccAddress(from.Bytes())
spender := sdk.AccAddress(contract.CallerAddress.Bytes()) // aka. grantee

if sender.Equals(spender) {
msgSrv := bankkeeper.NewMsgServerImpl(p.bankKeeper)
_, err = msgSrv.Send(sdk.WrapSDKContext(ctx), msg)
} else {
_, err = p.AuthzKeeper.DispatchActions(ctx, spender, []sdk.Msg{msg})
}

if err != nil {
// TODO: check if we need to return an error here
return method.Outputs.Pack(false)
}

if err := p.EmitTransferEvent(ctx, stateDB, from, to, amount); err != nil {
return nil, err
}

return method.Outputs.Pack(true)
}