added two more method tests

This commit is contained in:
Eddie Wang 2017-11-19 13:50:11 -05:00
parent 95d6b80fc3
commit fefe871a35
3 changed files with 52 additions and 33 deletions

View File

@ -8,19 +8,11 @@ import RPCClient from './client';
import RPCRequests from './requests';
import { isValidGetBalance } from '../../validators';
// TODO - understand response values
// "RPC" requests will sometimes resolve with 200, but contain a payload informing there was an error.
function ensureOkResponse(response: any) {
function errorOrResult(response) {
if (response.error) {
throw new Error(response.error.message);
}
if (response.status === 0) {
throw new Error('Error!: Status Code: RSP0');
}
if (response.result.toLowerCase().includes('error')) {
throw new Error('Error! Status Code: "RPCLIE"');
}
return response;
return response.result;
}
export default class RpcNode implements INode {
@ -44,20 +36,23 @@ export default class RpcNode implements INode {
return this.client
.call(this.requests.getBalance(address))
.then(isValidGetBalance)
.then(({ result }) => Wei(result));
.then(errorOrResult)
.catch(err => {
console.error('Error calling getBalance: ' + err);
})
.then(result => Wei(result));
}
public estimateGas(transaction: TransactionWithoutGas): Promise<Wei> {
return this.client
.call(this.requests.estimateGas(transaction))
.then(ensureOkResponse)
.then(({ result }) => Wei(result));
.then(errorOrResult)
.then(result => Wei(result));
}
public getTokenBalance(address: string, token: Token): Promise<TokenValue> {
return this.client
.call(this.requests.getTokenBalance(address, token))
.then(ensureOkResponse)
.then(response => {
if (response.error) {
// TODO - Error handling
@ -73,7 +68,6 @@ export default class RpcNode implements INode {
): Promise<TokenValue[]> {
return this.client
.batch(tokens.map(t => this.requests.getTokenBalance(address, t)))
.then(ensureOkResponse)
.then(response => {
return response.map(item => {
// FIXME wrap in maybe-like
@ -89,22 +83,24 @@ export default class RpcNode implements INode {
public getTransactionCount(address: string): Promise<string> {
return this.client
.call(this.requests.getTransactionCount(address))
.then(ensureOkResponse);
.then(errorOrResult);
}
public getCurrentBlock(): Promise<string> {
return this.client
.call(this.requests.getCurrentBlock())
.then(ensureOkResponse)
.then(({ result }) => new BN(stripHexPrefix(result)).toString());
.then(errorOrResult)
.then(result => new BN(stripHexPrefix(result)).toString());
}
public sendRawTx(signedTx: string): Promise<string> {
return this.client
.call(this.requests.sendRawTx(signedTx))
.then(ensureOkResponse)
.then(({ result }) => {
return result;
.then(response => {
if (response.error) {
throw new Error(response.error.message);
}
return response.result;
});
}
}

View File

@ -184,23 +184,21 @@ export const isValidAbiJson = (abiJson: string) =>
const v = new Validator();
export const schema = {
getBalance: {
RpcNode: {
type: 'object',
additionalProperties: false,
properties: {
jsonrpc: { type: 'string' },
id: { oneOf: [{ type: 'string' }, { type: 'integer' }] },
result: { type: 'string' },
status: { type: 'string' },
message: { type: 'string' }
result: { type: 'string' }
}
}
};
export const isValidGetBalance = (result: JsonRpcResponse) => {
const validateResult = v.validate(result, schema.getBalance);
export const isValidGetBalance = (response: JsonRpcResponse) => {
const validateResult = v.validate(response, schema.RpcNode);
if (!validateResult.valid) {
throw new Error('Invalid Balance Data Shape.');
}
return result;
return response;
};

View File

@ -7,19 +7,44 @@ import 'url-search-params-polyfill';
const v = new Validator();
const validRequests = {
address: '0x6b3a639eb96d8e0241fe4e114d99e739f906944e',
txObject: {}
address: '0x72948fa4200d10ffaa7c594c24bbba6ef627d4a3',
transaction: {
data: '',
from: '0x72948fa4200d10ffaa7c594c24bbba6ef627d4a3',
to: '0x72948fa4200d10ffaa7c594c24bbba6ef627d4a3',
value: '0xde0b6b3a7640000'
},
token: {
address: '0x4156d3342d5c385a87d264f90653733592000581',
symbol: 'SALT',
decimal: 8
}
};
const testGetBalance = async (n: RPCNode) => {
const data = await n.client.call(
n.requests.getBalance(validRequests.address)
);
return v.validate(data, schema.getBalance);
return v.validate(data, schema.RpcNode);
};
const testEstimateGas = async (n: RPCNode) => {
const data = await n.client.call(
n.requests.estimateGas(validRequests.transaction)
);
return v.validate(data, schema.RpcNode);
};
const testGetTokenBalance = async (n: RPCNode) => {
const { address, token } = validRequests;
const data = await n.client.call(n.requests.getTokenBalance(address, token));
return v.validate(data, schema.RpcNode);
};
const RPCTests = {
getBalance: testGetBalance
getBalance: testGetBalance,
estimateGas: testEstimateGas,
getTokenBalance: testGetTokenBalance
};
function testRequests(node: RPCNode, service: string) {
@ -33,7 +58,7 @@ function testRequests(node: RPCNode, service: string) {
}
const mapNodeEndpoints = (nodes: { [key: string]: NodeConfig }) => {
const testList = ['eth_mew', 'eth_ethscan', 'etc_epool'];
const testList = ['eth_mew', 'etc_epool'];
testList.forEach(n => {
testRequests(
nodes[n].lib as RPCNode,