feat: expose feeCalculator

This commit is contained in:
Michael Vines 2019-06-12 14:36:05 -07:00
parent 9fde1eb404
commit 10e3a26338
7 changed files with 91 additions and 20 deletions

View File

@ -32,6 +32,13 @@ declare module '@solana/web3.js' {
secretKey: Buffer;
}
// === src/fee-calculator.js ===
declare export type FeeCalculator = {
lamportsPerSignature: number,
targetSignaturesPerSlot: number,
targetLamportsPerSignature: number,
};
// === src/budget-program.js ===
/* TODO */
@ -55,6 +62,13 @@ declare module '@solana/web3.js' {
accountInfo: AccountInfo,
};
declare export type VoteAccountInfo = {
votePubkey: string,
nodePubkey: string,
stake: number,
commission: number,
};
declare type AccountChangeCallback = (accountInfo: AccountInfo) => void;
declare type ProgramAccountChangeCallback = (
keyedAccountInfo: KeyedAccountInfo,
@ -72,13 +86,14 @@ declare module '@solana/web3.js' {
getAccountInfo(publicKey: PublicKey): Promise<AccountInfo>;
getBalance(publicKey: PublicKey): Promise<number>;
getClusterNodes(): Promise<Array<ContactInfo>>;
getEpochVoteAccounts(): Promise<Array<VoteAccountInfo>>;
confirmTransaction(signature: TransactionSignature): Promise<boolean>;
getSlotLeader(): Promise<string>;
getSignatureStatus(
signature: TransactionSignature,
): Promise<SignatureSuccess | TransactionError | null>;
getTransactionCount(): Promise<number>;
getRecentBlockhash(): Promise<Blockhash>;
getRecentBlockhash(): Promise<[Blockhash, FeeCalculator]>;
requestAirdrop(
to: PublicKey,
amount: number,

View File

@ -12,6 +12,7 @@ import {PublicKey} from './publickey';
import {Transaction} from './transaction';
import {sleep} from './util/sleep';
import type {Blockhash} from './blockhash';
import type {FeeCalculator} from './fee-calculator';
import type {Account} from './account';
import type {TransactionSignature} from './transaction';
@ -203,8 +204,23 @@ const GetTransactionCountRpcResult = jsonRpcResult('number');
/**
* Expected JSON RPC response for the "getRecentBlockhash" message
*/
const GetRecentBlockhash = jsonRpcResult(['string', 'object']);
const GetRecentBlockhash_014 = jsonRpcResult('string'); // Legacy v0.14 response. TODO: Remove in July 2019
const GetRecentBlockhash = jsonRpcResult([
'string',
struct({
lamportsPerSignature: 'number',
targetLamportsPerSignature: 'number',
targetSignaturesPerSlot: 'number',
}),
]);
/**
* @ignore
*/
const GetRecentBlockhash_015 = jsonRpcResult([
'string',
struct({
lamportsPerSignature: 'number',
}),
]);
/**
* Expected JSON RPC response for the "requestAirdrop" message
@ -292,6 +308,12 @@ export type TransactionError = {|
Err: Object,
|};
/**
* @ignore
*/
type BlockhashAndFeeCalculator = [Blockhash, FeeCalculator]; // This type exists to workaround an esdoc parse error
/**
* A connection to a fullnode JSON RPC endpoint
*/
@ -473,28 +495,32 @@ export class Connection {
/**
* Fetch a recent blockhash from the cluster
*/
async getRecentBlockhash(): Promise<Blockhash> {
async getRecentBlockhash(): Promise<BlockhashAndFeeCalculator> {
const unsafeRes = await this._rpcRequest('getRecentBlockhash', []);
// Legacy v0.14 response. TODO: Remove in July 2019
// Legacy v0.15 response. TODO: Remove in August 2019
try {
const res_014 = GetRecentBlockhash_014(unsafeRes);
if (res_014.error) {
throw new Error(res_014.error.message);
const res_015 = GetRecentBlockhash_015(unsafeRes);
if (res_015.error) {
throw new Error(res_015.error.message);
}
return res_014.result;
const [blockhash, feeCalculator] = res_015.result;
feeCalculator.targetSignaturesPerSlot = 42;
feeCalculator.targetLamportsPerSignature =
feeCalculator.lamportsPerSignature;
return [blockhash, feeCalculator];
} catch (e) {
// Not legacy format
}
// End Legacy v0.14 response
// End Legacy v0.15 response
const res = GetRecentBlockhash(unsafeRes);
if (res.error) {
throw new Error(res.error.message);
}
assert(typeof res.result !== 'undefined');
// TODO: deserialize and expose FeeCalculator in res.result[1]
return res.result[0];
return res.result;
}
/**
@ -552,7 +578,10 @@ export class Connection {
let attempts = 0;
const startTime = Date.now();
for (;;) {
const recentBlockhash = await this.getRecentBlockhash();
const [
recentBlockhash,
//feeCalculator,
] = await this.getRecentBlockhash();
if (this._blockhashInfo.recentBlockhash != recentBlockhash) {
this._blockhashInfo = {

View File

@ -0,0 +1,13 @@
// @flow
/**
* @typedef {Object} FeeCalculator
* @property {number} lamportsPerSignature lamports Cost in lamports to validate a signature
* @property {number} targetLamportsPerSignature
* @property {number} targetSignaturesPerSlot
*/
export type FeeCalculator = {
lamportsPerSignature: number,
targetSignaturesPerSlot: number,
targetLamportsPerSignature: number,
};

View File

@ -1,6 +1,14 @@
// @flow
// These constants should match the values in
// https://github.com/solana-labs/solana/blob/master/sdk/src/timing.rs
// TODO: These constants should be removed in favor of reading them out of a
// Syscall account
/**
* @ignore
*/
export const NUM_TICKS_PER_SECOND = 10;
/**
* @ignore
*/
export const DEFAULT_TICKS_PER_SLOT = 8;

View File

@ -204,8 +204,12 @@ test('get recent blockhash', async () => {
mockGetRecentBlockhash();
const recentBlockhash = await connection.getRecentBlockhash();
const [
recentBlockhash,
feeCalculator,
] = await connection.getRecentBlockhash();
expect(recentBlockhash.length).toBeGreaterThanOrEqual(43);
expect(feeCalculator.lamportsPerSignature).toBeGreaterThanOrEqual(0);
});
test('request airdrop', async () => {
@ -482,7 +486,9 @@ test('multi-instruction transaction', async () => {
accountFrom.publicKey,
accountTo.publicKey,
100,
).add(SystemProgram.transfer(accountTo.publicKey, accountFrom.publicKey, 100));
).add(
SystemProgram.transfer(accountTo.publicKey, accountFrom.publicKey, 100),
);
const signature = await connection.sendTransaction(
transaction,
accountFrom,

View File

@ -18,7 +18,7 @@ export function mockGetRecentBlockhash() {
result: [
recentBlockhash.publicKey.toBase58(),
{
/* empty fee calculator */
lamportsPerSignature: 42,
},
],
},

View File

@ -137,11 +137,11 @@ test('transaction-payer', async () => {
},
]);
// accountPayer should be less less than 100 as it paid for the transaction
// accountPayer could be less than 100 as it paid for the transaction
// (exact amount less depends on the current cluster fees)
const balance = await connection.getBalance(accountPayer.publicKey);
expect(balance).toBeGreaterThan(0);
expect(balance).toBeLessThanOrEqual(99);
expect(balance).toBeLessThanOrEqual(100);
// accountFrom should have exactly 2, since it didn't pay for the transaction
mockRpc.push([