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(oracle): support update for eth-native-restaking balance change #170

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ func NewExocoreApp(
app.AssetsKeeper,
delegationTypes.VirtualSlashKeeper{},
&app.OperatorKeeper,
&app.OracleKeeper,
)

// the dogfood module is the first AVS. it receives slashing calls from either x/slashing
Expand Down
44 changes: 44 additions & 0 deletions proto/exocore/oracle/v1/native_token.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
syntax = "proto3";

package exocore.oracle.v1;

import "gogoproto/gogo.proto";

option go_package = "github.com/ExocoreNetwork/exocore/x/oracle/types";

message PriceInfo {
string price = 1[(gogoproto.customtype)="cosmossdk.io/math.LegacyDec",(gogoproto.nullable) = false ];
uint64 block = 2;
uint64 round_id = 3 [(gogoproto.customname)="RoundID"];
}

message OperatorInfo {
string operator_addr = 1;
string total_amount = 2[(gogoproto.customtype)="cosmossdk.io/math.Int",(gogoproto.nullable) = false];
repeated PriceInfo price_list = 3;
}

message StakerInfo {
string staker_addr = 1;
int64 staker_index = 2;
repeated uint64 validator_indexs = 3;
string total_deposit = 4[(gogoproto.customtype)="cosmossdk.io/math.Int",(gogoproto.nullable) = false];
repeated PriceInfo price_list = 5;
}

message NativeTokenPrice {
string token = 1;
repeated PriceInfo prece_list = 2;
}
Comment on lines +29 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the typo in the field name.

There is a typo in the field name prece_list. It should be price_list.

Apply this diff to fix the typo:

 message NativeTokenPrice {
   string token = 1;
-  repeated PriceInfo prece_list = 2;
+  repeated PriceInfo price_list = 2;
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
message NativeTokenPrice {
string token = 1;
repeated PriceInfo prece_list = 2;
}
message NativeTokenPrice {
string token = 1;
repeated PriceInfo price_list = 2;
}

Comment on lines +29 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the typo in the field name.

There is a typo in the field name prece_list. It should be price_list.

Apply this diff to fix the typo:

 message NativeTokenPrice {
   string token = 1;
-  repeated PriceInfo prece_list = 2;
+  repeated PriceInfo price_list = 2;
 }
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
message NativeTokenPrice {
string token = 1;
repeated PriceInfo prece_list = 2;
}
message NativeTokenPrice {
string token = 1;
repeated PriceInfo price_list = 2;
}


message StakerList {
repeated string staker_addrs = 1;
}

message DelegationInfo {
string operator_addr = 1;
string amount = 2[(gogoproto.customtype)="cosmossdk.io/math.Int",(gogoproto.nullable) = false];
}
message StakerDelegationInfo {
repeated DelegationInfo delegations = 1;
}
8 changes: 8 additions & 0 deletions x/assets/keeper/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/ExocoreNetwork/exocore/x/assets/types"

Check failure on line 9 in x/assets/keeper/bank.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

ST1019: package "github.com/ExocoreNetwork/exocore/x/assets/types" is being imported more than once (stylecheck)
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"

Check failure on line 10 in x/assets/keeper/bank.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

ST1019(related information): other import of "github.com/ExocoreNetwork/exocore/x/assets/types" (stylecheck)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove duplicate import.

The package github.com/ExocoreNetwork/exocore/x/assets/types is being imported more than once. Remove the duplicate import to clean up the code.

Apply this diff to remove the duplicate import:

-	"github.com/ExocoreNetwork/exocore/x/assets/types"
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"github.com/ExocoreNetwork/exocore/x/assets/types"
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"
Tools
GitHub Check: Run golangci-lint

[failure] 9-9:
ST1019: package "github.com/ExocoreNetwork/exocore/x/assets/types" is being imported more than once (stylecheck)


[failure] 10-10:
ST1019(related information): other import of "github.com/ExocoreNetwork/exocore/x/assets/types" (stylecheck)

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common/hexutil"
)

type DepositWithdrawParams struct {
Expand Down Expand Up @@ -43,6 +46,11 @@
return errorsmod.Wrapf(assetstypes.ErrInvalidOperationType, "the operation type is: %v", params.Action)
}

