Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Few improvements #11

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require('hardhat-storage-layout')
require('@nomiclabs/hardhat-waffle')
require('@nomiclabs/hardhat-etherscan')
require('@openzeppelin/hardhat-upgrades')
require('dotenv').config()

const SUPPORTED_NETWORKS = [
'rinkeby',
Expand Down
35 changes: 31 additions & 4 deletions lib/get-router-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
curry,
assoc,
flatten,
toPairs,
fromPairs,
mergeAll,
} = require('ramda')
/* eslint-disable-next-line no-shadow */
const ethers = require('ethers')
Expand All @@ -16,8 +19,8 @@
const { getDeployedRouterContract } = require('./get-deployed-contract')
const { constants: { metadataChainIds, ZERO_ADDRESS } } = require('@pnetwork-association/ptokens-utils')

// TODO Remove the need to decrypt a private key for this CLI command!

Check warning on line 22 in lib/get-router-state.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected 'todo' comment
// TODO Remove the need to decrypt the key twice due to getting the fee other contract!

Check warning on line 23 in lib/get-router-state.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected 'todo' comment

const INITIAL_STATE = {}
const EMPTY_FXN_ARGS = []
Expand All @@ -32,6 +35,7 @@
const TOKEN_INFOS_STATE_KEY = 'tokenInfos'
const FEE_CONTRACT_STATE_KEY = 'feeContract'
const ORIGIN_CHAIN_ID_FXN = 'ORIGIN_CHAIN_ID'
const ORIGIN_CHAIN_STATE_KEY = 'originChain'
const CHAIN_ID_NAMES = keys(metadataChainIds)
const GLOBAL_MIN_FEE_STATE_KEY = 'globalMinFee'
const SAFE_VAULT_ADDRESS_STATE_KEY = 'safeVault'
Expand All @@ -43,6 +47,7 @@
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'
Expand Down Expand Up @@ -190,9 +195,9 @@
name: normalizeName(_name),
address: _tokenContract.address,
symbol: normalizeSymbol(_symbol),
originChainId: _originChainIdHex,
[ORIGIN_CHAIN_ID_STATE_KEY]: _originChainIdHex,
totalSupply: _totalSupply.toString(),
originChain: getHumanReadableOriginChainIdFromHex(_originChainIdHex)
[ORIGIN_CHAIN_STATE_KEY]: getHumanReadableOriginChainIdFromHex(_originChainIdHex)
}

if (_maybeBalance === false)
Expand Down Expand Up @@ -221,7 +226,7 @@
const getPNetworkAddressesFromVaultContracts = _vaultContracts =>
Promise.all(_vaultContracts.map(getPNetworkAddressFromVaultContract))

// FIXME flatten this out too!

Check warning on line 229 in lib/get-router-state.js

View workflow job for this annotation

GitHub Actions / build (16.x)

Unexpected 'fixme' comment
const getVaultInfo = (_vaultNetworks, _vaultContracts) =>
Promise.all([
getSupportedTokensFromVaultContracts(_vaultContracts),
Expand Down Expand Up @@ -315,6 +320,7 @@
.divUnsafe(FixedNumber.fromString(`${1e18}`))
.round(4)
} ${_tokenInfos[_i].symbol}`,
actualAmount: _balance.toString(),
address: _tokenInfos[_i].address,
}
return assoc(_tokenInfos[_i].symbol, balanceInfo, _obj)
Expand All @@ -331,12 +337,33 @@
)
.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) =>
gitmp01 marked this conversation as resolved.
Show resolved Hide resolved
Promise.resolve(getErc777Contract(_signer, _keyValuePair[1].address))
.then(_contract => _contract.ORIGIN_CHAIN_ID())
.then(_originChainId => mergeAll([_keyValuePair[1], {
[ORIGIN_CHAIN_ID_STATE_KEY]: _originChainId,
[ORIGIN_CHAIN_STATE_KEY]: getHumanReadableOriginChainIdFromHex(_originChainId),
}]))
.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 =>
Expand Down Expand Up @@ -462,12 +489,12 @@
.then(getSafeVaultContractOwnerAndAddToState)
.then(getSafeVaultContractStateAndAddToState)
.then(prepareOutputAndPutInState)
.then(_state => console.dir(_state[OUTPUT_STATE_KEY], { depth: null }))
.then(_state => console.info(JSON.stringify(_state[OUTPUT_STATE_KEY])))
.catch(console.warn)

const getSupportedTokens = _routerAddress =>
getSupportedTokensList(_routerAddress)
.then(_state => console.dir(_state[TOKEN_INFOS_STATE_KEY], { depth: null }))
.then(_state => console.dir(JSON.stringify(_state[TOKEN_INFOS_STATE_KEY], { depth: null })))
.catch(console.warn)

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ptokens-router-contract",
"version": "2.3.0",
"version": "2.4.0",
"description": "The pTokens interim-chain router contract & CLI",
"main": "cli.js",
"scripts": {
Expand Down
Loading