Skip to content

Commit bf46520

Browse files
[SDK] Enhance compatibility with third party x402 libs (#8314)
1 parent 7defce7 commit bf46520

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

.changeset/all-dolls-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Enhance compatiblity with third party x402 libs

packages/thirdweb/src/x402/common.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Abi } from "abitype";
22
import { toFunctionSelector } from "viem/utils";
3-
import { type Money, moneySchema } from "x402/types";
3+
import { ChainIdToNetwork, type Money, moneySchema } from "x402/types";
44
import { getCachedChain } from "../chains/utils.js";
55
import type { ThirdwebClient } from "../client/client.js";
66
import { resolveContractAbi } from "../contract/actions/resolve-abi.js";
@@ -98,9 +98,10 @@ export async function decodePaymentRequest(
9898

9999
const paymentRequirements: RequestedPaymentRequirements[] = [];
100100

101+
const mappedNetwork = ChainIdToNetwork[chainId];
101102
paymentRequirements.push({
102103
scheme: "exact",
103-
network: `eip155:${chainId}`,
104+
network: mappedNetwork ? mappedNetwork : `eip155:${chainId}`,
104105
maxAmountRequired,
105106
resource: resourceUrl,
106107
description: description ?? "",
@@ -263,7 +264,7 @@ async function getDefaultAsset(
263264
): Promise<DefaultAsset | undefined> {
264265
const supportedAssets = await facilitator.supported();
265266
const matchingAsset = supportedAssets.kinds.find(
266-
(supported) => supported.network === `eip155:${chainId}`,
267+
(supported) => networkToChainId(supported.network) === chainId,
267268
);
268269
const assetConfig = matchingAsset?.extra?.defaultAsset as DefaultAsset;
269270
return assetConfig;

packages/thirdweb/src/x402/fetchWithPayment.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ChainIdToNetwork } from "x402/types";
21
import { getCachedChain } from "../chains/utils.js";
32
import type { ThirdwebClient } from "../client/client.js";
43
import type { Wallet } from "../wallets/interfaces/wallet.js";
@@ -149,10 +148,9 @@ function defaultPaymentRequirementsSelector(
149148
`No valid payment requirements found in server 402 response. ${error}`,
150149
);
151150
}
152-
const currentWalletNetwork = ChainIdToNetwork[chainId] || `eip155:${chainId}`;
153151
// find the payment requirements matching the connected wallet chain
154152
const matchingPaymentRequirements = paymentRequirements.find(
155-
(x) => x.network === currentWalletNetwork && x.scheme === scheme,
153+
(x) => networkToChainId(x.network) === chainId && x.scheme === scheme,
156154
);
157155

158156
if (matchingPaymentRequirements) {

0 commit comments

Comments
 (0)