Skip to content

Commit

Permalink
use transaction origin to create and update allowances
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Nov 21, 2023
1 parent 6092dfd commit 06c3c17
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 44 deletions.
12 changes: 6 additions & 6 deletions precompiles/erc20/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
// 4. authorization exists, amount positive -> update authorization
func (p Precompile) Approve(
ctx sdk.Context,
contract *vm.Contract,
origin common.Address,
stateDB vm.StateDB,
method *abi.Method,
args []interface{},
Expand All @@ -43,7 +43,7 @@ func (p Precompile) Approve(
}

grantee := spender
granter := contract.CallerAddress
granter := origin

// TODO: owner should be the owner of the contract
authorization, expiration, _ := auth.CheckAuthzExists(ctx, p.AuthzKeeper, grantee, granter, SendMsgURL) //#nosec:G703 -- we are handling the error case (authorization == nil) in the switch statement below
Expand Down Expand Up @@ -93,7 +93,7 @@ func (p Precompile) Approve(
// 3. authorization exists, addedValue positive -> update authorization
func (p Precompile) IncreaseAllowance(
ctx sdk.Context,
contract *vm.Contract,
origin common.Address,
stateDB vm.StateDB,
method *abi.Method,
args []interface{},
Expand All @@ -104,7 +104,7 @@ func (p Precompile) IncreaseAllowance(
}

grantee := spender
granter := contract.CallerAddress
granter := origin

// TODO: owner should be the owner of the contract
authorization, expiration, _ := auth.CheckAuthzExists(ctx, p.AuthzKeeper, grantee, granter, SendMsgURL) //#nosec:G703 -- we are handling the error case (authorization == nil) in the switch statement below
Expand Down Expand Up @@ -151,7 +151,7 @@ func (p Precompile) IncreaseAllowance(
// 6. authorization exists, subtractedValue positive and subtractedValue higher than allowance -> return error
func (p Precompile) DecreaseAllowance(
ctx sdk.Context,
contract *vm.Contract,
origin common.Address,
stateDB vm.StateDB,
method *abi.Method,
args []interface{},
Expand All @@ -162,7 +162,7 @@ func (p Precompile) DecreaseAllowance(
}

grantee := spender
granter := contract.CallerAddress
granter := origin

// TODO: owner should be the owner of the contract

Expand Down
36 changes: 3 additions & 33 deletions precompiles/erc20/approve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import (
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/core/vm"
"github.com/evmos/evmos/v15/precompiles/authorization"
"github.com/evmos/evmos/v15/precompiles/testutil"
)

//nolint:dupl // tests are not duplicate between the functions
func (s *PrecompileTestSuite) TestApprove() {
method := s.precompile.Methods[authorization.ApproveMethod]
amount := int64(100)
Expand Down Expand Up @@ -224,23 +221,14 @@ func (s *PrecompileTestSuite) TestApprove() {

ctx := s.network.GetContext()

var contract *vm.Contract
contract, ctx = testutil.NewPrecompileContract(
s.T(),
ctx,
s.keyring.GetAddr(0),
s.precompile,
200_000,
)

var args []interface{}
if tc.malleate != nil {
args = tc.malleate()
}

bz, err := s.precompile.Approve(
ctx,
contract,
s.keyring.GetAddr(0),
s.network.GetStateDB(),
&method,
args,
Expand Down Expand Up @@ -422,23 +410,14 @@ func (s *PrecompileTestSuite) TestIncreaseAllowance() {

ctx := s.network.GetContext()

var contract *vm.Contract
contract, ctx = testutil.NewPrecompileContract(
s.T(),
ctx,
s.keyring.GetAddr(0),
s.precompile,
200_000,
)

var args []interface{}
if tc.malleate != nil {
args = tc.malleate()
}

bz, err := s.precompile.IncreaseAllowance(
ctx,
contract,
s.keyring.GetAddr(0),
s.network.GetStateDB(),
&method,
args,
Expand Down Expand Up @@ -722,23 +701,14 @@ func (s *PrecompileTestSuite) TestDecreaseAllowance() {

ctx := s.network.GetContext()

var contract *vm.Contract
contract, ctx = testutil.NewPrecompileContract(
s.T(),
ctx,
s.keyring.GetAddr(0),
s.precompile,
200_000,
)

var args []interface{}
if tc.malleate != nil {
args = tc.malleate()
}

bz, err := s.precompile.DecreaseAllowance(
ctx,
contract,
s.keyring.GetAddr(0),
s.network.GetStateDB(),
&method,
args,
Expand Down
9 changes: 5 additions & 4 deletions precompiles/erc20/erc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
// It avoids panics and returns the out of gas error so the EVM can continue gracefully.
defer cmn.HandleGasError(ctx, contract, initialGas, &err)()

bz, err = p.HandleMethod(ctx, contract, stateDB, method, args)
bz, err = p.HandleMethod(ctx, evm, contract, stateDB, method, args)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -172,6 +172,7 @@ func (Precompile) IsTransaction(methodID string) bool {
// HandleMethod handles the execution of each of the ERC-20 methods.
func (p Precompile) HandleMethod(
ctx sdk.Context,
evm *vm.EVM,
contract *vm.Contract,
stateDB vm.StateDB,
method *abi.Method,
Expand All @@ -184,11 +185,11 @@ func (p Precompile) HandleMethod(
case TransferFromMethod:
bz, err = p.TransferFrom(ctx, contract, stateDB, method, args)
case auth.ApproveMethod:
bz, err = p.Approve(ctx, contract, stateDB, method, args)
bz, err = p.Approve(ctx, evm.Origin, stateDB, method, args)
case auth.IncreaseAllowanceMethod:
bz, err = p.IncreaseAllowance(ctx, contract, stateDB, method, args)
bz, err = p.IncreaseAllowance(ctx, evm.Origin, stateDB, method, args)
case auth.DecreaseAllowanceMethod:
bz, err = p.DecreaseAllowance(ctx, contract, stateDB, method, args)
bz, err = p.DecreaseAllowance(ctx, evm.Origin, stateDB, method, args)
// ERC-20 queries
case NameMethod:
bz, err = p.Name(ctx, contract, stateDB, method, args)
Expand Down
2 changes: 1 addition & 1 deletion precompiles/werc20/werc20.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
bz, err = p.Withdraw(ctx, contract, stateDB, method, args)
default:
// ERC20 transactions and queries
bz, err = p.Precompile.HandleMethod(ctx, contract, stateDB, method, args)
bz, err = p.Precompile.HandleMethod(ctx, evm, contract, stateDB, method, args)
}

if err != nil {
Expand Down

0 comments on commit 06c3c17

Please sign in to comment.