feat: add support for getSupply RPC method
This commit is contained in:
committed by
Michael Vines
parent
6d24110bcd
commit
9c677c7d3d
8
web3.js/module.d.ts
vendored
8
web3.js/module.d.ts
vendored
@ -173,6 +173,13 @@ declare module '@solana/web3.js' {
|
|||||||
firstNormalSlot: number;
|
firstNormalSlot: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Supply = {
|
||||||
|
total: number;
|
||||||
|
circulating: number;
|
||||||
|
nonCirculating: number;
|
||||||
|
nonCirculatingAccounts: Array<PublicKey>;
|
||||||
|
};
|
||||||
|
|
||||||
export type VoteAccountStatus = {
|
export type VoteAccountStatus = {
|
||||||
current: Array<VoteAccountInfo>;
|
current: Array<VoteAccountInfo>;
|
||||||
delinquent: Array<VoteAccountInfo>;
|
delinquent: Array<VoteAccountInfo>;
|
||||||
@ -200,6 +207,7 @@ declare module '@solana/web3.js' {
|
|||||||
getBalance(publicKey: PublicKey, commitment?: Commitment): Promise<number>;
|
getBalance(publicKey: PublicKey, commitment?: Commitment): Promise<number>;
|
||||||
getBlockTime(slot: number): Promise<number | null>;
|
getBlockTime(slot: number): Promise<number | null>;
|
||||||
getMinimumLedgerSlot(): Promise<number>;
|
getMinimumLedgerSlot(): Promise<number>;
|
||||||
|
getSupply(commitment?: Commitment): Promise<RpcResponseAndContext<Supply>>;
|
||||||
getClusterNodes(): Promise<Array<ContactInfo>>;
|
getClusterNodes(): Promise<Array<ContactInfo>>;
|
||||||
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
||||||
getConfirmedTransaction(
|
getConfirmedTransaction(
|
||||||
|
@ -186,6 +186,13 @@ declare module '@solana/web3.js' {
|
|||||||
absoluteSlot: number,
|
absoluteSlot: number,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
declare export type Supply = {
|
||||||
|
total: number,
|
||||||
|
circulating: number,
|
||||||
|
nonCirculating: number,
|
||||||
|
nonCirculatingAccounts: Array<PublicKey>,
|
||||||
|
};
|
||||||
|
|
||||||
declare export type VoteAccountStatus = {
|
declare export type VoteAccountStatus = {
|
||||||
current: Array<VoteAccountInfo>,
|
current: Array<VoteAccountInfo>,
|
||||||
delinquent: Array<VoteAccountInfo>,
|
delinquent: Array<VoteAccountInfo>,
|
||||||
@ -213,6 +220,7 @@ declare module '@solana/web3.js' {
|
|||||||
getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise<number>;
|
getBalance(publicKey: PublicKey, commitment: ?Commitment): Promise<number>;
|
||||||
getBlockTime(slot: number): Promise<number | null>;
|
getBlockTime(slot: number): Promise<number | null>;
|
||||||
getMinimumLedgerSlot(): Promise<number>;
|
getMinimumLedgerSlot(): Promise<number>;
|
||||||
|
getSupply(commitment: ?Commitment): Promise<RpcResponseAndContext<Supply>>;
|
||||||
getClusterNodes(): Promise<Array<ContactInfo>>;
|
getClusterNodes(): Promise<Array<ContactInfo>>;
|
||||||
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
getConfirmedBlock(slot: number): Promise<ConfirmedBlock>;
|
||||||
getConfirmedTransaction(
|
getConfirmedTransaction(
|
||||||
|
@ -403,6 +403,34 @@ const MinimumLedgerSlotRpcResult = struct({
|
|||||||
result: 'number',
|
result: 'number',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supply
|
||||||
|
*
|
||||||
|
* @typedef {Object} Supply
|
||||||
|
* @property {number} total Total supply in lamports
|
||||||
|
* @property {number} circulating Circulating supply in lamports
|
||||||
|
* @property {number} nonCirculating Non-circulating supply in lamports
|
||||||
|
* @property {Array<PublicKey>} nonCirculatingAccounts List of non-circulating account addresses
|
||||||
|
*/
|
||||||
|
type Supply = {
|
||||||
|
total: number,
|
||||||
|
circulating: number,
|
||||||
|
nonCirculating: number,
|
||||||
|
nonCirculatingAccounts: Array<PublicKey>,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expected JSON RPC response for the "getSupply" message
|
||||||
|
*/
|
||||||
|
const GetSupplyRpcResult = jsonRpcResultAndContext(
|
||||||
|
struct({
|
||||||
|
total: 'number',
|
||||||
|
circulating: 'number',
|
||||||
|
nonCirculating: 'number',
|
||||||
|
nonCirculatingAccounts: struct.array(['string']),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expected JSON RPC response for the "getVersion" message
|
* Expected JSON RPC response for the "getVersion" message
|
||||||
*/
|
*/
|
||||||
@ -1016,6 +1044,25 @@ export class Connection {
|
|||||||
return res.result;
|
return res.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch information about the current supply
|
||||||
|
*/
|
||||||
|
async getSupply(
|
||||||
|
commitment: ?Commitment,
|
||||||
|
): Promise<RpcResponseAndContext<Supply>> {
|
||||||
|
const args = this._argsWithCommitment([], commitment);
|
||||||
|
const unsafeRes = await this._rpcRequest('getSupply', args);
|
||||||
|
const res = GetSupplyRpcResult(unsafeRes);
|
||||||
|
if (res.error) {
|
||||||
|
throw new Error('failed to get supply: ' + res.error.message);
|
||||||
|
}
|
||||||
|
assert(typeof res.result !== 'undefined');
|
||||||
|
res.result.value.nonCirculatingAccounts = res.result.value.nonCirculatingAccounts.map(
|
||||||
|
account => new PublicKey(account),
|
||||||
|
);
|
||||||
|
return res.result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch all the account info for the specified public key, return with context
|
* Fetch all the account info for the specified public key, return with context
|
||||||
*/
|
*/
|
||||||
|
@ -1109,6 +1109,38 @@ test('get minimum ledger slot', async () => {
|
|||||||
expect(minimumLedgerSlot).toBeGreaterThanOrEqual(0);
|
expect(minimumLedgerSlot).toBeGreaterThanOrEqual(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('get supply', async () => {
|
||||||
|
const connection = new Connection(url);
|
||||||
|
|
||||||
|
mockRpc.push([
|
||||||
|
url,
|
||||||
|
{
|
||||||
|
method: 'getSupply',
|
||||||
|
params: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
error: null,
|
||||||
|
result: {
|
||||||
|
context: {
|
||||||
|
slot: 1,
|
||||||
|
},
|
||||||
|
value: {
|
||||||
|
total: 1000,
|
||||||
|
circulating: 100,
|
||||||
|
nonCirculating: 900,
|
||||||
|
nonCirculatingAccounts: [new Account().publicKey.toBase58()],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
const supply = (await connection.getSupply()).value;
|
||||||
|
expect(supply.total).toBeGreaterThan(0);
|
||||||
|
expect(supply.circulating).toBeGreaterThan(0);
|
||||||
|
expect(supply.nonCirculating).toBeGreaterThan(0);
|
||||||
|
expect(supply.nonCirculatingAccounts.length).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
test('getVersion', async () => {
|
test('getVersion', async () => {
|
||||||
const connection = new Connection(url);
|
const connection = new Connection(url);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user