Skip to content

Commit

Permalink
add evm amount calculation for tss migration
Browse files Browse the repository at this point in the history
  • Loading branch information
kingpinXD committed Jan 23, 2024
1 parent 04e0129 commit 8a3de0e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 83 deletions.
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
// Todo Move gas limits from zeta-client to this file
// 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)
assert.True(t, found)
})
t.Run("not enough funds in tss address for migration", 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.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())
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"
)

func GetProtocolFee() sdk.Uint {
Expand Down
Loading

0 comments on commit 8a3de0e

Please sign in to comment.