Skip to content

Commit dee6ae7

Browse files
josedahlquistmarcos-iov
authored andcommitted
Implement new version without full web3 package
- use web3-eth-abi package only - change argument from web3 to host - use fetch API to get Bridge state from host
1 parent 8521363 commit dee6ae7

File tree

3 files changed

+729
-3185
lines changed

3 files changed

+729
-3185
lines changed

index.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Bridge = require('@rsksmart/rsk-precompiled-abis').bridge;
22
const ethUtils = require('ethereumjs-util');
3+
const web3abi = require('web3-eth-abi');
34

45
const RLP = ethUtils.rlp;
56

@@ -10,6 +11,54 @@ const pegoutWaitingConfirmationsParser = require('./pegout-waiting-confirmation'
1011
const nextPegoutCreationBlockNumberParser =
1112
require('./next-pegout-creation-block-number').parseRLPToNextPegoutCreationBlockNumber;
1213

14+
const GET_STATE_FOR_DEBUGGING_METHOD_NAME = 'getStateForDebugging';
15+
16+
let requestId = 0;
17+
18+
async function call(host, rpcMethod, rpcParams = []) {
19+
const body = {
20+
jsonrpc: '2.0',
21+
id: `bridge-state-data-parser-${requestId++}`,
22+
method: rpcMethod,
23+
params: rpcParams
24+
};
25+
26+
const response = await fetch(host, {
27+
method: 'POST',
28+
headers: { 'Content-Type': 'application/json' },
29+
body: JSON.stringify(body)
30+
});
31+
32+
try {
33+
const data = await response.json();
34+
35+
if ('error' in data) {
36+
return undefined;
37+
}
38+
39+
return data.result;
40+
} catch (e) {
41+
return undefined;
42+
}
43+
}
44+
45+
async function getStateForDebugging(host) {
46+
const getMethodABI = Bridge.abi.find(m => m.name === GET_STATE_FOR_DEBUGGING_METHOD_NAME);
47+
const outputType = getMethodABI.outputs[0].type;
48+
const getMethodEncodedCall = web3abi.encodeFunctionCall(getMethodABI);
49+
const callArguments = {
50+
data: getMethodEncodedCall,
51+
to: '0x0000000000000000000000000000000001000006',
52+
from: '0x0000000000000000000000000000000000000000'
53+
};
54+
const callToBridge = await call(host, 'eth_call', [callArguments, 'latest']);
55+
if (callToBridge === undefined) {
56+
return undefined;
57+
}
58+
const decodedCallToBridge = web3abi.decodeParameter(outputType, callToBridge);
59+
return decodedCallToBridge;
60+
}
61+
1362
class BridgeState {
1463
constructor(
1564
activeFederationUtxos,
@@ -26,9 +75,8 @@ class BridgeState {
2675
}
2776
}
2877

29-
module.exports.getBridgeState = async web3 => {
30-
const bridge = Bridge.build(web3);
31-
const bridgeStateEncoded = await bridge.methods.getStateForDebugging().call();
78+
module.exports.getBridgeState = async host => {
79+
const bridgeStateEncoded = await getStateForDebugging(host);
3280
const decodedListOfStates = RLP.decode(bridgeStateEncoded);
3381

3482
const activeFederationUtxos = activeFederationUtxosParser(decodedListOfStates[1]);

0 commit comments

Comments
 (0)