Skip to content

Commit 89ccc80

Browse files
joaquim-vergesgregfromstlclaudekumaryash90d4mr
authored
Add Bridge.Onramp functions + port old functions (#7076)
Signed-off-by: samina <57885104+saminacodes@users.noreply.github.com> Signed-off-by: Maximilian Hubert <64627729+gap-editor@users.noreply.github.com> Co-authored-by: gregfromstl <gregfromstl@gmail.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: kumaryash90 <kumaryashcse@gmail.com> Co-authored-by: Prithvish Baidya <deformercoding@gmail.com> Co-authored-by: samina <57885104+saminacodes@users.noreply.github.com> Co-authored-by: Maximilian Hubert <64627729+gap-editor@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com> Co-authored-by: arcoraven <arcoraven@gmail.com>
1 parent 713ebe0 commit 89ccc80

31 files changed

+938
-1518
lines changed

.changeset/tidy-seas-sing.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Added Bridge.Onramp.prepare and Bridge.Onramp.status functions
6+
7+
## Bridge.Onramp.prepare
8+
9+
Prepares an onramp transaction, returning a link from the specified provider to onramp to the specified token.
10+
11+
```typescript
12+
import { Bridge } from "thirdweb";
13+
import { ethereum } from "thirdweb/chains";
14+
import { NATIVE_TOKEN_ADDRESS, toWei } from "thirdweb/utils";
15+
16+
const preparedOnramp = await Bridge.Onramp.prepare({
17+
client: thirdwebClient,
18+
onramp: "stripe",
19+
chainId: ethereum.id,
20+
tokenAddress: NATIVE_TOKEN_ADDRESS,
21+
receiver: "0x...", // receiver's address
22+
amount: toWei("10"), // 10 of the destination token
23+
// Optional params:
24+
// sender: "0x...", // sender's address
25+
// onrampTokenAddress: NATIVE_TOKEN_ADDRESS, // token to initially onramp to
26+
// onrampChainId: 1, // chain to initially onramp to
27+
// currency: "USD",
28+
// maxSteps: 2,
29+
// purchaseData: { customId: "123" }
30+
});
31+
32+
console.log(preparedOnramp.link); // URL to redirect the user to
33+
console.log(preparedOnramp.currencyAmount); // Price in fiat currency
34+
```
35+
36+
## Bridge.Onramp.status
37+
38+
Retrieves the status of an Onramp session created via Bridge.Onramp.prepare.
39+
40+
```typescript
41+
import { Bridge } from "thirdweb";
42+
43+
const onrampStatus = await Bridge.Onramp.status({
44+
id: "022218cc-96af-4291-b90c-dadcb47571ec",
45+
client: thirdwebClient,
46+
});
47+
48+
// Possible results:
49+
// {
50+
// status: "CREATED",
51+
// transactions: [],
52+
// purchaseData: {
53+
// orderId: "abc-123",
54+
// },
55+
// }
56+
//
57+
// or
58+
// {
59+
// status: "PENDING",
60+
// transactions: [],
61+
// purchaseData: {
62+
// orderId: "abc-123",
63+
// },
64+
// }
65+
//
66+
// or
67+
// {
68+
// status: "COMPLETED",
69+
// transactions: [
70+
// {
71+
// chainId: 1,
72+
// transactionHash: "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
73+
// },
74+
// ],
75+
// purchaseData: {
76+
// orderId: "abc-123",
77+
// },
78+
// }
79+
```

apps/portal/src/app/pay/customization/payembed/page.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ You can specify which onramp provider to present to your users. By default, we c
9292
<PayEmbed
9393
client={client}
9494
payOptions={{
95-
preferredProvider: "STRIPE" | "KADO" | "TRANSAK",
95+
preferredProvider: "COINBASE" | "STRIPE" | "TRANSAK",
9696
}}
9797
/>
9898
```

apps/portal/src/app/pay/customization/send-transaction/page.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ You can specify which onramp provider to present to your users. By default, we c
8888
```tsx
8989
const { mutate: sendTransaction } = useSendTransaction({
9090
payModal: {
91-
preferredProvider: "STRIPE" | "KADO" | "TRANSAK",
91+
preferredProvider: "COINBASE" | "STRIPE" | "TRANSAK",
9292
},
9393
});
9494
```

apps/portal/src/app/pay/testing-pay/page.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Developers can turn on Test Mode to test both fiat-to-crypto transactions and cr
1616

1717
## Buy With Fiat
1818

19-
By setting `testMode` to `true` for Buy With Fiat, you can enable test experiences for our underlying providers (Stripe, Kado, and Transak).
19+
By setting `testMode` to `true` for Buy With Fiat, you can enable test experiences for our underlying providers (Coinbase, Stripe, and Transak).
2020

2121
<Tabs defaultValue="connectbutton">
2222

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { toWei } from "src/utils/units.js";
2+
import { describe, expect, it } from "vitest";
3+
import { TEST_CLIENT } from "~test/test-clients.js";
4+
import * as Onramp from "./Onramp.js";
5+
6+
// Use the same receiver address as other bridge tests
7+
const RECEIVER_ADDRESS = "0x2a4f24F935Eb178e3e7BA9B53A5Ee6d8407C0709";
8+
const NATIVE_TOKEN_ADDRESS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
9+
10+
/**
11+
* These tests call the real Bridge Onramp API. They are executed only when a
12+
* `TW_SECRET_KEY` environment variable is present, mirroring the behaviour of
13+
* the other bridge tests in this package.
14+
*/
15+
describe.runIf(process.env.TW_SECRET_KEY)("Bridge.Onramp.prepare", () => {
16+
it("should prepare an onramp successfully", async () => {
17+
const prepared = await Onramp.prepare({
18+
client: TEST_CLIENT,
19+
onramp: "stripe",
20+
chainId: 1,
21+
tokenAddress: NATIVE_TOKEN_ADDRESS,
22+
receiver: RECEIVER_ADDRESS,
23+
amount: toWei("0.01"),
24+
});
25+
26+
expect(prepared).toBeDefined();
27+
28+
// The destinationAmount should be a bigint and greater than zero
29+
expect(typeof prepared.destinationAmount).toBe("bigint");
30+
expect(prepared.destinationAmount > 0n).toBe(true);
31+
32+
// A redirect link for the user should be provided
33+
expect(prepared.link).toBeDefined();
34+
expect(typeof prepared.link).toBe("string");
35+
36+
// Intent must be present and reference the correct receiver
37+
expect(prepared.intent).toBeDefined();
38+
expect(prepared.intent.receiver.toLowerCase()).toBe(
39+
RECEIVER_ADDRESS.toLowerCase(),
40+
);
41+
42+
// Steps array should be defined (it may be empty if the provider supports the destination token natively)
43+
expect(Array.isArray(prepared.steps)).toBe(true);
44+
});
45+
46+
it("should surface any errors", async () => {
47+
await expect(
48+
Onramp.prepare({
49+
client: TEST_CLIENT,
50+
onramp: "stripe",
51+
chainId: 444, // Unsupported chain ID
52+
tokenAddress: NATIVE_TOKEN_ADDRESS,
53+
receiver: RECEIVER_ADDRESS,
54+
amount: toWei("0.01"),
55+
}),
56+
).rejects.toThrowError();
57+
});
58+
59+
it("should prepare a Coinbase onramp successfully", async () => {
60+
const prepared = await Onramp.prepare({
61+
client: TEST_CLIENT,
62+
onramp: "coinbase",
63+
chainId: 1,
64+
tokenAddress: NATIVE_TOKEN_ADDRESS,
65+
receiver: RECEIVER_ADDRESS,
66+
amount: toWei("0.01"),
67+
});
68+
69+
expect(prepared).toBeDefined();
70+
71+
// The destinationAmount should be a bigint and greater than zero
72+
expect(typeof prepared.destinationAmount).toBe("bigint");
73+
expect(prepared.destinationAmount > 0n).toBe(true);
74+
75+
// A redirect link for the user should be provided
76+
expect(prepared.link).toBeDefined();
77+
expect(typeof prepared.link).toBe("string");
78+
79+
// Intent must be present and reference the correct receiver
80+
expect(prepared.intent).toBeDefined();
81+
expect(prepared.intent.receiver.toLowerCase()).toBe(
82+
RECEIVER_ADDRESS.toLowerCase(),
83+
);
84+
85+
// Steps array should be defined (it may be empty if the provider supports the destination token natively)
86+
expect(Array.isArray(prepared.steps)).toBe(true);
87+
});
88+
89+
it("should prepare a Transak onramp successfully", async () => {
90+
const prepared = await Onramp.prepare({
91+
client: TEST_CLIENT,
92+
onramp: "transak",
93+
chainId: 1,
94+
tokenAddress: NATIVE_TOKEN_ADDRESS,
95+
receiver: RECEIVER_ADDRESS,
96+
amount: toWei("0.01"),
97+
});
98+
99+
expect(prepared).toBeDefined();
100+
101+
// The destinationAmount should be a bigint and greater than zero
102+
expect(typeof prepared.destinationAmount).toBe("bigint");
103+
expect(prepared.destinationAmount > 0n).toBe(true);
104+
105+
// A redirect link for the user should be provided
106+
expect(prepared.link).toBeDefined();
107+
expect(typeof prepared.link).toBe("string");
108+
109+
// Intent must be present and reference the correct receiver
110+
expect(prepared.intent).toBeDefined();
111+
expect(prepared.intent.receiver.toLowerCase()).toBe(
112+
RECEIVER_ADDRESS.toLowerCase(),
113+
);
114+
115+
// Steps array should be defined (it may be empty if the provider supports the destination token natively)
116+
expect(Array.isArray(prepared.steps)).toBe(true);
117+
});
118+
});

0 commit comments

Comments
 (0)