Created
September 12, 2017 22:00
-
-
Save HenryNguyen5/76a4896efc50aca32e8e42125ddad165 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const JSONPostParser = (parser = null) => ({ result }) => | |
parser && typeof parser === 'function' ? parser(result) : result; | |
const JSONErrorHandler = (handler = null) => e => { | |
if (handler && typeof handler === 'function') { | |
return handler(e); | |
} else { | |
throw Error(e.message); | |
} | |
}; | |
const sendRawTx = (signedTx: string): SendRawTxRequest => ({ | |
methodAndParams: { | |
method: 'eth_sendRawTransaction', | |
params: [signedTx] | |
} | |
}); | |
const estimateGas = <T: *>(transaction: T): EstimateGasRequest => ({ | |
methodAndParams: { | |
method: 'eth_estimateGas', | |
params: [transaction] | |
}, | |
parser: r => new Big(r) | |
}); | |
const getBalance = (address: string): GetBalanceRequest => ({ | |
methodAndParams: { | |
method: 'eth_getBalance', | |
params: [hexEncodeData(address), 'pending'] | |
}, | |
parser: r => new Wei(r) | |
}); | |
const ethCall = (transaction: TxCallObject): CallRequest => ({ | |
methodAndParams: { | |
method: 'eth_call', | |
params: [transaction, 'pending'] | |
} | |
}); | |
const getTransactionCount = (address: string): GetTransactionCountRequest => ({ | |
methodAndParams: { | |
method: 'eth_getTransactionCount', | |
params: [address, 'pending'] | |
} | |
}); | |
const getTokenBalance = ( | |
address: string, | |
token: Token | |
): GetTokenBalanceRequest => ({ | |
methodAndParams: { | |
method: 'eth_call', | |
params: [ | |
{ | |
to: token.address, | |
data: ERC20.balanceOf(address) | |
}, | |
'pending' | |
] | |
}, | |
parser: r => new Big(r).div(new Big(10).pow(token.decimal)), | |
errorHandler: () => new Big(0) // TODO - Error handling | |
}); | |
// FIXME is it safe to generate that much entropy? | |
const _generateId = (): string => randomBytes(16).toString('hex'); | |
const _generateTxObj = ({ methodAndParams }) => ({ | |
id: _generateId(), | |
jsonrpc: '2.0', | |
...methodAndParams | |
}); | |
const rpcMethods = { | |
sendRawTx, | |
estimateGas, | |
getBalance, | |
ethCall, | |
getTransactionCount, | |
//TODO: get rid of these in favor of using contract-refactor because these arent actual ethereum JSON-RPC calls | |
getTokenBalance | |
}; | |
export function rerouteRPCMethodsHandler(obj) { | |
const rerouteRPCMethodsHandler = { | |
get(node, propKey) { | |
const rpcMethod = rpcMethods[propKey]; | |
const nodeMethod = node[propKey]; | |
if (!rpcMethod && !nodeMethod) { | |
throw Error( | |
`${propKey} does not exist on "Node" nor is it a RPC method` | |
); | |
} | |
if (nodeMethod) { | |
const result = (...args) => nodeMethod.apply(node, args); | |
return result; | |
} else { | |
return (...args) => { | |
const call = rpcMethod(...args); | |
const rpcObj = { | |
txObj: _generateTxObj(call), | |
parser: JSONPostParser(call.parser), | |
errorHandler: JSONErrorHandler(call.errorHandler) | |
}; | |
return node.sendRPCRequest.call(node, rpcObj); | |
}; | |
} | |
} | |
}; | |
return new Proxy(obj, rerouteRPCMethodsHandler); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment