UNPKG

@melonproject/protocol

Version:

Technology Regulated and Operated Investment Funds

252 lines (251 loc) 17.5 kB
"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 initTestEnvironment_1 = require("../utils/initTestEnvironment"); const token_math_1 = require("@melonproject/token-math"); const updateTestingPriceFeed_1 = require("../utils/updateTestingPriceFeed"); const getAllBalances_1 = require("../utils/getAllBalances"); const beginSetup_1 = require("../../contracts/factory/transactions/beginSetup"); const getToken_1 = require("../../contracts/dependencies/token/calls/getToken"); const completeSetup_1 = require("../../contracts/factory/transactions/completeSetup"); const createAccounting_1 = require("../../contracts/factory/transactions/createAccounting"); const createFeeManager_1 = require("../../contracts/factory/transactions/createFeeManager"); const createParticipation_1 = require("../../contracts/factory/transactions/createParticipation"); const createPolicyManager_1 = require("../../contracts/factory/transactions/createPolicyManager"); const createShares_1 = require("../../contracts/factory/transactions/createShares"); const createTrading_1 = require("../../contracts/factory/transactions/createTrading"); const createVault_1 = require("../../contracts/factory/transactions/createVault"); const getFundComponents_1 = require("../../utils/getFundComponents"); const withDifferentAccount_1 = require("../../utils/environment/withDifferentAccount"); const deployAndGetSystem_1 = require("../utils/deployAndGetSystem"); const Contracts_1 = require("../../Contracts"); const orderSignatures_1 = require("../../utils/constants/orderSignatures"); const randomHexOfSize_1 = require("../../utils/helpers/randomHexOfSize"); const addTokenPairWhitelist_1 = require("../../contracts/exchanges/transactions/addTokenPairWhitelist"); const performCalculations_1 = require("../../contracts/fund/accounting/calls/performCalculations"); const precisionUnits = token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(18)); let s = {}; beforeAll(() => __awaiter(this, void 0, void 0, function* () { s.environment = yield initTestEnvironment_1.initTestEnvironment(); s.accounts = yield s.environment.eth.getAccounts(); const { addresses, contracts } = yield deployAndGetSystem_1.deployAndGetSystem(s.environment); s.addresses = addresses; s = Object.assign(s, contracts); [s.deployer, s.manager, s.investor] = s.accounts; s.gas = 8000000; s.mlnTokenInterface = yield getToken_1.getToken(s.environment, s.mln.options.address); s.dgxTokenInterface = yield getToken_1.getToken(s.environment, s.dgx.options.address); s.wethTokenInterface = yield getToken_1.getToken(s.environment, s.weth.options.address); const exchangeConfigs = { [Contracts_1.Exchanges.MatchingMarket]: { adapter: s.matchingMarketAdapter.options.address, exchange: s.matchingMarket.options.address, takesCustody: true, }, }; const envManager = withDifferentAccount_1.withDifferentAccount(s.environment, s.manager); yield updateTestingPriceFeed_1.updateTestingPriceFeed(s, s.environment); yield beginSetup_1.beginSetup(envManager, s.version.options.address, { defaultTokens: [ s.wethTokenInterface, s.mlnTokenInterface, s.dgxTokenInterface, ], exchangeConfigs, fees: [], fundName: 'Test fund', quoteToken: s.dgxTokenInterface, }); yield createAccounting_1.createAccounting(envManager, s.version.options.address); yield createFeeManager_1.createFeeManager(envManager, s.version.options.address); yield createParticipation_1.createParticipation(envManager, s.version.options.address); yield createPolicyManager_1.createPolicyManager(envManager, s.version.options.address); yield createShares_1.createShares(envManager, s.version.options.address); yield createTrading_1.createTrading(envManager, s.version.options.address); yield createVault_1.createVault(envManager, s.version.options.address); const hubAddress = yield completeSetup_1.completeSetup(envManager, s.version.options.address); s.fund = yield getFundComponents_1.getFundComponents(envManager, hubAddress); yield addTokenPairWhitelist_1.addTokenPairWhitelist(s.environment, s.matchingMarket.options.address, { baseToken: s.mlnTokenInterface, quoteToken: s.dgxTokenInterface, }); yield updateTestingPriceFeed_1.updateTestingPriceFeed(s, s.environment); })); test('Transfer ethToken and mlnToken to the investor', () => __awaiter(this, void 0, void 0, function* () { const initialTokenAmount = token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(21)); const pre = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); yield s.weth.methods .transfer(s.investor, `${initialTokenAmount}`) .send({ from: s.deployer }); yield s.mln.methods .transfer(s.investor, `${initialTokenAmount}`) .send({ from: s.deployer }); const post = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); expect(post.investor.weth).toEqual(token_math_1.add(pre.investor.weth, initialTokenAmount)); expect(post.investor.mln).toEqual(token_math_1.add(pre.investor.mln, initialTokenAmount)); })); test(`fund gets non fund denomination asset from investment`, () => __awaiter(this, void 0, void 0, function* () { const wantedShares = token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(20)); const pre = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const preTotalSupply = yield s.fund.shares.methods.totalSupply().call(); yield s.weth.methods .approve(s.fund.participation.options.address, wantedShares) .send({ from: s.investor, gas: s.gas }); yield s.fund.participation.methods .requestInvestment(`${wantedShares}`, `${wantedShares}`, s.weth.options.address) .send({ from: s.investor, gas: s.gas, value: '10000000000000000' }); const fundDenominationAsset = yield s.fund.accounting.methods .DENOMINATION_ASSET() .call(); const [dgxPriceInWeth] = Object.values(yield s.priceSource.methods .getReferencePriceInfo(fundDenominationAsset, s.weth.options.address) .call()).map(e => new token_math_1.BigInteger(e)); const expectedCostOfShares = token_math_1.divide(token_math_1.multiply(wantedShares, dgxPriceInWeth), precisionUnits); const actualCostOfShares = new token_math_1.BigInteger(yield s.fund.accounting.methods .getShareCostInAsset(`${wantedShares}`, s.weth.options.address) .call()); yield updateTestingPriceFeed_1.updateTestingPriceFeed(s, s.environment); yield updateTestingPriceFeed_1.updateTestingPriceFeed(s, s.environment); yield s.fund.participation.methods .executeRequestFor(s.investor) .send({ from: s.investor, gas: s.gas }); const post = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const postTotalSupply = yield s.fund.shares.methods.totalSupply().call(); const postFundGav = new token_math_1.BigInteger(yield s.fund.accounting.methods.calcGav().call()); const [wethPriceInDgx] = Object.values(yield s.priceSource.methods .getReferencePriceInfo(s.weth.options.address, fundDenominationAsset) .call()).map(e => new token_math_1.BigInteger(e)); expect(fundDenominationAsset).toEqual(s.dgx.options.address); expect(postTotalSupply).toEqual(token_math_1.add(token_math_1.toBI(preTotalSupply), wantedShares)); expect(expectedCostOfShares).toEqual(actualCostOfShares); expect(post.investor.weth).toEqual(token_math_1.subtract(pre.investor.weth, expectedCostOfShares)); expect(post.fund.weth).toEqual(token_math_1.add(pre.fund.weth, expectedCostOfShares)); expect(postFundGav).toEqual(token_math_1.add(pre.fund.weth, token_math_1.divide(token_math_1.multiply(expectedCostOfShares, wethPriceInDgx), precisionUnits))); })); test(`investor redeems his shares`, () => __awaiter(this, void 0, void 0, function* () { const pre = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const investorShares = yield s.fund.shares.methods .balanceOf(s.investor) .call(); const preTotalSupply = yield s.fund.shares.methods.totalSupply().call(); yield s.fund.participation.methods .redeem() .send({ from: s.investor, gas: s.gas }); const postFundGav = new token_math_1.BigInteger(yield s.fund.accounting.methods.calcGav().call()); const post = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const postTotalSupply = yield s.fund.shares.methods.totalSupply().call(); expect(postTotalSupply).toEqual(token_math_1.subtract(token_math_1.toBI(preTotalSupply), token_math_1.toBI(investorShares))); expect(post.investor.weth).toEqual(token_math_1.add(pre.investor.weth, pre.fund.weth)); expect(post.fund.weth).toEqual(new token_math_1.BigInteger(0)); expect(postFundGav).toEqual(new token_math_1.BigInteger(0)); })); test(`fund gets non pricefeed quote asset from investment`, () => __awaiter(this, void 0, void 0, function* () { const wantedShares = token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(18)); const offeredValue = token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(21)); const pre = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const preFundGav = new token_math_1.BigInteger(yield s.fund.accounting.methods.calcGav().call()); const preTotalSupply = yield s.fund.shares.methods.totalSupply().call(); yield s.mln.methods .approve(s.fund.participation.options.address, `${offeredValue}`) .send({ from: s.investor, gas: s.gas }); yield s.fund.participation.methods .requestInvestment(`${wantedShares}`, `${offeredValue}`, s.mln.options.address) .send({ from: s.investor, gas: s.gas, value: '10000000000000000' }); const fundDenominationAsset = yield s.fund.accounting.methods .DENOMINATION_ASSET() .call(); const [dgxPriceInMln] = Object.values(yield s.priceSource.methods .getReferencePriceInfo(fundDenominationAsset, s.mln.options.address) .call()).map(e => new token_math_1.BigInteger(e)); const expectedCostOfShares = token_math_1.divide(token_math_1.multiply(wantedShares, dgxPriceInMln), precisionUnits); const actualCostOfShares = new token_math_1.BigInteger(yield s.fund.accounting.methods .getShareCostInAsset(`${wantedShares}`, s.mln.options.address) .call()); yield updateTestingPriceFeed_1.updateTestingPriceFeed(s, s.environment); yield updateTestingPriceFeed_1.updateTestingPriceFeed(s, s.environment); yield s.fund.participation.methods .executeRequestFor(s.investor) .send({ from: s.investor, gas: s.gas }); const post = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const postTotalSupply = yield s.fund.shares.methods.totalSupply().call(); const postFundGav = new token_math_1.BigInteger(yield s.fund.accounting.methods.calcGav().call()); const [mlnPriceInDgx] = Object.values(yield s.priceSource.methods .getReferencePriceInfo(s.mln.options.address, fundDenominationAsset) .call()).map(e => new token_math_1.BigInteger(e)); expect(fundDenominationAsset).toEqual(s.dgx.options.address); expect(postTotalSupply).toEqual(token_math_1.add(token_math_1.toBI(preTotalSupply), wantedShares)); expect(expectedCostOfShares).toEqual(actualCostOfShares); // TODO: Fix this // expect(post.investor.mln).toEqual( // subtract(pre.investor.mln, expectedCostOfShares), // ); expect(post.fund.mln).toEqual(token_math_1.add(pre.fund.mln, expectedCostOfShares)); expect(postFundGav).toEqual(token_math_1.add(preFundGav, token_math_1.divide(token_math_1.multiply(expectedCostOfShares, mlnPriceInDgx), precisionUnits))); })); test(`Fund make order with a non-18 decimal asset`, () => __awaiter(this, void 0, void 0, function* () { s.trade1 = {}; s.trade1.sellQuantity = token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(8)); yield s.dgx.methods .transfer(s.fund.vault.options.address, `${s.trade1.sellQuantity}`) .send({ from: s.deployer }); const [dgxPriceInMln] = Object.values(yield s.priceSource.methods .getReferencePriceInfo(s.dgx.options.address, s.mln.options.address) .call()).map(e => new token_math_1.BigInteger(e)); s.trade1.buyQuantity = token_math_1.divide(token_math_1.multiply(token_math_1.toBI(s.trade1.sellQuantity), dgxPriceInMln), token_math_1.power(new token_math_1.BigInteger(10), new token_math_1.BigInteger(9))); const pre = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const preFundCalculations = yield performCalculations_1.performCalculations(s.environment, s.fund.accounting.options.address); const exchangePreDgx = new token_math_1.BigInteger(yield s.dgx.methods.balanceOf(s.matchingMarket.options.address).call()); const exchangePreMln = new token_math_1.BigInteger(yield s.mln.methods.balanceOf(s.matchingMarket.options.address).call()); yield s.fund.trading.methods .callOnExchange(0, orderSignatures_1.makeOrderSignature, [ randomHexOfSize_1.randomHexOfSize(20), randomHexOfSize_1.randomHexOfSize(20), s.dgx.options.address, s.mln.options.address, randomHexOfSize_1.randomHexOfSize(20), randomHexOfSize_1.randomHexOfSize(20), ], [`${s.trade1.sellQuantity}`, `${s.trade1.buyQuantity}`, 0, 0, 0, 0, 0, 0], randomHexOfSize_1.randomHexOfSize(20), '0x0', '0x0', '0x0') .send({ from: s.manager, gas: s.gas }); const exchangePostDgx = new token_math_1.BigInteger(yield s.dgx.methods.balanceOf(s.matchingMarket.options.address).call()); const exchangePostMln = new token_math_1.BigInteger(yield s.mln.methods.balanceOf(s.matchingMarket.options.address).call()); const post = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const postFundCalculations = yield performCalculations_1.performCalculations(s.environment, s.fund.accounting.options.address); expect(exchangePostMln).toEqual(exchangePreMln); expect(exchangePostDgx).toEqual(token_math_1.add(exchangePreDgx, token_math_1.toBI(s.trade1.sellQuantity))); expect(post.fund.dgx).toEqual(pre.fund.dgx); expect(post.fund.mln).toEqual(pre.fund.mln); expect(postFundCalculations.gav).toEqual(preFundCalculations.gav); expect(postFundCalculations.sharePrice).toEqual(preFundCalculations.sharePrice); expect(post.deployer.mln).toEqual(pre.deployer.mln); })); test(`Third party takes entire order`, () => __awaiter(this, void 0, void 0, function* () { const orderId = yield s.matchingMarket.methods.last_offer_id().call(); const pre = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); const exchangePreMln = new token_math_1.BigInteger(yield s.mln.methods.balanceOf(s.matchingMarket.options.address).call()); const exchangePreDgx = new token_math_1.BigInteger(yield s.dgx.methods.balanceOf(s.matchingMarket.options.address).call()); yield s.mln.methods .approve(s.matchingMarket.options.address, `${s.trade1.buyQuantity}`) .send({ from: s.deployer, gasPrice: 8000000 }); yield s.matchingMarket.methods .buy(orderId, `${s.trade1.sellQuantity}`) .send({ from: s.deployer, gas: s.gas }); yield s.fund.trading.methods .returnBatchToVault([s.mln.options.address, s.weth.options.address]) .send({ from: s.manager, gas: s.gas }); const exchangePostMln = new token_math_1.BigInteger(yield s.mln.methods.balanceOf(s.matchingMarket.options.address).call()); const exchangePostDgx = new token_math_1.BigInteger(yield s.weth.methods.balanceOf(s.matchingMarket.options.address).call()); const post = yield getAllBalances_1.getAllBalances(s, s.accounts, s.fund, s.environment); expect(exchangePostMln).toEqual(exchangePreMln); expect(exchangePostDgx).toEqual(token_math_1.subtract(exchangePreDgx, token_math_1.toBI(s.trade1.sellQuantity))); expect(post.fund.dgx).toEqual(token_math_1.subtract(pre.fund.dgx, token_math_1.toBI(s.trade1.sellQuantity))); expect(post.fund.mln).toEqual(token_math_1.add(pre.fund.mln, token_math_1.toBI(s.trade1.buyQuantity))); expect(post.deployer.dgx).toEqual(token_math_1.add(pre.deployer.dgx, token_math_1.toBI(s.trade1.sellQuantity))); expect(post.deployer.mln).toEqual(token_math_1.subtract(pre.deployer.mln, token_math_1.toBI(s.trade1.buyQuantity))); }));