diff --git a/src/hooks/useHandleAttest.tsx b/src/hooks/useHandleAttest.tsx index d1a603eab..3bb778d15 100644 --- a/src/hooks/useHandleAttest.tsx +++ b/src/hooks/useHandleAttest.tsx @@ -107,15 +107,15 @@ import { XplaWallet } from "@xlabs-libs/wallet-aggregator-xpla"; import { useXplaWallet } from "../contexts/XplaWalletContext"; import { SuiWallet } from "@xlabs-libs/wallet-aggregator-sui"; import { getSuiProvider } from "../utils/sui"; + import { getEmitterAddressAndSequenceFromResponseSui, getOriginalPackageId, } from "@certusone/wormhole-sdk/lib/cjs/sui"; import { useSuiWallet } from "../contexts/SuiWalletContext"; import { useSeiWallet } from "../contexts/SeiWalletContext"; -import { SeiWallet, buildExecuteMessage } from "@xlabs-libs/wallet-aggregator-sei"; -import { parseSequenceFromLogSei } from "../utils/sei"; -import { calculateFee } from "@cosmjs/stargate"; +import { SeiWallet } from "@xlabs-libs/wallet-aggregator-sei"; +import { calculateFeeForContractExecution, parseSequenceFromLogSei } from "../utils/sei"; import { SuiTransactionBlockResponse } from "@mysten/sui.js"; async function algo( @@ -611,9 +611,7 @@ async function sei( dispatch(setIsSending(true)); try { const tokenBridgeAddress = getTokenBridgeAddressForChain(CHAIN_ID_SEI); - const nonce = Math.round(Math.random() * 100000); - const msg = { create_asset_meta: { asset_info: @@ -623,8 +621,7 @@ async function sei( nonce, }, }; - - const fee = calculateFee(10000000, "0.1usei"); + const fee = await calculateFeeForContractExecution(msg, wallet, tokenBridgeAddress, "Wormhole - Attest Token"); const tx = await wallet.executeMultiple({ instructions: [{ contractAddress: tokenBridgeAddress, msg }], fee, diff --git a/src/hooks/useHandleCreateWrapped.tsx b/src/hooks/useHandleCreateWrapped.tsx index 148124374..78fd1cd57 100644 --- a/src/hooks/useHandleCreateWrapped.tsx +++ b/src/hooks/useHandleCreateWrapped.tsx @@ -104,10 +104,9 @@ import { sleep } from "../utils/sleep"; import { useSuiWallet } from "../contexts/SuiWalletContext"; import { SuiWallet } from "@xlabs-libs/wallet-aggregator-sui"; import { createWrappedOnSuiPrepare } from "../utils/suiPublishHotfix"; -import { calculateFee } from "@cosmjs/stargate"; -import { createWrappedOnSei, updateWrappedOnSei } from "../utils/sei"; +import { calculateFeeForContractExecution, createWrappedOnSei, updateWrappedOnSei } from "../utils/sei"; import { useSeiWallet } from "../contexts/SeiWalletContext"; -import { SeiWallet, buildExecuteMessage } from "@xlabs-libs/wallet-aggregator-sei"; +import { SeiWallet } from "@xlabs-libs/wallet-aggregator-sei"; // TODO: replace with SDK method - export async function updateWrappedOnSui( @@ -381,20 +380,11 @@ async function sei( ? await updateWrappedOnSei(signedVAA) : await createWrappedOnSei(signedVAA); - const fee = calculateFee(100000000, "0.1usei"); - const currentFee = await wallet.calculateFee({ - msgs: [buildExecuteMessage( - wallet.getAddress()!, - tokenBridgeAddress, - [msg] - )], - fee, - memo: "Wormhole - Create Wrapped", - }) - // TODO: is this right? + const fee = await calculateFeeForContractExecution(msg, wallet, tokenBridgeAddress, "Wormhole - Create Wrapped"); + const tx = await wallet.executeMultiple({ instructions: [{ msg: msg, contractAddress: tokenBridgeAddress }], - fee: calculateFee(parseInt(currentFee), "0.1usei"), + fee, memo: "Wormhole - Create Wrapped", }); diff --git a/src/hooks/useHandleRedeem.tsx b/src/hooks/useHandleRedeem.tsx index 64e7a634b..fa6ccfa52 100644 --- a/src/hooks/useHandleRedeem.tsx +++ b/src/hooks/useHandleRedeem.tsx @@ -95,13 +95,10 @@ import { useSuiWallet } from "../contexts/SuiWalletContext"; import { redeemOnSui } from "../utils/suiRedeemHotfix"; import { ThresholdL2WormholeGateway } from "../utils/ThresholdL2WormholeGateway"; import { newThresholdWormholeGateway } from "../assets/providers/tbtc/solana/WormholeGateway.v2"; -import { calculateFee } from "@cosmjs/stargate"; import { fromUint8Array } from "js-base64"; import { useSeiWallet } from "../contexts/SeiWalletContext"; -import { - SeiWallet, - buildExecuteMessage, -} from "@xlabs-libs/wallet-aggregator-sei"; +import { SeiWallet } from "@xlabs-libs/wallet-aggregator-sei"; +import { calculateFeeForContractExecution } from "../utils/sei"; async function algo( dispatch: any, @@ -292,7 +289,7 @@ async function xpla( ) { dispatch(setIsRedeeming(true)); try { - const msg = await redeemOnXpla( + const msg = redeemOnXpla( getTokenBridgeAddressForChain(CHAIN_ID_XPLA), wallet.getAddress()!, signedVAA @@ -324,10 +321,6 @@ async function sei( ) { dispatch(setIsRedeeming(true)); try { - // TODO: is this right? - - const fee = calculateFee(100000000, "0.1usei"); - const vaa = parseVaa(signedVAA); const transfer = parseTokenTransferPayload(vaa.payload); const receiver = cosmos.humanAddress("sei", transfer.to); @@ -358,18 +351,10 @@ async function sei( }, }, ]; - const simulatedFee = await wallet.calculateFee({ - msgs: [ - buildExecuteMessage(wallet.getAddress()!, contractAddress, [ - instructions.map((i) => i.msg), - ]), - ], - fee, - memo: "Wormhole - Create Wrapped", - }); + const fee = await calculateFeeForContractExecution(instructions, wallet, contractAddress, "Wormhole - Complete Transfer"); const tx = await wallet.executeMultiple({ instructions, - fee: calculateFee(parseInt(simulatedFee), "0.1usei"), + fee, memo: "Wormhole - Complete Transfer", }); diff --git a/src/hooks/useHandleTransfer.tsx b/src/hooks/useHandleTransfer.tsx index 7dbb41d3d..dba6f038b 100644 --- a/src/hooks/useHandleTransfer.tsx +++ b/src/hooks/useHandleTransfer.tsx @@ -137,13 +137,9 @@ import { import { useSuiWallet } from "../contexts/SuiWalletContext"; import { ThresholdL2WormholeGateway } from "../utils/ThresholdL2WormholeGateway"; import { newThresholdWormholeGateway } from "../assets/providers/tbtc/solana/WormholeGateway.v2"; -import { calculateFee } from "@cosmjs/stargate"; -import { parseSequenceFromLogSei } from "../utils/sei"; +import { calculateFeeForContractExecution, parseSequenceFromLogSei } from "../utils/sei"; import { useSeiWallet } from "../contexts/SeiWalletContext"; -import { - SeiWallet, - buildExecuteMessage, -} from "@xlabs-libs/wallet-aggregator-sei"; +import { SeiWallet } from "@xlabs-libs/wallet-aggregator-sei"; import { SuiTransactionBlockResponse } from "@mysten/sui.js"; type AdditionalPayloadOverride = { @@ -848,7 +844,6 @@ async function sei( const tokenBridgeAddress = getTokenBridgeAddressForChain(CHAIN_ID_SEI); const encodedRecipient = Buffer.from(targetAddress).toString("base64"); - const fee = calculateFee(100000000, "0.1usei"); // NOTE: this only supports transferring out via the Sei CW20 <> Bank translator // or the usei native denomination @@ -893,21 +888,12 @@ async function sei( ], }, ]; - const simulatedFee = await wallet.calculateFee({ - msgs: [ - buildExecuteMessage( - wallet.getAddress()!, - tokenBridgeAddress, - instructions.map((i) => i.msg) - ), - ], - fee, - memo: "Wormhole - Create Wrapped", - }); - + + const fee = await calculateFeeForContractExecution(instructions, wallet, tokenBridgeAddress, "Wormhole - Complete Transfer"); + const tx = await wallet.executeMultiple({ instructions, - fee: calculateFee(parseInt(simulatedFee), "0.1usei"), + fee, memo: "Wormhole - Initiate Transfer", }); diff --git a/src/utils/sei.ts b/src/utils/sei.ts index 462938c65..9814f278f 100644 --- a/src/utils/sei.ts +++ b/src/utils/sei.ts @@ -11,7 +11,11 @@ import { getCosmWasmClient, getQueryClient } from "@sei-js/core"; import { fromUint8Array } from "js-base64"; import { SEI_CHAIN_CONFIGURATION, SEI_NATIVE_DENOM } from "./consts"; import { logs } from "@cosmjs/stargate"; +import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx'; +import { calculateFee } from "@cosmjs/stargate"; +import { StdFee } from "@cosmjs/amino"; import { utils } from "ethers"; +import { SeiWallet } from "@xlabs-libs/wallet-aggregator-sei"; export const getSeiWasmClient = () => getCosmWasmClient(SEI_CHAIN_CONFIGURATION.rpcUrl); @@ -181,3 +185,24 @@ export function searchInLogs( } return undefined; } + +const MSG_EXECUTE_CONTRACT_TYPE_URL = '/cosmwasm.wasm.v1.MsgExecuteContract'; + +export async function calculateFeeForContractExecution(msg: any, wallet: SeiWallet, contractAddress: string, memo = '', fee = 70000): Promise { + const seiTx = { + typeUrl: MSG_EXECUTE_CONTRACT_TYPE_URL, + value: MsgExecuteContract.fromPartial({ + sender: wallet.getAddress(), + contract: contractAddress, + msg: Buffer.from(JSON.stringify(msg)), + }), + } + const stimatedFee = await wallet.calculateFee({ + msgs: [seiTx], + fee: calculateFee(fee, "0.1usei"), + memo + }); + console.log(stimatedFee); + debugger; + return calculateFee(parseInt(stimatedFee), "0.1usei"); +} \ No newline at end of file