Created
April 15, 2020 16:12
-
-
Save rhlsthrm/94d001011c608f034fd9cf2a8c6d46fd to your computer and use it in GitHub Desktop.
Calculate pending balances
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { connect } from "ts-nats"; | |
import { StateChannelJSON, toBN, CoinTransfer } from "@connext/types"; | |
import { Zero, AddressZero } from "ethers/constants"; | |
import { formatEther } from "ethers/utils"; | |
type FreeBalanceAppState = { | |
activeApps: any[]; | |
tokenAddresses: string[]; | |
balances: CoinTransfer[][]; | |
}; | |
const nodeAddress = "0xf3f722f6Ca6026FB7cC9B63523bBC6a73D3aad39"; | |
const run = async () => { | |
const nats = await connect({ | |
servers: ["nats://indra.connext.network:4222"], | |
}); | |
const response = await nats.request("admin.get-all-channels", 30000, JSON.stringify({ | |
token: "ADMIN_TOKEN;", | |
id: Date.now(), | |
})); | |
const data = JSON.parse(response.data); | |
const channels = data.response as StateChannelJSON[]; | |
let lockedTokenBalanceToNode = Zero; | |
let lockedEthBalanceToNode = Zero; | |
let lockedTokenBalanceToUsers = Zero; | |
let lockedEthBalanceToUsers = Zero; | |
let count = 0; | |
for (const c of channels) { | |
const response = await nats.request("admin.get-state-channel-by-multisig", 30000, JSON.stringify({ | |
token: "ADMIN_TOKEN;", | |
id: Date.now(), | |
multisigAddress: c.multisigAddress, | |
})); | |
const data = JSON.parse(response.data); | |
const channel = data.response as StateChannelJSON; | |
const freeBalanceState = channel.freeBalanceAppInstance | |
.latestState as FreeBalanceAppState; | |
const tokenAddressIdx = freeBalanceState.tokenAddresses.findIndex( | |
(address) => address !== AddressZero | |
); | |
const ethIdx = freeBalanceState.tokenAddresses.findIndex( | |
(address) => address === AddressZero | |
); | |
// always will have eth | |
if (tokenAddressIdx === -1 && ethIdx === -1) { | |
console.warn( | |
`Channel ${channel.multisigAddress} has no free balance entries` | |
); | |
continue; | |
} | |
if (tokenAddressIdx > -1) { | |
const nodeBalance = toBN( | |
freeBalanceState.balances[tokenAddressIdx].find( | |
(balance) => balance.to === nodeAddress | |
).amount | |
); | |
lockedTokenBalanceToNode = lockedTokenBalanceToNode.add(nodeBalance); | |
const userBalance = toBN( | |
freeBalanceState.balances[tokenAddressIdx].find( | |
(balance) => balance.to !== nodeAddress | |
).amount | |
); | |
lockedTokenBalanceToUsers = lockedTokenBalanceToUsers.add(userBalance); | |
} | |
const nodeBalance = toBN( | |
freeBalanceState.balances[ethIdx].find( | |
(balance) => balance.to === nodeAddress | |
).amount | |
); | |
lockedEthBalanceToNode = lockedEthBalanceToNode.add(nodeBalance); | |
const userBalance = toBN( | |
freeBalanceState.balances[ethIdx].find( | |
(balance) => balance.to !== nodeAddress | |
).amount | |
); | |
lockedEthBalanceToUsers = lockedEthBalanceToUsers.add(userBalance); | |
count += 1; | |
} | |
console.log(`Counted ${count} channels`); | |
console.log('lockedTokenBalanceToNode: ', formatEther(lockedTokenBalanceToNode)); | |
console.log('lockedEthBalanceToNode: ', formatEther(lockedEthBalanceToNode)); | |
console.log('lockedTokenBalanceToUsers: ', formatEther(lockedTokenBalanceToUsers)); | |
console.log('lockedEthBalanceToUsers: ', formatEther(lockedEthBalanceToUsers)); | |
}; | |
run() | |
.then((a) => console.log("Done!")) | |
.catch((e) => console.error(e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment