Skip to content

Commit

Permalink
refactor(staking/precompile): split into method and abi (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Oct 25, 2024
1 parent c1b00aa commit daf4ce3
Show file tree
Hide file tree
Showing 15 changed files with 519 additions and 394 deletions.
26 changes: 18 additions & 8 deletions x/staking/precompile/allowance_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import (

type AllowanceSharesMethod struct {
*Keeper
abi.Method
AllowanceSharesABI
}

func NewAllowanceSharesMethod(keeper *Keeper) *AllowanceSharesMethod {
return &AllowanceSharesMethod{
Keeper: keeper,
Method: stakingABI.Methods["allowanceShares"],
Keeper: keeper,
AllowanceSharesABI: NewAllowanceSharesABI(),
}
}

Expand Down Expand Up @@ -46,25 +46,35 @@ func (m *AllowanceSharesMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte,
return m.PackOutput(allowance)
}

func (m *AllowanceSharesMethod) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
type AllowanceSharesABI struct {
abi.Method
}

func NewAllowanceSharesABI() AllowanceSharesABI {
return AllowanceSharesABI{
Method: stakingABI.Methods["allowanceShares"],
}
}

func (m AllowanceSharesABI) PackInput(args fxstakingtypes.AllowanceSharesArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Owner, args.Spender)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *AllowanceSharesMethod) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
func (m AllowanceSharesABI) UnpackInput(data []byte) (*fxstakingtypes.AllowanceSharesArgs, error) {
args := new(fxstakingtypes.AllowanceSharesArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *AllowanceSharesMethod) PackOutput(amount *big.Int) ([]byte, error) {
func (m AllowanceSharesABI) PackOutput(amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(amount)
}

func (m *AllowanceSharesMethod) UnpackOutput(data []byte) (*big.Int, error) {
func (m AllowanceSharesABI) UnpackOutput(data []byte) (*big.Int, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return nil, err
Expand Down
34 changes: 22 additions & 12 deletions x/staking/precompile/approve_shares.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import (

type ApproveSharesMethod struct {
*Keeper
abi.Method
abi.Event
ApproveSharesABI
}

func NewApproveSharesMethod(keeper *Keeper) *ApproveSharesMethod {
return &ApproveSharesMethod{
Keeper: keeper,
Method: stakingABI.Methods["approveShares"],
Event: stakingABI.Events["ApproveShares"],
Keeper: keeper,
ApproveSharesABI: NewApproveSharesABI(),
}
}

Expand Down Expand Up @@ -66,41 +64,53 @@ func (m *ApproveSharesMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, e
return m.PackOutput(true)
}

func (m *ApproveSharesMethod) NewApproveSharesEvent(owner, spender common.Address, validator string, shares *big.Int) (data []byte, topic []common.Hash, err error) {
type ApproveSharesABI struct {
abi.Method
abi.Event
}

func NewApproveSharesABI() ApproveSharesABI {
return ApproveSharesABI{
Method: stakingABI.Methods["approveShares"],
Event: stakingABI.Events["ApproveShares"],
}
}

func (m ApproveSharesABI) NewApproveSharesEvent(owner, spender common.Address, validator string, shares *big.Int) (data []byte, topic []common.Hash, err error) {
data, topic, err = types.PackTopicData(m.Event, []common.Hash{owner.Hash(), spender.Hash()}, validator, shares)
if err != nil {
return nil, nil, err
}
return data, topic, nil
}

func (m *ApproveSharesMethod) PackInput(args fxstakingtypes.ApproveSharesArgs) ([]byte, error) {
func (m ApproveSharesABI) PackInput(args fxstakingtypes.ApproveSharesArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Spender, args.Shares)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *ApproveSharesMethod) UnpackInput(data []byte) (*fxstakingtypes.ApproveSharesArgs, error) {
func (m ApproveSharesABI) UnpackInput(data []byte) (*fxstakingtypes.ApproveSharesArgs, error) {
args := new(fxstakingtypes.ApproveSharesArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *ApproveSharesMethod) PackOutput(result bool) ([]byte, error) {
func (m ApproveSharesABI) PackOutput(result bool) ([]byte, error) {
return m.Method.Outputs.Pack(result)
}

func (m *ApproveSharesMethod) UnpackOutput(data []byte) (bool, error) {
func (m ApproveSharesABI) UnpackOutput(data []byte) (bool, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return false, err
}
return amount[0].(bool), nil
}

func (m *ApproveSharesMethod) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingApproveShares, error) {
func (m ApproveSharesABI) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingApproveShares, error) {
if log == nil {
return nil, errors.New("empty log")
}
Expand Down
8 changes: 4 additions & 4 deletions x/staking/precompile/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ type PrecompileTestSuite struct {
delegateV2Method *precompile.DelegateV2Method
delegationMethod *precompile.DelegationMethod
delegationRewardsMethod *precompile.DelegationRewardsMethod
redelegateMethodV2 *precompile.RedelegateMethodV2
redelegateV2Method *precompile.RedelegateV2Method
// slashingInfoMethod *precompile.SlashingInfoMethod
transferSharesMethod *precompile.TransferShares
transferFromSharesMethod *precompile.TransferFromShares
transferSharesMethod *precompile.TransferSharesMethod
transferFromSharesMethod *precompile.TransferFromSharesMethod
undelegateV2Method *precompile.UndelegateV2Method
validatorListMethod *precompile.ValidatorListMethod
withdrawMethod *precompile.WithdrawMethod
Expand Down Expand Up @@ -85,7 +85,7 @@ func (suite *PrecompileTestSuite) SetupTest() {
suite.delegateV2Method = precompile.NewDelegateV2Method(nil)
suite.delegationMethod = precompile.NewDelegationMethod(nil)
suite.delegationRewardsMethod = precompile.NewDelegationRewardsMethod(nil)
suite.redelegateMethodV2 = precompile.NewRedelegateV2Method(nil)
suite.redelegateV2Method = precompile.NewRedelegateV2Method(nil)
// suite.slashingInfoMethod = precompile.NewSlashingInfoMethod(nil)
suite.transferSharesMethod = precompile.NewTransferSharesMethod(nil)
suite.transferFromSharesMethod = precompile.NewTransferFromSharesMethod(nil)
Expand Down
34 changes: 22 additions & 12 deletions x/staking/precompile/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ import (

type DelegateV2Method struct {
*Keeper
abi.Method
abi.Event
DelegateV2ABI
}

func NewDelegateV2Method(keeper *Keeper) *DelegateV2Method {
return &DelegateV2Method{
Keeper: keeper,
Method: stakingABI.Methods["delegateV2"],
Event: stakingABI.Events["DelegateV2"],
Keeper: keeper,
DelegateV2ABI: NewDelegateV2ABI(),
}
}

Expand Down Expand Up @@ -71,41 +69,53 @@ func (m *DelegateV2Method) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
return m.PackOutput(true)
}

func (m *DelegateV2Method) NewDelegateEvent(sender common.Address, validator string, amount *big.Int) (data []byte, topic []common.Hash, err error) {
type DelegateV2ABI struct {
abi.Method
abi.Event
}

func NewDelegateV2ABI() DelegateV2ABI {
return DelegateV2ABI{
Method: stakingABI.Methods["delegateV2"],
Event: stakingABI.Events["DelegateV2"],
}
}

func (m DelegateV2ABI) NewDelegateEvent(sender common.Address, validator string, amount *big.Int) (data []byte, topic []common.Hash, err error) {
data, topic, err = types.PackTopicData(m.Event, []common.Hash{sender.Hash()}, validator, amount)
if err != nil {
return nil, nil, err
}
return data, topic, nil
}

func (m *DelegateV2Method) PackInput(args fxstakingtypes.DelegateV2Args) ([]byte, error) {
func (m DelegateV2ABI) PackInput(args fxstakingtypes.DelegateV2Args) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Amount)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *DelegateV2Method) UnpackInput(data []byte) (*fxstakingtypes.DelegateV2Args, error) {
func (m DelegateV2ABI) UnpackInput(data []byte) (*fxstakingtypes.DelegateV2Args, error) {
args := new(fxstakingtypes.DelegateV2Args)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *DelegateV2Method) PackOutput(result bool) ([]byte, error) {
func (m DelegateV2ABI) PackOutput(result bool) ([]byte, error) {
return m.Method.Outputs.Pack(result)
}

func (m *DelegateV2Method) UnpackOutput(data []byte) (bool, error) {
func (m DelegateV2ABI) UnpackOutput(data []byte) (bool, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return false, err
}
return amount[0].(bool), nil
}

func (m *DelegateV2Method) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingDelegateV2, error) {
func (m DelegateV2ABI) UnpackEvent(log *ethtypes.Log) (*fxcontract.IStakingDelegateV2, error) {
if log == nil {
return nil, errors.New("empty log")
}
Expand Down
26 changes: 18 additions & 8 deletions x/staking/precompile/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (

type DelegationMethod struct {
*Keeper
abi.Method
DelegationABI
}

func NewDelegationMethod(keeper *Keeper) *DelegationMethod {
return &DelegationMethod{
Keeper: keeper,
Method: stakingABI.Methods["delegation"],
Keeper: keeper,
DelegationABI: NewDelegationABI(),
}
}

Expand Down Expand Up @@ -63,25 +63,35 @@ func (m *DelegationMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byte, erro
return m.PackOutput(delegation.GetShares().TruncateInt().BigInt(), delegationAmt.TruncateInt().BigInt())
}

func (m *DelegationMethod) PackInput(args fxstakingtypes.DelegationArgs) ([]byte, error) {
type DelegationABI struct {
abi.Method
}

func NewDelegationABI() DelegationABI {
return DelegationABI{
Method: stakingABI.Methods["delegation"],
}
}

func (m DelegationABI) PackInput(args fxstakingtypes.DelegationArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Delegator)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *DelegationMethod) UnpackInput(data []byte) (*fxstakingtypes.DelegationArgs, error) {
func (m DelegationABI) UnpackInput(data []byte) (*fxstakingtypes.DelegationArgs, error) {
args := new(fxstakingtypes.DelegationArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *DelegationMethod) PackOutput(shares, amount *big.Int) ([]byte, error) {
func (m DelegationABI) PackOutput(shares, amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(shares, amount)
}

func (m *DelegationMethod) UnpackOutput(data []byte) (*big.Int, *big.Int, error) {
func (m DelegationABI) UnpackOutput(data []byte) (*big.Int, *big.Int, error) {
unpack, err := m.Method.Outputs.Unpack(data)
if err != nil {
return nil, nil, err
Expand Down
26 changes: 18 additions & 8 deletions x/staking/precompile/delegation_rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (

type DelegationRewardsMethod struct {
*Keeper
abi.Method
DelegationRewardsABI
}

func NewDelegationRewardsMethod(keeper *Keeper) *DelegationRewardsMethod {
return &DelegationRewardsMethod{
Keeper: keeper,
Method: stakingABI.Methods["delegationRewards"],
Keeper: keeper,
DelegationRewardsABI: NewDelegationRewardsABI(),
}
}

Expand Down Expand Up @@ -69,25 +69,35 @@ func (m *DelegationRewardsMethod) Run(evm *vm.EVM, contract *vm.Contract) ([]byt
return m.PackOutput(rewards.AmountOf(m.stakingDenom).TruncateInt().BigInt())
}

func (m *DelegationRewardsMethod) PackInput(args fxstakingtypes.DelegationRewardsArgs) ([]byte, error) {
type DelegationRewardsABI struct {
abi.Method
}

func NewDelegationRewardsABI() DelegationRewardsABI {
return DelegationRewardsABI{
Method: stakingABI.Methods["delegationRewards"],
}
}

func (m DelegationRewardsABI) PackInput(args fxstakingtypes.DelegationRewardsArgs) ([]byte, error) {
arguments, err := m.Method.Inputs.Pack(args.Validator, args.Delegator)
if err != nil {
return nil, err
}
return append(m.GetMethodId(), arguments...), nil
return append(m.Method.ID, arguments...), nil
}

func (m *DelegationRewardsMethod) UnpackInput(data []byte) (*fxstakingtypes.DelegationRewardsArgs, error) {
func (m DelegationRewardsABI) UnpackInput(data []byte) (*fxstakingtypes.DelegationRewardsArgs, error) {
args := new(fxstakingtypes.DelegationRewardsArgs)
err := types.ParseMethodArgs(m.Method, args, data[4:])
return args, err
}

func (m *DelegationRewardsMethod) PackOutput(amount *big.Int) ([]byte, error) {
func (m DelegationRewardsABI) PackOutput(amount *big.Int) ([]byte, error) {
return m.Method.Outputs.Pack(amount)
}

func (m *DelegationRewardsMethod) UnpackOutput(data []byte) (*big.Int, error) {
func (m DelegationRewardsABI) UnpackOutput(data []byte) (*big.Int, error) {
amount, err := m.Method.Outputs.Unpack(data)
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit daf4ce3

Please sign in to comment.