Skip to content

Commit

Permalink
collectFaucetTokens function accepts raw faucet package payload
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Aug 31, 2023
1 parent bb89ac1 commit 8f174cd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `dotobject` helper returns null when key is not found.

### Changed

- `collectFaucetTokens` function accepts raw faucet package payload.

### Added

- Census3 error typings
- Census3 error typings.

## [0.1.1] - 2023-08-14

Expand Down
14 changes: 8 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,15 +832,17 @@ export class VocdoniSDKClient {
/**
* Calls the faucet to get new tokens. Only under development.
*
* @param {string} faucetPackage The faucet package
* @returns {Promise<AccountData>} Account data information updated with new balance
*/
collectFaucetTokens(): Promise<AccountData> {
collectFaucetTokens(faucetPackage?: string): Promise<AccountData> {
invariant(this.wallet, 'No wallet or signer set');
return Promise.all([this.fetchAccountInfo(), this.fetchFaucetPayload(), this.fetchChainId()])
.then((data) => {
const faucetPackage = this.parseFaucetPackage(data[1]);
const collectFaucetTx = AccountCore.generateCollectFaucetTransaction(data[0], faucetPackage);
return AccountCore.signTransaction(collectFaucetTx, data[2], this.wallet);
const faucet = faucetPackage ? Promise.resolve(faucetPackage) : this.fetchFaucetPayload();
return Promise.all([this.fetchAccountInfo(), faucet, this.fetchChainId()])
.then(([account, faucet, chainId]) => {
const faucetPackage = this.parseFaucetPackage(faucet);
const collectFaucetTx = AccountCore.generateCollectFaucetTransaction(account, faucetPackage);
return AccountCore.signTransaction(collectFaucetTx, chainId, this.wallet);
})
.then((signedTx) => ChainAPI.submitTx(this.url, signedTx))
.then((txData) => this.waitForTransaction(txData.hash))
Expand Down
9 changes: 9 additions & 0 deletions test/integration/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ describe('Account integration tests', () => {
.collectFaucetTokens()
.then((finalAccountInfo) => expect(finalAccountInfo.balance).toBeGreaterThan(accountInfo.balance));
}, 75000);
it('should bootstrap a new account and fetch tokens from raw faucet package', async () => {
const accountInfo = await client.createAccount();
expect(accountInfo.balance).toBeGreaterThan(0);

await client
.fetchFaucetPayload()
.then((faucetPackage) => client.collectFaucetTokens(faucetPackage))
.then((finalAccountInfo) => expect(finalAccountInfo.balance).toBeGreaterThan(accountInfo.balance));
}, 75000);
it('should bootstrap a new account and do nothing when creating it twice', async () => {
const accountInfo = await client.createAccount();
const accountInfoAfter = await client.createAccount();
Expand Down

0 comments on commit 8f174cd

Please sign in to comment.