Skip to content

Commit

Permalink
adding api for pay direct transfers (#4224)
Browse files Browse the repository at this point in the history
Co-authored-by: Joaquim Verges <[email protected]>
Co-authored-by: Edward Sun <[email protected]>
Co-authored-by: Edward Sun <[email protected]>
  • Loading branch information
4 people authored Aug 23, 2024
1 parent 120d61f commit 1405598
Show file tree
Hide file tree
Showing 15 changed files with 627 additions and 278 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-fishes-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Added support for direct transfers in Pay
8 changes: 4 additions & 4 deletions apps/playground-web/src/components/pay/direct-payment.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use client";

import { base } from "thirdweb/chains";
import { sepolia } from "thirdweb/chains";
import { PayEmbed, getDefaultToken } from "thirdweb/react";
import { THIRDWEB_CLIENT } from "../../lib/client";
import { StyledConnectButton } from "../styled-connect-button";
Expand All @@ -16,9 +16,9 @@ export function BuyMerchPreview() {
payOptions={{
mode: "direct_payment",
paymentInfo: {
amount: "15",
chain: base,
token: getDefaultToken(base, "USDC"),
amount: "0.1",
chain: sepolia,
token: getDefaultToken(sepolia, "USDC"),
sellerAddress: "0xEb0effdFB4dC5b3d5d3aC6ce29F3ED213E95d675",
},
metadata: {
Expand Down
8 changes: 5 additions & 3 deletions apps/playground-web/src/components/pay/transaction-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import { useTheme } from "next-themes";
import { getContract } from "thirdweb";
import { base, polygon } from "thirdweb/chains";
import { polygon, sepolia } from "thirdweb/chains";
import { transfer } from "thirdweb/extensions/erc20";
import { claimTo, getNFT } from "thirdweb/extensions/erc1155";
import {
PayEmbed,
TransactionButton,
getDefaultToken,
useActiveAccount,
useReadContract,
} from "thirdweb/react";
Expand All @@ -21,8 +22,9 @@ const nftContract = getContract({
});

const usdcContract = getContract({
address: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
chain: base,
// biome-ignore lint/style/noNonNullAssertion: its there
address: getDefaultToken(sepolia, "USDC")!.address,
chain: sepolia,
client: THIRDWEB_CLIENT,
});

Expand Down
13 changes: 11 additions & 2 deletions packages/thirdweb/src/exports/pay.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
export {
getBuyWithCryptoQuote,
type BuyWithCryptoQuote,
type QuoteApprovalParams,
type QuoteTokenInfo,
type GetBuyWithCryptoQuoteParams,
} from "../pay/buyWithCrypto/getQuote.js";

export {
getBuyWithCryptoTransfer,
type BuyWithCryptoTransfer,
type GetBuyWithCryptoTransferParams,
} from "../pay/buyWithCrypto/getTransfer.js";

export {
type QuoteApprovalParams,
type QuoteTokenInfo,
} from "../pay/buyWithCrypto/commonTypes.js";

export {
getBuyWithCryptoStatus,
type BuyWithCryptoStatus,
Expand Down
13 changes: 11 additions & 2 deletions packages/thirdweb/src/exports/thirdweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,14 @@ export { toEther, toTokens, toUnits, toWei, fromGwei } from "../utils/units.js";
export {
getBuyWithCryptoQuote,
type BuyWithCryptoQuote,
type QuoteApprovalParams,
type QuoteTokenInfo,
type GetBuyWithCryptoQuoteParams,
} from "../pay/buyWithCrypto/getQuote.js";

export {
type QuoteApprovalParams,
type QuoteTokenInfo,
} from "../pay/buyWithCrypto/commonTypes.js";

export {
getBuyWithCryptoStatus,
type BuyWithCryptoStatus,
Expand All @@ -183,6 +186,12 @@ export {
type BuyWithCryptoHistoryParams,
} from "../pay/buyWithCrypto/getHistory.js";

export {
getBuyWithCryptoTransfer,
type GetBuyWithCryptoTransferParams,
type BuyWithCryptoTransfer,
} from "../pay/buyWithCrypto/getTransfer.js";

export type {
PayOnChainTransactionDetails,
PayTokenInfo,
Expand Down
40 changes: 40 additions & 0 deletions packages/thirdweb/src/pay/buyWithCrypto/commonTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { ApproveParams } from "../../extensions/erc20/write/approve.js";
import type { BaseTransactionOptions } from "../../transaction/types.js";

export type QuoteTokenInfo = {
chainId: number;
tokenAddress: string;
decimals: number;
priceUSDCents: number;
name?: string;
symbol?: string;
};

export type QuoteTransactionRequest = {
data: string;
to: string;
value: string;
from: string;
chainId: number;
gasPrice: string;
gasLimit: string;
};

export type QuoteApprovalInfo = {
chainId: number;
tokenAddress: string;
spenderAddress: string;
amountWei: string;
};

export type QuotePaymentToken = {
token: QuoteTokenInfo;
amountWei: string;
amount: string;
amountUSDCents: number;
};

/**
* @buyCrypto
*/
export type QuoteApprovalParams = BaseTransactionOptions<ApproveParams>;
50 changes: 8 additions & 42 deletions packages/thirdweb/src/pay/buyWithCrypto/getQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import type { Hash } from "viem";
import { getCachedChain } from "../../chains/utils.js";
import type { ThirdwebClient } from "../../client/client.js";
import { getContract } from "../../contract/contract.js";
import {
type ApproveParams,
approve,
} from "../../extensions/erc20/write/approve.js";
import { approve } from "../../extensions/erc20/write/approve.js";
import type { PrepareTransactionOptions } from "../../transaction/prepare-transaction.js";
import type { BaseTransactionOptions } from "../../transaction/types.js";
import { getClientFetch } from "../../utils/fetch.js";
import { getPayBuyWithCryptoQuoteEndpoint } from "../utils/definitions.js";
import type {
QuoteApprovalInfo,
QuotePaymentToken,
QuoteTokenInfo,
QuoteTransactionRequest,
} from "./commonTypes.js";

/**
* The parameters for [`getBuyWithCryptoQuote`](https://portal.thirdweb.com/references/typescript/v5/getBuyWithCryptoQuote) function
Expand Down Expand Up @@ -107,40 +109,9 @@ export type GetBuyWithCryptoQuoteParams = {
/**
* @buyCrypto
*/
export type QuoteTokenInfo = {
chainId: number;
tokenAddress: string;
decimals: number;
priceUSDCents: number;
name?: string;
symbol?: string;
};

type QuotePaymentToken = {
token: QuoteTokenInfo;
amountWei: string;
amount: string;
amountUSDCents: number;
};

type QuoteTransactionRequest = {
data: string;
to: string;
value: string;
from: string;
chainId: number;
gasPrice: string;
gasLimit: string;
};

type BuyWithCryptoQuoteRouteResponse = {
transactionRequest: QuoteTransactionRequest;
approval?: {
chainId: number;
tokenAddress: string;
spenderAddress: string;
amountWei: string;
};
approval?: QuoteApprovalInfo;

fromAddress: string;
toAddress: string;
Expand Down Expand Up @@ -173,11 +144,6 @@ type BuyWithCryptoQuoteRouteResponse = {
bridge?: string;
};

/**
* @buyCrypto
*/
export type QuoteApprovalParams = BaseTransactionOptions<ApproveParams>;

/**
* @buyCrypto
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export type BuyWithCryptoSubStatuses =
| "UNKNOWN_ERROR"
| "REFUNDED";

export type SwapType = "SAME_CHAIN" | "CROSS_CHAIN";
export type SwapType = "SAME_CHAIN" | "CROSS_CHAIN" | "TRANSFER";

/**
* The object returned by the [`getBuyWithCryptoStatus`](https://portal.thirdweb.com/references/typescript/v5/getBuyWithCryptoStatus) function to represent the status of a quoted transaction
Expand Down
Loading

0 comments on commit 1405598

Please sign in to comment.