Skip to content

Commit

Permalink
add fee calculation function for sei
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianscatularo committed Sep 19, 2023
1 parent 409857b commit 727be02
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 62 deletions.
11 changes: 4 additions & 7 deletions src/hooks/useHandleAttest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down
20 changes: 5 additions & 15 deletions src/hooks/useHandleCreateWrapped.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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",
});

Expand Down
25 changes: 5 additions & 20 deletions src/hooks/useHandleRedeem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
});

Expand Down
26 changes: 6 additions & 20 deletions src/hooks/useHandleTransfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
});

Expand Down
25 changes: 25 additions & 0 deletions src/utils/sei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<StdFee> {
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");
}

0 comments on commit 727be02

Please sign in to comment.