"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const token_math_1 = require("@melonproject/token-math");
const initTestEnvironment_1 = require("../../../../tests/utils/initTestEnvironment");
const balanceOf_1 = require("../calls/balanceOf");
const getToken_1 = require("../calls/getToken");
const deploy_1 = require("../transactions/deploy");
const transfer_1 = require("../transactions/transfer");
const operators_1 = require("rxjs/operators");
describe('balanceOf', () => {
    const shared = {};
    beforeAll(() => __awaiter(this, void 0, void 0, function* () {
        shared.env = yield initTestEnvironment_1.initTestEnvironment();
        shared.address = yield deploy_1.deployToken(shared.env);
        shared.token = yield getToken_1.getToken(shared.env, shared.address);
        shared.accounts = (yield shared.env.eth.getAccounts()).map(account => new token_math_1.Address(account));
    }));
    it('balanceOf', () => __awaiter(this, void 0, void 0, function* () {
        const balance = yield balanceOf_1.balanceOf(shared.env, shared.address, {
            address: shared.accounts[0],
        });
        const expected = token_math_1.createQuantity(shared.token, '1000000000000000000000000');
        expect(token_math_1.isEqual(balance, expected)).toBe(true);
    }));
    it('balanceOf.observable', () => __awaiter(this, void 0, void 0, function* () {
        let counter = 0;
        const observable = balanceOf_1.balanceOf.observable(shared.env, shared.address, {
            address: shared.accounts[1],
        });
        observable.subscribe((balance) => {
            counter += 1;
            if (counter === 2) {
                const expected = token_math_1.createQuantity(shared.token, '2000000000000000000');
                expect(token_math_1.isEqual(balance, expected)).toBe(true);
            }
        });
        yield transfer_1.transfer(shared.env, {
            howMuch: token_math_1.createQuantity(shared.token, '1000000000000000000'),
            to: shared.accounts[1],
        });
        yield transfer_1.transfer(shared.env, {
            howMuch: token_math_1.createQuantity(shared.token, '1000000000000000000'),
            to: shared.accounts[1],
        });
        yield transfer_1.transfer(shared.env, {
            howMuch: token_math_1.createQuantity(shared.token, '1000000000000000000'),
            to: shared.accounts[2],
        });
        yield operators_1.delay(2000);
        expect(counter).toBe(2);
    }));
});
