Skip to content

Commit

Permalink
feat: skip custom fee handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nelitow committed Dec 21, 2024
1 parent 6d8ecea commit bdce830
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 42 deletions.
4 changes: 2 additions & 2 deletions examples/cra-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
},
"dependencies": {
"@fuels/connectors": "0.35.1",
"@fuels/react": "0.35.1",
"@fuels/react": "0.39.0-pr-446-73",
"@tanstack/react-query": "5.28.4",
"fuels": "0.96.1",
"fuels": "0.0.0-pr-3487-20241220190150",
"react": "18.3.1",
"react-dom": "18.3.1"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"send@<0.19.0": ">=0.19.0",
"serve-static@<1.16.0": ">=1.16.0",
"rollup@>=4.0.0 <4.22.4": ">=4.22.4",
"fuels": "0.96.1",
"fuels": "0.0.0-pr-3487-20241220190150",
"secp256k1@=5.0.0": ">=5.0.1",
"elliptic@<6.6.0": ">=6.6.0",
"cross-spawn@<7.0.5": ">=7.0.5",
Expand Down
4 changes: 3 additions & 1 deletion packages/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
},
"dependencies": {
"@fontsource/source-code-pro": "5.0.13",
"@fuel-ts/math": "0.97.2",
"@fuel-ts/providers": "0.73.0",
"@fuel-ui/css": "0.23.3",
"@fuel-ui/icons": "0.23.3",
"@fuel-ui/react": "0.23.3",
Expand All @@ -44,7 +46,7 @@
"events": "3.3.0",
"fake-indexeddb": "4.0.2",
"framer-motion": "10.16.4",
"fuels": "0.96.1",
"fuels": "0.0.0-pr-3487-20241220190150",
"json-edit-react": "1.13.3",
"json-rpc-2.0": "1.7.0",
"lodash.debounce": "4.0.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export function useTransactionRequest(opts: UseTransactionRequestOpts = {}) {
}

function request(input: TxInputs['request']) {
console.log('request', input);
service.send('START', { input });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ export function TransactionRequest() {
errors={errors.simulateTxErrors}
isConfirm
fees={fees}
skipCustomFee={txRequest.input.skipCustomFee}
/>
)}
{shouldShowTxExecuted && (
<TxContent.Info
showDetails
tx={txSummaryExecuted}
txStatus={executedStatus()}
skipCustomFee={txRequest.input.skipCustomFee}
footer={
status('failed') && (
<Button
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/systems/Send/hooks/useSend.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export function useSend() {
regularTip,
fastTip,
maxGasLimit,
skipCustomFee,
} = ctx;
if (!providerUrl || !transactionRequest || !address) {
throw new Error('Params are required');
Expand All @@ -302,7 +303,7 @@ export function useSend() {
fastTip,
maxGasLimit,
},
skipCustomFee: true,
skipCustomFee,
});
},
},
Expand Down
3 changes: 3 additions & 0 deletions packages/app/src/systems/Send/machines/sendMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type MachineContext = {
maxGasLimit?: BN;
input?: TxInputs['createTransfer'];
error?: string;
skipCustomFee?: boolean;
};

type EstimateDefaultTipsReturn = {
Expand All @@ -43,6 +44,7 @@ type CreateTransactionReturn = {
transactionRequest?: TransactionRequest;
providerUrl: string;
address: string;
skipCustomFee?: boolean;
};

type MachineServices = {
Expand Down Expand Up @@ -183,6 +185,7 @@ export const sendMachine = createMachine(
address: ev.data.address,
baseFee: ev.data.baseFee ?? ctx.baseFee,
gasLimit: ev.data.gasLimit ?? ctx.gasLimit,
skipCustomFee: ev.data.skipCustomFee ?? ctx.skipCustomFee,
error: undefined,
})),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export type TxContentInfoProps = {
fastTip?: BN;
};
txRequest?: TransactionRequest;
skipCustomFee?: boolean;
};

