|
| 1 | +/* eslint @typescript-eslint/no-var-requires: "off" */ |
| 2 | +import { createSubstrateAPI } from "../src/factory"; |
| 3 | +import { ApiPromise, Keyring } from "@polkadot/api"; |
| 4 | +import { DefaultTransactionAPI } from "../src/parachain"; |
| 5 | +import { cryptoWaitReady } from "@polkadot/util-crypto"; |
| 6 | +import { XcmVersionedMultiLocation } from "@polkadot/types/lookup"; |
| 7 | + |
| 8 | +import { SubmittableExtrinsic } from "@polkadot/api/types"; |
| 9 | +import { assert } from "console"; |
| 10 | +import { isForeignAsset } from "../src"; |
| 11 | +import { BN } from "bn.js"; |
| 12 | + |
| 13 | +const readline = require("readline"); |
| 14 | +const yargs = require("yargs/yargs"); |
| 15 | +const { hideBin } = require("yargs/helpers"); |
| 16 | + |
| 17 | +const args = yargs(hideBin(process.argv)) |
| 18 | + .option("parachain-endpoint", { |
| 19 | + description: "The wss url of the parachain", |
| 20 | + type: "string", |
| 21 | + }) |
| 22 | + .option("with-defaults-of", { |
| 23 | + description: "Which default values to use", |
| 24 | + choices: ['testnet-kintsugi', 'testnet-interlay'], |
| 25 | + }) |
| 26 | + .argv; |
| 27 | + |
| 28 | +main().catch((err) => { |
| 29 | + console.log("Error thrown by script:"); |
| 30 | + console.log(err); |
| 31 | +}); |
| 32 | + |
| 33 | +function constructLendingSetup(api: ApiPromise) { |
| 34 | + const addMarkets = [ |
| 35 | + api.tx.loans.addMarket( |
| 36 | + { |
| 37 | + Token: 'KBTC' |
| 38 | + }, |
| 39 | + { |
| 40 | + collateralFactor: 630000, |
| 41 | + liquidationThreshold: 670000, |
| 42 | + reserveFactor: 200000, |
| 43 | + closeFactor: 500000, |
| 44 | + liquidateIncentive: "1100000000000000000", |
| 45 | + liquidateIncentiveReservedFactor: 25000, |
| 46 | + rateModel: { |
| 47 | + Jump: { |
| 48 | + baseRate: 0, |
| 49 | + jumpRate: "50000000000000000", |
| 50 | + fullRate: "500000000000000000", |
| 51 | + jumpUtilization: 900000 |
| 52 | + } |
| 53 | + }, |
| 54 | + state: "Pending", |
| 55 | + supplyCap: "2000000000", |
| 56 | + borrowCap: "2000000000", |
| 57 | + lendTokenId: {LendToken: 1} |
| 58 | + } |
| 59 | + ), api.tx.loans.addMarket( |
| 60 | + { |
| 61 | + Token: 'KSM' |
| 62 | + }, |
| 63 | + { |
| 64 | + collateralFactor: 540000, |
| 65 | + liquidationThreshold: 610000, |
| 66 | + reserveFactor: 200000, |
| 67 | + closeFactor: 500000, |
| 68 | + liquidateIncentive: "1100000000000000000", |
| 69 | + liquidateIncentiveReservedFactor: 25000, |
| 70 | + rateModel: { |
| 71 | + Jump: { |
| 72 | + baseRate: 0, |
| 73 | + jumpRate: "150000000000000000", |
| 74 | + fullRate: "400000000000000000", |
| 75 | + jumpUtilization: 900000 |
| 76 | + } |
| 77 | + }, |
| 78 | + state: "Pending", |
| 79 | + supplyCap: "30000000000000000", |
| 80 | + borrowCap: "30000000000000000", |
| 81 | + lendTokenId: {LendToken: 2} |
| 82 | + } |
| 83 | + ), api.tx.loans.addMarket( |
| 84 | + { |
| 85 | + ForeignAsset: 1 // usdt |
| 86 | + }, |
| 87 | + { |
| 88 | + collateralFactor: 650000, |
| 89 | + liquidationThreshold: 690000, |
| 90 | + reserveFactor: 200000, |
| 91 | + closeFactor: 500000, |
| 92 | + liquidateIncentive: "1100000000000000000", |
| 93 | + liquidateIncentiveReservedFactor: 25000, |
| 94 | + rateModel: { |
| 95 | + Jump: { |
| 96 | + baseRate: 0, |
| 97 | + jumpRate: "100000000000000000", |
| 98 | + fullRate: "400000000000000000", |
| 99 | + jumpUtilization: 900000 |
| 100 | + } |
| 101 | + }, |
| 102 | + state: "Pending", |
| 103 | + supplyCap: "80000000000", |
| 104 | + borrowCap: "80000000000", |
| 105 | + lendTokenId: {LendToken: 3} |
| 106 | + } |
| 107 | + ), api.tx.loans.addMarket( |
| 108 | + { |
| 109 | + ForeignAsset: 2 // movr |
| 110 | + }, |
| 111 | + { |
| 112 | + collateralFactor: 470000, |
| 113 | + liquidationThreshold: 560000, |
| 114 | + reserveFactor: 200000, |
| 115 | + closeFactor: 500000, |
| 116 | + liquidateIncentive: "1100000000000000000", |
| 117 | + liquidateIncentiveReservedFactor: 25000, |
| 118 | + rateModel: { |
| 119 | + Jump: { |
| 120 | + baseRate: 0, |
| 121 | + jumpRate: "150000000000000000", |
| 122 | + fullRate: "400000000000000000", |
| 123 | + jumpUtilization: 900000 |
| 124 | + } |
| 125 | + }, |
| 126 | + state: "Pending", |
| 127 | + supplyCap: "20000000000000000000000", |
| 128 | + borrowCap: "20000000000000000000000", |
| 129 | + lendTokenId: {LendToken: 4} |
| 130 | + } |
| 131 | + ) |
| 132 | + ]; |
| 133 | + |
| 134 | + |
| 135 | + let activateMarkets = [ |
| 136 | + api.tx.loans.activateMarket({Token: "KBTC"}), |
| 137 | + api.tx.loans.activateMarket({Token: "KSM"}), |
| 138 | + api.tx.loans.activateMarket({ForeignAsset: 1}), |
| 139 | + api.tx.loans.activateMarket({ForeignAsset: 2}), |
| 140 | + ]; |
| 141 | + |
| 142 | + return addMarkets.concat(activateMarkets); |
| 143 | +} |
| 144 | + |
| 145 | +function constructFundingSetup(api: ApiPromise) { |
| 146 | + const tokens = [{Token: "KSM"}, {Token: "KINT"}]; |
| 147 | + const faucetSetup = tokens.map((token) => { |
| 148 | + return api.tx.tokens.setBalance( |
| 149 | + "5DqzGaydetDXGya818gyuHA7GAjEWRsQN6UWNKpvfgq2KyM7", |
| 150 | + token, |
| 151 | + 20000000000000, |
| 152 | + 0 |
| 153 | + ); |
| 154 | + }); |
| 155 | + const calls = faucetSetup.concat([ |
| 156 | + api.tx.tokens.setBalance( |
| 157 | + "a3cgeH7D28bBsHY4hGLzxkMFUcFQmjGgDa2kmxg3D9Z6AyhtL", // treasury |
| 158 | + {Token: "KINT"}, |
| 159 | + "1000000000000000000000000", //1e12 KINT |
| 160 | + 0 |
| 161 | + ) |
| 162 | + ]); |
| 163 | + return calls; |
| 164 | +} |
| 165 | + |
| 166 | +function constructVaultRegistrySetup(api: ApiPromise) { |
| 167 | + const currencyPair = { |
| 168 | + collateral: {ForeignAsset: 3}, // lksm |
| 169 | + wrapped: {Token: "KBTC"} |
| 170 | + }; |
| 171 | + return [ |
| 172 | + api.tx.vaultRegistry.setLiquidationCollateralThreshold(currencyPair, "1450000000000000000"), |
| 173 | + api.tx.vaultRegistry.setPremiumRedeemThreshold(currencyPair, "1650000000000000000"), |
| 174 | + api.tx.vaultRegistry.setSecureCollateralThreshold(currencyPair, "1800000000000000000"), |
| 175 | + api.tx.vaultRegistry.setMinimumCollateral(currencyPair.collateral, "20000000000000"), |
| 176 | + api.tx.vaultRegistry.setSystemCollateralCeiling(currencyPair, "38000000000000000"), |
| 177 | + ]; |
| 178 | +} |
| 179 | + |
| 180 | +function constructRewardsSetup(api: ApiPromise) { |
| 181 | + const blocksPerYears = 365 * 24 * 60 * 5; // 5 per minute |
| 182 | + const vaultAnnuity = [ |
| 183 | + api.tx.tokens.setBalance( |
| 184 | + "a3cgeH7D3w3wu37yHx4VZeae4EUqNTw5RobTp5KvcMsrPLWJg", // vaultAnnuity |
| 185 | + {Token: "KINT"}, |
| 186 | + new BN(102803978514).muln(blocksPerYears), |
| 187 | + 0 |
| 188 | + ), |
| 189 | + api.tx.vaultAnnuity.updateRewards(), |
| 190 | + ]; |
| 191 | + const escrowAnnuity = [ |
| 192 | + api.tx.tokens.setBalance( |
| 193 | + "a3cgeH7CzXoGgXh453eaSJRCvbbBKZN4mejwUVkic8efQUi5R", // escrowAnnuity |
| 194 | + {Token: "KINT"}, |
| 195 | + new BN(47564687975).muln(blocksPerYears), |
| 196 | + 0 |
| 197 | + ), |
| 198 | + api.tx.escrowAnnuity.updateRewards(), |
| 199 | + ]; |
| 200 | + return vaultAnnuity.concat(escrowAnnuity); |
| 201 | +} |
| 202 | + |
| 203 | +function constructAmmSetup(api: ApiPromise) { |
| 204 | + const pools = [ |
| 205 | + [{ Token: "KBTC" }, { Token: "KSM" }, 45_000], |
| 206 | + [{ Token: "KBTC" }, { ForeignAsset: 1 }, 40_000], // usdt |
| 207 | + [{ Token: "KBTC" }, { ForeignAsset: 2 }, 20_000], // movr |
| 208 | + [{ Token: "KINT" }, { ForeignAsset: 2 }, 35_000], // movr |
| 209 | + ]; |
| 210 | + const basicPoolSetup = pools.map(([token1, token2, reward]) => { |
| 211 | + return [ |
| 212 | + api.tx.zenlinkProtocol.createPair(token1, token2), |
| 213 | + api.tx.farming.updateRewardSchedule( |
| 214 | + { LpToken: [token1, token2] }, |
| 215 | + { Token: "KINT" }, |
| 216 | + 60 * 24 * 7 * 12, // three months, reward period is per minute |
| 217 | + new BN(10).pow(new BN(12)).muln(reward as any), |
| 218 | + ), |
| 219 | + ]; |
| 220 | + }).reduce((x, y) => { return x.concat(y);}); |
| 221 | + |
| 222 | + return basicPoolSetup; |
| 223 | +} |
| 224 | + |
| 225 | +function constructForeignAssetSetup(api: ApiPromise) { |
| 226 | + return [ |
| 227 | + api.tx.assetRegistry.registerAsset( |
| 228 | + { |
| 229 | + decimals: 6, |
| 230 | + name: "Tether USD", |
| 231 | + symbol: "USDT", |
| 232 | + existentialDeposit: 0, |
| 233 | + location: null, |
| 234 | + additional: { feePerSecond: 8153838, coingeckoId: "" } |
| 235 | + }, |
| 236 | + 1 |
| 237 | + ), api.tx.assetRegistry.registerAsset( |
| 238 | + { |
| 239 | + decimals: 18, |
| 240 | + name: "Moonriver Token", |
| 241 | + symbol: "MOVR", |
| 242 | + existentialDeposit: 0, |
| 243 | + location: null, |
| 244 | + additional: { feePerSecond: 0, coingeckoId: "" } |
| 245 | + }, |
| 246 | + 2 |
| 247 | + ), api.tx.assetRegistry.registerAsset( |
| 248 | + { |
| 249 | + decimals: 12, |
| 250 | + name: "Liquid KSM", |
| 251 | + symbol: "LKSM", |
| 252 | + existentialDeposit: 0, |
| 253 | + location: { |
| 254 | + V1: { |
| 255 | + parents: 1, |
| 256 | + interior: { |
| 257 | + X2: [ |
| 258 | + { |
| 259 | + Parachain: 2000 |
| 260 | + }, |
| 261 | + { |
| 262 | + GeneralKey: "0x0083" |
| 263 | + } |
| 264 | + ] |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + }, |
| 269 | + additional: { feePerSecond: 233100000000, coingeckoId: "liquid-ksm" } |
| 270 | + }, |
| 271 | + 3 |
| 272 | + ) |
| 273 | + ]; |
| 274 | +} |
| 275 | + |
| 276 | +function toUrl(extrinsic: SubmittableExtrinsic<"promise">, endpoint: string) { |
| 277 | + return "https://polkadot.js.org/apps/?rpc=" + |
| 278 | + encodeURIComponent(endpoint) + |
| 279 | + "#/extrinsics/decode/" + |
| 280 | + extrinsic.method.toHex(); |
| 281 | +} |
| 282 | + |
| 283 | +async function main(): Promise<void> { |
| 284 | + await cryptoWaitReady(); |
| 285 | + |
| 286 | + switch (args['with-defaults-of']) { |
| 287 | + case 'testnet-interlay': |
| 288 | + if (args['parachain-endpoint'] === undefined) { |
| 289 | + args['parachain-endpoint'] = "wss://api.interlay.io/parachain"; |
| 290 | + } |
| 291 | + break; |
| 292 | + case 'testnet-kintsugi': |
| 293 | + if (args['parachain-endpoint'] === undefined) { |
| 294 | + args['parachain-endpoint'] = "wss://api-dev-kintsugi.interlay.io/parachain"; |
| 295 | + } |
| 296 | + break; |
| 297 | + } |
| 298 | + |
| 299 | + const paraApi = await createSubstrateAPI(args['parachain-endpoint']); |
| 300 | + |
| 301 | + let calls = [ |
| 302 | + constructFundingSetup(paraApi), |
| 303 | + constructForeignAssetSetup(paraApi), |
| 304 | + constructLendingSetup(paraApi), |
| 305 | + constructVaultRegistrySetup(paraApi), |
| 306 | + constructRewardsSetup(paraApi), |
| 307 | + constructAmmSetup(paraApi), |
| 308 | + ].reduce((x, y) => { return x.concat(y);}); |
| 309 | + |
| 310 | + const batched = paraApi.tx.utility.batchAll(calls); |
| 311 | + const sudo = paraApi.tx.sudo.sudo(batched.method.toHex()); |
| 312 | + |
| 313 | + console.log(toUrl(sudo, args['parachain-endpoint'])); |
| 314 | + |
| 315 | + await paraApi.disconnect(); |
| 316 | +} |
| 317 | + |
| 318 | + |
0 commit comments