if types.IsNativeToken(assetID) {
// TODO: we skip check for case like withdraw amount>withdrawable is fine since it will fail for later check and the state will be rollback
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: Address the TODO comment.

The TODO comment indicates a potential edge case regarding withdrawal amounts. Ensure that this edge case is handled appropriately in the future.

Do you want me to help address this TODO comment or open a GitHub issue to track this task?

actualOpAmount = k.UpdateNativeTokenByDepositOrWithdraw(ctx, assetID, hexutil.Encode(params.StakerAddress), params.OpAmount)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: Add tests for the new logic.

Ensure that the UpdateNativeTokenByDepositOrWithdraw function is thoroughly tested to verify its correctness.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?


changeAmount := assetstypes.DeltaStakerSingleAsset{
TotalDepositAmount: actualOpAmount,
WithdrawableAmount: actualOpAmount,
Expand Down
2 changes: 2 additions & 0 deletions x/assets/types/expected_keepers.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package types

import (
sdkmath "cosmossdk.io/math"
oracletypes "github.com/ExocoreNetwork/exocore/x/oracle/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type OracleKeeper interface {
GetSpecifiedAssetsPrice(ctx sdk.Context, assetID string) (oracletypes.Price, error)
UpdateNativeTokenByDepositOrWithdraw(ctx sdk.Context, assetID, stakerAddr string, amount sdkmath.Int) sdkmath.Int
}
22 changes: 22 additions & 0 deletions x/assets/types/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ const (
MaxChainTokenMetaInfoLength = 200

MinClientChainAddrLength = 20

// TODO: update before merge
NativeETHAssetID = "0x01_0x01"
ETHAssetID = "0x02_0x01"
)

var NativeTokenBaseMap = map[string]string{
NativeETHAssetID: ETHAssetID,
}

const (
Deposit CrossChainOpType = iota
WithdrawPrincipal
Expand Down Expand Up @@ -162,3 +170,17 @@ func UpdateAssetDecValue(valueToUpdate *math.LegacyDec, changeValue *math.Legacy
}
return nil
}

func IsNativeToken(assetID string) bool {
// TODO: native token should be a list which able to config, we currently only support ETH, so it's fine for now
return assetID == NativeETHAssetID
}

func GetNativeTokenAssetIDs() []string {
// TODO: we currently have native_eth only
return []string{NativeETHAssetID}
}

func GetBaseTokenForNativeToken(assetID string) string {
return NativeTokenBaseMap[assetID]
}
1 change: 1 addition & 0 deletions x/delegation/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
}
// and the other is the fact that it matures at the next block
err = k.StorePendingUndelegationRecord(ctx, recordKey, record)
//TODO: remove previous index with currentHeight for pendingUndelegationRecord

Check failure on line 49 in x/delegation/keeper/abci.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

File is not `gofumpt`-ed (gofumpt)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acknowledged TODO comment.

The TODO comment about removing the previous index with currentHeight for pendingUndelegationRecord is noted. If you need assistance with this task, please let me know.

Do you want me to generate the code for this task or open a GitHub issue to track it?

Tools
GitHub Check: Run golangci-lint

[failure] 49-49:
File is not gofumpt-ed (gofumpt)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: Address the TODO comment.

The TODO comment indicates a planned enhancement to remove the previous index with the current height for pendingUndelegationRecord. Ensure this enhancement is tracked and implemented.

Do you want me to open a GitHub issue to track this task?

Tools
golangci-lint

49-49: File is not gofumpt-ed

(gofumpt)

GitHub Check: Run golangci-lint

[failure] 49-49:
File is not gofumpt-ed (gofumpt)

if err != nil {
panic(err)
}
Expand Down
10 changes: 9 additions & 1 deletion x/delegation/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"
delegationtype "github.com/ExocoreNetwork/exocore/x/delegation/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/ethereum/go-ethereum/common/hexutil"
)

