Files

145 lines
4.2 KiB
TypeScript
Raw Permalink Normal View History

2020-06-26 00:31:25 +08:00
/*
Copyright 2020 DODO ZOO.
SPDX-License-Identifier: Apache-2.0
*/
import BigNumber from 'bignumber.js';
import Web3 from 'web3';
import { Contract } from 'web3-eth-contract';
import * as contracts from './Contracts';
2020-11-05 14:10:59 +08:00
import { decimalStr, MAX_UINT256 } from './Converter';
import { EVM, getDefaultWeb3 } from './EVM';
import * as log from './Log';
2020-06-26 00:31:25 +08:00
BigNumber.config({
EXPONENTIAL_AT: 1000,
DECIMAL_PLACES: 80,
});
2020-10-23 17:11:50 +08:00
export interface DVMContextInitConfig {
lpFeeRate: string;
mtFeeRate: string;
k: string;
2020-10-23 17:11:50 +08:00
i: string;
2020-06-26 00:31:25 +08:00
}
/*
price curve when k=0.1
+++
| purchase percentage | avg slippage |
+++
| 1% | 0.1% |
| 5% | 0.5% |
| 10% | 1.1% |
| 20% | 2.5% |
| 50% | 10% |
| 70% | 23.3% |
+++
*/
2020-10-24 12:44:51 +08:00
export let DefaultDVMContextInitConfig = {
2020-06-26 00:31:25 +08:00
lpFeeRate: decimalStr("0.002"),
mtFeeRate: decimalStr("0.001"),
k: decimalStr("0.1"),
2020-10-23 17:11:50 +08:00
i: decimalStr("100"),
};
2020-06-26 00:31:25 +08:00
2020-10-24 12:44:51 +08:00
export class DVMContext {
EVM: EVM;
Web3: Web3;
2020-10-24 12:44:51 +08:00
DVM: Contract;
BASE: Contract;
QUOTE: Contract;
Deployer: string;
Maintainer: string;
2021-01-07 14:31:15 +08:00
MtFeeRate: string;
2020-10-24 12:44:51 +08:00
SpareAccounts: string[];
2021-01-12 18:12:42 +08:00
mtFeeRateModel: Contract;
2020-10-23 17:11:50 +08:00
constructor() { }
2020-06-26 00:31:25 +08:00
2020-10-24 12:44:51 +08:00
async init(config: DVMContextInitConfig) {
this.EVM = new EVM();
this.Web3 = getDefaultWeb3();
2020-10-24 12:44:51 +08:00
2020-11-25 17:16:49 +08:00
this.DVM = await contracts.newContract(contracts.DVM_NAME)
2021-01-12 18:12:42 +08:00
var lpFeeRateModel = await contracts.newContract(contracts.FEE_RATE_MODEL_NAME)
var mtFeeRateModel = await contracts.newContract(contracts.FEE_RATE_MODEL_NAME)
this.mtFeeRateModel = mtFeeRateModel;
2021-01-07 14:31:15 +08:00
this.MtFeeRate = mtFeeRateModel.options.address
2020-11-25 17:16:49 +08:00
var permissionManager = await contracts.newContract(contracts.PERMISSION_MANAGER_NAME)
2020-11-20 18:58:35 +08:00
var gasPriceSource = await contracts.newContract(contracts.EXTERNAL_VALUE_NAME)
2020-10-24 12:44:51 +08:00
this.BASE = await contracts.newContract(
2020-10-26 19:19:10 +08:00
contracts.MINTABLE_ERC20_CONTRACT_NAME,
["TestBase", "BASE", 18]
);
this.QUOTE = await contracts.newContract(
2020-10-26 19:19:10 +08:00
contracts.MINTABLE_ERC20_CONTRACT_NAME,
["TestQuote", "QUOTE", 18]
);
2020-06-26 00:31:25 +08:00
const allAccounts = await this.Web3.eth.getAccounts();
this.Deployer = allAccounts[0];
2020-10-24 12:44:51 +08:00
this.Maintainer = allAccounts[1];
this.SpareAccounts = allAccounts.slice(2, 10);
2020-11-25 17:16:49 +08:00
await this.DVM.methods.init(
2021-01-07 12:49:31 +08:00
// this.Deployer,
2020-11-25 17:16:49 +08:00
this.Maintainer,
2020-10-24 12:44:51 +08:00
this.BASE.options.address,
this.QUOTE.options.address,
2021-01-07 14:31:15 +08:00
// lpFeeRateModel.options.address,
0,
2020-11-25 17:16:49 +08:00
mtFeeRateModel.options.address,
2021-01-07 12:49:31 +08:00
// permissionManager.options.address,
// gasPriceSource.options.address,
2020-10-24 12:44:51 +08:00
config.i,
2021-01-21 11:00:43 +08:00
config.k,
true
2020-11-05 14:10:59 +08:00
).send(this.sendParam(this.Deployer))
2020-10-24 12:44:51 +08:00
2020-11-20 18:58:35 +08:00
await gasPriceSource.methods.initOwner(this.Deployer).send(this.sendParam(this.Deployer))
await gasPriceSource.methods.set(MAX_UINT256).send(this.sendParam(this.Deployer))
2020-11-05 14:10:59 +08:00
2021-01-12 18:12:42 +08:00
2020-10-24 12:44:51 +08:00
console.log(log.blueText("[Init DVM context]"));
2020-06-26 00:31:25 +08:00
}
sendParam(sender, value = "0") {
return {
from: sender,
gas: process.env["COVERAGE"] ? 10000000000 : 7000000,
gasPrice: process.env.GAS_PRICE,
value: decimalStr(value),
};
2020-06-26 00:31:25 +08:00
}
async mintTestToken(to: string, base: string, quote: string) {
await this.BASE.methods.mint(to, base).send(this.sendParam(this.Deployer));
await this.QUOTE.methods
.mint(to, quote)
.send(this.sendParam(this.Deployer));
2020-06-26 00:31:25 +08:00
}
2020-11-25 17:16:49 +08:00
async transferBaseToDVM(account: string, amount: string) {
await this.BASE.methods.transfer(this.DVM.options.address, amount).send(this.sendParam(account))
}
async transferQuoteToDVM(account: string, amount: string) {
await this.QUOTE.methods.transfer(this.DVM.options.address, amount).send(this.sendParam(account))
2020-06-26 00:31:25 +08:00
}
}
2020-10-26 19:19:10 +08:00
export async function getDVMContext(
2020-10-24 12:44:51 +08:00
config: DVMContextInitConfig = DefaultDVMContextInitConfig
): Promise<DVMContext> {
var context = new DVMContext();
await context.init(config);
return context;
}