Skip to content

Commit a906c6e

Browse files
committed
feat: sort by dollar balance
1 parent 8763876 commit a906c6e

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

packages/thirdweb/src/react/core/hooks/usePaymentMethods.ts

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { ThirdwebClient } from "../../../client/client.js";
66
import { NATIVE_TOKEN_ADDRESS } from "../../../constants/addresses.js";
77
import { isInsightEnabled } from "../../../insight/common.js";
88
import { getOwnedTokens } from "../../../insight/get-tokens.js";
9+
import { toTokens } from "../../../utils/units.js";
910
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
1011
import type { PaymentMethod } from "../machines/paymentMachine.js";
1112
import { useActiveWallet } from "./wallets/useActiveWallet.js";
@@ -126,31 +127,42 @@ export function usePaymentMethods(options: {
126127
Number.parseFloat(destinationAmount) * destinationToken.priceUsd;
127128
console.log("requiredDollarAmount", requiredDollarAmount);
128129

129-
// TODO (bridge): sort owned by priceUsd if there's a way to get it from the routes endpoint
130-
// owned.sort((a, b) => {
131-
// const aDollarBalance =
132-
// Number.parseFloat(a.balance.displayValue) * a.originToken.priceUsd;
133-
// const bDollarBalance =
134-
// Number.parseFloat(b.balance.displayValue) * b.originToken.priceUsd;
135-
// return bDollarBalance - aDollarBalance;
136-
// });
130+
owned.sort((a, b) => {
131+
const aDollarBalance =
132+
Number.parseFloat(toTokens(a.balance, a.originToken.decimals)) *
133+
a.originToken.priceUsd;
134+
const bDollarBalance =
135+
Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) *
136+
b.originToken.priceUsd;
137+
return bDollarBalance - aDollarBalance;
138+
});
137139

138140
const suitableOriginTokens: OwnedTokenWithQuote[] = [];
139141

140142
for (const b of owned) {
141143
if (b.originToken && b.balance > 0n) {
142-
// TODO (bridge): add back in if we get priceUsd from the routes endpoint
143-
// const dollarBalance =
144-
// Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) *
145-
// b.originToken.priceUsd;
146-
// if (b.originToken.priceUsd && dollarBalance < requiredDollarAmount) {
147-
// console.log(
148-
// "skipping",
149-
// b.originToken.symbol,
150-
// "because it's not enough",
151-
// );
152-
// continue;
153-
// }
144+
const dollarBalance =
145+
Number.parseFloat(toTokens(b.balance, b.originToken.decimals)) *
146+
b.originToken.priceUsd;
147+
console.log(
148+
"required amount for",
149+
b.originToken.symbol,
150+
"is",
151+
requiredDollarAmount,
152+
"Price is",
153+
b.originToken.priceUsd,
154+
"Chain is",
155+
b.originToken.chainId,
156+
);
157+
console.log("dollarBalance", dollarBalance);
158+
if (b.originToken.priceUsd && dollarBalance < requiredDollarAmount) {
159+
console.log(
160+
"skipping",
161+
b.originToken.symbol,
162+
"because it's not enough",
163+
);
164+
continue;
165+
}
154166

155167
suitableOriginTokens.push({
156168
balance: b.balance,
@@ -163,14 +175,8 @@ export function usePaymentMethods(options: {
163175
console.log("suitableOriginTokens", suitableOriginTokens.length);
164176
console.timeEnd("routes");
165177

166-
// sort by popular tokens - same chain first, then all native currencies, then USDC, then USDT, then other tokens
167-
const sortedSuitableOriginTokens = sortOwnedTokens(
168-
suitableOriginTokens,
169-
destinationToken,
170-
);
171-
172178
const transformedRoutes = [
173-
...sortedSuitableOriginTokens.map((s) => ({
179+
...suitableOriginTokens.map((s) => ({
174180
type: "wallet" as const,
175181
payerWallet: wallet,
176182
originToken: s.originToken,

0 commit comments

Comments
 (0)