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

fix: use NST instead of LST #213

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
128 changes: 113 additions & 15 deletions precompiles/assets/assets_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package assets_test

import (
"fmt"
"math/big"
"strings"

sdkmath "cosmossdk.io/math"
assetsprecompile "github.com/ExocoreNetwork/exocore/precompiles/assets"
assetskeeper "github.com/ExocoreNetwork/exocore/x/assets/keeper"
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"
"github.com/ExocoreNetwork/exocore/x/oracle/types"
"github.com/evmos/evmos/v16/x/evm/statedb"

"github.com/ExocoreNetwork/exocore/app"
Expand Down Expand Up @@ -70,19 +74,25 @@ func paddingClientChainAddress(input []byte, outputLength int) []byte {
}

// TestRunDepositTo tests DepositOrWithdraw method through calling Run function..
func (s *AssetsPrecompileSuite) TestRunDepositLST() {
func (s *AssetsPrecompileSuite) TestRunDeposit() {
// assetsprecompile params for test
exocoreLzAppAddress := "0x3fC91A3afd70395Cd496C647d5a6CC9D4B2b7FAD"
exocoreLzAppEventTopic := "0xc6a377bfc4eb120024a8ac08eef205be16b817020812c73223e81d1bdb9708ec"
usdtAddress := paddingClientChainAddress(common.FromHex("0xdAC17F958D2ee523a2206206994597C13D831ec7"), assetstype.GeneralClientChainAddrLength)
usdcAddress := paddingClientChainAddress(common.FromHex("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"), assetstype.GeneralClientChainAddrLength)
clientChainLzID := 101
stakerAddr := paddingClientChainAddress(s.Address.Bytes(), assetstype.GeneralClientChainAddrLength)
stakerAddrStr := strings.ToLower(s.Address.String())
NSTAssetAddr := assetstypes.GenerateNSTAddr(s.ClientChains[0].AddressLength)
stakerID, assetID := assetstype.GetStakerIDAndAssetID(s.ClientChains[0].LayerZeroChainID, s.Address.Bytes(), NSTAssetAddr)

opAmount := big.NewInt(100)
opAmount32, _ := new(big.Int).SetString("32000000000000000000", 10)
assetAddr := usdtAddress
commonMalleate := func() (common.Address, []byte) {
assetAddrNST := paddingClientChainAddress(assetstype.GenerateNSTAddr(s.ClientChains[0].AddressLength), assetstype.GeneralClientChainAddrLength)
commonMalleate := func(method string, assetAddr []byte, opAmount *big.Int) (common.Address, []byte) {
input, err := s.precompile.Pack(
assetsprecompile.MethodDepositLST,
method,
uint32(clientChainLzID),
assetAddr,
stakerAddr,
Expand All @@ -92,6 +102,7 @@ func (s *AssetsPrecompileSuite) TestRunDepositLST() {
return s.Address, input
}
successRet, err := s.precompile.Methods[assetsprecompile.MethodDepositLST].Outputs.Pack(true, opAmount)
successRetNST, err := s.precompile.Methods[assetsprecompile.MethodDepositNST].Outputs.Pack(true, opAmount32)
s.Require().NoError(err)

testcases := []struct {
Expand All @@ -101,11 +112,12 @@ func (s *AssetsPrecompileSuite) TestRunDepositLST() {
expPass bool
errContains string
returnBytes []byte
extra func()
}{
{
name: "fail - depositTo transaction will fail because the exocoreLzAppAddress is mismatched",
malleate: func() (common.Address, []byte) {
return commonMalleate()
return commonMalleate(assetsprecompile.MethodDepositLST, assetAddr, opAmount)
},
readOnly: false,
expPass: false,
Expand All @@ -120,7 +132,7 @@ func (s *AssetsPrecompileSuite) TestRunDepositLST() {
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
return commonMalleate()
return commonMalleate(assetsprecompile.MethodDepositLST, assetAddr, opAmount)
},
readOnly: false,
expPass: false,
Expand All @@ -136,7 +148,7 @@ func (s *AssetsPrecompileSuite) TestRunDepositLST() {
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
assetAddr = usdcAddress
return commonMalleate()
return commonMalleate(assetsprecompile.MethodDepositLST, assetAddr, opAmount)
},
readOnly: false,
expPass: false,
Expand All @@ -152,12 +164,50 @@ func (s *AssetsPrecompileSuite) TestRunDepositLST() {
assetAddr = usdtAddress
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
return commonMalleate()
return commonMalleate(assetsprecompile.MethodDepositLST, assetAddr, opAmount)
},
returnBytes: successRet,
readOnly: false,
expPass: true,
},
{
name: "pass - depositNST",
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
assetAddr = usdtAddress
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
return commonMalleate(assetsprecompile.MethodDepositNST, assetAddrNST, opAmount32)
},
returnBytes: successRetNST,
readOnly: false,
expPass: true,
extra: func() {
amount32 := sdkmath.NewIntWithDecimal(32, 18)
// check depositNST successfully updated stakerAssetInfo in assets_module
stakerAssetInfo, _ := s.App.AssetsKeeper.GetStakerSpecifiedAssetInfo(s.Ctx, stakerID, assetID)
s.Equal(&assetstypes.StakerAssetInfo{
TotalDepositAmount: amount32,
WithdrawableAmount: amount32,
PendingUndelegationAmount: sdkmath.ZeroInt(),
}, stakerAssetInfo)

// check depositNST successfully updated stakerList in oracle_module
stakerList := s.App.OracleKeeper.GetStakerList(s.Ctx, assetID)
s.Equal(stakerList.StakerAddrs[0], stakerAddrStr)
// check depositNST successfully update stakerInfo with correct validatorPubkey
stakerInfo := s.App.OracleKeeper.GetStakerInfo(s.Ctx, assetID, stakerAddrStr)
s.Equal(types.BalanceInfo{
Block: 1,
RoundID: 0,
Change: types.Action_ACTION_DEPOSIT,
Balance: 32,
}, *stakerInfo.BalanceList[0])
},
},
}

for _, tc := range testcases {
Expand Down Expand Up @@ -235,6 +285,10 @@ func (s *AssetsPrecompileSuite) TestRunDepositLST() {
s.Require().True(ok)
s.Require().False(success)
}
if tc.extra != nil {
// run extra logic/checking for this test case
tc.extra()
}
})
}
}
Expand All @@ -247,24 +301,29 @@ func (s *AssetsPrecompileSuite) TestRunWithdrawPrincipal() {
clientChainLzID := 101
withdrawAmount := big.NewInt(10)
depositAmount := big.NewInt(100)
amount32, _ := new(big.Int).SetString("32000000000000000000", 10)
assetAddr := paddingClientChainAddress(usdtAddress, assetstype.GeneralClientChainAddrLength)
depositAsset := func(staker []byte, depositAmount sdkmath.Int) {
assetAddrNST := paddingClientChainAddress(assetstype.GenerateNSTAddr(s.ClientChains[0].AddressLength), assetstype.GeneralClientChainAddrLength)
NSTAddress := assetstype.GenerateNSTAddr(s.ClientChains[0].AddressLength)
depositAsset := func(staker []byte, depositAmount sdkmath.Int, assetAddress []byte) {
// deposit asset for withdraw test
params := &assetskeeper.DepositWithdrawParams{
ClientChainLzID: 101,
Action: assetstype.DepositLST,
StakerAddress: staker,
AssetsAddress: usdtAddress,
OpAmount: depositAmount,
// AssetsAddress: usdtAddress,
AssetsAddress: assetAddress,
OpAmount: depositAmount,
}
err := s.App.AssetsKeeper.PerformDepositOrWithdraw(s.Ctx, params)
fmt.Println("Debug---", assetAddress, len(assetAddress))
s.Require().NoError(err)
}

commonMalleate := func() (common.Address, []byte) {
commonMalleate := func(method string, assetAddr []byte, withdrawAmount *big.Int) (common.Address, []byte) {
// Prepare the call input for withdraw test
input, err := s.precompile.Pack(
assetsprecompile.MethodWithdrawLST,
method,
uint32(clientChainLzID),
assetAddr,
paddingClientChainAddress(s.Address.Bytes(), assetstype.GeneralClientChainAddrLength),
Expand All @@ -274,6 +333,7 @@ func (s *AssetsPrecompileSuite) TestRunWithdrawPrincipal() {
return s.Address, input
}
successRet, err := s.precompile.Methods[assetsprecompile.MethodWithdrawLST].Outputs.Pack(true, new(big.Int).Sub(depositAmount, withdrawAmount))
successRetNST, err := s.precompile.Methods[assetsprecompile.MethodWithdrawNST].Outputs.Pack(true, big.NewInt(0))
s.Require().NoError(err)
testcases := []struct {
name string
Expand All @@ -282,23 +342,58 @@ func (s *AssetsPrecompileSuite) TestRunWithdrawPrincipal() {
expPass bool
errContains string
returnBytes []byte
extra func()
}{
{
name: "pass - withdraw via pre-compiles",
name: "pass - withdraw via pre-compiles, LST",
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
depositAsset(s.Address.Bytes(), sdkmath.NewIntFromBigInt(depositAmount))
return commonMalleate()
depositAsset(s.Address.Bytes(), sdkmath.NewIntFromBigInt(depositAmount), usdtAddress)
return commonMalleate(assetsprecompile.MethodWithdrawLST, assetAddr, withdrawAmount)
},
returnBytes: successRet,
readOnly: false,
expPass: true,
},
{
name: "pass - withdraw via pre-compiles, NST",
malleate: func() (common.Address, []byte) {
depositModuleParam := &assetstype.Params{
ExocoreLzAppAddress: s.Address.String(),
ExocoreLzAppEventTopic: exocoreLzAppEventTopic,
}
err := s.App.AssetsKeeper.SetParams(s.Ctx, depositModuleParam)
s.Require().NoError(err)
depositAsset(s.Address.Bytes(), sdkmath.NewIntFromBigInt(amount32), NSTAddress)
return commonMalleate(assetsprecompile.MethodWithdrawLST, assetAddrNST, amount32)
},
returnBytes: successRetNST,
readOnly: false,
expPass: true,
extra: func() {

stakerID, assetID := assetstype.GetStakerIDAndAssetID(s.ClientChains[0].LayerZeroChainID, s.Address.Bytes(), NSTAddress)
// check depositNST successfully updated stakerAssetInfo in assets_module
stakerAssetInfo, _ := s.App.AssetsKeeper.GetStakerSpecifiedAssetInfo(s.Ctx, stakerID, assetID)
s.Equal(&assetstypes.StakerAssetInfo{
TotalDepositAmount: sdkmath.ZeroInt(),
WithdrawableAmount: sdkmath.ZeroInt(),
PendingUndelegationAmount: sdkmath.ZeroInt(),
}, stakerAssetInfo)

// check depositNST successfully updated stakerList in oracle_module
stakerList := s.App.OracleKeeper.GetStakerList(s.Ctx, assetID)
s.Equal(len(stakerList.StakerAddrs), 0)
// check depositNST successfully update stakerInfo with correct validatorPubkey
stakerInfo := s.App.OracleKeeper.GetStakerInfo(s.Ctx, assetID, s.Address.String())
s.Equal(0, len(stakerInfo.BalanceList))
},
},
}
for _, tc := range testcases {
tc := tc
Expand Down Expand Up @@ -366,6 +461,9 @@ func (s *AssetsPrecompileSuite) TestRunWithdrawPrincipal() {
s.Require().Nil(bz, "expected returned bytes to be nil")
s.Require().ErrorContains(err, tc.errContains)
}
if tc.extra != nil {
tc.extra()
}
})
}
}
Expand Down
14 changes: 14 additions & 0 deletions precompiles/assets/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package assets_test
import (
"testing"

"cosmossdk.io/math"
"github.com/ExocoreNetwork/exocore/precompiles/assets"
assetstypes "github.com/ExocoreNetwork/exocore/x/assets/types"

"github.com/ExocoreNetwork/exocore/testutil"

Expand Down Expand Up @@ -35,4 +37,16 @@ func (s *AssetsPrecompileSuite) SetupTest() {
precompile, err := assets.NewPrecompile(s.App.AssetsKeeper, s.App.AuthzKeeper)
s.Require().NoError(err)
s.precompile = precompile
depositAmountNST := math.NewInt(32)
s.App.AssetsKeeper.SetStakingAssetInfo(s.Ctx, &assetstypes.StakingAssetInfo{
AssetBasicInfo: assetstypes.AssetInfo{
Name: "Native Restaking ETH",
Symbol: "NSTETH",
Address: "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
Decimals: 18,
LayerZeroChainID: s.ClientChains[0].LayerZeroChainID,
MetaInfo: "native restaking token",
},
StakingTotalAmount: depositAmountNST,
})
}
2 changes: 1 addition & 1 deletion precompiles/assets/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p Precompile) DepositOrWithdraw(
if depositWithdrawParams.Action == assetstypes.DepositNST ||
depositWithdrawParams.Action == assetstypes.WithdrawNST {
opAmount := depositWithdrawParams.OpAmount
if depositWithdrawParams.Action == assetstypes.WithdrawLST {
if depositWithdrawParams.Action == assetstypes.WithdrawNST {
opAmount = opAmount.Neg()
}
_, assetID := assetstypes.GetStakerIDAndAssetID(depositWithdrawParams.ClientChainLzID,
Expand Down
6 changes: 3 additions & 3 deletions precompiles/assets/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p Precompile) DepositWithdrawParams(ctx sdk.Context, method *abi.Method, a
case MethodDepositLST, MethodWithdrawLST:
// the length of client chain address inputted by caller is 32, so we need to check the length and remove the padding according to the actual length.
assetAddr, ok := args[1].([]byte)
if !ok || assetAddr == nil {
if !ok || len(assetAddr) == 0 {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", args[1])
}
// #nosec G115
Expand All @@ -64,7 +64,7 @@ func (p Precompile) DepositWithdrawParams(ctx sdk.Context, method *abi.Method, a
}
case MethodDepositNST, MethodWithdrawNST:
validatorPubkey, ok := args[1].([]byte)
if !ok || validatorPubkey == nil {
if !ok || len(validatorPubkey) == 0 {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 1, "[]byte", args[1])
}
// generate the virtual address for native restaking asset
Expand All @@ -81,7 +81,7 @@ func (p Precompile) DepositWithdrawParams(ctx sdk.Context, method *abi.Method, a
}

stakerAddr, ok := args[2].([]byte)
if !ok || stakerAddr == nil {
if !ok || len(stakerAddr) == 0 {
return nil, fmt.Errorf(exocmn.ErrContractInputParaOrType, 2, "[]byte", args[2])
}
// #nosec G115
Expand Down
Loading