feat: add getFeeCalculatorForBlockhash method
This commit is contained in:
		
				
					committed by
					
						 Michael Vines
						Michael Vines
					
				
			
			
				
	
			
			
			
						parent
						
							e876081d52
						
					
				
				
					commit
					e622bb12b3
				
			
							
								
								
									
										4
									
								
								web3.js/module.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								web3.js/module.d.ts
									
									
									
									
										vendored
									
									
								
							| @@ -271,6 +271,10 @@ declare module '@solana/web3.js' { | ||||
|     getRecentBlockhashAndContext( | ||||
|       commitment?: Commitment, | ||||
|     ): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>>; | ||||
|     getFeeCalculatorForBlockhash( | ||||
|       blockhash: Blockhash, | ||||
|       commitment?: Commitment, | ||||
|     ): Promise<RpcResponseAndContext<FeeCalculator | null>>; | ||||
|     getRecentBlockhash( | ||||
|       commitment?: Commitment, | ||||
|     ): Promise<BlockhashAndFeeCalculator>; | ||||
|   | ||||
| @@ -284,6 +284,10 @@ declare module '@solana/web3.js' { | ||||
|     getRecentBlockhashAndContext( | ||||
|       commitment: ?Commitment, | ||||
|     ): Promise<RpcResponseAndContext<BlockhashAndFeeCalculator>>; | ||||
|     getFeeCalculatorForBlockhash( | ||||
|       blockhash: Blockhash, | ||||
|       commitment: ?Commitment, | ||||
|     ): Promise<RpcResponseAndContext<FeeCalculator | null>>; | ||||
|     getRecentBlockhash( | ||||
|       commitment: ?Commitment, | ||||
|     ): Promise<BlockhashAndFeeCalculator>; | ||||
|   | ||||
| @@ -783,6 +783,20 @@ const GetRecentBlockhashAndContextRpcResult = jsonRpcResultAndContext( | ||||
|   }), | ||||
| ); | ||||
|  | ||||
| /** | ||||
|  * Expected JSON RPC response for the "getFeeCalculatorForBlockhash" message | ||||
|  */ | ||||
| const GetFeeCalculatorRpcResult = jsonRpcResultAndContext( | ||||
|   struct.union([ | ||||
|     'null', | ||||
|     struct({ | ||||
|       feeCalculator: struct({ | ||||
|         lamportsPerSignature: 'number', | ||||
|       }), | ||||
|     }), | ||||
|   ]), | ||||
| ); | ||||
|  | ||||
| /** | ||||
|  * Expected JSON RPC response for the "requestAirdrop" message | ||||
|  */ | ||||
| @@ -1506,6 +1520,31 @@ export class Connection { | ||||
|     return res.result; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Fetch the fee calculator for a recent blockhash from the cluster, return with context | ||||
|    */ | ||||
|   async getFeeCalculatorForBlockhash( | ||||
|     blockhash: Blockhash, | ||||
|     commitment: ?Commitment, | ||||
|   ): Promise<RpcResponseAndContext<FeeCalculator | null>> { | ||||
|     const args = this._argsWithCommitment([blockhash], commitment); | ||||
|     const unsafeRes = await this._rpcRequest( | ||||
|       'getFeeCalculatorForBlockhash', | ||||
|       args, | ||||
|     ); | ||||
|  | ||||
|     const res = GetFeeCalculatorRpcResult(unsafeRes); | ||||
|     if (res.error) { | ||||
|       throw new Error('failed to get fee calculator: ' + res.error.message); | ||||
|     } | ||||
|     assert(typeof res.result !== 'undefined'); | ||||
|     const {context, value} = res.result; | ||||
|     return { | ||||
|       context, | ||||
|       value: value && value.feeCalculator, | ||||
|     }; | ||||
|   } | ||||
|  | ||||
|   /** | ||||
|    * Fetch a recent blockhash from the cluster | ||||
|    * @return {Promise<{blockhash: Blockhash, feeCalculator: FeeCalculator}>} | ||||
|   | ||||
| @@ -203,6 +203,7 @@ test('get program accounts', async () => { | ||||
|       }, | ||||
|     }, | ||||
|   ]); | ||||
|  | ||||
|   transaction = SystemProgram.assign({ | ||||
|     accountPubkey: account1.publicKey, | ||||
|     programId: programId.publicKey, | ||||
| @@ -213,8 +214,40 @@ test('get program accounts', async () => { | ||||
|     skipPreflight: true, | ||||
|   }); | ||||
|  | ||||
|   mockGetRecentBlockhash('recent'); | ||||
|   const {feeCalculator} = await connection.getRecentBlockhash(); | ||||
|   mockRpc.push([ | ||||
|     url, | ||||
|     { | ||||
|       method: 'getFeeCalculatorForBlockhash', | ||||
|       params: [transaction.recentBlockhash, {commitment: 'recent'}], | ||||
|     }, | ||||
|     { | ||||
|       error: null, | ||||
|       result: { | ||||
|         context: { | ||||
|           slot: 11, | ||||
|         }, | ||||
|         value: { | ||||
|           feeCalculator: { | ||||
|             lamportsPerSignature: 42, | ||||
|           }, | ||||
|         }, | ||||
|       }, | ||||
|     }, | ||||
|   ]); | ||||
|  | ||||
|   if (transaction.recentBlockhash === null) { | ||||
|     expect(transaction.recentBlockhash).not.toBeNull(); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   const feeCalculator = ( | ||||
|     await connection.getFeeCalculatorForBlockhash(transaction.recentBlockhash) | ||||
|   ).value; | ||||
|  | ||||
|   if (feeCalculator === null) { | ||||
|     expect(feeCalculator).not.toBeNull(); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   mockRpc.push([ | ||||
|     url, | ||||
| @@ -1077,6 +1110,43 @@ test('get recent blockhash', async () => { | ||||
|   } | ||||
| }); | ||||
|  | ||||
| test('get fee calculator', async () => { | ||||
|   const connection = new Connection(url); | ||||
|  | ||||
|   mockGetRecentBlockhash('recent'); | ||||
|   const {blockhash} = await connection.getRecentBlockhash('recent'); | ||||
|  | ||||
|   mockRpc.push([ | ||||
|     url, | ||||
|     { | ||||
|       method: 'getFeeCalculatorForBlockhash', | ||||
|       params: [blockhash, {commitment: 'recent'}], | ||||
|     }, | ||||
|     { | ||||
|       error: null, | ||||
|       result: { | ||||
|         context: { | ||||
|           slot: 11, | ||||
|         }, | ||||
|         value: { | ||||
|           feeCalculator: { | ||||
|             lamportsPerSignature: 5000, | ||||
|           }, | ||||
|         }, | ||||
|       }, | ||||
|     }, | ||||
|   ]); | ||||
|  | ||||
|   const feeCalculator = ( | ||||
|     await connection.getFeeCalculatorForBlockhash(blockhash, 'recent') | ||||
|   ).value; | ||||
|   if (feeCalculator === null) { | ||||
|     expect(feeCalculator).not.toBeNull(); | ||||
|     return; | ||||
|   } | ||||
|   expect(feeCalculator.lamportsPerSignature).toEqual(5000); | ||||
| }); | ||||
|  | ||||
| test('get block time', async () => { | ||||
|   const connection = new Connection(url); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user