In the example below, we'll fetch a rate and then trade WETH
for MLN
on Uniswap.
This example requires an environment instance as described here.
import {Hub,Trading,UniswapExchange,UniswapTradingAdapter,UniswapFactory,} from '@melonproject/melonjs';// your hub addressconst hubAddress = '0x05263237f43190ce0e93b48afb25dd60a03ad3c5';// the address of the fund's managerconst fundManager = '0x0b64bf0fae1b9ffa80cd880f5b82d467ee34c28e';// declare an instance of the fund's hub to access the spoke contract addressesconst hub = new Hub(environment, hubAddress);// the address of the fund's trading contractconst tradingAddress = hub.getRoutes().trading;// specify the gas price (refer to http://ethgasstation.info/).const gasPrice = 30000000000;// the maker token objectconst maker = environment.getToken('WETH');// the taker token objectconst taker = environment.getToken('MLN');// we'll buy 100 MLN (denominated in the decimals of the respective token)const takerQuantity = new BigNumber(100).multipliedBy(new BigNumber(10).exponentiatedBy(taker.decimals));// first we'll find the available price for 100 MLN// get the address of the WETH/MLN uniswap exchangeconst factory = new UniswapFactory(environment, fundManager)const exchangeAddress = await factory.getExchange(taker.address);// create an instance of that exchangeconst exchange = new UniswapExchange(environment, exchangeAddress);// query the available MLNconst makerQuantity = await exchange.getTokenToEthInputPrice(takerQuantity);// do the math to deduce the rate of MLN per WETHconst price = makerQuantity.dividedBy(takerQuantity);// We'll next submit an order by preparing a transaction and pushing it through the normal pattern// create a new instance of the fund's Trading contractconst trading = new Trading(environment, tradingAddress);// assemble an orderArgs object to pass to the takeOrderFunctionconst orderArgs = {makerQuantity: makerQty; // amount of the makerTokentakerQuantity: takerQty; // amount of the takerTokenmakerAsset: maker.address; // address of the makerTokentakerAsset: taker.address; // address of the takerToken};// create the adapter instanceconst adapter = await UniswapTradingAdapter.create(environment, exchangeAddress, trading);// create and execute the transactionconst transaction = adapter.takeOrder( fundManager, orderArgs );const opts = transaction.prepare({ gasPrice });const receipt = transaction.send(opts);