Implement requestAirdrop

This commit is contained in:
Michael Vines 2018-08-23 16:39:52 -07:00
parent b33633fb9c
commit 392672a5a7
2 changed files with 29 additions and 5 deletions

View File

@ -16,9 +16,9 @@ export type TransactionSignature = string;
/**
* @typedef {string} TransactionId
*/
export type TransactionId= string;
export type TransactionId = string;
type RpcRequest = (methodName: string, args: Array<string>) => any;
type RpcRequest = (methodName: string, args: Array<string|number>) => any;
function createRpcRequest(url): RpcRequest {
const server = jayson(
@ -88,6 +88,12 @@ const GetFinalityRpcResult = struct({
error: 'any?',
result: 'number?',
});
const RequestAirdropRpcResult = struct({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'boolean?',
});
function sleep(duration = 0) {
return new Promise((accept) => {
@ -158,9 +164,14 @@ export class Connection {
return Number(res.result);
}
async requestAirdrop(account: Account, amount: number): Promise<void> {
console.log(`TODO: airdrop ${amount} to ${account.publicKey}`);
await sleep(500); // TODO
async requestAirdrop(to: PublicKey, amount: number): Promise<void> {
const unsafeRes = await this._rpcRequest('requestAirdrop', [to, amount]);
const res = RequestAirdropRpcResult(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
assert(res.result);
}
async sendTokens(from: Account, to: PublicKey, amount: number): Promise<TransactionSignature> {

View File

@ -42,4 +42,17 @@ test.skip('get finality', async () => {
expect(finality).toBeGreaterThanOrEqual(0);
});
test.skip('request airdrop', async () => {
const account = new Account();
const connection = new Connection(url);
await Promise.all([
connection.requestAirdrop(account.publicKey, 40),
connection.requestAirdrop(account.publicKey, 2),
]);
const balance = await connection.getBalance(account.publicKey);
expect(balance).toBe(42);
});