0x

0x is a transaction layer for the Ethereum blockchain. You can read more about them here. In the example below, we'll trade on a 0x order, which looks like this:

"order": {
"signature": "0x1cdd947fd6c110d6d4204c5a3e4ad08282a79574d6fcc89e0782a2bf003a92042833fc2efd2435c89bc1ad3074ed9b1df1cc37ba8399305c84002040deca4226e002",
"senderAddress": "0x0000000000000000000000000000000000000000",
"makerAddress": "0x167f897440cfd5227f22349779841f83ddda126a",
"takerAddress": "0x0000000000000000000000000000000000000000",
"makerFee": "0",
"takerFee": "30000000000000000000",
"makerAssetAmount": "10000000000000000000000",
"takerAssetAmount": "20000000000000000000",
"makerAssetData": "0xf47261b000000000000000000000000010ef64cb79fd4d75d4aa7e8502d95c42124e434b",
"takerAssetData": "0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"salt": "1582812333130",
"exchangeAddress": "0x61935cbdd02287b511119ddb11aeb42f1593b7ef",
"feeRecipientAddress": "0x5265bde27f57e738be6c1f6ab3544e82cdc92a8f",
"expirationTimeSeconds": "1594908333",
"makerFeeAssetData": "0x",
"chainId": 1,
"takerFeeAssetData": "0xf47261b000000000000000000000000010ef64cb79fd4d75d4aa7e8502d95c42124e434b"
}

We'll assign the above values to a variable called offer and execute a trade on it in the example below:

This example requires an environment instance as described here.

import {
ExchangeIdentifier,
Hub,
Trading,
ZeroExV3TradingAdapter
} from '@melonproject/melonjs';
// your hub address
const hubAddress = '0x05263237f43190ce0e93b48afb25dd60a03ad3c5';
// the address of the fund's manager
const fundManager = '0x0b64bf0fae1b9ffa80cd880f5b82d467ee34c28e';
// declare an instance of the fund's hub to access the spoke contract addresses
const hub = new Hub(environment, hubAddress);
// the address of the fund's trading contract
const tradingAddress = hub.getRoutes().trading;
// declare an exchange object, shape noted in /buildingblocks/environment
const exchange = environment.getExchange(ExchangeIdentifier.ZeroExV3);
// in the format from the preceeding code block
const offer = { json };
// declare the maker asset object
const makerAsset = environment.getToken('WETH');
// you'll dictate the number of makerAssets to swap for takerAssets with the appropriate number of decimals
const quantity = new BigNumber(10).multipliedBy(
new BigNumber(10).exponentiatedBy(makerAsset.decimals)
);
// specify the gas price (refer to http://ethgasstation.info/)
const gasPrice = 30000000000;
// create a new isntance of the fund's Trading contract
const trading = new Trading(environment, tradingAddress);
// create an instance of the fund's 0x trading adapter
const adapter = await ZeroExV3TradingAdapter.create(
environment,
exchange.exchange,
trading
);
// create and execute the transaction
const transaction = adapter.takeOrder(fundManager, offer, quantity);
const opts = await transaction.prepare({ gasPrice });
const receipt = await transaction.send(opts);