-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
60 lines (55 loc) · 1.76 KB
/
index.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
// or use @ardrive/turbo-sdk/web depending on your environment
import {
TurboFactory,
USD,
WinstonToTokenAmount,
developmentTurboConfiguration,
} from '@ardrive/turbo-sdk/node';
import Arweave from 'arweave';
import fs from 'fs';
(async () => {
/**
* Generate a key from the arweave wallet.
*/
const arweave = new Arweave({});
const jwk = await arweave.wallets.generate();
/**
* Use the arweave key to create an authenticated turbo client
*/
const turboAuthClient = TurboFactory.authenticated({
privateKey: jwk,
...developmentTurboConfiguration,
});
/**
* Fetch the balance for the private key.
*/
const balance = await turboAuthClient.getBalance();
console.log('Balance:', balance);
/**
* Fetch the estimated amount of winc returned for 10 USD (1000 cents).
*/
const estimatedWinc = await turboAuthClient.getWincForFiat({
amount: USD(10),
});
console.log('10 USD to winc:', estimatedWinc);
/**
* Post local files to the Turbo service.
*/
console.log('Posting raw file to Turbo service...');
const filePath = new URL('../../files/1KB_file', import.meta.url).pathname;
const fileSize = fs.statSync(filePath).size;
const uploadResult = await turboAuthClient.uploadFile({
fileStreamFactory: () => fs.createReadStream(filePath),
fileSizeFactory: () => fileSize,
signal: AbortSignal.timeout(10_000), // cancel the upload after 10 seconds
});
console.log(JSON.stringify(uploadResult, null, 2));
/**
* Tops up a wallet with Credits using tokens.
* Default token is AR, using Winston as the unit.
*/
const topUpResult = await turboAuthClient.topUpWithTokens({
tokenAmount: WinstonToTokenAmount(100_000_000), // 0.0001 AR
});
console.log(JSON.stringify(topUpResult, null, 2));
})();