function TxContentInfo({
Expand All @@ -109,13 +110,15 @@ function TxContentInfo({
errors,
fees,
txRequest,
skipCustomFee,
}: TxContentInfoProps) {
const { getValues } = useFormContext<SendFormValues>();

const status = txStatus || tx?.status || txStatus;
const hasErrors = Boolean(Object.keys(errors || {}).length);
const isExecuted = !!tx?.id;
const txRequestGasLimit = getGasLimitFromTxRequest(txRequest);
const shouldShowFees = !skipCustomFee && showDetails;

const initialAdvanced = useMemo(() => {
if (!fees?.regularTip || !fees?.fastTip) return false;
Expand Down Expand Up @@ -155,9 +158,9 @@ function TxContentInfo({
status={status}
isLoading={isLoading}
/>
{isLoading && !showDetails && <TxFee.Loader />}
{showDetails && !fees && <TxFee fee={tx?.fee} />}
{showDetails &&
{isLoading && !shouldShowFees && <TxFee.Loader />}
{shouldShowFees && !fees && <TxFee fee={tx?.fee} />}
{shouldShowFees &&
fees?.baseFee &&
txRequestGasLimit &&
fees?.regularTip &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ export const TxApprove = () => {
isLoading={isLoading}
errors={ctx.errors.simulateTxErrors}
isConfirm
skipCustomFee={ctx.input.skipCustomFee}
/>
)}
{ctx.shouldShowTxExecuted && (
<TxContent.Info
showDetails
tx={ctx.txSummaryExecuted}
txStatus={ctx.executedStatus()}
skipCustomFee={ctx.input.skipCustomFee}
footer={
ctx.status('failed') && (
<Button
Expand Down
75 changes: 41 additions & 34 deletions packages/app/src/systems/Transaction/services/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ export class TxService {
tip: inputCustomTip,
gasLimit: inputCustomGasLimit,
}: TxInputs['simulateTransaction']) {
console.log('simulateTransaction', {
skipCustomFee,
inputTransactionRequest,
providerUrl,
inputCustomTip,
inputCustomGasLimit,
});
const [provider, account] = await Promise.all([
createProvider(providerUrl || ''),
AccountService.getCurrentAccount(),
Expand All @@ -212,40 +219,40 @@ export class TxService {
// debugger;

const proposedTxRequest = clone(inputTransactionRequest);
// if (!skipCustomFee) {
// // if the user has inputted a custom tip, we set it to the proposedTxRequest
// if (inputCustomTip) {
// proposedTxRequest.tip = inputCustomTip;
// }
// // if the user has inputted a custom gas Limit, we set it to the proposedTxRequest
// if (inputCustomGasLimit) {
// setGasLimitToTxRequest(proposedTxRequest, inputCustomGasLimit);
// } else {
// // if the user has not inputted a custom gas Limit, we increase the original one in 20% to avoid OutOfGas errors
// setGasLimitToTxRequest(
// proposedTxRequest,
// initialGasLimit.mul(12).div(10)
// );
// }
// const { maxFee } = await provider.estimateTxGasAndFee({
// transactionRequest: proposedTxRequest,
// });

// // if the maxFee is greater than the initial maxFee, we set it to the new maxFee, and refund the transaction
// if (maxFee.gt(initialMaxFee)) {
// proposedTxRequest.maxFee = maxFee;
// const txCost = await wallet.getTransactionCost(proposedTxRequest, {
// estimateTxDependencies: true,
// });
// await wallet.fund(proposedTxRequest, {
// estimatedPredicates: txCost.estimatedPredicates,
// addedSignatures: txCost.addedSignatures,
// gasPrice: txCost.gasPrice,
// updateMaxFee: txCost.updateMaxFee,
// requiredQuantities: [],
// });
// }
// }
if (!skipCustomFee) {
// if the user has inputted a custom tip, we set it to the proposedTxRequest
if (inputCustomTip) {
proposedTxRequest.tip = inputCustomTip;
}
// if the user has inputted a custom gas Limit, we set it to the proposedTxRequest
if (inputCustomGasLimit) {
setGasLimitToTxRequest(proposedTxRequest, inputCustomGasLimit);
} else {
// if the user has not inputted a custom gas Limit, we increase the original one in 20% to avoid OutOfGas errors
setGasLimitToTxRequest(
proposedTxRequest,
initialGasLimit.mul(12).div(10)
);
}
const { maxFee } = await provider.estimateTxGasAndFee({
transactionRequest: proposedTxRequest,
});

// if the maxFee is greater than the initial maxFee, we set it to the new maxFee, and refund the transaction
if (maxFee.gt(initialMaxFee)) {
proposedTxRequest.maxFee = maxFee;
const txCost = await wallet.getTransactionCost(proposedTxRequest, {
estimateTxDependencies: true,
});
await wallet.fund(proposedTxRequest, {
estimatedPredicates: txCost.estimatedPredicates,
addedSignatures: txCost.addedSignatures,
gasPrice: txCost.gasPrice,
updateMaxFee: txCost.updateMaxFee,
requiredQuantities: [],
});
}
}

const transaction = proposedTxRequest.toTransaction();
const abiMap = await getAbiMap({
Expand Down

0 comments on commit bdce830

Please sign in to comment.