Skip to content

Commit

Permalink
disabled fee control svm
Browse files Browse the repository at this point in the history
  • Loading branch information
marxeille committed Jan 22, 2025
1 parent 3eb5b53 commit 31faaf4
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 86 deletions.
6 changes: 6 additions & 0 deletions apps/extension/src/components/input/fee-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export const FeeControl: FunctionComponent<{

disableAutomaticFeeSet?: boolean;
isForEVMTx?: boolean;
disabled?: boolean;
}> = observer(
({
senderConfig,
Expand All @@ -182,6 +183,7 @@ export const FeeControl: FunctionComponent<{
gasSimulator,
disableAutomaticFeeSet,
isForEVMTx,
disabled,
}) => {
const {
analyticsStore,
Expand Down Expand Up @@ -223,6 +225,10 @@ export const FeeControl: FunctionComponent<{
onClick={(e) => {
e.preventDefault();

if (disabled) {
return;
}

analyticsStore.logEvent("click_txFeeSet");
setIsModalOpen(true);
}}
Expand Down
2 changes: 2 additions & 0 deletions apps/extension/src/components/input/fee-control/modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const TransactionFeeModal: FunctionComponent<{
gasSimulator?: IGasSimulator;
disableAutomaticFeeSet?: boolean;
isForEVMTx?: boolean;
disabled?: boolean;
}> = observer(
({
close,
Expand All @@ -67,6 +68,7 @@ export const TransactionFeeModal: FunctionComponent<{
gasSimulator,
disableAutomaticFeeSet,
isForEVMTx,
disabled,
}) => {
const { queriesStore, uiConfigStore, priceStore } = useStore();
const intl = useIntl();
Expand Down
183 changes: 97 additions & 86 deletions apps/extension/src/pages/send/amount/svm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,41 @@ import { Stack } from "../../../components/stack";
import styled from "styled-components";
import { useSearchParams } from "react-router-dom";
import { useStore } from "../../../stores";
import {useSendBtcTxConfig, useSendSvmTxConfig, useTxConfigsValidate} from "@owallet/hooks";
import {
useSendBtcTxConfig,
useSendSvmTxConfig,
useTxConfigsValidate,
} from "@owallet/hooks";
import { useNavigate } from "react-router";
import { TokenAmountInput } from "../../../components/input";
import { Box } from "../../../components/box";
import { YAxis } from "../../../components/axis";
import { Gutter } from "../../../components/gutter";
import { FeeControl } from "../../../components/input/fee-control";
import { useNotification } from "../../../hooks/notification";
import {_getPriorityFeeSolana, DenomHelper} from "@owallet/common";
import {CoinPretty, Dec, DecUtils} from "@owallet/unit";
import { _getPriorityFeeSolana, DenomHelper } from "@owallet/common";
import { CoinPretty, Dec, DecUtils } from "@owallet/unit";
import { ColorPalette } from "../../../styles";
import { openPopupWindow } from "@owallet/popup";
import { useIntl } from "react-intl";
import { isRunningInSidePanel } from "../../../utils";
import { CustomRecipientInput } from "../../../components/input/reciepient-input/custom-input";
import Color from "color";
import { TransactionBtcType } from "@owallet/types";
import {ComputeBudgetProgram, Connection, PublicKey, SystemProgram, Transaction} from "@solana/web3.js";
import {createTransferInstruction, getAssociatedTokenAddress, TOKEN_2022_PROGRAM_ID} from "@solana/spl-token";
import {createMemoInstruction} from "@solana/spl-memo";
import {encode} from "bs58";
import {
ComputeBudgetProgram,
Connection,
PublicKey,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import {
createTransferInstruction,
getAssociatedTokenAddress,
TOKEN_2022_PROGRAM_ID,
} from "@solana/spl-token";
import { createMemoInstruction } from "@solana/spl-memo";
import { encode } from "bs58";
import delay from "delay";
const Styles = {
Flex1: styled.div`
Expand Down Expand Up @@ -105,16 +119,14 @@ export const SendSvmPage: FunctionComponent = observer(() => {
const queryBalances = queriesStore.get(chainId).queryBalances;
const denomHelper = new DenomHelper(coinMinimalDenom);
const sender = account.base58Address;
const balance = queryBalances
.getQueryByAddress(sender)
.getBalance(currency);
const balance = queryBalances.getQueryByAddress(sender).getBalance(currency);

const sendConfigs = useSendSvmTxConfig(
chainStore,
queriesStore,
chainId,
sender,
1
chainStore,
queriesStore,
chainId,
sender,
1
);

sendConfigs.amountConfig.setCurrency(currency);
Expand All @@ -128,11 +140,11 @@ export const SendSvmPage: FunctionComponent = observer(() => {
try {
account.setIsSendingTx(true);
const amount = DecUtils.getTenExponentN(
sendConfigs.amountConfig.amount[0].currency.coinDecimals
sendConfigs.amountConfig.amount[0].currency.coinDecimals
)
.mul(sendConfigs.amountConfig.amount[0].toDec())
.roundUp()
.toString();
.mul(sendConfigs.amountConfig.amount[0].toDec())
.roundUp()
.toString();
const unsignedTx = await account.makeSendTokenTx({
currency: sendConfigs.amountConfig.amount[0].currency,
amount: amount,
Expand All @@ -144,44 +156,43 @@ export const SendSvmPage: FunctionComponent = observer(() => {
onBroadcasted: async (txHash) => {
account.setIsSendingTx(false);
notification.show(
"success",
intl.formatMessage({
id: "notification.transaction-success",
}),
""
"success",
intl.formatMessage({
id: "notification.transaction-success",
}),
""
);
await delay(3000);
queryBalances
.getQueryByAddress(account.base58Address)
.balances.forEach((balance) => {
if (
.getQueryByAddress(account.base58Address)
.balances.forEach((balance) => {
if (
balance.currency.coinMinimalDenom === coinMinimalDenom ||
sendConfigs.feeConfig.fees.some(
(fee) =>
fee.currency.coinMinimalDenom ===
balance.currency.coinMinimalDenom
(fee) =>
fee.currency.coinMinimalDenom ===
balance.currency.coinMinimalDenom
)
) {
balance.fetch();
}
});
) {
balance.fetch();
}
});
},
onFulfill: (txReceipt) => {
queryBalances
.getQueryByAddress(account.base58Address)
.balances.forEach((balance) => {
if (
.getQueryByAddress(account.base58Address)
.balances.forEach((balance) => {
if (
balance.currency.coinMinimalDenom === coinMinimalDenom ||
sendConfigs.feeConfig.fees.some(
(fee) =>
fee.currency.coinMinimalDenom ===
balance.currency.coinMinimalDenom
(fee) =>
fee.currency.coinMinimalDenom ===
balance.currency.coinMinimalDenom
)
) {
balance.fetch();
}
});

) {
balance.fetch();
}
});
},
});
} catch (e) {
Expand All @@ -203,7 +214,6 @@ export const SendSvmPage: FunctionComponent = observer(() => {
}
};


const isDetachedMode = searchParams.get("detached") === "true";

const loadingSend = account.isSendingTx;
Expand All @@ -214,49 +224,49 @@ export const SendSvmPage: FunctionComponent = observer(() => {
const connection = new Connection(chainInfo.rpc, "confirmed");
const fromPublicKey = new PublicKey(account.base58Address);
const toPublicKey = new PublicKey(
sendConfigs.recipientConfig.recipient
sendConfigs.recipientConfig.recipient
);
const amount = DecUtils.getTenExponentN(
sendConfigs.amountConfig.amount[0].currency.coinDecimals
sendConfigs.amountConfig.amount[0].currency.coinDecimals
)
.mul(sendConfigs.amountConfig.amount[0].toDec())
.roundUp()
.toString();
.mul(sendConfigs.amountConfig.amount[0].toDec())
.roundUp()
.toString();
let transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: fromPublicKey,
toPubkey: toPublicKey,
lamports: BigInt(amount),
})
SystemProgram.transfer({
fromPubkey: fromPublicKey,
toPubkey: toPublicKey,
lamports: BigInt(amount),
})
);
const denom = new DenomHelper(
sendConfigs.amountConfig.amount[0].currency.coinMinimalDenom
sendConfigs.amountConfig.amount[0].currency.coinMinimalDenom
);
if (denom.type.startsWith("spl")) {
const mintPublicKey = new PublicKey(denom.contractAddress);
const isToken2020 = denom.type.includes("spl20");
// Get the associated token accounts for the sender and receiver
const senderTokenAccount = await getAssociatedTokenAddress(
mintPublicKey,
fromPublicKey,
isToken2020 ? true : undefined, // Allow Token2022
isToken2020 ? TOKEN_2022_PROGRAM_ID : undefined // Token2022 Program ID
mintPublicKey,
fromPublicKey,
isToken2020 ? true : undefined, // Allow Token2022
isToken2020 ? TOKEN_2022_PROGRAM_ID : undefined // Token2022 Program ID
);
const receiverTokenAccount = await getAssociatedTokenAddress(
mintPublicKey,
toPublicKey,
isToken2020 ? true : undefined, // Allow Token2022
isToken2020 ? TOKEN_2022_PROGRAM_ID : undefined // Token2022 Program ID
mintPublicKey,
toPublicKey,
isToken2020 ? true : undefined, // Allow Token2022
isToken2020 ? TOKEN_2022_PROGRAM_ID : undefined // Token2022 Program ID
);

// Create SPL token transfer instruction
const transferInstruction = createTransferInstruction(
senderTokenAccount, // Sender's token account
receiverTokenAccount, // Receiver's token account
fromPublicKey, // Payer's public key
BigInt(amount),
undefined, // Amount to transfer (raw amount, not adjusted for decimals)
isToken2020 ? TOKEN_2022_PROGRAM_ID : undefined // Token2022 Program ID
senderTokenAccount, // Sender's token account
receiverTokenAccount, // Receiver's token account
fromPublicKey, // Payer's public key
BigInt(amount),
undefined, // Amount to transfer (raw amount, not adjusted for decimals)
isToken2020 ? TOKEN_2022_PROGRAM_ID : undefined // Token2022 Program ID
);

// Create a transaction
Expand All @@ -269,7 +279,7 @@ export const SendSvmPage: FunctionComponent = observer(() => {
transaction.recentBlockhash = blockhash;
transaction.feePayer = fromPublicKey;
const txStr = encode(
transaction.serialize({ requireAllSignatures: false })
transaction.serialize({ requireAllSignatures: false })
);
const dynamicMicroLamports = await _getPriorityFeeSolana(txStr);
const message = transaction.compileMessage();
Expand All @@ -278,33 +288,33 @@ export const SendSvmPage: FunctionComponent = observer(() => {
throw new Error("Unable to estimate the fee");
}
const simulationResult = await connection.simulateTransaction(
transaction
transaction
);
if (!simulationResult.value.unitsConsumed)
throw new Error("Unable to estimate the fee");
const DefaultUnitLimit = new Dec(200_000);
const unitsConsumed = new Dec(simulationResult.value.unitsConsumed);
const units = unitsConsumed.lte(DefaultUnitLimit)
? DefaultUnitLimit
: unitsConsumed.mul(new Dec(1.2)); // Request up to 1,000,000 compute units
? DefaultUnitLimit
: unitsConsumed.mul(new Dec(1.2)); // Request up to 1,000,000 compute units
const microLamports = new Dec(
dynamicMicroLamports > 0 ? dynamicMicroLamports : 50000
dynamicMicroLamports > 0 ? dynamicMicroLamports : 50000
);

transaction.add(
// Request a specific number of compute units
ComputeBudgetProgram.setComputeUnitLimit({
units: Number(units.roundUp().toString()),
}),
// Attach a priority fee (in lamports)
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: Number(microLamports.roundUp().toString()), // Set priority fee per compute unit in micro-lamports
})
// Request a specific number of compute units
ComputeBudgetProgram.setComputeUnitLimit({
units: Number(units.roundUp().toString()),
}),
// Attach a priority fee (in lamports)
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: Number(microLamports.roundUp().toString()), // Set priority fee per compute unit in micro-lamports
})
);
const baseFee = new Dec(feeInLamports.value);
const PriorityFee = units
.mul(microLamports)
.quoTruncate(DecUtils.getTenExponentNInPrecisionRange(6));
.mul(microLamports)
.quoTruncate(DecUtils.getTenExponentNInPrecisionRange(6));
const fee = [
{
amount: baseFee.add(PriorityFee).roundUp().toString(),
Expand Down Expand Up @@ -416,6 +426,7 @@ export const SendSvmPage: FunctionComponent = observer(() => {
feeConfig={sendConfigs.feeConfig}
gasConfig={null}
gasSimulator={null}
disabled={true}
/>
</Stack>
</Box>
Expand Down

0 comments on commit 31faaf4

Please sign in to comment.