2020-11-24 16:12:38 +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-26 21:03:36 +08:00
|
|
|
import { decimalStr, mweiStr, MAX_UINT256 } from './Converter';
|
2020-11-24 16:12:38 +08:00
|
|
|
import { EVM, getDefaultWeb3 } from './EVM';
|
|
|
|
|
import * as log from './Log';
|
|
|
|
|
|
|
|
|
|
BigNumber.config({
|
|
|
|
|
EXPONENTIAL_AT: 1000,
|
|
|
|
|
DECIMAL_PLACES: 80,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class ProxyContext {
|
|
|
|
|
EVM: EVM;
|
|
|
|
|
Web3: Web3;
|
2020-12-01 11:32:47 +08:00
|
|
|
DODOProxyV2: Contract;
|
2020-11-24 16:12:38 +08:00
|
|
|
DVMFactory: Contract;
|
|
|
|
|
DPPFactory: Contract;
|
2020-12-15 01:07:15 +08:00
|
|
|
CPFactory: Contract;
|
2020-12-01 11:32:47 +08:00
|
|
|
DODOApprove: Contract;
|
2021-01-26 17:53:49 +08:00
|
|
|
DODOApproveProxy: Contract;
|
2020-11-30 11:36:36 +08:00
|
|
|
DODOCalleeHelper: Contract;
|
2020-11-30 12:36:59 +08:00
|
|
|
DODOSellHelper: Contract;
|
2020-11-24 16:12:38 +08:00
|
|
|
|
|
|
|
|
//token
|
|
|
|
|
DODO: Contract;
|
2020-12-31 11:53:54 +08:00
|
|
|
USDC: Contract;
|
2020-11-24 16:12:38 +08:00
|
|
|
USDT: Contract;
|
|
|
|
|
WETH: Contract;
|
|
|
|
|
|
2021-01-08 17:19:23 +08:00
|
|
|
//Functions
|
|
|
|
|
DODOIncentive: Contract;
|
2021-01-13 12:15:55 +08:00
|
|
|
mtFeeRateModel: Contract;
|
2021-01-08 17:19:23 +08:00
|
|
|
|
2020-11-24 16:12:38 +08:00
|
|
|
Deployer: string;
|
|
|
|
|
Maintainer: string;
|
|
|
|
|
SpareAccounts: string[];
|
|
|
|
|
|
|
|
|
|
constructor() { }
|
|
|
|
|
|
2021-03-14 10:02:20 +08:00
|
|
|
async init(weth: string) {
|
2020-11-24 16:12:38 +08:00
|
|
|
this.EVM = new EVM();
|
|
|
|
|
this.Web3 = getDefaultWeb3();
|
2020-11-25 17:08:28 +08:00
|
|
|
const allAccounts = await this.Web3.eth.getAccounts();
|
|
|
|
|
this.Deployer = allAccounts[0];
|
|
|
|
|
this.Maintainer = allAccounts[1];
|
|
|
|
|
this.SpareAccounts = allAccounts.slice(2, 10);
|
|
|
|
|
|
2020-12-01 01:47:22 +08:00
|
|
|
this.WETH = contracts.getContractWithAddress(contracts.WETH_CONTRACT_NAME, weth);
|
2020-11-24 16:12:38 +08:00
|
|
|
|
2021-01-08 17:19:23 +08:00
|
|
|
this.DODO = await contracts.newContract(
|
|
|
|
|
contracts.MINTABLE_ERC20_CONTRACT_NAME,
|
|
|
|
|
["DODO Token", "DODO", 18]
|
|
|
|
|
);
|
|
|
|
|
this.USDT = await contracts.newContract(
|
|
|
|
|
contracts.MINTABLE_ERC20_CONTRACT_NAME,
|
|
|
|
|
["USDT Token", "USDT", 6]
|
|
|
|
|
);
|
|
|
|
|
this.USDC = await contracts.newContract(
|
|
|
|
|
contracts.MINTABLE_ERC20_CONTRACT_NAME,
|
|
|
|
|
["USDC Token", "USDC", 6]
|
|
|
|
|
);
|
|
|
|
|
|
2020-11-24 16:12:38 +08:00
|
|
|
var cloneFactory = await contracts.newContract(
|
|
|
|
|
contracts.CLONE_FACTORY_CONTRACT_NAME
|
|
|
|
|
);
|
|
|
|
|
var dvmTemplate = await contracts.newContract(contracts.DVM_NAME)
|
|
|
|
|
var dppTemplate = await contracts.newContract(contracts.DPP_NAME)
|
2020-12-15 01:07:15 +08:00
|
|
|
var cpTemplate = await contracts.newContract(contracts.CROWD_POOLING_NAME)
|
2020-11-24 17:25:10 +08:00
|
|
|
var dppAdminTemplate = await contracts.newContract(contracts.DPP_ADMIN_NAME)
|
2020-11-24 16:12:38 +08:00
|
|
|
var permissionManagerTemplate = await contracts.newContract(contracts.PERMISSION_MANAGER_NAME)
|
2020-12-31 11:53:54 +08:00
|
|
|
var mtFeeRateModelTemplate = await contracts.newContract(contracts.FEE_RATE_MODEL_NAME)
|
2021-01-13 12:15:55 +08:00
|
|
|
this.mtFeeRateModel = mtFeeRateModelTemplate;
|
2020-11-24 16:12:38 +08:00
|
|
|
|
2021-01-19 17:10:46 +08:00
|
|
|
|
2020-11-24 16:12:38 +08:00
|
|
|
this.DVMFactory = await contracts.newContract(contracts.DVM_FACTORY_NAME,
|
|
|
|
|
[
|
|
|
|
|
cloneFactory.options.address,
|
|
|
|
|
dvmTemplate.options.address,
|
2020-12-15 01:07:15 +08:00
|
|
|
this.Deployer,
|
2020-12-31 11:53:54 +08:00
|
|
|
mtFeeRateModelTemplate.options.address
|
2021-03-14 10:02:20 +08:00
|
|
|
]
|
2020-12-15 01:07:15 +08:00
|
|
|
)
|
|
|
|
|
|
2020-12-01 11:32:47 +08:00
|
|
|
this.DODOApprove = await contracts.newContract(
|
2020-11-25 17:08:28 +08:00
|
|
|
contracts.SMART_APPROVE
|
|
|
|
|
);
|
|
|
|
|
|
2021-01-26 17:53:49 +08:00
|
|
|
this.DODOApproveProxy = await contracts.newContract(
|
|
|
|
|
contracts.SMART_APPROVE_PROXY,
|
|
|
|
|
[this.DODOApprove.options.address]
|
|
|
|
|
)
|
|
|
|
|
|
2021-03-14 10:02:20 +08:00
|
|
|
//DODO Incentive (ETH)
|
2021-01-08 17:19:23 +08:00
|
|
|
this.DODOIncentive = await contracts.newContract(
|
|
|
|
|
contracts.DODO_INCENTIVE,
|
|
|
|
|
[this.DODO.options.address]
|
|
|
|
|
)
|
2020-12-15 01:07:15 +08:00
|
|
|
|
2020-11-24 16:12:38 +08:00
|
|
|
this.DPPFactory = await contracts.newContract(contracts.DPP_FACTORY_NAME,
|
|
|
|
|
[
|
|
|
|
|
cloneFactory.options.address,
|
|
|
|
|
dppTemplate.options.address,
|
2020-11-24 17:25:10 +08:00
|
|
|
dppAdminTemplate.options.address,
|
2020-12-31 11:53:54 +08:00
|
|
|
this.Deployer,
|
|
|
|
|
mtFeeRateModelTemplate.options.address,
|
2021-01-26 17:53:49 +08:00
|
|
|
this.DODOApproveProxy.options.address
|
2020-11-24 16:12:38 +08:00
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-15 01:07:15 +08:00
|
|
|
this.CPFactory = await contracts.newContract(contracts.CROWD_POOLING_FACTORY,
|
|
|
|
|
[
|
|
|
|
|
cloneFactory.options.address,
|
|
|
|
|
cpTemplate.options.address,
|
2020-12-24 11:51:55 +08:00
|
|
|
this.DVMFactory.options.address,
|
2020-12-15 01:07:15 +08:00
|
|
|
this.Deployer,
|
2020-12-31 11:53:54 +08:00
|
|
|
mtFeeRateModelTemplate.options.address,
|
|
|
|
|
permissionManagerTemplate.options.address
|
2021-03-14 10:02:20 +08:00
|
|
|
]
|
2020-12-15 01:07:15 +08:00
|
|
|
)
|
|
|
|
|
|
2020-11-30 12:36:59 +08:00
|
|
|
this.DODOSellHelper = await contracts.newContract(
|
|
|
|
|
contracts.DODO_SELL_HELPER
|
|
|
|
|
);
|
|
|
|
|
|
2021-03-14 10:02:20 +08:00
|
|
|
//ETH proxy
|
2020-12-01 11:32:47 +08:00
|
|
|
this.DODOProxyV2 = await contracts.newContract(contracts.DODO_PROXY_NAME,
|
2020-11-24 16:12:38 +08:00
|
|
|
[
|
2020-11-25 17:08:28 +08:00
|
|
|
this.DVMFactory.options.address,
|
|
|
|
|
this.DPPFactory.options.address,
|
2020-12-15 01:07:15 +08:00
|
|
|
this.CPFactory.options.address,
|
2020-11-25 22:12:51 +08:00
|
|
|
this.WETH.options.address,
|
2021-01-26 17:53:49 +08:00
|
|
|
this.DODOApproveProxy.options.address,
|
2021-01-08 17:19:23 +08:00
|
|
|
this.DODOSellHelper.options.address,
|
|
|
|
|
"0x0000000000000000000000000000000000000000",
|
|
|
|
|
this.DODOIncentive.options.address
|
2020-11-24 16:12:38 +08:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
|
2020-12-15 01:07:15 +08:00
|
|
|
await this.DODOProxyV2.methods.initOwner(this.Deployer).send(this.sendParam(this.Deployer));
|
2021-01-26 17:53:49 +08:00
|
|
|
|
2021-03-14 10:02:20 +08:00
|
|
|
|
|
|
|
|
await this.DODOApprove.methods.init(this.Deployer, this.DODOApproveProxy.options.address).send(this.sendParam(this.Deployer));
|
2021-03-15 20:10:38 +08:00
|
|
|
await this.DODOApproveProxy.methods.init(this.Deployer, [this.DODOProxyV2.options.address]).send(this.sendParam(this.Deployer));
|
2021-03-14 10:02:20 +08:00
|
|
|
|
|
|
|
|
//DODOIncentive ETH
|
2021-01-08 17:19:23 +08:00
|
|
|
await this.DODOIncentive.methods.initOwner(this.Deployer).send(this.sendParam(this.Deployer));
|
|
|
|
|
await this.DODOIncentive.methods.changeDODOProxy(this.DODOProxyV2.options.address).send(this.sendParam(this.Deployer));
|
2020-11-24 16:12:38 +08:00
|
|
|
|
2020-11-30 11:36:36 +08:00
|
|
|
this.DODOCalleeHelper = await contracts.newContract(
|
|
|
|
|
contracts.DODO_CALLEE_HELPER_NAME,
|
|
|
|
|
[this.WETH.options.address]
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-08 16:16:11 +08:00
|
|
|
console.log(log.blueText("[Init ProxyV2 context]"));
|
2020-11-24 16:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendParam(sender, value = "0") {
|
|
|
|
|
return {
|
|
|
|
|
from: sender,
|
|
|
|
|
gas: process.env["COVERAGE"] ? 10000000000 : 7000000,
|
2020-11-26 21:03:36 +08:00
|
|
|
gasPrice: mweiStr("1000"),
|
2020-11-24 16:12:38 +08:00
|
|
|
value: decimalStr(value),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async mintTestToken(to: string, token: Contract, amount: string) {
|
|
|
|
|
await token.methods.mint(to, amount).send(this.sendParam(this.Deployer));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async approveProxy(account: string) {
|
|
|
|
|
await this.DODO.methods
|
2020-12-01 11:32:47 +08:00
|
|
|
.approve(this.DODOApprove.options.address, MAX_UINT256)
|
2020-11-24 16:12:38 +08:00
|
|
|
.send(this.sendParam(account));
|
|
|
|
|
await this.USDT.methods
|
2020-12-01 11:32:47 +08:00
|
|
|
.approve(this.DODOApprove.options.address, MAX_UINT256)
|
2020-11-24 16:12:38 +08:00
|
|
|
.send(this.sendParam(account));
|
2020-12-31 11:53:54 +08:00
|
|
|
await this.USDC.methods
|
|
|
|
|
.approve(this.DODOApprove.options.address, MAX_UINT256)
|
|
|
|
|
.send(this.sendParam(account));
|
2020-11-24 16:12:38 +08:00
|
|
|
await this.WETH.methods
|
2020-12-01 11:32:47 +08:00
|
|
|
.approve(this.DODOApprove.options.address, MAX_UINT256)
|
2020-11-24 16:12:38 +08:00
|
|
|
.send(this.sendParam(account));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-14 10:02:20 +08:00
|
|
|
export async function getProxyContext(weth: string): Promise<ProxyContext> {
|
2020-11-24 16:12:38 +08:00
|
|
|
var context = new ProxyContext();
|
2020-12-01 01:47:22 +08:00
|
|
|
await context.init(weth);
|
2020-11-24 16:12:38 +08:00
|
|
|
return context;
|
|
|
|
|
}
|