Skip to content

Commit

Permalink
Multiple Sec3 fixes (sei-protocol#1642)
Browse files Browse the repository at this point in the history
  • Loading branch information
codchen authored May 9, 2024
1 parent a6ca72f commit 8fc4dc6
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 8 deletions.
6 changes: 3 additions & 3 deletions precompiles/pointer/Pointer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ IPointer constant POINTER_CONTRACT = IPointer(POINTER_PRECOMPILE_ADDRESS);
interface IPointer {
function addNativePointer(
string memory token
) payable external returns (address ret);
) external returns (address ret);

function addCW20Pointer(
string memory cwAddr
) payable external returns (address ret);
) external returns (address ret);

function addCW721Pointer(
string memory cwAddr
) payable external returns (address ret);
) external returns (address ret);
}
9 changes: 9 additions & 0 deletions precompiles/pointer/pointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ func (p Precompile) Run(*vm.EVM, common.Address, common.Address, []byte, *big.In
}

func (p Precompile) AddNative(ctx sdk.Context, method *ethabi.Method, caller common.Address, args []interface{}, value *big.Int, evm *vm.EVM, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
if err := pcommon.ValidateNonPayable(value); err != nil {
return nil, 0, err
}
if err := pcommon.ValidateArgsLength(args, 1); err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -187,6 +190,9 @@ func (p Precompile) AddNative(ctx sdk.Context, method *ethabi.Method, caller com
}

func (p Precompile) AddCW20(ctx sdk.Context, method *ethabi.Method, caller common.Address, args []interface{}, value *big.Int, evm *vm.EVM, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
if err := pcommon.ValidateNonPayable(value); err != nil {
return nil, 0, err
}
if err := pcommon.ValidateArgsLength(args, 1); err != nil {
return nil, 0, err
}
Expand Down Expand Up @@ -238,6 +244,9 @@ func (p Precompile) AddCW20(ctx sdk.Context, method *ethabi.Method, caller commo
}

func (p Precompile) AddCW721(ctx sdk.Context, method *ethabi.Method, caller common.Address, args []interface{}, value *big.Int, evm *vm.EVM, suppliedGas uint64) (ret []byte, remainingGas uint64, err error) {
if err := pcommon.ValidateNonPayable(value); err != nil {
return nil, 0, err
}
if err := pcommon.ValidateArgsLength(args, 1); err != nil {
return nil, 0, err
}
Expand Down
10 changes: 7 additions & 3 deletions precompiles/wasmd/wasmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (p Precompile) RunAndCalculateGas(evm *vm.EVM, caller common.Address, calli
case ExecuteMethod:
return p.execute(ctx, method, caller, callingContract, args, value, readOnly)
case ExecuteBatchMethod:
return p.execute_batch(ctx, method, caller, callingContract, args, value, readOnly)
return p.executeBatch(ctx, method, caller, callingContract, args, value, readOnly)
case QueryMethod:
return p.query(ctx, method, args, value)
}
Expand Down Expand Up @@ -256,7 +256,7 @@ func (p Precompile) instantiate(ctx sdk.Context, method *abi.Method, caller comm
return
}

func (p Precompile) execute_batch(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool) (ret []byte, remainingGas uint64, rerr error) {
func (p Precompile) executeBatch(ctx sdk.Context, method *abi.Method, caller common.Address, callingContract common.Address, args []interface{}, value *big.Int, readOnly bool) (ret []byte, remainingGas uint64, rerr error) {
defer func() {
if err := recover(); err != nil {
ret = nil
Expand Down Expand Up @@ -319,7 +319,11 @@ func (p Precompile) execute_batch(ctx sdk.Context, method *abi.Method, caller co
rerr = err
return
}
senderAddr := p.evmKeeper.GetSeiAddressOrDefault(ctx, caller)
senderAddr, senderAssociated := p.evmKeeper.GetSeiAddress(ctx, caller)
if !senderAssociated {
rerr = fmt.Errorf("sender %s is not associated", caller.Hex())
return
}
msg := executeMsg.Msg
coinsBz := executeMsg.Coins
coins := sdk.NewCoins()
Expand Down
9 changes: 9 additions & 0 deletions x/evm/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,23 @@ func CmdQueryERC20Payload() *cobra.Command {
var bz []byte
switch args[0] {
case "transfer":
if len(args) != 3 {
return errors.New("expected usage: `seid tx evm erc20-payload transfer [to] [amount]`")
}
to := common.HexToAddress(args[1])
amt, _ := sdk.NewIntFromString(args[2])
bz, err = abi.Pack(args[0], to, amt.BigInt())
case "approve":
if len(args) != 3 {
return errors.New("expected usage: `seid tx evm erc20-payload approve [spender] [amount]`")
}
spender := common.HexToAddress(args[1])
amt, _ := sdk.NewIntFromString(args[2])
bz, err = abi.Pack(args[0], spender, amt.BigInt())
case "transferFrom":
if len(args) != 4 {
return errors.New("expected usage: `seid tx evm erc20-payload transferFrom [from] [to] [amount]`")
}
from := common.HexToAddress(args[1])
to := common.HexToAddress(args[2])
amt, _ := sdk.NewIntFromString(args[3])
Expand Down
3 changes: 3 additions & 0 deletions x/evm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ func NewHandler(k *keeper.Keeper) sdk.Handler {
case *types.MsgSend:
res, err := msgServer.Send(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgRegisterPointer:
res, err := msgServer.RegisterPointer(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
5 changes: 3 additions & 2 deletions x/evm/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ func (server msgServer) EVMTransaction(goCtx context.Context, msg *types.MsgEVMT
)
return
}
receipt, err := server.writeReceipt(ctx, msg, tx, emsg, serverRes, stateDB)
if err != nil {
receipt, rerr := server.writeReceipt(ctx, msg, tx, emsg, serverRes, stateDB)
if rerr != nil {
err = rerr
ctx.Logger().Error(fmt.Sprintf("failed to write EVM receipt: %s", err))

telemetry.IncrCounterWithLabels(
Expand Down
2 changes: 2 additions & 0 deletions x/evm/keeper/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/sei-protocol/sei-chain/precompiles/bank"
"github.com/sei-protocol/sei-chain/precompiles/gov"
"github.com/sei-protocol/sei-chain/precompiles/staking"
"github.com/sei-protocol/sei-chain/precompiles/wasmd"
)

// add any payable precompiles here
Expand All @@ -13,6 +14,7 @@ var payablePrecompiles = map[string]struct{}{
bank.BankAddress: {},
staking.StakingAddress: {},
gov.GovAddress: {},
wasmd.WasmdAddress: {},
}

func IsPayablePrecompile(addr *common.Address) bool {
Expand Down

0 comments on commit 8fc4dc6

Please sign in to comment.