Skip to content

Commit

Permalink
chore: fetch account data when not available
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeron committed Jan 27, 2025
1 parent d1e1a79 commit b4e4991
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { Account, AccountWithBalance } from '@fuel-wallet/types';
import type { BN, TransactionRequest, TransactionSummary } from 'fuels';
import type { InterpreterFrom, StateFrom } from 'xstate';
import { assign, createMachine } from 'xstate';
import { AccountService } from '~/systems/Account';
import { FetchMachine, assignErrorMessage, delay } from '~/systems/Core';
import { NetworkService } from '~/systems/Network';
import type { GroupedErrors, VMApiError } from '~/systems/Transaction';
import type { TxInputs } from '~/systems/Transaction/services';
import { TxService } from '~/systems/Transaction/services';
Expand All @@ -25,6 +27,7 @@ type MachineContext = {
isOriginRequired?: boolean;
providerUrl?: string;
transactionRequest?: TransactionRequest;
address?: string;
account?: AccountWithBalance;
tip?: BN;
gasLimit?: BN;
Expand All @@ -47,7 +50,7 @@ type MachineContext = {
};
};

type EstimateGasLimitAndDefaultTipsReturn = {
type PrepareInputForSimulateTransactionReturn = {
regularTip: BN;
fastTip: BN;
maxGasLimit: BN;
Expand All @@ -64,8 +67,8 @@ type MachineServices = {
send: {
data: TransactionSummary;
};
estimatingGasLimitAndDefaultTips: {
data: EstimateGasLimitAndDefaultTipsReturn;
prepareInputForSimulateTransaction: {
data: PrepareInputForSimulateTransactionReturn;
};
simulateTransaction: {
data: SimulateTransactionReturn;
Expand Down Expand Up @@ -111,15 +114,21 @@ export const transactionRequestMachine = createMachine(
},
{
actions: ['assignTxRequestData'],
target: 'estimatingGasLimitAndDefaultTips',
target: 'prepareInputForSimulateTransaction',
},
],
},
},
estimatingGasLimitAndDefaultTips: {
prepareInputForSimulateTransaction: {
tags: ['loading'],
invoke: {
src: 'estimatingGasLimitAndDefaultTips',
src: 'prepareInputForSimulateTransaction',
data: {
input: (ctx: MachineContext) => ({
address: ctx.input.address,
account: ctx.input.account,
}),
},
onDone: [
{
cond: FetchMachine.hasError,
Expand Down Expand Up @@ -252,17 +261,15 @@ export const transactionRequestMachine = createMachine(
favIconUrl,
skipCustomFee,
account,
address,
fees,
} = ev.input || {};

if (!providerUrl) {
throw new Error('providerUrl is required');
}
if (!account) {
throw new Error('account is required');
}
if (!account.address) {
throw new Error('address is required');
if (!account?.address && !address) {
throw new Error('account or address is required');
}
if (!transactionRequest) {
throw new Error('transaction is required');
Expand All @@ -275,6 +282,7 @@ export const transactionRequestMachine = createMachine(
transactionRequest,
origin,
account,
address,
providerUrl,
title,
favIconUrl,
Expand Down Expand Up @@ -343,15 +351,30 @@ export const transactionRequestMachine = createMachine(
}),
},
services: {
estimatingGasLimitAndDefaultTips: FetchMachine.create<
never,
EstimateGasLimitAndDefaultTipsReturn
prepareInputForSimulateTransaction: FetchMachine.create<
{ address?: string; account?: AccountWithBalance },
{
estimated: PrepareInputForSimulateTransactionReturn;
account: AccountWithBalance;
}
>({
showError: false,
maxAttempts: 1,
async fetch() {
const estimated = await TxService.estimateGasLimitAndDefaultTips();
return estimated;
async fetch({ input }) {
const [estimated, acc] = await Promise.all([
TxService.estimateGasLimitAndDefaultTips(),
input?.account ||
AccountService.fetchAccount({
address: input?.address as string,
}).then(async (_account) => {
const network = await NetworkService.getSelectedNetwork();
return await AccountService.fetchBalance({
account: _account,
providerUrl: network?.url as string,
});
}),
]);
return { estimated, account: acc };
},
}),
simulateTransaction: FetchMachine.create<
Expand All @@ -377,7 +400,7 @@ export const transactionRequestMachine = createMachine(
async fetch(params) {
const { input } = params;
if (
!input?.account ||
(!input?.account && !input?.address) ||
!input?.transactionRequest ||
!input?.providerUrl
) {
Expand Down
21 changes: 1 addition & 20 deletions packages/app/src/systems/DApp/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,12 @@ export class RequestMethods extends ExtensionPageConnection {
const { origin, address, provider, transaction, title, favIconUrl } = input;
const providerUrl = provider.url;
const transactionRequest = transactionRequestify(JSON.parse(transaction));
let currentAccountWithBalance = store.getStateFrom(Services.accounts)
.context.account;
if (!currentAccountWithBalance && address) {
const account = await AccountService.fetchAccount({
address: input.address,
});
currentAccountWithBalance = await AccountService.fetchBalance({
account,
providerUrl,
});
}
if (
currentAccountWithBalance?.address !== address ||
!currentAccountWithBalance
) {
throw new Error(
`Origin: ${address} does not match current account: ${currentAccountWithBalance?.address}`
);
}

const state = await store
.requestTransaction({
origin,
transactionRequest,
address,
account: currentAccountWithBalance,
providerUrl,
title,
favIconUrl,
Expand Down
16 changes: 10 additions & 6 deletions packages/app/src/systems/Transaction/services/transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export type TxInputs = {
request: {
providerUrl: string;
transactionRequest: TransactionRequest;
address: string | undefined;
origin?: string | undefined;
address?: string;
origin?: string;
title?: string;
favIconUrl?: string;
account: AccountWithBalance | undefined;
account?: AccountWithBalance;
skipCustomFee?: boolean;
fees?: {
baseFee?: BN;
Expand All @@ -59,6 +59,7 @@ export type TxInputs = {
};
};
send: {
address?: string;
account?: Account;
transactionRequest: TransactionRequest;
providerUrl?: string;
Expand Down Expand Up @@ -154,12 +155,15 @@ export class TxService {

static async send({
account,
// address: _address,
address,
transactionRequest,
providerUrl = '',
}: TxInputs['send']) {
const provider = await createProvider(providerUrl);
const wallet = new WalletLockedCustom(address, provider);
const wallet = new WalletLockedCustom(
(account?.address?.toString() || address) as string,
provider
);
const txSent = await wallet.sendTransaction(transactionRequest);

return txSent;
Expand Down Expand Up @@ -380,7 +384,7 @@ export class TxService {
const currentNetwork = await NetworkService.getSelectedNetwork();
const provider = await createProvider(currentNetwork?.url || '');
const [{ regularTip, fastTip }, { consensusParameters }] =
await Promise.all([getCurrentTips(provider), provider.getChain()]);
await Promise.all([await getCurrentTips(provider), provider.getChain()]);
return {
regularTip: bn(regularTip),
fastTip: bn(fastTip),
Expand Down

0 comments on commit b4e4991

Please sign in to comment.