route testing

This commit is contained in:
owen05
2020-11-10 18:37:51 +08:00
parent d965ce1409
commit aadf5eb844
9 changed files with 802 additions and 202 deletions

View File

@@ -49,47 +49,48 @@ export let DefaultDODOContextInitConfig = {
export class DODOContext {
EVM: EVM;
Web3: Web3;
DODO: Contract;
DODOZoo: Contract;
BASE: Contract;
BaseCapital: Contract;
QUOTE: Contract;
QuoteCapital: Contract;
ORACLE: Contract;
SmartSwap: Contract;
SmartApprove: Contract;
Deployer: string;
Supervisor: string;
Maintainer: string;
spareAccounts: string[];
lpFeeRate: string;
mtFeeRate: string;
k: string;
//token
DODO:Contract;
USDT:Contract;
USDC:Contract;
WETH:Contract;
//pair
DODO_USDT: Contract;
USDT_USDC: Contract;
WETH_USDC: Contract;
DODO_USDT_ORACLE: Contract;
USDT_USDC_ORACLE: Contract;
WETH_USDC_ORACLE: Contract;
//SmartRoute
SmartSwap: Contract;
SmartApprove: Contract;
constructor() {}
async init(config: DODOContextInitConfig) {
this.k = config.k;
this.mtFeeRate = config.mtFeeRate;
this.lpFeeRate = config.lpFeeRate;
this.EVM = new EVM();
this.Web3 = getDefaultWeb3();
var cloneFactory = await contracts.newContract(
contracts.CLONE_FACTORY_CONTRACT_NAME
);
this.BASE = await contracts.newContract(
contracts.TEST_ERC20_CONTRACT_NAME,
["TestBase", 18]
);
this.QUOTE = await contracts.newContract(
contracts.TEST_ERC20_CONTRACT_NAME,
["TestQuote", 18]
);
this.ORACLE = await contracts.newContract(
contracts.NAIVE_ORACLE_CONTRACT_NAME
);
const allAccounts = await this.Web3.eth.getAccounts();
this.Deployer = allAccounts[0];
this.Supervisor = allAccounts[1];
this.Maintainer = allAccounts[2];
this.spareAccounts = allAccounts.slice(3, 10);
var DODOTemplate = await contracts.newContract(
contracts.DODO_CONTRACT_NAME
);
@@ -101,13 +102,66 @@ export class DODOContext {
this.Supervisor,
]
);
//发币
this.DODO = await contracts.newContract(
contracts.TEST_ERC20_CONTRACT_NAME,
["DODO", 18]
);
this.USDT = await contracts.newContract(
contracts.TEST_ERC20_CONTRACT_NAME,
["USDT", 6]
);
this.USDC = await contracts.newContract(
contracts.TEST_ERC20_CONTRACT_NAME,
["USDC", 6]
);
this.WETH = await contracts.newContract(
contracts.TEST_ERC20_CONTRACT_NAME,
["WETH", 18]
);
//创建交易对
//DODO-USDT
this.DODO_USDT_ORACLE = await contracts.newContract(
contracts.NAIVE_ORACLE_CONTRACT_NAME
);
await this.DODOZoo.methods
.breedDODO(
this.Maintainer,
this.BASE.options.address,
this.QUOTE.options.address,
this.ORACLE.options.address,
this.DODO.options.address,
this.USDT.options.address,
this.DODO_USDT_ORACLE.options.address,
config.lpFeeRate,
config.mtFeeRate,
config.k,
config.gasPriceLimit
)
.send(this.sendParam(this.Deployer));
//USDT-USDC
this.USDT_USDC_ORACLE = await contracts.newContract(
contracts.NAIVE_ORACLE_CONTRACT_NAME
);
await this.DODOZoo.methods
.breedDODO(
this.Maintainer,
this.USDT.options.address,
this.USDC.options.address,
this.USDT_USDC_ORACLE.options.address,
config.lpFeeRate,
config.mtFeeRate,
config.k,
config.gasPriceLimit
)
.send(this.sendParam(this.Deployer));
//WETH-USDC
this.WETH_USDC_ORACLE = await contracts.newContract(
contracts.NAIVE_ORACLE_CONTRACT_NAME
);
await this.DODOZoo.methods
.breedDODO(
this.Maintainer,
this.WETH.options.address,
this.USDC.options.address,
this.WETH_USDC_ORACLE.options.address,
config.lpFeeRate,
config.mtFeeRate,
config.k,
@@ -115,30 +169,50 @@ export class DODOContext {
)
.send(this.sendParam(this.Deployer));
this.DODO = contracts.getContractWithAddress(
this.DODO_USDT = contracts.getContractWithAddress(
contracts.DODO_CONTRACT_NAME,
await this.DODOZoo.methods
.getDODO(this.BASE.options.address, this.QUOTE.options.address)
.getDODO(this.DODO.options.address, this.USDT.options.address)
.call()
);
await this.DODO.methods
this.USDT_USDC = contracts.getContractWithAddress(
contracts.DODO_CONTRACT_NAME,
await this.DODOZoo.methods
.getDODO(this.USDT.options.address, this.USDC.options.address)
.call()
);
this.WETH_USDC = contracts.getContractWithAddress(
contracts.DODO_CONTRACT_NAME,
await this.DODOZoo.methods
.getDODO(this.WETH.options.address, this.USDC.options.address)
.call()
);
await this.DODO_USDT.methods
.enableBaseDeposit()
.send(this.sendParam(this.Deployer));
await this.DODO.methods
await this.DODO_USDT.methods
.enableQuoteDeposit()
.send(this.sendParam(this.Deployer));
await this.DODO.methods.enableTrading().send(this.sendParam(this.Deployer));
await this.DODO_USDT.methods.enableTrading().send(this.sendParam(this.Deployer));
this.BaseCapital = contracts.getContractWithAddress(
contracts.DODO_LP_TOKEN_CONTRACT_NAME,
await this.DODO.methods._BASE_CAPITAL_TOKEN_().call()
);
this.QuoteCapital = contracts.getContractWithAddress(
contracts.DODO_LP_TOKEN_CONTRACT_NAME,
await this.DODO.methods._QUOTE_CAPITAL_TOKEN_().call()
);
await this.USDT_USDC.methods
.enableBaseDeposit()
.send(this.sendParam(this.Deployer));
await this.USDT_USDC.methods
.enableQuoteDeposit()
.send(this.sendParam(this.Deployer));
await this.USDT_USDC.methods.enableTrading().send(this.sendParam(this.Deployer));
await this.WETH_USDC.methods
.enableBaseDeposit()
.send(this.sendParam(this.Deployer));
await this.WETH_USDC.methods
.enableQuoteDeposit()
.send(this.sendParam(this.Deployer));
await this.WETH_USDC.methods.enableTrading().send(this.sendParam(this.Deployer));
/*v1.5*/
this.SmartApprove = await contracts.newContract(
contracts.SMART_APPROVE
);
@@ -149,7 +223,6 @@ export class DODOContext {
);
await this.SmartApprove.methods.setSmartSwap(this.SmartSwap.options.address).send(this.sendParam(this.Deployer));
/*****/
console.log(log.blueText("[Init dodo context]"));
}
@@ -163,25 +236,25 @@ export class DODOContext {
};
}
async setOraclePrice(price: string) {
await this.ORACLE.methods
async setOraclePrice(oracle:Contract,price: string) {
await oracle.methods
.setPrice(price)
.send(this.sendParam(this.Deployer));
}
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)
async mintToken(tokenBase:Contract,tokenQuote:Contract,to: string, base: string, quote: string) {
await tokenBase.methods.mint(to, base).send(this.sendParam(this.Deployer));
await tokenQuote.methods
.mint(to,  quote)
.send(this.sendParam(this.Deployer));
}
async approveDODO(account: string) {
await this.BASE.methods
.approve(this.DODO.options.address, MAX_UINT256)
async approvePair(tokenBase:Contract,tokenQuote:Contract, approveTarget:string,account: string) {
await tokenBase.methods
.approve(approveTarget, MAX_UINT256)
.send(this.sendParam(account));
await this.QUOTE.methods
.approve(this.DODO.options.address, MAX_UINT256)
await tokenQuote.methods
.approve(approveTarget, MAX_UINT256)
.send(this.sendParam(account));
}
}

View File

@@ -1,4 +1,5 @@
import BigNumber from "bignumber.js";
import { getDefaultWeb3 } from './EVM';
export const MAX_UINT256 = "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
@@ -12,4 +13,9 @@ export function mweiStr(value: string): string {
export function gweiStr(gwei: string): string {
return new BigNumber(gwei).multipliedBy(10 ** 9).toFixed(0, BigNumber.ROUND_DOWN)
}
export function fromWei(value:string,unit:any): string {
var web3 = getDefaultWeb3();
return web3.utils.fromWei(value,unit);
}

366
test/utils-v1/dodoHelper.ts Normal file
View File

@@ -0,0 +1,366 @@
import { BigNumber } from 'bignumber.js';
export const RStatusOne = 0;
export const RStatusAboveOne = 1;
export const RStatusBelowOne = 2;
export class DODOHelper {
// unstable
public B!: BigNumber; // DODO._BASE_BALANCE_() / 10^baseDecimals
public Q!: BigNumber; // DODO._QUOTE_BALANCE_() / 10^quoteDecimals
public B0!: BigNumber; // DODO._TARGET_BASE_TOKEN_AMOUNT_() / 10^baseDecimals
public Q0!: BigNumber; // DODO._TARGET_QUOTE_TOKEN_AMOUNT_() / 10^quoteDecimals
public RStatus!: number; // DODO._R_STATUS_()
public OraclePrice!: BigNumber; // DODO.getOraclePrice() / 10^(18-baseDecimals+quoteDecimals)
// stable
public k!: BigNumber; // DODO._K_()/10^18
public mtFeeRate!: BigNumber; // DODO._MT_FEE_RATE_()/10^18
public lpFeeRate!: BigNumber; // DODO._LP_FEE_RATE_()/10^18
constructor(pairDetail:any) {
this.B = pairDetail.B
this.Q = pairDetail.Q
this.B0 = pairDetail.B0
this.Q0 = pairDetail.Q0
this.RStatus = pairDetail.RStatus
this.OraclePrice = pairDetail.OraclePrice
this.k = pairDetail.k
this.mtFeeRate = pairDetail.mtFeeRate
this.lpFeeRate = pairDetail.lpFeeRate
}
// return mid price
public getMidPrice(): BigNumber {
if (this.RStatus === RStatusOne) {
return this.OraclePrice;
}
if (this.RStatus === RStatusAboveOne) {
let R = this.B0.div(this.B);
R = R.multipliedBy(R)
.multipliedBy(this.k)
.minus(this.k)
.plus(new BigNumber(1));
return this.OraclePrice.multipliedBy(R);
}
if (this.RStatus === RStatusBelowOne) {
let R = this.Q0.div(this.Q);
R = R.multipliedBy(R)
.multipliedBy(this.k)
.minus(this.k)
.plus(new BigNumber(1));
return this.OraclePrice.div(R);
}
return this.OraclePrice;
}
// return the targetBase and targetQuote assuming system balanced
public getExpectedTarget(): { base: BigNumber; quote: BigNumber } {
let baseTarget: BigNumber;
let quoteTarget: BigNumber;
baseTarget = this.B0;
quoteTarget = this.Q0;
if (this.RStatus === RStatusOne) {
baseTarget = this.B0;
quoteTarget = this.Q0;
}
if (this.RStatus === RStatusAboveOne) {
quoteTarget = this.Q0;
baseTarget = solveQuadraticFunctionForTarget(this.B, this.k, this.Q.minus(this.Q0).div(this.OraclePrice));
}
if (this.RStatus === RStatusBelowOne) {
baseTarget = this.B0;
quoteTarget = solveQuadraticFunctionForTarget(
this.Q,
this.k,
this.B.minus(this.B0).multipliedBy(this.OraclePrice)
);
}
return {
base: baseTarget,
quote: quoteTarget
};
}
// return paid quote amount (fee deducted)
public queryBuyBase(amount: BigNumber) {
let mtFee = amount.multipliedBy(this.mtFeeRate);
let lpFee = amount.multipliedBy(this.lpFeeRate);
amount = amount.plus(mtFee).plus(lpFee);
let target = this.getExpectedTarget();
let quote = new BigNumber(0);
if (this.RStatus === RStatusOne) {
quote = this.ROneBuyBase(amount, target.base);
} else if (this.RStatus === RStatusAboveOne) {
quote = this.RAboveBuyBase(amount, target.base);
} else {
let backOneBase = this.B.minus(target.base);
let backOneQuote = target.quote.minus(this.Q);
if (amount.isLessThanOrEqualTo(backOneBase)) {
quote = this.RBelowBuyBase(amount, target.quote);
} else {
quote = backOneQuote.plus(this.ROneBuyBase(amount.minus(backOneBase), target.base));
}
}
return quote
}
// return received quote amount (fee deducted)
public querySellBase(amount: BigNumber) {
let result: BigNumber;
let target = this.getExpectedTarget();
if (this.RStatus === RStatusOne) {
result = this.ROneSellBase(amount, target.quote);
} else if (this.RStatus === RStatusBelowOne) {
result = this.RBelowSellBase(amount, target.quote);
} else {
let backOneBase = target.base.minus(this.B);
let backOneQuote = this.Q.minus(target.quote);
if (amount.isLessThanOrEqualTo(backOneBase)) {
result = this.RAboveSellBase(amount, target.base);
} else {
result = backOneQuote.plus(this.ROneSellBase(amount.minus(backOneBase), target.quote));
}
}
let mtFee = result.multipliedBy(this.mtFeeRate);
let lpFee = result.multipliedBy(this.lpFeeRate);
const quote = result.minus(mtFee).minus(lpFee);
return quote
}
// return paid base amount (fee deducted)
public queryBuyQuote(amount: BigNumber): BigNumber {
let mtFee = amount.multipliedBy(this.mtFeeRate);
let lpFee = amount.multipliedBy(this.lpFeeRate);
amount = amount.plus(mtFee).plus(lpFee);
let target = this.getExpectedTarget();
if (this.RStatus === RStatusOne) {
return this.ROneBuyQuote(amount, target.quote);
} else if (this.RStatus === RStatusBelowOne) {
return this.RBelowBuyQuote(amount, target.quote);
} else {
let backOneBase = target.base.minus(this.B);
let backOneQuote = this.Q.minus(target.quote);
if (amount.isLessThanOrEqualTo(backOneQuote)) {
return this.RAboveBuyQuote(amount, target.base);
} else {
return backOneBase.plus(this.ROneBuyQuote(amount.minus(backOneQuote), target.quote));
}
}
}
// return received base amount (fee deducted)
public querySellQuote(amount: BigNumber): BigNumber {
let result: BigNumber;
let target = this.getExpectedTarget();
if (this.RStatus === RStatusOne) {
result = this.ROneSellQuote(amount, target.base);
} else if (this.RStatus === RStatusAboveOne) {
result = this.RAboveSellQuote(amount, target.base);
} else {
let backOneBase = this.B.minus(target.base);
let backOneQuote = target.quote.minus(this.Q);
if (amount.isLessThanOrEqualTo(backOneQuote)) {
result = this.RBelowSellQuote(amount, target.quote);
} else {
result = backOneBase.plus(this.ROneSellQuote(amount.minus(backOneQuote), target.base));
}
}
let mtFee = result.multipliedBy(this.mtFeeRate);
let lpFee = result.multipliedBy(this.lpFeeRate);
return result.minus(mtFee).minus(lpFee);
}
public getWithdrawBasePenalty(amount: BigNumber): BigNumber {
if (this.RStatus === RStatusAboveOne) {
let baseTarget = solveQuadraticFunctionForTarget(this.B, this.k, this.Q.minus(this.Q0).div(this.OraclePrice));
let baseTargetWithdraw = solveQuadraticFunctionForTarget(
this.B.minus(amount),
this.k,
this.Q.minus(this.Q0).div(this.OraclePrice)
);
let penalty = baseTarget.minus(baseTargetWithdraw).minus(amount);
return penalty;
} else {
return new BigNumber(0);
}
}
public getWithdrawQuotePenalty(amount: BigNumber): BigNumber {
if (this.RStatus === RStatusBelowOne) {
let quoteTarget = solveQuadraticFunctionForTarget(
this.Q,
this.k,
this.B.minus(this.B0).multipliedBy(this.OraclePrice)
);
let quoteTargetWithdraw = solveQuadraticFunctionForTarget(
this.Q.minus(amount),
this.k,
this.B.minus(this.B0).multipliedBy(this.OraclePrice)
);
let penalty = quoteTarget.minus(quoteTargetWithdraw).minus(amount);
return penalty;
} else {
return new BigNumber(0);
}
}
// =========== helper ROne ===========
public ROneBuyBase(amount: BigNumber, targetBase: BigNumber): BigNumber {
if (amount.isGreaterThanOrEqualTo(targetBase)) {
throw new Error('ROne Buy Base Amount Exceed Limitation');
}
return integrate(targetBase, targetBase, targetBase.minus(amount), this.OraclePrice, this.k);
}
public ROneBuyQuote(amount: BigNumber, targetQuote: BigNumber): BigNumber {
if (amount.isGreaterThanOrEqualTo(targetQuote)) {
throw new Error('ROne Buy Quote Amount Exceed Limitation');
}
return integrate(
targetQuote,
targetQuote,
targetQuote.minus(amount),
new BigNumber(1).div(this.OraclePrice),
this.k
);
}
public ROneSellBase(amount: BigNumber, targetQuote: BigNumber): BigNumber {
let newQ = solveQuadraticFunctionForTrade(targetQuote, targetQuote, this.OraclePrice, amount.negated(), this.k);
return targetQuote.minus(newQ);
}
public ROneSellQuote(amount: BigNumber, targetBase: BigNumber): BigNumber {
let newB = solveQuadraticFunctionForTrade(
targetBase,
targetBase,
new BigNumber(1).div(this.OraclePrice),
amount.negated(),
this.k
);
return targetBase.minus(newB);
}
// =========== helper RAbove ===========
public RAboveBuyBase(amount: BigNumber, targetBase: BigNumber): BigNumber {
if (amount.isGreaterThanOrEqualTo(this.B)) {
throw new Error('RAbove Buy Base Amount Exceed Limitation');
}
return integrate(targetBase, this.B, this.B.minus(amount), this.OraclePrice, this.k);
}
public RAboveSellBase(amount: BigNumber, targetBase: BigNumber): BigNumber {
if (amount.plus(this.B).isGreaterThan(targetBase)) {
throw new Error('RAbove Sell Base Amount Exceed Limitation');
}
return integrate(targetBase, this.B.plus(amount), this.B, this.OraclePrice, this.k);
}
public RAboveBuyQuote(amount: BigNumber, targetBase: BigNumber): BigNumber {
let newB = solveQuadraticFunctionForTrade(
targetBase,
this.B,
new BigNumber(1).div(this.OraclePrice),
amount,
this.k
);
return newB.minus(this.B);
}
public RAboveSellQuote(amount: BigNumber, targetBase: BigNumber): BigNumber {
let newB = solveQuadraticFunctionForTrade(
targetBase,
this.B,
new BigNumber(1).div(this.OraclePrice),
amount.negated(),
this.k
);
return this.B.minus(newB);
}
// =========== helper RBelow ===========
public RBelowBuyQuote(amount: BigNumber, targetQuote: BigNumber): BigNumber {
if (amount.isGreaterThanOrEqualTo(this.Q)) {
throw new Error('RBelow Buy Quote Amount Exceed Limitation');
}
return integrate(targetQuote, this.Q, this.Q.minus(amount), new BigNumber(1).div(this.OraclePrice), this.k);
}
public RBelowSellQuote(amount: BigNumber, targetQuote: BigNumber): BigNumber {
if (amount.plus(this.Q).isGreaterThan(targetQuote)) {
throw new Error('RBelow Sell Quote Amount Exceed Limitation');
}
return integrate(targetQuote, this.Q.plus(amount), this.Q, new BigNumber(1).div(this.OraclePrice), this.k);
}
public RBelowBuyBase(amount: BigNumber, targetQuote: BigNumber): BigNumber {
let newQ = solveQuadraticFunctionForTrade(targetQuote, this.Q, this.OraclePrice, amount, this.k);
return newQ.minus(this.Q);
}
public RBelowSellBase(amount: BigNumber, targetQuote: BigNumber): BigNumber {
let newQ = solveQuadraticFunctionForTrade(targetQuote, this.Q, this.OraclePrice, amount.negated(), this.k);
return this.Q.minus(newQ);
}
}
export const integrate = (V0: BigNumber, V1: BigNumber, V2: BigNumber, i: BigNumber, k: BigNumber): BigNumber => {
let fairAmount = i.multipliedBy(V1.minus(V2));
let penalty = V0.multipliedBy(V0)
.div(V1)
.div(V2)
.multipliedBy(k);
return fairAmount.multipliedBy(new BigNumber(1).minus(k).plus(penalty));
};
export const solveQuadraticFunctionForTrade = (
V0: BigNumber,
V1: BigNumber,
i: BigNumber,
delta: BigNumber,
k: BigNumber
): BigNumber => {
// -b = (1-k)V1-kV0^2/V1+i*delta
let minusB = new BigNumber(1).minus(k).multipliedBy(V1);
minusB = minusB.minus(
k
.multipliedBy(V0)
.multipliedBy(V0)
.div(V1)
);
minusB = minusB.plus(i.multipliedBy(delta));
// sqrt(b*b+4(1-k)kQ0*Q0)
let squareRoot = new BigNumber(4)
.multipliedBy(new BigNumber(1).minus(k))
.multipliedBy(k)
.multipliedBy(V0)
.multipliedBy(V0);
squareRoot = minusB
.multipliedBy(minusB)
.plus(squareRoot)
.sqrt();
// 2(1-k)
let denominator = new BigNumber(2).multipliedBy(new BigNumber(1).minus(k));
return minusB.plus(squareRoot).div(denominator);
};
export const solveQuadraticFunctionForTarget = (V1: BigNumber, k: BigNumber, fairAmount: BigNumber): BigNumber => {
// V0 = V1+V1*(sqrt-1)/2k
let sqrt = new BigNumber(4)
.multipliedBy(k)
.multipliedBy(fairAmount)
.div(V1);
sqrt = new BigNumber(1).plus(sqrt).sqrt();
let premium = sqrt.minus(new BigNumber(1)).div(k.multipliedBy(new BigNumber(2)));
return V1.multipliedBy(new BigNumber(1).plus(premium));
};