Implement requestAirdrop
This commit is contained in:
parent
b33633fb9c
commit
392672a5a7
|
@ -16,9 +16,9 @@ export type TransactionSignature = string;
|
||||||
/**
|
/**
|
||||||
* @typedef {string} TransactionId
|
* @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 {
|
function createRpcRequest(url): RpcRequest {
|
||||||
const server = jayson(
|
const server = jayson(
|
||||||
|
@ -88,6 +88,12 @@ const GetFinalityRpcResult = struct({
|
||||||
error: 'any?',
|
error: 'any?',
|
||||||
result: 'number?',
|
result: 'number?',
|
||||||
});
|
});
|
||||||
|
const RequestAirdropRpcResult = struct({
|
||||||
|
jsonrpc: struct.literal('2.0'),
|
||||||
|
id: 'string',
|
||||||
|
error: 'any?',
|
||||||
|
result: 'boolean?',
|
||||||
|
});
|
||||||
|
|
||||||
function sleep(duration = 0) {
|
function sleep(duration = 0) {
|
||||||
return new Promise((accept) => {
|
return new Promise((accept) => {
|
||||||
|
@ -158,9 +164,14 @@ export class Connection {
|
||||||
return Number(res.result);
|
return Number(res.result);
|
||||||
}
|
}
|
||||||
|
|
||||||
async requestAirdrop(account: Account, amount: number): Promise<void> {
|
async requestAirdrop(to: PublicKey, amount: number): Promise<void> {
|
||||||
console.log(`TODO: airdrop ${amount} to ${account.publicKey}`);
|
const unsafeRes = await this._rpcRequest('requestAirdrop', [to, amount]);
|
||||||
await sleep(500); // TODO
|
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> {
|
async sendTokens(from: Account, to: PublicKey, amount: number): Promise<TransactionSignature> {
|
||||||
|
|
|
@ -42,4 +42,17 @@ test.skip('get finality', async () => {
|
||||||
expect(finality).toBeGreaterThanOrEqual(0);
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue