11const Bridge = require ( '@rsksmart/rsk-precompiled-abis' ) . bridge ;
22const ethUtils = require ( 'ethereumjs-util' ) ;
3+ const web3abi = require ( 'web3-eth-abi' ) ;
34
45const RLP = ethUtils . rlp ;
56
@@ -10,6 +11,54 @@ const pegoutWaitingConfirmationsParser = require('./pegout-waiting-confirmation'
1011const 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+
1362class 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