12import * as fs from 'fs';3import {usingPlaygrounds} from '../../util';4import path, {dirname} from 'path';5import {isInteger, parse} from 'lossless-json';6import {fileURLToPath} from 'url';7import config from '../../config';8910const WS_ENDPOINT = config.substrateUrl;11const DONOR_SEED = '//Alice';12const UPDATE_IF_VERSION = 942057;1314export function customNumberParser(value: any) {15 return isInteger(value) ? BigInt(value) : parseFloat(value);16}1718export const migrateLockedToFreeze = async(options: { wsEndpoint: string; donorSeed: string } = {19 wsEndpoint: WS_ENDPOINT,20 donorSeed: DONOR_SEED,21}) => {22 await usingPlaygrounds(async (helper, privateKey) => {23 const api = helper.getApi();24 25 console.log((api.consts.system.version as any).specVersion.toNumber());26 if((api.consts.system.version as any).specVersion.toNumber() != UPDATE_IF_VERSION) {27 console.log("Version isn't 942057.");28 return;29 }3031 32 const signer = await privateKey(options.donorSeed);33 console.log('2. Getting sudo:', signer.address);3435 36 console.log('3. Parsing chainql results...');37 const dirName = dirname(fileURLToPath(import.meta.url));38 const parsingResult = parse(fs.readFileSync(path.resolve(dirName, 'output.json'), 'utf-8'), undefined, customNumberParser);3940 const chainqlImportData = parsingResult as {41 address: string;42 balance: string;43 account: {44 fee_frozen: string,45 free: string,46 misc_frozen: string,47 reserved: string,48 },49 locks: {50 amount: string,51 id: string,52 }[],53 stakes: object,54 unstakes: object,55 }[];56 testChainqlData(chainqlImportData);5758 const stakers = chainqlImportData.map((i) => i.address);5960 61 console.log('3.1 Splitting into chunks...');62 const stakersChunks = chunk(stakers, 100);63 console.log('3.1 Done, total chunks:', stakersChunks.length);6465 66 console.log('4. Getting sudo nonce...');67 const signerAccount = (68 await api.query.system.account(signer.address)69 ).toJSON() as any;7071 let nonce: number = signerAccount.nonce;72 console.log('4. Sudo nonce is:', nonce);7374 75 console.log('5. Signing transactions...');76 const signedTxs = [];77 for(const chunk of stakersChunks) {78 const tx = api.tx.sudo.sudo(api.tx.appPromotion.upgradeAccounts(chunk));79 const signed = tx.sign(signer, {80 blockHash: api.genesisHash,81 genesisHash: api.genesisHash,82 runtimeVersion: api.runtimeVersion,83 nonce: nonce++,84 });85 signedTxs.push(signed);86 }8788 89 console.log('6. Sending transactions...');90 const promises = signedTxs.map((tx) => api.rpc.author.submitAndWatchExtrinsic(tx));91 92 console.log('6.1 Waiting all transactions settled...');93 const res = await Promise.allSettled(promises);9495 console.log('Wait 5 blocks for transactions to be included in a block...');96 await helper.wait.newBlocks(5);97 98 console.log('6.2 Getting failed transactions...');99 const failedTx = res.filter((r) => r.status == 'rejected') as PromiseRejectedResult[];100 console.log('6.2. total failedTxs:', failedTx.length);101102 103 for(const tx of failedTx) {104 console.log(tx.reason);105 }106107 108 console.log('7. Check balances...');109 let blocksLeft = 10;110 let notMigrated = stakers;111 const suspiciousAccounts = [];112 do {113 console.log('blocks left:', blocksLeft);114 const _notMigrated: string[] = [];115 console.log('accounts to migrate...', notMigrated.length);116 for(const accountToMigrate of notMigrated) {117 let accountMigrated = true;118 119 const oldAccount = chainqlImportData.find(acc => acc.address === accountToMigrate);120 if(!oldAccount) {121 console.log('Cannot find old account data for', accountMigrated);122 accountMigrated = false;123 _notMigrated.push(accountToMigrate);124 continue;125 }126127 128 const balance = await api.query.system.account(accountToMigrate) as any;129 130 const free = balance.data.free;131 const reserved = balance.data.reserved;132 const frozen = balance.data.frozen;133 134 const oldFree = oldAccount.account.free;135 const oldReserved = oldAccount.account.reserved;136 const oldFrozen = oldAccount.account.fee_frozen;137 138 if(oldFree.toString() !== free.toString()) {139 console.log('Old free !== New free, which is probably not a problem', oldFree.toString(), free.toString());140 suspiciousAccounts.push(accountToMigrate);141 }142 if(oldFrozen.toString() !== frozen.toString()) {143 console.log('Old frozen !== New frozen, which is probably not a problem', oldFrozen.toString(), frozen.toString());144 suspiciousAccounts.push(accountToMigrate);145 }146 if(oldReserved.toString() !== reserved.toString()) {147 console.log('Old reserved !== New reserved, which is probably not a problem', oldReserved.toString(), reserved.toString());148 suspiciousAccounts.push(accountToMigrate);149 }150151 152 const locks = await helper.balance.getLocked(accountToMigrate);153 const appPromoLocks = locks.filter(lock => lock.id === 'appstake');154 if(appPromoLocks.length !== 0) {155 console.log('Account still has app-promo lock');156 accountMigrated = false;157 }158159 160 let freezes = await api.query.balances.freezes(accountToMigrate) as any;161 freezes = freezes.map((freez: any) => ({id: freez.id.toString(), amount: freez.amount.toString()}));162 if(!freezes) {163 console.log('Account does not have freezes');164 accountMigrated = false;165 } else {166 const oldAppPromoLocks = oldAccount.locks.filter(l => l.id === '0x6170707374616b65'); 167 168 if(freezes.length !== 1) {169 console.log('freezes.length !== 1 and old appPromoLocks.length', freezes.length, oldAppPromoLocks.length);170 accountMigrated = false;171 } else {172 const appPromoFreez = freezes[0];173 174 if(appPromoFreez.amount.toString() !== oldAppPromoLocks[0].amount.toString()) {175 console.log('freezes amount !== old appPromoLocks amount', appPromoFreez.amount.toString(), oldAppPromoLocks[0].amount.toString());176 accountMigrated = false;177 }178 179 if(appPromoFreez.id !== '0x6170707374616b656170707374616b65') {180 console.log('Got freez with incorrect id:', appPromoFreez.id);181 accountMigrated = false;182 }183 }184 }185186 187 const stakesNumber = await helper.staking.getStakesNumber({Substrate: accountToMigrate});188 const oldStakesNumber = oldAccount.stakes ? Object.keys(oldAccount.stakes).length : 0;189 if(stakesNumber.toString() !== oldStakesNumber.toString()) {190 console.log('Old stakes number !== New stakes number', oldStakesNumber, stakesNumber);191 accountMigrated = false;192 }193194 195 const pendingUnstakes = await helper.staking.getPendingUnstake({Substrate: accountToMigrate});196 const totalStaked = await helper.staking.getTotalStaked({Substrate: accountToMigrate});197 const totalBalanceInAppPromo = pendingUnstakes + totalStaked;198 if(totalBalanceInAppPromo.toString() !== oldAccount.balance.toString()) {199 console.log('totalBalanceInAppPromo !== old locked in app promo', totalBalanceInAppPromo.toString(), oldAccount.balance.toString());200 accountMigrated = false;201 }202203 204 if(!accountMigrated) {205 console.log('Add to not migrated:', accountToMigrate);206 _notMigrated.push(accountToMigrate);207 }208 }209210 blocksLeft--;211 notMigrated = _notMigrated;212 await helper.wait.newBlocks(1);213 } while(blocksLeft > 0 && notMigrated.length !== 0);214215 console.log('Not migrated accounts...', notMigrated.length);216 if(suspiciousAccounts.length > 0) {217 console.log('Saving suspicious accounts to suspicious.json:');218 fs.writeFileSync('./suspicious.json', JSON.stringify(suspiciousAccounts));219 }220 if(notMigrated.length > 0) {221 console.log('Saving not migrated list to failed.json:');222 notMigrated.forEach(console.log);223 fs.writeFileSync('./failed.json', JSON.stringify(notMigrated));224 process.exit(1);225 } else {226 console.log('Migration success');227 }228 }, options.wsEndpoint);229};230231const chunk = <T>(arr: T[], size: number) =>232 Array.from({length: Math.ceil(arr.length / size)}, (_: any, i: number) =>233 arr.slice(i * size, i * size + size));234235const testChainqlData = (data: any) => {236 const wrongData = [];237 for(const account of data) {238 try {239 if(account.address == null) throw Error('no address in data');240 if(account.balance == null) throw Error('no balance in data');241 if(account.account == null) throw Error('no account in data');242 if(account.account.fee_frozen == null) throw Error('no account.fee_frozen in data');243 if(account.account.misc_frozen == null) throw Error('no account.misc_frozen in data');244 if(account.account.free == null) throw Error('no account.free in data');245 if(account.account.reserved == null) throw Error('no account.reserved in data');246 if(account.locks == null) throw Error('no locks in data');247 if(account.locks[0].amount == null) throw Error('no locks.amount in data');248 if(account.locks[0].id == null) throw Error('no locks.id in data');249 } catch (error) {250 wrongData.push(account.address);251 console.log((error as Error).message, account.address);252 }253 if(wrongData.length > 0) {254 console.log(data);255 throw Error('Chainql data not correct');256 }257 }258 console.log('Chainql data correct');259};