Skip to content

Instantly share code, notes, and snippets.

@erin-koen
Last active January 7, 2021 03:24
Show Gist options
  • Save erin-koen/f0497befffec56d4e12fd717b92bbc29 to your computer and use it in GitHub Desktop.
Save erin-koen/f0497befffec56d4e12fd717b92bbc29 to your computer and use it in GitHub Desktop.
The Melon Bot's Magic Function
public fortuneTeller(expectedPrice: PriceQueryResult) {
// this is my sophisticated trading strategy. you could build
// something more elaborate with the PriceQueryResult object passed in.
return Math.random() > 0.5;
}
public async makeMeRich() {
// call the getFundHoldings method which returns an array of holdings.
const balances = await this.accountingContract.getFundHoldings();
// deduce holdings in each token your bot cares about
const tokenOneHolding =
balances.find((balance) => sameAddress(balance.address, this.tokenOne.address))?.amount || new BigNumber(0);
const tokenTwoHolding =
balances.find((balance) => sameAddress(balance.address, this.tokenTwo.address))?.amount || new BigNumber(0);
/**
* Specific to my strategy, where we are either long MLN or long ETH but never long both,
* baseCurrency is the currency with holdings, quote currency is the currency without.
* It will be the case that they're both non-zero only if the bot starts running
* with balances that it has not traded. In that case, I've set MLN to be
* the base ccy (bot will sell MLN balance buy WETH)
*/
const baseCurrency = tokenOneHolding.isGreaterThan(tokenTwoHolding) ? this.tokenOne : this.tokenTwo;
const quoteCurrency = baseCurrency === this.tokenOne ? this.tokenTwo : this.tokenOne;
const baseQuantity = baseCurrency === this.tokenOne ? tokenOneHolding : tokenTwoHolding;
// pass them all to the getPrice function to see what the rates are
const priceObject = await this.getPrice(baseCurrency, quoteCurrency, baseQuantity);
if (this.fortuneTeller(priceObject)) {
return this.makeTransaction(priceObject);
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment