2019-05-08 16:12:02 +03:00
|
|
|
require('dotenv').config()
|
|
|
|
const BN = require('bignumber.js')
|
|
|
|
const Web3 = require('web3')
|
|
|
|
const logger = require('./logger')('getBalances')
|
2019-07-12 15:49:46 +03:00
|
|
|
const { BRIDGE_MODES } = require('../commons')
|
2019-05-08 16:12:02 +03:00
|
|
|
|
|
|
|
const Web3Utils = Web3.utils
|
|
|
|
|
2019-09-13 10:11:38 +03:00
|
|
|
const {
|
|
|
|
COMMON_HOME_RPC_URL,
|
|
|
|
COMMON_FOREIGN_RPC_URL,
|
|
|
|
COMMON_HOME_BRIDGE_ADDRESS,
|
|
|
|
COMMON_FOREIGN_BRIDGE_ADDRESS
|
|
|
|
} = process.env
|
2019-05-08 16:12:02 +03:00
|
|
|
|
2019-09-13 10:11:38 +03:00
|
|
|
const homeProvider = new Web3.providers.HttpProvider(COMMON_HOME_RPC_URL)
|
2019-05-08 16:12:02 +03:00
|
|
|
const web3Home = new Web3(homeProvider)
|
|
|
|
|
2019-09-13 10:11:38 +03:00
|
|
|
const foreignProvider = new Web3.providers.HttpProvider(COMMON_FOREIGN_RPC_URL)
|
2019-05-08 16:12:02 +03:00
|
|
|
const web3Foreign = new Web3(foreignProvider)
|
|
|
|
|
2019-07-12 15:49:46 +03:00
|
|
|
const {
|
|
|
|
ERC20_ABI,
|
|
|
|
ERC677_ABI,
|
|
|
|
BLOCK_REWARD_ABI,
|
|
|
|
HOME_ERC_TO_ERC_ABI,
|
|
|
|
HOME_ERC_TO_NATIVE_ABI,
|
|
|
|
FOREIGN_ERC_TO_ERC_ABI,
|
|
|
|
FOREIGN_ERC_TO_NATIVE_ABI,
|
|
|
|
FOREIGN_NATIVE_TO_ERC_ABI
|
|
|
|
} = require('../commons')
|
2019-05-08 16:12:02 +03:00
|
|
|
|
|
|
|
async function main(bridgeMode) {
|
2019-05-30 21:05:36 +03:00
|
|
|
if (bridgeMode === BRIDGE_MODES.ERC_TO_ERC) {
|
2019-09-13 10:11:38 +03:00
|
|
|
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ERC_TO_ERC_ABI, COMMON_FOREIGN_BRIDGE_ADDRESS)
|
2019-05-30 21:05:36 +03:00
|
|
|
const erc20Address = await foreignBridge.methods.erc20token().call()
|
|
|
|
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
|
|
|
|
logger.debug('calling erc20Contract.methods.balanceOf')
|
2019-09-13 10:11:38 +03:00
|
|
|
const foreignErc20Balance = await erc20Contract.methods.balanceOf(COMMON_FOREIGN_BRIDGE_ADDRESS).call()
|
|
|
|
const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_ERC_ABI, COMMON_HOME_BRIDGE_ADDRESS)
|
2019-05-30 21:05:36 +03:00
|
|
|
logger.debug('calling homeBridge.methods.erc677token')
|
|
|
|
const tokenAddress = await homeBridge.methods.erc677token().call()
|
|
|
|
const tokenContract = new web3Home.eth.Contract(ERC677_ABI, tokenAddress)
|
|
|
|
logger.debug('calling tokenContract.methods.totalSupply()')
|
|
|
|
const totalSupply = await tokenContract.methods.totalSupply().call()
|
|
|
|
const foreignBalanceBN = new BN(foreignErc20Balance)
|
|
|
|
const foreignTotalSupplyBN = new BN(totalSupply)
|
|
|
|
const diff = foreignBalanceBN.minus(foreignTotalSupplyBN).toString(10)
|
|
|
|
logger.debug('Done')
|
|
|
|
return {
|
|
|
|
home: {
|
|
|
|
totalSupply: Web3Utils.fromWei(totalSupply)
|
|
|
|
},
|
|
|
|
foreign: {
|
|
|
|
erc20Balance: Web3Utils.fromWei(foreignErc20Balance)
|
|
|
|
},
|
|
|
|
balanceDiff: Number(Web3Utils.fromWei(diff)),
|
|
|
|
lastChecked: Math.floor(Date.now() / 1000)
|
|
|
|
}
|
2019-08-01 16:10:22 +03:00
|
|
|
} else if (bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC || bridgeMode === BRIDGE_MODES.NATIVE_TO_ERC_V1) {
|
2019-05-30 21:05:36 +03:00
|
|
|
logger.debug('calling web3Home.eth.getBalance')
|
2019-09-13 10:11:38 +03:00
|
|
|
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_NATIVE_TO_ERC_ABI, COMMON_FOREIGN_BRIDGE_ADDRESS)
|
2019-05-30 21:05:36 +03:00
|
|
|
const erc20Address = await foreignBridge.methods.erc677token().call()
|
2019-09-13 10:11:38 +03:00
|
|
|
const homeBalance = await web3Home.eth.getBalance(COMMON_HOME_BRIDGE_ADDRESS)
|
2019-05-30 21:05:36 +03:00
|
|
|
const tokenContract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
|
|
|
|
logger.debug('calling tokenContract.methods.totalSupply()')
|
|
|
|
const totalSupply = await tokenContract.methods.totalSupply().call()
|
|
|
|
const homeBalanceBN = new BN(homeBalance)
|
|
|
|
const foreignTotalSupplyBN = new BN(totalSupply)
|
|
|
|
const diff = homeBalanceBN.minus(foreignTotalSupplyBN).toString(10)
|
|
|
|
logger.debug('Done')
|
|
|
|
return {
|
|
|
|
home: {
|
|
|
|
balance: Web3Utils.fromWei(homeBalance)
|
|
|
|
},
|
|
|
|
foreign: {
|
|
|
|
totalSupply: Web3Utils.fromWei(totalSupply)
|
|
|
|
},
|
|
|
|
balanceDiff: Number(Web3Utils.fromWei(diff)),
|
|
|
|
lastChecked: Math.floor(Date.now() / 1000)
|
|
|
|
}
|
|
|
|
} else if (bridgeMode === BRIDGE_MODES.ERC_TO_NATIVE) {
|
2019-09-13 10:11:38 +03:00
|
|
|
const foreignBridge = new web3Foreign.eth.Contract(FOREIGN_ERC_TO_NATIVE_ABI, COMMON_FOREIGN_BRIDGE_ADDRESS)
|
2019-05-30 21:05:36 +03:00
|
|
|
const erc20Address = await foreignBridge.methods.erc20token().call()
|
|
|
|
const erc20Contract = new web3Foreign.eth.Contract(ERC20_ABI, erc20Address)
|
2020-02-18 19:17:01 +03:00
|
|
|
let investedAmountInDai = 0
|
|
|
|
let bridgeDsrBalance = 0
|
|
|
|
let displayChaiToken = false
|
2019-12-19 12:39:41 +03:00
|
|
|
|
2020-02-18 19:17:01 +03:00
|
|
|
try {
|
|
|
|
logger.debug('calling foreignBridge.methods.isChaiTokenEnabled')
|
|
|
|
if (await foreignBridge.methods.isChaiTokenEnabled().call()) {
|
|
|
|
displayChaiToken = true
|
|
|
|
logger.debug('calling foreignBridge.methods.investedAmountInDai')
|
|
|
|
investedAmountInDai = await foreignBridge.methods.investedAmountInDai().call()
|
|
|
|
logger.debug('calling foreignBridge.methods.dsrBalance')
|
|
|
|
bridgeDsrBalance = await foreignBridge.methods.dsrBalance().call()
|
|
|
|
} else {
|
|
|
|
logger.debug('Chai token is currently disabled')
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
logger.debug('Methods for chai token are not present')
|
|
|
|
}
|
|
|
|
|
2019-05-30 21:05:36 +03:00
|
|
|
logger.debug('calling erc20Contract.methods.balanceOf')
|
2019-09-13 10:11:38 +03:00
|
|
|
const foreignErc20Balance = await erc20Contract.methods.balanceOf(COMMON_FOREIGN_BRIDGE_ADDRESS).call()
|
2019-05-08 16:12:02 +03:00
|
|
|
|
2019-09-13 10:11:38 +03:00
|
|
|
const homeBridge = new web3Home.eth.Contract(HOME_ERC_TO_NATIVE_ABI, COMMON_HOME_BRIDGE_ADDRESS)
|
2019-05-30 21:05:36 +03:00
|
|
|
logger.debug('calling homeBridge.methods.blockRewardContract')
|
|
|
|
const blockRewardAddress = await homeBridge.methods.blockRewardContract().call()
|
|
|
|
const blockRewardContract = new web3Home.eth.Contract(BLOCK_REWARD_ABI, blockRewardAddress)
|
|
|
|
logger.debug('calling blockReward.methods.mintedTotally')
|
2019-12-19 12:39:41 +03:00
|
|
|
const mintedCoins = await blockRewardContract.methods.mintedTotallyByBridge(COMMON_HOME_BRIDGE_ADDRESS).call()
|
2019-05-30 21:05:36 +03:00
|
|
|
logger.debug('calling homeBridge.methods.totalBurntCoins')
|
|
|
|
const burntCoins = await homeBridge.methods.totalBurntCoins().call()
|
2019-05-08 16:12:02 +03:00
|
|
|
|
2019-05-30 21:05:36 +03:00
|
|
|
const mintedCoinsBN = new BN(mintedCoins)
|
|
|
|
const burntCoinsBN = new BN(burntCoins)
|
|
|
|
const totalSupplyBN = mintedCoinsBN.minus(burntCoinsBN)
|
|
|
|
const foreignErc20BalanceBN = new BN(foreignErc20Balance)
|
2020-02-18 19:17:01 +03:00
|
|
|
const investedAmountInDaiBN = new BN(investedAmountInDai)
|
|
|
|
const bridgeDsrBalanceBN = new BN(bridgeDsrBalance)
|
2019-12-19 12:39:41 +03:00
|
|
|
|
|
|
|
const diff = foreignErc20BalanceBN
|
2020-02-18 19:17:01 +03:00
|
|
|
.plus(investedAmountInDaiBN)
|
2019-12-19 12:39:41 +03:00
|
|
|
.minus(totalSupplyBN)
|
|
|
|
.toFixed()
|
|
|
|
|
2020-05-16 14:08:08 +03:00
|
|
|
const foreign = {
|
2019-12-19 12:39:41 +03:00
|
|
|
erc20Balance: Web3Utils.fromWei(foreignErc20Balance)
|
|
|
|
}
|
|
|
|
|
2020-02-18 19:17:01 +03:00
|
|
|
if (displayChaiToken) {
|
|
|
|
foreign.investedErc20Balance = Web3Utils.fromWei(investedAmountInDai)
|
|
|
|
foreign.accumulatedInterest = Web3Utils.fromWei(bridgeDsrBalanceBN.minus(investedAmountInDaiBN).toString(10))
|
|
|
|
}
|
|
|
|
|
2019-05-30 21:05:36 +03:00
|
|
|
logger.debug('Done')
|
|
|
|
return {
|
|
|
|
home: {
|
|
|
|
totalSupply: Web3Utils.fromWei(totalSupplyBN.toFixed())
|
|
|
|
},
|
2019-12-19 12:39:41 +03:00
|
|
|
foreign,
|
2019-05-30 21:05:36 +03:00
|
|
|
balanceDiff: Number(Web3Utils.fromWei(diff)),
|
|
|
|
lastChecked: Math.floor(Date.now() / 1000)
|
2019-05-08 16:12:02 +03:00
|
|
|
}
|
2019-10-21 15:57:28 +03:00
|
|
|
} else if (bridgeMode === BRIDGE_MODES.ARBITRARY_MESSAGE) {
|
|
|
|
return {
|
|
|
|
home: {},
|
|
|
|
foreign: {},
|
|
|
|
lastChecked: Math.floor(Date.now() / 1000)
|
|
|
|
}
|
2019-05-30 21:05:36 +03:00
|
|
|
} else {
|
|
|
|
throw new Error(`Unrecognized bridge mode: '${bridgeMode}'`)
|
2019-05-08 16:12:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = main
|