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

refactor: add evm amount calculation for tss migration #1619

Merged
merged 8 commits into from
Jan 26, 2024
Merged
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
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

* [1585](https://github.com/zeta-chain/node/pull/1585) - Updated release instructions

### Refactoring
* [1619](https://github.com/zeta-chain/node/pull/1619) - Add evm fee calculation to tss migration of evm chains

## Version: v12.0.0

### Breaking Changes
Expand Down
7 changes: 7 additions & 0 deletions common/gas_limits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package common

const (
EVMSend = 21000
lumtis marked this conversation as resolved.
Show resolved Hide resolved
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
// Todo Move gas limits from zeta-client to this file
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
// https://github.com/zeta-chain/node/issues/1606
)
18 changes: 18 additions & 0 deletions x/crosschain/keeper/msg_server_migrate_tss_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ func (k Keeper) MigrateTSSFundsForChain(ctx sdk.Context, chainID int64, amount s
}
cctx.InboundTxParams.Sender = ethAddressOld.String()
cctx.GetCurrentOutTxParam().Receiver = ethAddressNew.String()
// Tss migration is a send transaction, so the gas limit is set to 21000
cctx.GetCurrentOutTxParam().OutboundTxGasLimit = common.EVMSend
// Multiple current gas price with standard multiplier to add some buffer
multiplier, err := sdk.NewDecFromStr(types.TssMigrationGasMultiplierEVM)
if err != nil {
return err
}
gasPrice, err := sdk.NewDecFromStr(medianGasPrice.String())
if err != nil {
return err
}
newGasPrice := gasPrice.Mul(multiplier)
cctx.GetCurrentOutTxParam().OutboundTxGasPrice = newGasPrice.TruncateInt().String()
evmFee := sdkmath.NewUint(cctx.GetCurrentOutTxParam().OutboundTxGasLimit).Mul(medianGasPrice)
if evmFee.GT(amount) {
return errorsmod.Wrap(types.ErrInsufficientFundsTssMigration, fmt.Sprintf("insufficient funds to pay for gas fee, amount: %s, gas fee: %s, chainid: %d", amount.String(), evmFee.String(), chainID))
}
cctx.GetCurrentOutTxParam().Amount = amount.Sub(evmFee)
}
// Set the sender and receiver addresses for Bitcoin chain
if common.IsBitcoinChain(chainID) {
Expand Down
30 changes: 24 additions & 6 deletions x/crosschain/keeper/msg_server_migrate_tss_funds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestMsgServer_MigrateTssFunds(t *testing.T) {
setAdminPolicies(ctx, zk, admin)
msgServer := keeper.NewMsgServerImpl(*k)
chain := getValidEthChain(t)
amount := sdkmath.NewUint(100)
amount := sdkmath.NewUintFromString("10000000000000000000")
indexString, _ := setupTssMigrationParams(zk, k, ctx, *chain, amount, true, true)
_, err := msgServer.MigrateTssFunds(ctx, &crosschaintypes.MsgMigrateTssFunds{
Creator: admin,
Expand All @@ -35,20 +35,38 @@ func TestMsgServer_MigrateTssFunds(t *testing.T) {
_, found := k.GetCrossChainTx(ctx, index)
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
assert.True(t, found)
})
t.Run("not enough funds in tss address for migration", func(t *testing.T) {
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
msgServer := keeper.NewMsgServerImpl(*k)
chain := getValidEthChain(t)
amount := sdkmath.NewUintFromString("100")
indexString, _ := setupTssMigrationParams(zk, k, ctx, *chain, amount, true, true)
_, err := msgServer.MigrateTssFunds(ctx, &crosschaintypes.MsgMigrateTssFunds{
Creator: admin,
ChainId: chain.ChainId,
Amount: amount,
})
assert.ErrorContains(t, err, crosschaintypes.ErrCannotMigrateTssFunds.Error())
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
hash := crypto.Keccak256Hash([]byte(indexString))
index := hash.Hex()
_, found := k.GetCrossChainTx(ctx, index)
assert.False(t, found)
})
t.Run("unable to migrate funds if new TSS is not created ", func(t *testing.T) {
k, ctx, _, zk := keepertest.CrosschainKeeper(t)
admin := sample.AccAddress()
setAdminPolicies(ctx, zk, admin)
msgServer := keeper.NewMsgServerImpl(*k)
chain := getValidEthChain(t)
amount := sdkmath.NewUint(100)
amount := sdkmath.NewUintFromString("10000000000000000000")
indexString, _ := setupTssMigrationParams(zk, k, ctx, *chain, amount, false, true)
_, err := msgServer.MigrateTssFunds(ctx, &crosschaintypes.MsgMigrateTssFunds{
Creator: admin,
ChainId: chain.ChainId,
Amount: amount,
})
assert.ErrorIs(t, err, crosschaintypes.ErrCannotMigrateTssFunds)
assert.ErrorContains(t, err, "no new tss address has been generated")
hash := crypto.Keccak256Hash([]byte(indexString))
index := hash.Hex()
Expand All @@ -61,7 +79,7 @@ func TestMsgServer_MigrateTssFunds(t *testing.T) {
setAdminPolicies(ctx, zk, admin)
msgServer := keeper.NewMsgServerImpl(*k)
chain := getValidEthChain(t)
amount := sdkmath.NewUint(100)
amount := sdkmath.NewUintFromString("10000000000000000000")
indexString, tssPubkey := setupTssMigrationParams(zk, k, ctx, *chain, amount, true, true)
k.GetObserverKeeper().SetPendingNonces(ctx, observertypes.PendingNonces{
NonceLow: 1,
Expand All @@ -87,7 +105,7 @@ func TestMsgServer_MigrateTssFunds(t *testing.T) {
setAdminPolicies(ctx, zk, admin)
msgServer := keeper.NewMsgServerImpl(*k)
chain := getValidEthChain(t)
amount := sdkmath.NewUint(100)
amount := sdkmath.NewUintFromString("10000000000000000000")
indexString, tssPubkey := setupTssMigrationParams(zk, k, ctx, *chain, amount, true, true)
k.GetObserverKeeper().SetPendingNonces(ctx, observertypes.PendingNonces{
NonceLow: 1,
Expand Down Expand Up @@ -123,7 +141,7 @@ func TestMsgServer_MigrateTssFunds(t *testing.T) {
setAdminPolicies(ctx, zk, admin)
msgServer := keeper.NewMsgServerImpl(*k)
chain := getValidEthChain(t)
amount := sdkmath.NewUint(100)
amount := sdkmath.NewUintFromString("10000000000000000000")
indexString, _ := setupTssMigrationParams(zk, k, ctx, *chain, amount, false, false)
currentTss, found := k.GetObserverKeeper().GetTSS(ctx)
assert.True(t, found)
Expand Down
2 changes: 2 additions & 0 deletions x/crosschain/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ var (
ErrReceiverIsEmpty = errorsmod.Register(ModuleName, 1142, "receiver is empty")
ErrUnsupportedStatus = errorsmod.Register(ModuleName, 1143, "unsupported status")
ErrObservedTxAlreadyFinalized = errorsmod.Register(ModuleName, 1144, "observed tx already finalized")

ErrInsufficientFundsTssMigration = errorsmod.Register(ModuleName, 1145, "insufficient funds for TSS migration")
)
2 changes: 2 additions & 0 deletions x/crosschain/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
MemStoreKey = "mem_metacore"

ProtocolFee = 2000000000000000000

TssMigrationGasMultiplierEVM = "2.5"
kingpinXD marked this conversation as resolved.
Show resolved Hide resolved
)

func GetProtocolFee() sdk.Uint {
Expand Down
Loading
Loading