@ardrive/turbo-sdk 1.0.0-beta.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +88 -9
- package/package.json +1 -1
package/README.md
CHANGED
@@ -5,6 +5,7 @@ Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionality for interac
|
|
5
5
|
## Table of Contents
|
6
6
|
|
7
7
|
- [Installation](#installation)
|
8
|
+
- [Quick Start](#quick-start)
|
8
9
|
- [Usage](#usage)
|
9
10
|
- [NodeJS Environments](#nodejs)
|
10
11
|
- [CommonJS](#commonjs)
|
@@ -13,7 +14,11 @@ Welcome to the `@ardrive/turbo-sdk`! This SDK provides functionality for interac
|
|
13
14
|
- [Bundlers (Webpack, Rollup, ESbuild, etc.)](#bundlers-webpack-rollup-esbuild-etc)
|
14
15
|
- [Browser](#browser)
|
15
16
|
- [Typescript](#typescript)
|
16
|
-
|
17
|
+
- [APIs](#apis)
|
18
|
+
- [TurboFactory](#turbofactory)
|
19
|
+
- [TurboUnauthenticatedClient](#turbounauthenticatedclient)
|
20
|
+
- [TurboAuthenticatedClient](#turboauthenticatedclient)
|
21
|
+
- [Examples](./examples)
|
17
22
|
- [Contributions](#contributions)
|
18
23
|
|
19
24
|
## Installation
|
@@ -28,6 +33,57 @@ or
|
|
28
33
|
yarn add @ardrive/turbo-sdk
|
29
34
|
```
|
30
35
|
|
36
|
+
## Quick Start
|
37
|
+
|
38
|
+
```typescript
|
39
|
+
import { TurboFactory } from '@ardrive/turbo-sdk';
|
40
|
+
|
41
|
+
// load your JWK from a file or generate a new one
|
42
|
+
const jwk = fs.readFileSync('./my-jwk.json');
|
43
|
+
const address = arweave.wallets.jwkToAddress(jwk);
|
44
|
+
const turbo = TurboFactory.authenticated({ privateKey: jwk });
|
45
|
+
|
46
|
+
// get the wallet balance
|
47
|
+
const { winc: balance } = await turbo.getBalance();
|
48
|
+
|
49
|
+
// prep file for upload
|
50
|
+
const filePath = path.join(__dirname, './my-image.png');
|
51
|
+
const fileSize = fs.statSync(filePath).size;
|
52
|
+
|
53
|
+
// get the cost of uploading the file
|
54
|
+
const [{ winc: fileSizeCost }] = await turbo.getUploadCosts({
|
55
|
+
bytes: [fileSize],
|
56
|
+
});
|
57
|
+
|
58
|
+
// check if balance greater than upload cost
|
59
|
+
if (balance < fileSizeCost) {
|
60
|
+
const { url } = await turbo.createCheckoutSession({
|
61
|
+
amount: fileSizeCost,
|
62
|
+
owner: address,
|
63
|
+
// add a promo code if you have one
|
64
|
+
});
|
65
|
+
// open the URL to top-up, continue when done
|
66
|
+
open(url);
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
|
70
|
+
// upload the file
|
71
|
+
try {
|
72
|
+
const { id, owner, dataCaches, fastFinalityIndexes } = await turbo.uploadFile(() => {
|
73
|
+
fileStreamFactory => () => fs.createReadStream(filePath),
|
74
|
+
fileSizeFactory => () => fileSize,
|
75
|
+
});
|
76
|
+
// upload complete!
|
77
|
+
console.log('Successfully upload data item!', { id, owner, dataCaches, fastFinalityIndexes });
|
78
|
+
} catch (error) {
|
79
|
+
// upload failed
|
80
|
+
console.error('Failed to upload data item!', error);
|
81
|
+
} finally {
|
82
|
+
const { winc: newBalance } = await turbo.getBalance();
|
83
|
+
console.log('New balance:', newBalance);
|
84
|
+
}
|
85
|
+
```
|
86
|
+
|
31
87
|
## Usage
|
32
88
|
|
33
89
|
The SDK is provided in both CommonJS and ESM formats, and it's compatible with bundlers such as Webpack, Rollup, and ESbuild. Utilize the appropriate named exports provided by this SDK's [package.json] based on your project's configuration. Refer to the [examples] directory to see how to use the SDK in various environments.
|
@@ -127,7 +183,7 @@ import { TurboFactory } from '@ardrive/turbo-sdk/web';
|
|
127
183
|
// or '@ardrive/turbo-sdk/node' for Node.js projects
|
128
184
|
```
|
129
185
|
|
130
|
-
Types are exported from `./lib/types/index.d.ts` and should be automatically recognized, offering benefits such as type-checking and autocompletion.
|
186
|
+
Types are exported from `./lib/types/[node/web]/index.d.ts` and should be automatically recognized, offering benefits such as type-checking and autocompletion.
|
131
187
|
|
132
188
|
## APIs
|
133
189
|
|
@@ -160,7 +216,7 @@ Types are exported from `./lib/types/index.d.ts` and should be automatically rec
|
|
160
216
|
const countries = await turbo.getSupportedCountries();
|
161
217
|
```
|
162
218
|
|
163
|
-
- `getFiatToAR()` - Returns the current raw fiat to AR conversion rate for a specific currency as reported by third-party pricing oracles.
|
219
|
+
- `getFiatToAR({ currency })` - Returns the current raw fiat to AR conversion rate for a specific currency as reported by third-party pricing oracles.
|
164
220
|
|
165
221
|
```typescript
|
166
222
|
const fiatToAR = await turbo.getFiatToAR({ currency: 'USD' });
|
@@ -178,6 +234,7 @@ Types are exported from `./lib/types/index.d.ts` and should be automatically rec
|
|
178
234
|
const { winc, paymentAmount, quotedPaymentAmount, adjustments } =
|
179
235
|
await turbo.getWincForFiat({
|
180
236
|
amount: USD(100),
|
237
|
+
// promo codes require an authenticated client
|
181
238
|
});
|
182
239
|
```
|
183
240
|
|
@@ -200,14 +257,14 @@ Types are exported from `./lib/types/index.d.ts` and should be automatically rec
|
|
200
257
|
});
|
201
258
|
```
|
202
259
|
|
203
|
-
- `createCheckoutSession({ amount, owner
|
260
|
+
- `createCheckoutSession({ amount, owner })` - Creates a Stripe checkout session for a Turbo Top Up with the provided amount, currency, owner. The returned URL can be opened in the browser, all payments are processed by Stripe. To leverage promo codes, see [TurboAuthenticatedClient].
|
204
261
|
|
205
262
|
```typescript
|
206
263
|
const { url, winc, paymentAmount, quotedPaymentAmount, adjustments } =
|
207
264
|
await turbo.createCheckoutSession({
|
208
265
|
amount: USD(10.0), // $10.00 USD
|
209
266
|
owner: publicArweaveAddress,
|
210
|
-
|
267
|
+
// promo codes require an authenticated client
|
211
268
|
});
|
212
269
|
|
213
270
|
// Open checkout session in a browser
|
@@ -231,14 +288,37 @@ Types are exported from `./lib/types/index.d.ts` and should be automatically rec
|
|
231
288
|
const { winc: balance } = await turbo.getBalance();
|
232
289
|
```
|
233
290
|
|
234
|
-
- `getWincForFiat({ amount, promoCodes })` - Returns the current amount of Winston Credits including all adjustments for the provided fiat currency, amount, and optional promo codes.
|
291
|
+
- `getWincForFiat({ amount, promoCodes })` - Returns the current amount of Winston Credits including all adjustments for the provided fiat currency, amount, and optional promo codes.
|
235
292
|
|
236
293
|
```typescript
|
237
294
|
const { winc, paymentAmount, quotedPaymentAmount, adjustments } =
|
238
295
|
await turbo.getWincForFiat({
|
239
296
|
amount: USD(100),
|
240
|
-
promoCodes: ['MY_PROMO_CODE'],
|
297
|
+
promoCodes: ['MY_PROMO_CODE'], // promo codes require an authenticated client
|
298
|
+
});
|
299
|
+
```
|
300
|
+
|
301
|
+
- `createCheckoutSession({ amount, owner, promoCodes })` - Creates a Stripe checkout session for a Turbo Top Up with the provided amount, currency, owner, and optional promo codes. The returned URL can be opened in the browser, all payments are processed by Stripe. Promo codes require an authenticated client.
|
302
|
+
|
303
|
+
```typescript
|
304
|
+
const { url, winc, paymentAmount, quotedPaymentAmount, adjustments } =
|
305
|
+
await turbo.createCheckoutSession({
|
306
|
+
amount: USD(10.0), // $10.00 USD
|
307
|
+
owner: publicArweaveAddress,
|
308
|
+
promoCodes: ['MY_PROMO_CODE'], // promo codes require an authenticated client
|
241
309
|
});
|
310
|
+
|
311
|
+
// Open checkout session in a browser
|
312
|
+
if (process.platform === 'darwin') {
|
313
|
+
// macOS
|
314
|
+
exec(`open ${url}`);
|
315
|
+
} else if (process.platform === 'win32') {
|
316
|
+
// Windows
|
317
|
+
exec(`start "" "${url}"`, { shell: true });
|
318
|
+
} else {
|
319
|
+
// Linux/Unix
|
320
|
+
open(url);
|
321
|
+
}
|
242
322
|
```
|
243
323
|
|
244
324
|
- `uploadFile({ fileStreamFactory, fileSizeFactory, signal })` - Signs and uploads a raw file. The provided `fileStreamFactory` should produce a NEW file data stream each time is it invoked. The `fileSizeFactory` is a function that returns the size of the file. The `signal` is an optional [AbortSignal] that can be used to cancel the upload or timeout the request.
|
@@ -259,6 +339,5 @@ If you encounter any issues or have feature requests, please file an issue on ou
|
|
259
339
|
|
260
340
|
[package.json]: ./package.json
|
261
341
|
[examples]: ./examples
|
262
|
-
[
|
263
|
-
[TurboAuthenticatedClient]: #turboAuthenticatedClient
|
342
|
+
[TurboAuthenticatedClient]: #turboauthenticatedclient
|
264
343
|
[AbortSignal]: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal
|