Skip to content

Commit 46f0bfa

Browse files
committed
Clear loading
Calculate source network cost of all steps.
1 parent 3b14a9f commit 46f0bfa

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

packages/transaction-pay-controller/src/TransactionPayController.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ export class TransactionPayController extends BaseController<
123123
transactionData: this.state.transactionData[transactionId],
124124
transactionId,
125125
updateTransactionData: this.#updateTransactionData.bind(this),
126-
}).catch(noop);
126+
})
127+
.finally(() => {
128+
this.#updateTransactionData(transactionId, (data) => {
129+
data.isLoading = false;
130+
});
131+
})
132+
.catch(noop);
127133
}
128134
}
129135

packages/transaction-pay-controller/src/strategy/relay/relay-quotes.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ function normalizeQuote(
165165
const { messenger, transaction } = fullRequest;
166166
const { details } = quote;
167167
const { currencyIn, currencyOut } = details;
168-
const params = quote.steps[0].items[0].data;
169168

170169
const { usdToFiatRate } = getFiatRates(messenger, request);
171170

@@ -179,12 +178,7 @@ function normalizeQuote(
179178
usdToFiatRate,
180179
);
181180

182-
const sourceNetwork = calculateGasCost({
183-
...params,
184-
maxFeePerGas: undefined,
185-
maxPriorityFeePerGas: undefined,
186-
messenger,
187-
});
181+
const sourceNetwork = calculateSourceNetworkCost(quote, messenger);
188182

189183
const targetNetwork = quote.skipTransaction
190184
? {
@@ -303,3 +297,39 @@ function getFeatureFlags(messenger: TransactionPayControllerMessenger) {
303297
relayQuoteUrl,
304298
};
305299
}
300+
301+
/**
302+
* Calculates source network cost from a Relay quote.
303+
*
304+
* @param quote - Relay quote.
305+
* @param messenger - Controller messenger.
306+
* @returns Total source network cost in USD and fiat.
307+
*/
308+
function calculateSourceNetworkCost(
309+
quote: RelayQuote,
310+
messenger: TransactionPayControllerMessenger,
311+
) {
312+
const allParams = quote.steps.flatMap((s) => s.items.map((i) => i.data));
313+
314+
const result = allParams.reduce(
315+
(total, params) => {
316+
const gasCost = calculateGasCost({
317+
...params,
318+
maxFeePerGas: undefined,
319+
maxPriorityFeePerGas: undefined,
320+
messenger,
321+
});
322+
323+
return {
324+
usd: new BigNumber(total.usd).plus(gasCost.usd),
325+
fiat: new BigNumber(total.fiat).plus(gasCost.fiat),
326+
};
327+
},
328+
{ usd: new BigNumber(0), fiat: new BigNumber(0) },
329+
);
330+
331+
return {
332+
usd: result.usd.toString(10),
333+
fiat: result.fiat.toString(10),
334+
};
335+
}

packages/transaction-pay-controller/src/utils/quotes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export async function updateQuotes(request: UpdateQuotesRequest) {
117117
data.quotes = quotes as never;
118118
data.quotesLastUpdated = Date.now();
119119
data.totals = totals;
120-
data.isLoading = false;
121120
});
122121
}
123122

0 commit comments

Comments
 (0)