Skip to content

Commit

Permalink
Initial framework complete
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenshively committed Oct 26, 2020
1 parent 0d9aea9 commit a81dda0
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 341 deletions.
10 changes: 10 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ACCOUNT_ADDRESS=
ACCOUNT_SECRET=

PROVIDER_INFURA_ID=
PROVIDER_INFURA_SECRET=
PROVIDER_ALCHEMY_KEY=

GAS_STATION_ENDPOINT=

SLACK_WEBHOOK=
22 changes: 22 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"txManagers": {
"A": {
"envKeyAddress": "ACCOUNT_ADDRESS",
"envKeySecret": "ACCOUNT_SECRET",
"interval": 60
}
},
"network": {
"name": "mainnet",
"providers": [
{
"type": "HTTP_Infura",
"envKeyID": "PROVIDER_INFURA_ID"
},
{
"type": "HTTP_Alchemy",
"envKeyKey": "PROVIDER_ALCHEMY_KEY"
}
]
}
}
4 changes: 2 additions & 2 deletions networks.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module.exports = {
process.env.ACCOUNT_SECRET,
"https://mainnet.infura.io/v3/" + process.env.PROVIDER_INFURA_ID
),
gas: 4000000,
gasPrice: 26e9,
gas: 140*36609 + 60000,
gasPrice: 14e9,
networkId: "*"
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"winston": "^3.3.3"
},
"scripts": {
"cafe-chi": "node './src/start' '../config.json'",
"test": "mocha --file './tests/setup.js' 'tests/**/*.test.js' --recursive"
}
}
135 changes: 1 addition & 134 deletions src/network/webthree/goldenage/cafechi.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,11 @@ const Big = require("big.js");
Big.DP = 40;
Big.RM = 0;

const Web3Utils = require("web3-utils");

const SmartContract = require("../smartcontract");

class CafeChi extends SmartContract {
arb() {
return this._txFor(this._inner.methods.arb());
}


liquidateSNWithPrice(
messages,
signatures,
symbols,
borrowers,
repayCTokens,
seizeCTokens,
gasPrice,
chi = false
) {
const cTokens = this._combineTokens(repayCTokens, seizeCTokens);
let method = chi
? this._inner.methods.liquidateSNWithPriceChi
: this._inner.methods.liquidateSNWithPrice;
method = method(messages, signatures, symbols, borrowers, cTokens);
// TODO we cheat here by just estimating gas for first candidate since
// that's all that TxManager cares about at the moment.
const gasLimit = this._estimateGas(repayCTokens[0], seizeCTokens[0], true);
return this._txFor(method, gasLimit, gasPrice);
}

/**
* Performs liquidation on multiple accounts (SEND -- uses gas)
*
* @param {Array<String>} borrowers addresses of users with negative liquidity
* @param {Array<String>} repayCTokens address of token to repay
* @param {Array<String>} seizeCTokens address of token to seize
* @param {Number} gasPrice the gas price to use, in gwei
* @return {Object} the transaction object
*/
liquidateSN(borrowers, repayCTokens, seizeCTokens, gasPrice, chi = false) {
const cTokens = this._combineTokens(repayCTokens, seizeCTokens);
let method = chi
? this._inner.methods.liquidateSNChi
: this._inner.methods.liquidateSN;
method = method(borrowers, cTokens);
// TODO we cheat here by just estimating gas for first candidate since
// that's all that TxManager cares about at the moment.
const gasLimit = this._estimateGas(repayCTokens[0], seizeCTokens[0]);
return this._txFor(method, gasLimit, gasPrice);
}

liquidateSWithPrice(
messages,
signatures,
symbols,
borrower,
repayCToken,
seizeCToken,
gasPrice,
chi = false
) {
let method = chi
? this._inner.methods.liquidateSWithPriceChi
: this._inner.methods.liquidateSWithPrice;
method = method(
messages,
signatures,
symbols,
borrower,
repayCToken,
seizeCToken
);
const gasLimit = this._estimateGas(repayCToken, seizeCToken, true);
return this._txFor(method, gasLimit, gasPrice);
}

liquidateS(borrower, repayCToken, seizeCToken, gasPrice, chi = false) {
let method = chi
? this._inner.methods.liquidateSChi
: this._inner.methods.liquidateS;
method = method(borrower, repayCToken, seizeCToken);
const gasLimit = this._estimateGas(repayCToken, seizeCToken);
return this._txFor(method, gasLimit, gasPrice);
}

/**
* Performs liquidation (SEND -- uses gas)
*
* @param {string} borrower address of any user with negative liquidity
* @param {string} repayCToken address of token to repay
* @param {string} seizeCToken address of token to seize
* @param {Big} amount debt to repay, in units of the ordinary asset
* @param {Number} gasPrice the gas price to use, in gwei
* @return {Promise<Object>} the transaction object
*/
liquidate(borrower, repayCToken, seizeCToken, amount, gasPrice) {
const hexAmount = Web3Utils.toHex(amount.toFixed(0));
const method = this._inner.methods.liquidate(
borrower,
repayCToken,
seizeCToken,
hexAmount
);
const gasLimit = this._estimateGas(repayCToken, seizeCToken, false, false);
return this.txFor(method, gasLimit, gasPrice);
}

_combineTokens(repayList, seizeList) {
let cTokens = [];
for (let i = 0; i < repayList.length; i++)
cTokens.push(repayList[i], seizeList[i]);
return cTokens;
}

_estimateGas(
repayCToken,
seizeCToken,
postPrices = false,
solveAmount = true
) {
let gas = Big(GAS_CUSHION);

// NOTE: we assume everything is lowercase when comparing addresses
if (repayCToken === CETH) gas = gas.plus(GAS_ETH2TOKEN);
else if (seizeCToken === CETH) gas = gas.plus(GAS_TOKEN2ETH);
// TODO The following conditional should really have an `or` clause to account
// for cases where Uniswap has sufficient liquidity to go straight from repay
// to seize without using Eth as an intermediate, but that's difficult to compute
else if (repayCToken === seizeCToken) gas = gas.plus(GAS_TOKEN2TOKEN);
else gas = gas.plus(GAS_TOKEN2TOKEN2ETH);

if (V2S.includes(repayCToken) || V2S.includes(seizeCToken))
gas = gas.plus(GAS_V2_PENALTY);
if (postPrices) gas = gas.plus(GAS_ORACLE);
if (solveAmount) gas = gas.plus(GAS_COMPUTE_AMOUNT);

return gas;
return this._txFor(this._inner.methods.arb(), Big(7000000));
}
}

Expand Down
Loading

0 comments on commit a81dda0

Please sign in to comment.