-
Notifications
You must be signed in to change notification settings - Fork 818
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: staking precompile delegation and stakingpool queries #1356
Draft
kyriediculous
wants to merge
4
commits into
sei-protocol:evm
Choose a base branch
from
kyriediculous:evm
base: evm
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1139fed
feat: staking precompile delegation and stakingpool queries
kyriediculous 0cb705a
feat: improve undelegation precompiles
kyriediculous 2632f15
fix: simplify delegate/undelegate
kyriediculous 8cc35f8
fix: get UnbondingDelegations for delegator/validator pair, and get U…
kyriediculous File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -14,9 +14,13 @@ import ( | |
) | ||
|
||
const ( | ||
DelegateMethod = "delegate" | ||
RedelegateMethod = "redelegate" | ||
UndelegateMethod = "undelegate" | ||
DelegateMethod = "delegate" | ||
RedelegateMethod = "redelegate" | ||
UndelegateMethod = "undelegate" | ||
DelegationQuery = "getDelegation" | ||
StakingPoolQuery = "getStakingPool" | ||
UnbondingDelegationQuery = "getUnbondingDelegation" | ||
UnbondingDelegationByUnbondingIDQuery = "getUnbondingDelegationByUnbondingId" | ||
) | ||
|
||
const ( | ||
|
@@ -49,9 +53,13 @@ type Precompile struct { | |
evmKeeper pcommon.EVMKeeper | ||
address common.Address | ||
|
||
DelegateID []byte | ||
RedelegateID []byte | ||
UndelegateID []byte | ||
DelegateID []byte | ||
RedelegateID []byte | ||
UndelegateID []byte | ||
DelegationID []byte | ||
StakingPoolID []byte | ||
UnbondingDelegationID []byte | ||
UnbondingDelegationByUnbondingID []byte | ||
} | ||
|
||
func NewPrecompile(stakingKeeper pcommon.StakingKeeper, evmKeeper pcommon.EVMKeeper) (*Precompile, error) { | ||
|
@@ -72,7 +80,16 @@ func NewPrecompile(stakingKeeper pcommon.StakingKeeper, evmKeeper pcommon.EVMKee | |
p.RedelegateID = m.ID | ||
case UndelegateMethod: | ||
p.UndelegateID = m.ID | ||
case DelegationQuery: | ||
p.DelegationID = m.ID | ||
case StakingPoolQuery: | ||
p.StakingPoolID = m.ID | ||
case UnbondingDelegationQuery: | ||
p.UnbondingDelegationID = m.ID | ||
case UnbondingDelegationByUnbondingIDQuery: | ||
p.UnbondingDelegationByUnbondingID = m.ID | ||
} | ||
|
||
} | ||
|
||
return p, nil | ||
|
@@ -88,6 +105,12 @@ func (p Precompile) RequiredGas(input []byte) uint64 { | |
return 70000 | ||
} else if bytes.Equal(methodID, p.UndelegateID) { | ||
return 50000 | ||
} else if bytes.Equal(methodID, p.DelegationID) { | ||
return 5000 | ||
} else if bytes.Equal(methodID, p.StakingPoolID) { | ||
return 5000 | ||
} else if bytes.Equal(methodID, p.UnbondingDelegationID) { | ||
return 10000 | ||
} | ||
panic("unknown method") | ||
} | ||
|
@@ -109,31 +132,40 @@ func (p Precompile) Run(evm *vm.EVM, caller common.Address, input []byte) (bz [] | |
return p.redelegate(ctx, method, caller, args) | ||
case UndelegateMethod: | ||
return p.undelegate(ctx, method, caller, args) | ||
case DelegationQuery: | ||
return p.getDelegation(ctx, method, caller, args) | ||
case StakingPoolQuery: | ||
return p.getStakingPool(ctx, method, args) | ||
case UnbondingDelegationQuery: | ||
return p.getUnbondingDelegation(ctx, method, args) | ||
case UnbondingDelegationByUnbondingIDQuery: | ||
return p.getUnbondingDelegationByUnbondingId(ctx, method, args) | ||
default: | ||
return | ||
} | ||
return | ||
} | ||
|
||
func (p Precompile) delegate(ctx sdk.Context, method *abi.Method, caller common.Address, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 2) | ||
delegator := p.evmKeeper.GetSeiAddressOrDefault(ctx, caller) | ||
validatorBech32 := args[0].(string) | ||
validatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
amount := args[1].(*big.Int) | ||
_, err := p.stakingKeeper.Delegate(sdk.WrapSDKContext(ctx), &stakingtypes.MsgDelegate{ | ||
res, err := p.stakingKeeper.Delegate(sdk.WrapSDKContext(ctx), &stakingtypes.MsgDelegate{ | ||
DelegatorAddress: delegator.String(), | ||
ValidatorAddress: validatorBech32, | ||
Amount: sdk.NewCoin(p.evmKeeper.GetBaseDenom(ctx), sdk.NewIntFromBigInt(amount)), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return method.Outputs.Pack(true) | ||
return method.Outputs.Pack(res.Shares) | ||
} | ||
|
||
func (p Precompile) redelegate(ctx sdk.Context, method *abi.Method, caller common.Address, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 3) | ||
delegator := p.evmKeeper.GetSeiAddressOrDefault(ctx, caller) | ||
srcValidatorBech32 := args[0].(string) | ||
dstValidatorBech32 := args[1].(string) | ||
srcValidatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
dstValidatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[1].(common.Address)).String() | ||
amount := args[2].(*big.Int) | ||
_, err := p.stakingKeeper.BeginRedelegate(sdk.WrapSDKContext(ctx), &stakingtypes.MsgBeginRedelegate{ | ||
DelegatorAddress: delegator.String(), | ||
|
@@ -150,15 +182,69 @@ func (p Precompile) redelegate(ctx sdk.Context, method *abi.Method, caller commo | |
func (p Precompile) undelegate(ctx sdk.Context, method *abi.Method, caller common.Address, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 2) | ||
delegator := p.evmKeeper.GetSeiAddressOrDefault(ctx, caller) | ||
validatorBech32 := args[0].(string) | ||
validatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
amount := args[1].(*big.Int) | ||
_, err := p.stakingKeeper.Undelegate(sdk.WrapSDKContext(ctx), &stakingtypes.MsgUndelegate{ | ||
res, err := p.stakingKeeper.Undelegate(sdk.WrapSDKContext(ctx), &stakingtypes.MsgUndelegate{ | ||
DelegatorAddress: delegator.String(), | ||
ValidatorAddress: validatorBech32, | ||
Amount: sdk.NewCoin(p.evmKeeper.GetBaseDenom(ctx), sdk.NewIntFromBigInt(amount)), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return method.Outputs.Pack(true) | ||
return method.Outputs.Pack(res.UnbondingId) | ||
} | ||
|
||
func (p Precompile) getDelegation(ctx sdk.Context, method *abi.Method, caller common.Address, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 2) | ||
delegator := p.evmKeeper.GetSeiAddressOrDefault(ctx, caller) | ||
validatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
delegation, err := p.stakingKeeper.Delegation(sdk.WrapSDKContext(ctx), delegator.String(), validatorBech32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return method.Outputs.Pack(delegation.DelegationResponse.Balance) | ||
} | ||
|
||
func (p Precompile) getStakingPool(ctx sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 1) | ||
validatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
validator, err := p.stakingKeeper.Validator(sdk.WrapSDKContext(ctx), validatorBech32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return method.Outputs.Pack(struct { | ||
TotalShares *big.Int | ||
TotalTokens *big.Int | ||
Status stakingtypes.BondStatus | ||
Jailed bool | ||
}{ | ||
validator.Validator.DelegatorShares.BigInt(), | ||
validator.Validator.Tokens.BigInt(), | ||
validator.Validator.Status, | ||
validator.Validator.Jailed, | ||
}) | ||
} | ||
|
||
func (p Precompile) getUnbondingDelegation(ctx sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 2) | ||
delegatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
validatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[1].(common.Address)).String() | ||
unbonding, err := p.stakingKeeper.UnbondingDelegation(sdk.WrapSDKContext(ctx), delegatorBech32, validatorBech32) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return method.Outputs.Pack(unbonding) | ||
} | ||
|
||
func (p Precompile) getUnbondingDelegationByUnbondingId(ctx sdk.Context, method *abi.Method, args []interface{}) ([]byte, error) { | ||
pcommon.AssertArgsLength(args, 1) | ||
delegatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[0].(common.Address)).String() | ||
validatorBech32 := p.evmKeeper.GetSeiAddressOrDefault(ctx, args[1].(common.Address)).String() | ||
unbondingId := args[2].(uint64) | ||
unbondingEntry, err := p.stakingKeeper.UnbondingDelegationByUnbondingId(sdk.WrapSDKContext(ctx), delegatorBech32, validatorBech32, unbondingId) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return method.Outputs.Pack(unbondingEntry) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validator address is different from normal account address and doesn't have associated EVM address, so we can't change this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the length of these ?
I changed these do
address
mainly because they were previouslystring
which in solidity is really a dynamic bytes array.So if the validator addresses are 32 Bytes or shorter we can use a normal value type like
bytes32
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kind bump @codchen