// DelegateTo : It doesn't need to check the active status of the operator in middlewares when
Expand Down Expand Up @@ -46,7 +48,9 @@ func (k *Keeper) delegateTo(
if err != nil {
return err
}

if assetstype.IsNativeToken(assetID) {
params.OpAmount = k.oracleKeeper.UpdateNativeTokenByDelegation(ctx, assetID, params.OperatorAddress.String(), hexutil.Encode(params.StakerAddress), params.OpAmount)
}
if info.WithdrawableAmount.LT(params.OpAmount) {
return errorsmod.Wrap(delegationtype.ErrDelegationAmountTooBig, fmt.Sprintf("the opAmount is:%s the WithdrawableAmount amount is:%s", params.OpAmount, info.WithdrawableAmount))
}
Expand Down Expand Up @@ -116,6 +120,10 @@ func (k *Keeper) UndelegateFrom(ctx sdk.Context, params *delegationtype.Delegati
// get staker delegation state, then check the validation of Undelegation amount
stakerID, assetID := assetstype.GetStakeIDAndAssetID(params.ClientChainID, params.StakerAddress, params.AssetsAddress)

if assetstype.IsNativeToken(assetID) {
params.OpAmount = k.oracleKeeper.UpdateNativeTokenByDelegation(ctx, assetID, params.OperatorAddress.String(), hexutil.Encode(params.StakerAddress), params.OpAmount.Neg()).Neg()
}

// verify the undelegation amount
share, err := k.ValidateUndelegationAmount(ctx, params.OperatorAddress, stakerID, assetID, params.OpAmount)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions x/delegation/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Keeper struct {
assetsKeeper delegationtype.AssetsKeeper
slashKeeper delegationtype.SlashKeeper
operatorKeeper delegationtype.OperatorKeeper
oracleKeeper delegationtype.OracleKeeper
hooks delegationtype.DelegationHooks
}

Expand All @@ -26,13 +27,15 @@ func NewKeeper(
assetsKeeper delegationtype.AssetsKeeper,
slashKeeper delegationtype.SlashKeeper,
operatorKeeper delegationtype.OperatorKeeper,
oracleKeeper delegationtype.OracleKeeper,
) Keeper {
return Keeper{
storeKey: storeKey,
cdc: cdc,
assetsKeeper: assetsKeeper,
slashKeeper: slashKeeper,
operatorKeeper: operatorKeeper,
oracleKeeper: oracleKeeper,
}
}

Expand Down
4 changes: 4 additions & 0 deletions x/delegation/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,7 @@ type AssetsKeeper interface {

ClientChainExists(ctx sdk.Context, index uint64) bool
}

type OracleKeeper interface {
UpdateNativeTokenByDelegation(ctx sdk.Context, assetID, operatorAddr, stakerAddr string, amountOriginal sdkmath.Int) (amount sdkmath.Int)
}
4 changes: 3 additions & 1 deletion x/delegation/types/hooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import sdk "github.com/cosmos/cosmos-sdk/types"
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ DelegationHooks = &MultiDelegationHooks{}

Expand Down
7 changes: 7 additions & 0 deletions x/operator/keeper/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package keeper

import (
"errors"
"strings"

sdkmath "cosmossdk.io/math"
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"
operatortypes "github.com/ExocoreNetwork/exocore/x/operator/types"
oracletypes "github.com/ExocoreNetwork/exocore/x/oracle/types"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -62,6 +64,11 @@ func (k *Keeper) UpdateVotingPower(ctx sdk.Context, avsAddr string) error {
SelfUSDValue: sdkmath.LegacyNewDec(0),
ActiveUSDValue: sdkmath.LegacyNewDec(0),
}
for _, assetID := range assetstypes.GetNativeTokenAssetIDs() {
if _, ok := prices[assetID]; ok {
prices[assetID], err = k.oracleKeeper.GetSpecifiedAssetsPrice(ctx, strings.Join([]string{assetID, operator}, "_"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the returned error be addressed?

}
}
Comment on lines +67 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: Add tests for the new logic.

Ensure that the asset price retrieval logic is thoroughly tested to verify its correctness.

Do you want me to generate the unit testing code or open a GitHub issue to track this task?

stakingInfo, err := k.CalculateUSDValueForOperator(ctx, false, operator, assets, decimals, prices)
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions x/operator/keeper/usd_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"errors"
"fmt"
"strings"

assetstype "github.com/ExocoreNetwork/exocore/x/assets/types"
delegationkeeper "github.com/ExocoreNetwork/exocore/x/delegation/keeper"
Expand Down Expand Up @@ -299,6 +300,9 @@ func (k *Keeper) CalculateUSDValueForOperator(
if isForSlash {
// when calculated the USD value for slashing, the input prices map is null
// so the price needs to be retrieved here
if assetstype.IsNativeToken(assetID) {
assetID = strings.Join([]string{assetID, operator}, "_")
}
price, err = k.oracleKeeper.GetSpecifiedAssetsPrice(ctx, assetID)
if err != nil {
// TODO: when assetID is not registered in oracle module, this error will finally lead to panic
Expand Down
Loading
Loading