swapable
Version:
Swapable: Automated Liquidity Pools
161 lines (160 loc) • 8.71 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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 });
/**
* This file is part of Swapable shared under AGPL-3.0
* Copyright (C) 2021 Using Blockchain Ltd, Reg No.: 12658136, United Kingdom
*
* @package Swapable
* @author Grégory Saive for Using Blockchain Ltd <greg@ubc.digital>
* @license AGPL-3.0
*/
const sinon = __importStar(require("sinon"));
const chai_1 = require("chai");
const mocha_1 = require("mocha");
const rxjs_1 = require("rxjs");
const symbol_hd_wallets_1 = require("symbol-hd-wallets");
// internal dependencies
const index_1 = require("../../index");
const index_2 = require("../mocks/index");
// prepare
const seedHash = 'ACECD90E7B248E012803228ADB4424F0D966D24149B72E58987D2BF2F2AF03C4';
const market = index_2.getTestMarket();
const target = index_2.getTestAccount('target');
const defaultPoolTarget = 'TCS5GSPGMCTGTI46SSZIBZMRVTLM4BDQ7MRXAYI'; // m/44'/4343'/0'/0'/0'
mocha_1.describe('contracts/DigitalMarket --->', () => {
mocha_1.describe('constructor() should', () => {
mocha_1.it('derive correct pool target account', () => {
chai_1.expect(market.target.address.plain()).to.be.equal(defaultPoolTarget);
});
mocha_1.it('use correct asset source', () => {
chai_1.expect(market.source.source).to.be.equal(seedHash);
chai_1.expect(market.source.network).to.be.equal(symbol_hd_wallets_1.Network.CATAPULT_PUBLIC);
});
});
mocha_1.describe('identifier() should', () => {
mocha_1.it('derive correct pool shares identifier', () => {
chai_1.expect(market.identifier.id).to.be.equal('c2aa7f8d');
chai_1.expect(market.identifier.target.address.plain()).to.be.equal(defaultPoolTarget);
});
});
mocha_1.describe('getContext() should', () => {
mocha_1.it('use correct Revision', () => {
const c = market.fakeGetContext(target);
chai_1.expect(c.revision).to.be.equal(index_1.Swapable.Revision);
});
mocha_1.it('use correct Reader implementation', () => {
const c = market.fakeGetContext(target);
chai_1.expect(c.reader).to.be.instanceof(index_1.Symbol.Reader);
chai_1.expect(c.reader.generationHash).to.be.equal(seedHash);
});
mocha_1.it('use correct Signer implementation', () => {
const c = market.fakeGetContext(target);
chai_1.expect(c.signer).to.be.instanceof(index_1.Symbol.Signer);
chai_1.expect(c.signer.keyChain.curve).to.be.equal(symbol_hd_wallets_1.Network.CATAPULT_PUBLIC.curve);
});
mocha_1.it('permit overwrite of transaction parameters', () => {
const c = market.fakeGetContext(target, new index_1.TransactionParameters(1234));
chai_1.expect(c.parameters).to.be.instanceof(index_1.TransactionParameters);
chai_1.expect(c.parameters.epochAjustment).to.be.equal(1234);
});
mocha_1.it('permit overwrite of contract options', () => {
const c = market.fakeGetContext(target, new index_1.TransactionParameters(), [
new index_1.CommandOption('dummy', 'value')
]);
chai_1.expect(c.getInput('dummy', null)).to.not.be.null;
chai_1.expect(c.getInput('dummy', null)).to.be.equal('value');
});
});
mocha_1.describe('getCommand() should', () => {
mocha_1.it('throw an error on unknown automated pool command', () => {
const context = market.fakeGetContext(target);
chai_1.expect(() => {
const command = market.fakeGetCommand('unknown', context);
}).to.throw(index_1.FailureInvalidCommand);
});
mocha_1.it('use correct Context for execution', () => {
const context = market.fakeGetContext(target);
const command = market.fakeGetCommand('CreatePool', context);
chai_1.expect(command.context.actor.publicKey).to.be.equal(target.publicKey);
chai_1.expect(command.context.revision).to.be.equal(index_1.Swapable.Revision);
});
mocha_1.it('use correct automated pool command', () => {
const context = market.fakeGetContext(target);
const command = market.fakeGetCommand('CreatePool', context);
chai_1.expect(command).to.be.instanceof(index_1.BaseCommand);
chai_1.expect(command.name).to.be.equal('CreatePool');
});
});
mocha_1.describe('synchronize() should', () => {
mocha_1.it('exist and execute', () => __awaiter(void 0, void 0, void 0, function* () {
// - Prepare
const mockMarket = sinon.mock(market);
const stubSynchronize = mockMarket.expects('synchronize').once();
// - Act
yield market.synchronize();
// - Assert
chai_1.expect(stubSynchronize.calledOnce).to.be.true;
}));
mocha_1.it('get correct mosaic information', () => __awaiter(void 0, void 0, void 0, function* () {
// - Prepare
const context = market.fakeGetContext(target);
const factory = context.reader.factoryHttp;
const stubMosaics = new index_2.Stubs.MosaicRepository('http://localhost:3000');
const stubRepository = sinon.stub(factory, 'createMosaicRepository').returns(stubMosaics);
const stubMosaicInfo = rxjs_1.of(new index_2.Stubs.MosaicInfo(market.identifier.toMosaicId()));
const repository = factory.createMosaicRepository();
const stubGetMosaic = sinon.stub(repository, 'getMosaic').returns(stubMosaicInfo);
// - Act
const mosaicId = yield repository.getMosaic(market.identifier.toMosaicId()).toPromise();
const expectId = yield stubMosaicInfo.toPromise();
// - Assert
chai_1.expect(stubRepository.calledOnce).to.be.true;
chai_1.expect(stubGetMosaic.calledOnce).to.be.true;
chai_1.expect(mosaicId.id.toHex()).to.be.equal(expectId.id.toHex());
}));
mocha_1.it('get correct account information', () => __awaiter(void 0, void 0, void 0, function* () {
// - Prepare
const context = market.fakeGetContext(target);
const factory = context.reader.factoryHttp;
const stubAccounts = new index_2.Stubs.AccountRepository('http://localhost:3000');
const stubRepository = sinon.stub(factory, 'createAccountRepository').returns(stubAccounts);
const stubAccountInfo = rxjs_1.of(index_2.getTestAccountInfo('target'));
const repository = factory.createAccountRepository();
const stubGetAccountInfo = sinon.stub(repository, 'getAccountInfo').returns(stubAccountInfo);
// - Act
const accountInfo = yield repository.getAccountInfo(target.address).toPromise();
// - Assert
chai_1.expect(stubRepository.calledOnce).to.be.true;
chai_1.expect(stubGetAccountInfo.calledOnce).to.be.true;
chai_1.expect(accountInfo.address.plain()).to.be.equal(target.address.plain());
}));
});
});