diff --git a/lib/get-router-state.js b/lib/get-router-state.js index 10d7d65..3f0b18c 100644 --- a/lib/get-router-state.js +++ b/lib/get-router-state.js @@ -4,6 +4,8 @@ const { curry, assoc, flatten, + toPairs, + fromPairs, } = require('ramda') /* eslint-disable-next-line no-shadow */ const ethers = require('ethers') @@ -43,6 +45,7 @@ const VAULT_CONTRACTS_STATE_KEY = 'vaultContracts' const ROUTER_CONTRACT_STATE_KEY = 'routerContract' const TOKEN_ADDRESSES_STATE_KEY = 'tokenAddresses' const SAFE_VAULT_STATE_STATE_KEY = 'safeVaultState' +const ORIGIN_CHAIN_ID_STATE_KEY = 'originChainId' const SUPPORTED_TOKENS_STATE_KEY = 'supportedTokens' const GET_SUPPORTED_TOKENS_FXN = 'getSupportedTokens' const CHAIN_MULTIPLIERS_STATE_KEY = 'chainMultipliers' @@ -315,6 +318,7 @@ const getAllTokenBalances = (_tokenInfos, _tokenContracts, _address) => .divUnsafe(FixedNumber.fromString(`${1e18}`)) .round(4) } ${_tokenInfos[_i].symbol}`, + actualAmount: _balance.toString(), address: _tokenInfos[_i].address, } return assoc(_tokenInfos[_i].symbol, balanceInfo, _obj) @@ -331,12 +335,30 @@ const getSafeVaultTokenBalancesAndAddToState = _state => ) .then(_balances => assoc(SAFE_VAULT_CONTRACT_TOKEN_BALANCES_STATE_KEY, _balances, _state)) +// _keyValuePair is +// ['pNAME', { balance: '...', approxAmount: '..', address: '..'} ] +// Will get +// ['pNAME', { balance: '...', approxAmount: '..', address: '..', originChainId: '..'} ] +const attachOriginChainId = curry((_signer, _keyValuePair) => + Promise.resolve(getErc777Contract(_signer, _keyValuePair[1].address)) + .then(_contract => _contract.ORIGIN_CHAIN_ID()) + .then(_originChainId => assoc(ORIGIN_CHAIN_ID_STATE_KEY, _originChainId, _keyValuePair[1])) + .then(_newObj => [_keyValuePair[0], _newObj]) +) + +const attachOriginChainIdToEachTokenBalance = curry((_signer, _balances) => + Promise.resolve(toPairs(_balances)) + .then(_pairs => Promise.all(_pairs.map(attachOriginChainId(_signer)))) + .then(fromPairs) +) + const getNodeOperatorFeeSinkTokenBalancesAndAddToState = _state => getAllTokenBalances( _state[TOKEN_INFOS_STATE_KEY], _state[TOKEN_CONTRACTS_STATE_KEY], _state[FEE_SINK_ADDRESSES_STATE_KEY].nodeOperatorsFeeSinkAddress ) + .then(attachOriginChainIdToEachTokenBalance(_state[ROUTER_CONTRACT_STATE_KEY].signer)) .then(_balances => assoc(NODE_OPERATORS_FEE_TOKEN_BALANCES_STATE_KEY, _balances, _state)) const getNetworkFeeSinkTokenBalancesAndAddToState = _state => @@ -462,7 +484,7 @@ const getRouterState = _routerAddress => .then(getSafeVaultContractOwnerAndAddToState) .then(getSafeVaultContractStateAndAddToState) .then(prepareOutputAndPutInState) - .then(_state => console.log(JSON.stringify(_state[OUTPUT_STATE_KEY]))) + .then(_state => console.info(JSON.stringify(_state[OUTPUT_STATE_KEY]))) .catch(console.warn) const getSupportedTokens = _routerAddress => diff --git a/package-lock.json b/package-lock.json index dff63e9..9cdce00 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ptokens-router-contract", - "version": "2.2.0", + "version": "2.3.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "ptokens-router-contract", - "version": "2.2.0", + "version": "2.3.0", "license": "MIT", "dependencies": { "@nomiclabs/hardhat-etherscan": "^3.1.7",