Skip to content

Instantly share code, notes, and snippets.

@erin-koen
erin-koen / LocalDeployment
Last active December 9, 2021 15:35
How to run a local fork of mainnet along with the associated subgraphs
### PROTOCOL
- confirm node url:
ETHEREUM_NODE_MAINNET=https://eth-mainnet.alchemyapi.io/v2/ALxM6coYqWo1MUS12C4vQGTRVD0JfvYx
- `yarn`, `yarn compile`, `yarn build`
- `yarn hardhat node --export ../subgraphs/deployments/local/v4.json --hostname 0.0.0.0`
### SUBGRAPHS
- `yarn` in parent dir
@erin-koen
erin-koen / MelonBot.MagicFunction.ts
Last active January 7, 2021 03:24
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();
@erin-koen
erin-koen / MelonBot.runScript.ts
Last active April 30, 2020 21:32
A script to run the Melon Bot
async function run(bot: UniswapBot) {
try {
console.log('CONSULTING THE ORACLE ==> ');
const transaction = await bot.makeMeRich();
if (!transaction) {
console.log('NO TRADING BY ORDER OF THE ORACLE');
} else {
console.log('THE ORACLE SAYS TO TRADE');
@erin-koen
erin-koen / MelonBot.ts
Last active February 27, 2021 16:24
The Melon Fund Uniswap Bot
export class UniswapBot {
public static async create(hubAddress: string, tokenOneSymbol: string, tokenTwoSymbol: string) {
const environment = createEnvironment();
const hub = new Hub(environment, hubAddress);
const routes = await hub.getRoutes();
const manager = await hub.getManager();
const account = (await environment.client.getAccounts())[0];
@erin-koen
erin-koen / MelonBot.environment.ts
Last active April 30, 2020 21:04
The Melon Bot's Environment Configuration
import { DeployedEnvironment } from '@melonproject/melonjs';
import { Eth } from 'web3-eth';
import { HttpProvider, WebsocketProvider, HttpProviderOptions, WebsocketProviderOptions } from 'web3-providers';
import deployment from '../deployments/mainnet-deployment.json';
function createProvider(endpoint: string, options?: HttpProviderOptions | WebsocketProviderOptions) {
if (endpoint.startsWith('https://') || endpoint.startsWith('http://')) {
return new HttpProvider(endpoint, options as HttpProviderOptions);
}
@erin-koen
erin-koen / melonBot.package.json
Last active August 11, 2020 03:02
The Melon Bot's package.json
{
...
"scripts": {
"dev": "cross-env NODE_ENV=development ts-node --require dotenv-extended/config --transpile-only src"
},
"dependencies": {
"@melonproject/melonjs": "^1.0.1",
"axios": "^0.19.2",
"bignumber.js": "^9.0.0",
"web3-core": "^2.0.0-alpha.1",
@erin-koen
erin-koen / MelonBot.makeTrade.ts
Last active April 30, 2020 21:27
The Melon Bot's Trade Method
public async makeTransaction(priceInfo: PriceQueryResult){
// adjust the target amount of token to buy
const slippage = 0.97;
// use the price query results to construct the uniswap order argument object
const orderArgs = {
makerQuantity: priceInfo.sizeInQuote.integerValue().multipliedBy(slippage),
takerQuantity: priceInfo.sizeInBase.integerValue(),
makerAsset: priceInfo.quoteCurrency.address,
takerAsset: priceInfo.baseCurrency.address,
@erin-koen
erin-koen / MelonBot.getPrice.ts
Last active May 1, 2020 15:23
The Melon Bot's Price Query
public async getPrice(baseCurrency: TokenDefinition, quoteCurrency: TokenDefinition, baseQuantity: BigNumber) {
// Every uniswap exchange is WETH/someToken, and identified by the non-weth token
const exchangeToken = baseCurrency.symbol === 'WETH' ? quoteCurrency : baseCurrency;
// call the method to find the address
const exchangeAddress = await this.uniswapFactoryContract.getExchange(exchangeToken.address);
// instantiate the exchange contract
const exchange = new UniswapExchange(this.environment, exchangeAddress);
@erin-koen
erin-koen / MelonBot.getBalances.ts
Last active May 1, 2020 15:24
The Melon Bot's Balance Method
public async getBalances() {
// find the fund's accounting address
const accountingAddress = (await this.hub.getRoutes()).accounting;
// and instantiate a js representation of the contract
const accounting = new Accounting(this.environment, accountingAddress);
// to call the getFundHoldings method
const fundHoldings = await accounting.getFundHoldings();
@erin-koen
erin-koen / MelonBot.constructor.ts
Last active May 5, 2020 13:35
The Melon Bot's Constructor
export class UniswapBot {
public static async create(hubAddress: string, tokenOneSymbol: string, tokenTwoSymbol: string) {
const environment = createEnvironment();
const hub = new Hub(environment, hubAddress);
const routes = await hub.getRoutes();
const manager = await hub.getManager();
const account = (await environment.client.getAccounts())[0];
if (!sameAddress(manager, account)) {
throw new Error('You are not the manager of this fund.');