Improve rpc result validation

This commit is contained in:
Michael Vines 2018-09-26 19:54:59 -07:00
parent 92af6e3341
commit a3aecba1d0
2 changed files with 39 additions and 58 deletions

View File

@ -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 * Expected JSON RPC response for the "getAccountInfo" message
*/ */
const GetAccountInfoRpcResult = struct({ const GetAccountInfoRpcResult = jsonRpcResult({
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: struct.optional({
program_id: 'array', program_id: 'array',
tokens: 'number', tokens: 'number',
userdata: 'array', userdata: 'array',
}),
}); });
/** /**
* Expected JSON RPC response for the "confirmTransaction" message * Expected JSON RPC response for the "confirmTransaction" message
*/ */
const ConfirmTransactionRpcResult = struct({ const ConfirmTransactionRpcResult = jsonRpcResult('boolean');
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'boolean?',
});
/** /**
* Expected JSON RPC response for the "getSignatureStatus" message * Expected JSON RPC response for the "getSignatureStatus" message
*/ */
const GetSignatureStatusRpcResult = struct({ const GetSignatureStatusRpcResult = jsonRpcResult(struct.enum([
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: struct.optional(struct.enum([
'Confirmed', 'Confirmed',
'SignatureNotFound', 'SignatureNotFound',
'ProgramRuntimeError', 'ProgramRuntimeError',
'GenericFailure', 'GenericFailure',
])), ]));
});
/** /**
* Expected JSON RPC response for the "getTransactionCount" message * Expected JSON RPC response for the "getTransactionCount" message
*/ */
const GetTransactionCountRpcResult = struct({ const GetTransactionCountRpcResult = jsonRpcResult('number');
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'number?',
});
/** /**
* Expected JSON RPC response for the "getLastId" message * Expected JSON RPC response for the "getLastId" message
*/ */
const GetLastId = struct({ const GetLastId = jsonRpcResult('string');
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'string?',
});
/** /**
* Expected JSON RPC response for the "getFinality" message * Expected JSON RPC response for the "getFinality" message
*/ */
const GetFinalityRpcResult = struct({ const GetFinalityRpcResult = jsonRpcResult('number');
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'number?',
});
/** /**
* Expected JSON RPC response for the "requestAirdrop" message * Expected JSON RPC response for the "requestAirdrop" message
*/ */
const RequestAirdropRpcResult = struct({ const RequestAirdropRpcResult = jsonRpcResult('string');
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'string?',
});
/** /**
* Expected JSON RPC response for the "sendTransaction" message * Expected JSON RPC response for the "sendTransaction" message
*/ */
const SendTokensRpcResult = struct({ const SendTokensRpcResult = jsonRpcResult('string');
jsonrpc: struct.literal('2.0'),
id: 'string',
error: 'any?',
result: 'string?',
});
/** /**
* Information describing an account * Information describing an account

View File

@ -5,9 +5,9 @@ import {Connection} from '../src/connection';
import {SystemProgram} from '../src/system-program'; import {SystemProgram} from '../src/system-program';
import {mockRpc} from './__mocks__/node-fetch'; 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://master.testnet.solana.com:8899';
//url = 'http://localhost:8899';
const errorMessage = 'Invalid request'; const errorMessage = 'Invalid request';
const errorResponse = { const errorResponse = {