-
Notifications
You must be signed in to change notification settings - Fork 6
/
auction.js
86 lines (81 loc) · 2.94 KB
/
auction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* This example shows how to participate auctions in hydro protocol v2 contract.
*/
const hydro = require("../src/hydro");
const { web3 } = require("../src/web3");
const { toHumanReadableDecimal } = require("../src/helper");
const { getErc20Decimal } = require("../src/erc20");
const BigNumber = require("bignumber.js");
/*
You can always treat an auction as an limit order.
The order sell collateral asset for debt asset. The following
function shows how to calculate the order price and the maximum
debt asset you could pay, at current block and next block.
*/
const parseAuctionToOrder = async auctionID => {
const rawAuctionInfo = await hydro.methods
.getAuctionDetails(auctionID)
.call();
debtDecimal = await getErc20Decimal(rawAuctionInfo.debtAsset);
collateralDecimal = await getErc20Decimal(rawAuctionInfo.collateralAsset);
ratio = toHumanReadableDecimal(rawAuctionInfo.ratio);
leftDebt = toHumanReadableDecimal(rawAuctionInfo.leftDebtAmount, debtDecimal);
leftCollateral = toHumanReadableDecimal(
rawAuctionInfo.leftCollateralAmount,
collateralDecimal
);
price = leftDebt.div(leftCollateral).div(ratio);
maxFillableDebt = ratio.gt(1) ? leftDebt.div(ratio) : leftDebt;
const market = await hydro.methods.getMarket(rawAuctionInfo.marketID).call();
ratioPerBlock = toHumanReadableDecimal(market.auctionRatioPerBlock);
ratioNextBlock = ratio.plus(ratioPerBlock);
priceNextBlock = leftDebt.div(leftCollateral).div(ratioNextBlock);
maxFillableDebtNextBlock = ratioNextBlock.gt(1)
? leftDebt.div(ratioNextBlock)
: leftDebt;
return {
debtAsset: rawAuctionInfo.debtAsset,
collateralAsset: rawAuctionInfo.collateralAsset,
price: price,
maxFillableDebt: maxFillableDebt,
priceNextBlock: priceNextBlock,
maxFillableDebtNextBlock: maxFillableDebtNextBlock,
ratio: ratio,
ratioNextBlock: ratioNextBlock,
ratioPerBlock: ratioPerBlock
};
};
/*
Fill auction
You need to prepare enough assets in your trading balance.
Be careful about decimals, all following amount values are raw
*/
const fillAuction = async (bidderAddress, auctionID, fillAmount) => {
var txHash;
var filledDebt = new BigNumber(0);
var getCollateral = new BigNumber(0);
await hydro.methods
.fillAuctionWithAmount(auctionID, fillAmount)
.send({ from: bidderAddress, gas: 1000000, gasPrice: 20 * 10 ** 9 })
.on("receipt", res => {
txHash = res.transactionHash;
});
const receipt = await web3.eth.getTransactionReceipt(txHash);
console.log("receipt", receipt);
if (receipt.status) {
for (let log of receipt.logs) {
console.log(log);
if (
log.topics[0] ==
"0x42a553656a0da7239e70a4a3c864c1ac7d46d7968bfe2e1fb14f42dbb67135e8"
) {
filledDebt = new BigNumber("0x" + log.data.slice(130, 194));
getCollateral = new BigNumber("0x" + log.data.slice(194, 258));
}
}
}
return {
filledDebt: filledDebt,
getCollateral: getCollateral
};
};