Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/all-dolls-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Enhance compatiblity with third party x402 libs
7 changes: 4 additions & 3 deletions packages/thirdweb/src/x402/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Abi } from "abitype";
import { toFunctionSelector } from "viem/utils";
import { type Money, moneySchema } from "x402/types";
import { ChainIdToNetwork, type Money, moneySchema } from "x402/types";
import { getCachedChain } from "../chains/utils.js";
import type { ThirdwebClient } from "../client/client.js";
import { resolveContractAbi } from "../contract/actions/resolve-abi.js";
Expand Down Expand Up @@ -98,9 +98,10 @@ export async function decodePaymentRequest(

const paymentRequirements: RequestedPaymentRequirements[] = [];

const mappedNetwork = ChainIdToNetwork[chainId];
paymentRequirements.push({
scheme: "exact",
network: `eip155:${chainId}`,
network: mappedNetwork ? mappedNetwork : `eip155:${chainId}`,
maxAmountRequired,
resource: resourceUrl,
description: description ?? "",
Expand Down Expand Up @@ -263,7 +264,7 @@ async function getDefaultAsset(
): Promise<DefaultAsset | undefined> {
const supportedAssets = await facilitator.supported();
const matchingAsset = supportedAssets.kinds.find(
(supported) => supported.network === `eip155:${chainId}`,
(supported) => networkToChainId(supported.network) === chainId,
);
const assetConfig = matchingAsset?.extra?.defaultAsset as DefaultAsset;
return assetConfig;
Expand Down
4 changes: 1 addition & 3 deletions packages/thirdweb/src/x402/fetchWithPayment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ChainIdToNetwork } from "x402/types";
import { getCachedChain } from "../chains/utils.js";
import type { ThirdwebClient } from "../client/client.js";
import type { Wallet } from "../wallets/interfaces/wallet.js";
Expand Down Expand Up @@ -149,10 +148,9 @@ function defaultPaymentRequirementsSelector(
`No valid payment requirements found in server 402 response. ${error}`,
);
}
const currentWalletNetwork = ChainIdToNetwork[chainId] || `eip155:${chainId}`;
// find the payment requirements matching the connected wallet chain
const matchingPaymentRequirements = paymentRequirements.find(
(x) => x.network === currentWalletNetwork && x.scheme === scheme,
(x) => networkToChainId(x.network) === chainId && x.scheme === scheme,
);

if (matchingPaymentRequirements) {
Expand Down
Loading