Skip to content

Commit

Permalink
docs(fund with ar): add examples for topUpWithTokens PE-5849
Browse files Browse the repository at this point in the history
  • Loading branch information
fedellen committed Mar 28, 2024
1 parent b4f3dc6 commit 05dc210
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ Types are exported from `./lib/types/[node/web]/index.d.ts` and should be automa
});
```

- `topUpWithTokens({ tokenAmount, feeMultiplier })` - Tops up the connected wallet with Credits by submitting a payment transaction for the token amount to the Turbo wallet and then submitting that transaction id to the Turbo for processing.s The `tokenAmount` is the amount of tokens in the token type's smallest unit value (e.g: Winston for arweave token type) to fund the wallet with. The `feeMultiplier` is the multiplier to apply to the reward for the transaction to modify its chances of being mined. Credits will be added to the wallet balance after the transaction is confirmed on the given blockchains.
- `topUpWithTokens({ tokenAmount, feeMultiplier })` - Tops up the connected wallet with Credits by submitting a payment transaction for the token amount to the Turbo wallet and then submitting that transaction id to Turbo Payment Service for processing.
- The `tokenAmount` is the amount of tokens in the token type's smallest unit value (e.g: Winston for arweave token type) to fund the wallet with.
- The `feeMultiplier` is the multiplier to apply to the reward for the transaction to modify its chances of being mined. Credits will be added to the wallet balance after the transaction is confirmed on the given blockchain.

```typescript
const turbo = TurboFactory.authenticated({ signer, token: 'arweave' });
Expand Down
9 changes: 9 additions & 0 deletions examples/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,13 @@ const {
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: 1, /// 0.000_000_000_000_001 AR
});
console.log(JSON.stringify(topUpResult, null, 2));
})();
9 changes: 9 additions & 0 deletions examples/esm/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ import fs from 'fs';
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: 1, /// 0.000_000_000_000_001 AR
});
console.log(JSON.stringify(topUpResult, null, 2));
})();
9 changes: 9 additions & 0 deletions examples/typescript/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ import path from 'path';
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: 1, /// 0.000_000_000_000_001 AR
});
console.log(JSON.stringify(topUpResult, null, 2));
})();
9 changes: 9 additions & 0 deletions examples/typescript/esm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,13 @@ import fs from 'fs';
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: 1, /// 0.000_000_000_000_001 AR
});
console.log(JSON.stringify(topUpResult, null, 2));
})();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"test:node": "c8 mocha --config .mocharc --exclude tests/**/*.web.test.ts --exit",
"test:docker": "docker compose up --exit-code-from test-runner --build",
"prepare": "husky install",
"examples": "yarn example:mjs & yarn example:cjs & yarn example:ts:esm & yarn example:ts:cjs",
"examples": "yarn example:esm & yarn example:cjs & yarn example:ts:esm & yarn example:ts:cjs",
"example:esm": "cd examples/esm && yarn && node index.mjs",
"example:cjs": "cd examples/cjs && yarn && node index.cjs",
"example:web": "http-server --port 8080 --host -o examples/web",
Expand Down
2 changes: 1 addition & 1 deletion src/common/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class TurboAuthenticatedPaymentService
reward: signedTx.reward,
};

Check warning on line 316 in src/common/payment.ts

View check run for this annotation

Codecov / codecov/patch

src/common/payment.ts#L312-L316

Added lines #L312 - L316 were not covered by tests
} catch (e) {
this.logger.error('Failed to submit fund transaction...', { e });
this.logger.error('Failed to submit fund transaction...', e);

throw Error(
`Failed to submit fund transaction! Save this Transaction ID and try again with 'turbo.submitFundTransaction(id)': ${txId}`,
Expand Down
6 changes: 3 additions & 3 deletions src/common/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export class ArweaveToken implements BaseToken<Transaction.default> {
logger = new TurboWinstonLogger(),
mintU = true,
pollingOptions = {
maxAttempts: 30,
pollingIntervalMs: 1_000,
initialBackoffMs: 5_000,
maxAttempts: 10,
pollingIntervalMs: 3_000,
initialBackoffMs: 7_000,
},
}: {
arweave: Arweave;
Expand Down

0 comments on commit 05dc210

Please sign in to comment.