Add getAccountInfo
This commit is contained in:
parent
da2496872b
commit
7148b0f7d8
|
@ -4,6 +4,7 @@ import assert from 'assert';
|
||||||
import fetch from 'node-fetch';
|
import fetch from 'node-fetch';
|
||||||
import jayson from 'jayson/lib/client/browser';
|
import jayson from 'jayson/lib/client/browser';
|
||||||
import {struct} from 'superstruct';
|
import {struct} from 'superstruct';
|
||||||
|
import bs58 from 'bs58';
|
||||||
|
|
||||||
import {Transaction} from './transaction';
|
import {Transaction} from './transaction';
|
||||||
import type {Account, PublicKey} from './account';
|
import type {Account, PublicKey} from './account';
|
||||||
|
@ -56,6 +57,22 @@ const GetBalanceRpcResult = struct({
|
||||||
result: 'number?',
|
result: 'number?',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expected JSON RPC response for the "getAccountInfo" message
|
||||||
|
*/
|
||||||
|
const GetAccountInfoRpcResult = struct({
|
||||||
|
jsonrpc: struct.literal('2.0'),
|
||||||
|
id: 'string',
|
||||||
|
error: 'any?',
|
||||||
|
result: struct.optional({
|
||||||
|
contract_id: 'array',
|
||||||
|
tokens: 'number',
|
||||||
|
userdata: 'array',
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expected JSON RPC response for the "confirmTransaction" message
|
* Expected JSON RPC response for the "confirmTransaction" message
|
||||||
*/
|
*/
|
||||||
|
@ -116,6 +133,15 @@ const SendTokensRpcResult = struct({
|
||||||
result: 'string?',
|
result: 'string?',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Information describing an account
|
||||||
|
*/
|
||||||
|
type AccountInfo = {
|
||||||
|
tokens: number,
|
||||||
|
programId: PublicKey,
|
||||||
|
userdata: Buffer | null,
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A connection to a fullnode JSON RPC endpoint
|
* A connection to a fullnode JSON RPC endpoint
|
||||||
*/
|
*/
|
||||||
|
@ -150,6 +176,33 @@ export class Connection {
|
||||||
return res.result;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch all the account info for the specified public key
|
||||||
|
*/
|
||||||
|
async getAccountInfo(publicKey: PublicKey): Promise<AccountInfo> {
|
||||||
|
const unsafeRes = await this._rpcRequest(
|
||||||
|
'getAccountInfo',
|
||||||
|
[publicKey]
|
||||||
|
);
|
||||||
|
const res = GetAccountInfoRpcResult(unsafeRes);
|
||||||
|
if (res.error) {
|
||||||
|
throw new Error(res.error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
const {result} = res;
|
||||||
|
assert(typeof result !== 'undefined');
|
||||||
|
|
||||||
|
let userdata = null;
|
||||||
|
if (result.userdata.length > 0) {
|
||||||
|
userdata = Buffer.from(result.userdata);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
tokens: result.tokens,
|
||||||
|
programId: bs58.encode(result.contract_id),
|
||||||
|
userdata,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Confirm the transaction identified by the specified signature
|
* Confirm the transaction identified by the specified signature
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -10,7 +10,7 @@ type RpcRequest = {
|
||||||
type RpcResponseError = {
|
type RpcResponseError = {
|
||||||
message: string;
|
message: string;
|
||||||
}
|
}
|
||||||
type RpcResponseResult = boolean | string | number;
|
type RpcResponseResult = any;
|
||||||
type RpcResponse = {
|
type RpcResponse = {
|
||||||
error: ?RpcResponseError;
|
error: ?RpcResponseError;
|
||||||
result: ?RpcResponseResult;
|
result: ?RpcResponseResult;
|
||||||
|
|
|
@ -16,6 +16,7 @@ test('pay', () => {
|
||||||
123,
|
123,
|
||||||
);
|
);
|
||||||
console.log('Pay:', transaction);
|
console.log('Pay:', transaction);
|
||||||
|
// TODO: Validate transaction contents
|
||||||
|
|
||||||
transaction = BudgetProgram.pay(
|
transaction = BudgetProgram.pay(
|
||||||
from.publicKey,
|
from.publicKey,
|
||||||
|
@ -25,6 +26,7 @@ test('pay', () => {
|
||||||
BudgetProgram.signatureCondition(from.publicKey),
|
BudgetProgram.signatureCondition(from.publicKey),
|
||||||
);
|
);
|
||||||
console.log('After:', transaction);
|
console.log('After:', transaction);
|
||||||
|
// TODO: Validate transaction contents
|
||||||
|
|
||||||
transaction = BudgetProgram.pay(
|
transaction = BudgetProgram.pay(
|
||||||
from.publicKey,
|
from.publicKey,
|
||||||
|
@ -35,5 +37,6 @@ test('pay', () => {
|
||||||
BudgetProgram.timestampCondition(from.publicKey, new Date()),
|
BudgetProgram.timestampCondition(from.publicKey, new Date()),
|
||||||
);
|
);
|
||||||
console.log('Or:', transaction);
|
console.log('Or:', transaction);
|
||||||
|
// TODO: Validate transaction contents
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,24 @@ const errorResponse = {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
test('get account info - error', () => {
|
||||||
|
const account = new Account();
|
||||||
|
const connection = new Connection(url);
|
||||||
|
|
||||||
|
mockRpc.push([
|
||||||
|
url,
|
||||||
|
{
|
||||||
|
method: 'getAccountInfo',
|
||||||
|
params: [account.publicKey],
|
||||||
|
},
|
||||||
|
errorResponse,
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(connection.getAccountInfo(account.publicKey))
|
||||||
|
.rejects.toThrow(errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
test('get balance', async () => {
|
test('get balance', async () => {
|
||||||
const account = new Account();
|
const account = new Account();
|
||||||
const connection = new Connection(url);
|
const connection = new Connection(url);
|
||||||
|
@ -176,6 +194,31 @@ test('request airdrop', async () => {
|
||||||
|
|
||||||
const balance = await connection.getBalance(account.publicKey);
|
const balance = await connection.getBalance(account.publicKey);
|
||||||
expect(balance).toBe(42);
|
expect(balance).toBe(42);
|
||||||
|
|
||||||
|
mockRpc.push([
|
||||||
|
url,
|
||||||
|
{
|
||||||
|
method: 'getAccountInfo',
|
||||||
|
params: [account.publicKey],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
error: null,
|
||||||
|
result: {
|
||||||
|
contract_id: [
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||||
|
],
|
||||||
|
tokens: 42,
|
||||||
|
userdata: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
|
||||||
|
const accountInfo = await connection.getAccountInfo(account.publicKey);
|
||||||
|
expect(accountInfo.tokens).toBe(42);
|
||||||
|
expect(accountInfo.userdata).toBe(null);
|
||||||
|
expect(accountInfo.programId).toBe(SystemProgram.programId);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('request airdrop - error', () => {
|
test('request airdrop - error', () => {
|
||||||
|
|
Loading…
Reference in New Issue