Improve rpc result validation
This commit is contained in:
parent
92af6e3341
commit
a3aecba1d0
|
@ -58,95 +58,76 @@ const GetBalanceRpcResult = struct({
|
|||
});
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
function jsonRpcResult(resultDescription: any) {
|
||||
const jsonRpcVersion = struct.literal('2.0');
|
||||
return struct.union([
|
||||
struct({
|
||||
jsonrpc: jsonRpcVersion,
|
||||
id: 'string',
|
||||
error: 'any'
|
||||
}),
|
||||
struct({
|
||||
jsonrpc: jsonRpcVersion,
|
||||
id: 'string',
|
||||
error: 'null?',
|
||||
result: resultDescription,
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "getAccountInfo" message
|
||||
*/
|
||||
const GetAccountInfoRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: struct.optional({
|
||||
const GetAccountInfoRpcResult = jsonRpcResult({
|
||||
program_id: 'array',
|
||||
tokens: 'number',
|
||||
userdata: 'array',
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "confirmTransaction" message
|
||||
*/
|
||||
const ConfirmTransactionRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'boolean?',
|
||||
});
|
||||
const ConfirmTransactionRpcResult = jsonRpcResult('boolean');
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "getSignatureStatus" message
|
||||
*/
|
||||
const GetSignatureStatusRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: struct.optional(struct.enum([
|
||||
const GetSignatureStatusRpcResult = jsonRpcResult(struct.enum([
|
||||
'Confirmed',
|
||||
'SignatureNotFound',
|
||||
'ProgramRuntimeError',
|
||||
'GenericFailure',
|
||||
])),
|
||||
});
|
||||
]));
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "getTransactionCount" message
|
||||
*/
|
||||
const GetTransactionCountRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'number?',
|
||||
});
|
||||
const GetTransactionCountRpcResult = jsonRpcResult('number');
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "getLastId" message
|
||||
*/
|
||||
const GetLastId = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'string?',
|
||||
});
|
||||
const GetLastId = jsonRpcResult('string');
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "getFinality" message
|
||||
*/
|
||||
const GetFinalityRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'number?',
|
||||
});
|
||||
const GetFinalityRpcResult = jsonRpcResult('number');
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "requestAirdrop" message
|
||||
*/
|
||||
const RequestAirdropRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'string?',
|
||||
});
|
||||
const RequestAirdropRpcResult = jsonRpcResult('string');
|
||||
|
||||
/**
|
||||
* Expected JSON RPC response for the "sendTransaction" message
|
||||
*/
|
||||
const SendTokensRpcResult = struct({
|
||||
jsonrpc: struct.literal('2.0'),
|
||||
id: 'string',
|
||||
error: 'any?',
|
||||
result: 'string?',
|
||||
});
|
||||
const SendTokensRpcResult = jsonRpcResult('string');
|
||||
|
||||
/**
|
||||
* Information describing an account
|
||||
|
|
|
@ -5,9 +5,9 @@ import {Connection} from '../src/connection';
|
|||
import {SystemProgram} from '../src/system-program';
|
||||
import {mockRpc} from './__mocks__/node-fetch';
|
||||
|
||||
let url = 'http://testnet.solana.com:8899';
|
||||
let url = 'http://localhost:8899';
|
||||
//url = 'http://testnet.solana.com:8899';
|
||||
//url = 'http://master.testnet.solana.com:8899';
|
||||
//url = 'http://localhost:8899';
|
||||
|
||||
const errorMessage = 'Invalid request';
|
||||
const errorResponse = {
|
||||
|
|
Loading…
Reference in New Issue