|
| 1 | +import { ApiPromise, WsProvider } from '@polkadot/api'; |
| 2 | +import { isNull } from '@polkadot/util'; |
| 3 | +import { BN } from "bn.js"; |
| 4 | + |
| 5 | +async function main() { |
| 6 | + const wsProvider = new WsProvider('wss://api-kusama.interlay.io/parachain'); |
| 7 | + const api = await ApiPromise.create({ provider: wsProvider }); |
| 8 | + |
| 9 | + // Select the block hash at which to compare the vaults |
| 10 | + const apiAt = await api.at("0x228ddb99b4a5f2944f3c0ad13d81c0e68310fb2e254b3e73e2ce93c2b54df9e7"); |
| 11 | + |
| 12 | + const allVaults = await apiAt.query.vaultRegistry.vaults.entries(); |
| 13 | + |
| 14 | + let ksmSecureThresholds = await apiAt.query.vaultRegistry.secureCollateralThreshold({"collateral":{"token":"KSM"},"wrapped":{"token":"KBTC"}}); |
| 15 | + let kintSecureThresholds = await apiAt.query.vaultRegistry.secureCollateralThreshold({"collateral":{"token":"KINT"},"wrapped":{"token":"KBTC"}}); |
| 16 | + let lksmSecureThresholds = await apiAt.query.vaultRegistry.secureCollateralThreshold({"collateral":{"foreignAsset":2},"wrapped":{"token":"KBTC"}}); |
| 17 | + |
| 18 | + let thresholds = new Map(); |
| 19 | + thresholds.set('{"collateral":{"token":"KSM"},"wrapped":{"token":"KBTC"}}',new BN(ksmSecureThresholds.toString())); |
| 20 | + thresholds.set('{"collateral":{"token":"KINT"},"wrapped":{"token":"KBTC"}}',new BN(kintSecureThresholds.toString())); |
| 21 | + thresholds.set('{"collateral":{"foreignAsset":2},"wrapped":{"token":"KBTC"}}',new BN(lksmSecureThresholds.toString())); |
| 22 | + |
| 23 | + let totalCollateralAvailable = new Map(); |
| 24 | + totalCollateralAvailable.set('{"collateral":{"token":"KSM"},"wrapped":{"token":"KBTC"}}',new BN(0)); |
| 25 | + totalCollateralAvailable.set('{"collateral":{"token":"KINT"},"wrapped":{"token":"KBTC"}}',new BN(0)); |
| 26 | + totalCollateralAvailable.set('{"collateral":{"foreignAsset":2},"wrapped":{"token":"KBTC"}}',new BN(0)); |
| 27 | + |
| 28 | + |
| 29 | + // Look up all Vaults from test chain and compare the vault objects |
| 30 | + for (const [key, vault] of allVaults) { |
| 31 | + let vaultJSON: any = vault.toJSON(); |
| 32 | + let isActive = JSON.stringify(vaultJSON.status).includes('true'); |
| 33 | + |
| 34 | + // inactive vaults don't contribute capacity |
| 35 | + if (!isActive) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + let n = JSON.stringify(vaultJSON.secureCollateralThreshold).includes("null"); |
| 39 | + |
| 40 | + let vaultId = vaultJSON.id; |
| 41 | + let collateral = new BN((await apiAt.query.vaultStaking.totalCurrentStake(0, vaultId)).toString()); |
| 42 | + |
| 43 | + if (collateral.isZero()) { |
| 44 | + // skip usdt vaults since there aren't any |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + let customThreshold = new BN(0); |
| 49 | + if (!n) { |
| 50 | + customThreshold = new BN(vaultJSON.secureCollateralThreshold.toString().replace(/0x/, ''), 16); |
| 51 | + } |
| 52 | + let globalThreshold = thresholds.get(JSON.stringify(vaultJSON.id.currencies)); |
| 53 | + |
| 54 | + let threshold = customThreshold.gt(globalThreshold) ? customThreshold : globalThreshold; |
| 55 | + |
| 56 | + let availableCollateral = collateral.div(threshold); |
| 57 | + let key = JSON.stringify(vaultId.currencies); |
| 58 | + totalCollateralAvailable.set(key, availableCollateral.add(totalCollateralAvailable.get(key))); |
| 59 | + } |
| 60 | + |
| 61 | + for (const [a,availableCollateral] of totalCollateralAvailable.entries()) { |
| 62 | + let key = JSON.parse(a).collateral; |
| 63 | + let exchangeRate = await apiAt.query.oracle.aggregate({exchangerate: JSON.parse(a).collateral}); |
| 64 | + let mintingCapacity = availableCollateral.mul(new BN("1000000000000000000")).div(new BN(exchangeRate.toString())); |
| 65 | + console.log("Expected capacity for", key, "=", mintingCapacity.toString(), "* 1e18"); |
| 66 | + } |
| 67 | + |
| 68 | + await api.disconnect(); |
| 69 | +} |
| 70 | + |
| 71 | +main(); |
0 commit comments