-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetOnrampUrl.ts
144 lines (134 loc) · 5.35 KB
/
getOnrampUrl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { ONRAMP_BUY_URL } from "@/constants/constants";
/**
* Props used to get an Onramp buy URL by directly providing a CDP project ID.
* See https://docs.cdp.coinbase.com/onramp/docs/api-initializing#generating-the-coinbase-onramp-buysell-url
*
* Note: exported as public Type
*/
export type GetOnrampUrlWithProjectIdParams = {
/**
* The Project ID of your CDP project created at https://portal.cdp.coinbase.com/
* This must be provided if you don't provide a sessionToken.
*/
projectId: string;
sessionToken?: never;
/**
* The addresses that the customer's funds should be delivered to.
*
* Each entry in the record represents a wallet address and the networks it is valid for. There should only be a
* single address for each network your app supports. Users will be able to buy/send any asset supported by any of
* the networks you specify. See the assets param if you want to restrict the available assets.
*
* Some common examples:
*
* Support all assets that are available for sending on the base network, only on the base network:
*
* `{ "0x1": ["base"] }`
*/
addresses: Record<string, string[]>;
/**
* This optional parameter will restrict the assets available for the user to buy/send. It acts as a filter on the
* networks specified in the {addresses} param.
*
* Some common examples:
*
* Support only USDC on either the base network or the ethereum network:
*
* `addresses: { "0x1": ["base", "ethereum"] }, assets: ["USDC"]`
*
* The values in this list can either be asset symbols like BTC, ETH, or asset UUIDs that you can get from the Buy
* Options API {@link https://docs.cdp.coinbase.com/onramp/docs/api-configurations/#buy-options}.
*/
assets?: string[];
} & GetOnrampBuyUrlOptionalProps;
/**
* Props used to get an Onramp buy URL using a session token created using the Onramp session token API.
* See https://docs.cdp.coinbase.com/onramp/docs/api-initializing#getting-an-coinbase-onramp-buysell-session-token
*
* Note: exported as public Type
*/
export type GetOnrampUrlWithSessionTokenParams = {
/**
* A session token create using the Onramp session token API. The token will be linked to the project ID, addresses,
* and assets params provided in the create session token API request.
*/
sessionToken: string;
projectId?: never;
addresses?: never;
assets?: never;
} & GetOnrampBuyUrlOptionalProps;
/**
* The optional properties that can be used to create an Onramp buy URL.
*/
type GetOnrampBuyUrlOptionalProps = {
/**
* If specified, this asset will be automatically selected for the user in the Onramp UI. Should be a valid asset
* symbol e.g. BTC, ETH, USDC.
*/
defaultAsset?: string;
/**
* If specified, this network will be automatically selected for the user in the Onramp UI. Should be a valid network
* name in lower case e.g. ethereum, base.
*/
defaultNetwork?: string;
/**
* A unique identifier that will be associated with any transactions created by the user during their Onramp session.
* You can use this with the Transaction Status API to check the status of the user's transaction.
* See https://docs.cdp.coinbase.com/onramp/docs/api-reporting#buy-transaction-status
*/
partnerUserId?: string;
/**
* This amount will be used to pre-fill the amount of crypto the user is buying or sending. The user can choose to
* change this amount in the UI. Only one of presetCryptoAmount or presetFiatAmount should be provided.
*/
presetCryptoAmount?: number;
/**
* This amount will be used to pre-fill the fiat value of the crypto the user is buying or sending. The user can
* choose to change this amount in the UI. Only one of presetCryptoAmount or presetFiatAmount should be provided.
*/
presetFiatAmount?: number;
/**
* The default payment method that will be selected for the user in the Onramp UI. Should be one of the payment methods
*/
defaultPaymentMethod?: string;
/**
* The currency code of the fiat amount provided in the presetFiatAmount param e.g. USD, CAD, EUR.
*/
fiatCurrency?: string;
/**
* A URL that the user will be automatically redirected to after a successful buy/send. The domain must match a domain
* on the domain allowlist in Coinbase Developer Platform (https://portal.cdp.coinbase.com/products/onramp).
*/
redirectUrl?: string;
/**
* The name of the component that is calling the Onramp buy URL. This will be used for analytics.
*/
originComponentName?: string;
};
/**
* Builds a Coinbase Onramp buy URL using the provided parameters.
* @param projectId a projectId generated in the Coinbase Developer Portal
* @returns the URL
*/
export function getOnrampBuyUrl({
projectId,
...props
}: GetOnrampUrlWithProjectIdParams | GetOnrampUrlWithSessionTokenParams) {
const url = new URL(ONRAMP_BUY_URL);
if (projectId !== undefined) {
// Coinbase Onramp requires projectId to be passed as appId
url.searchParams.append("appId", projectId);
}
for (const key of Object.keys(props) as (keyof typeof props)[]) {
const value = props[key];
if (value !== undefined) {
if (["string", "number", "boolean"].includes(typeof value)) {
url.searchParams.append(key as string, value.toString());
} else {
url.searchParams.append(key as string, JSON.stringify(value));
}
}
}
url.searchParams.sort();
return url.toString();
}