"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const api_1 = require("@polkadot/api");
/**
 * This script allows to verify that Vaults are replicated on another networks.
 * This script was orginially used to verify the correctness of the Kintsugi Vault v6
 * migration bug.
 */
function main() {
    return __awaiter(this, void 0, void 0, function* () {
        // Select any two networks to verify against
        const wsProvider = new api_1.WsProvider('wss://api-kusama.interlay.io/parachain');
        const testWsProvider = new api_1.WsProvider('wss://api-kusama.interlay.io/parachain');
        const api = yield api_1.ApiPromise.create({ provider: wsProvider });
        const testApi = yield api_1.ApiPromise.create({ provider: testWsProvider });
        // Select the block hash at which to compare the vaults
        const preUpgradeApi = yield api.at("0x3a4293ca2b4d24ea852b9510059cb8769e8321b3051bb690aef2703ebc74a65e");
        const allVaults = yield preUpgradeApi.query.vaultRegistry.vaults.entries();
        const postStorageSetVaults = yield testApi.query.vaultRegistry.vaults.entries();
        // Sanity check total issuance
        const expectedIssuedTokens = parseInt((yield preUpgradeApi.query.tokens.totalIssuance({ Token: "KBTC" })).toString());
        let actualIssuedTokens = 0;
        // Look up all Vaults from test chain and compare the vault objects
        allVaults.map(([key, vault]) => {
            let vaultJSON = vault.toJSON();
            delete vaultJSON.wallet;
            if (vaultJSON.status.committedTheft === null) {
                vaultJSON.status = { liquidated: null };
            }
            let [_, updatedVault] = postStorageSetVaults.find(([updatedKey, _]) => {
                return updatedKey.toHex() === key.toHex();
            });
            const updatedVaultJSON = updatedVault.toJSON();
            if (JSON.stringify(vaultJSON) !== JSON.stringify(updatedVaultJSON)) {
                throw new Error(`Error. Expected ${vaultJSON} got ${updatedVaultJSON}`);
            }
            else {
                console.log(`Vault ${JSON.stringify(vaultJSON.id)} updated correctly.`);
                actualIssuedTokens += parseInt(vaultJSON.issuedTokens);
            }
            ;
        });
        // Sanity check total issuance
        if (actualIssuedTokens !== expectedIssuedTokens) {
            throw new Error(`Expected ${expectedIssuedTokens} got ${actualIssuedTokens}`);
        }
        ;
        yield api.disconnect();
        yield testApi.disconnect();
    });
}
main();
//# sourceMappingURL=verify-vaults.js.map