Skip to content

Instantly share code, notes, and snippets.

@fragosti
Created March 20, 2020 15:27
Show Gist options
  • Save fragosti/11847f639b18dba967e8414bc919dfa7 to your computer and use it in GitHub Desktop.
Save fragosti/11847f639b18dba967e8414bc919dfa7 to your computer and use it in GitHub Desktop.
Posting order code
const connect = require('@0x/connect');
const subproviders = require('@0x/subproviders');
const utils = require('@0x/utils');
const wrapper = require('@0x/web3-wrapper');
const orderUtils = require('@0x/order-utils');
const contractAddresses = require('@0x/contract-addresses');
const handler = {
onUpdate: (...args) => console.log('onUpdate:', ...args),
onError: (...args) => console.log('onError:', ...args),
onClose: (...args) => console.log('onClose:', ...args),
}
// const WS_URL = 'wss://kovan.api.0x.org/sra/v3';
// const HTTP_URL = 'https://kovan.api.0x.org/sra/v3';
const CHAIN_ID = 1337;
const WS_URL = 'ws://localhost:3000/';
const HTTP_URL = 'http://localhost:3000/sra/v3';
const PRIVATE_KEY = 'f2f48ee19680706196e2e339e5da3491186e0c4c5030670656b0e0164837257d'; // ganache
const PUBLIC_KEY = '0x5409ED021D9299bf6814279A6A1411A7e866A631'; // ganache
const addresses = contractAddresses.getContractAddressesForChainOrThrow(CHAIN_ID);
const ONE_SECOND_MS = 1000;
const ONE_MINUTE_MS = ONE_SECOND_MS * 60;
const TEN_MINUTES_MS = ONE_MINUTE_MS * 10;
const DECIMALS = 18;
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
const NULL_BYTES = '0x';
const ZERO = new utils.BigNumber(0);
const GANACHE_NETWORK_ID = 50;
const KOVAN_NETWORK_ID = 42;
const ROPSTEN_NETWORK_ID = 3;
const RINKEBY_NETWORK_ID = 4;
const createPrivateKeyProvider = () => {
const pe = new subproviders.Web3ProviderEngine();
const pkw = new subproviders.PrivateKeyWalletSubprovider(PRIVATE_KEY);
pe.addProvider(pkw);
utils.providerUtils.startProviderEngine(pe);
return pe;
};
const provider = createPrivateKeyProvider();
const getRandomFutureDateInSeconds = () => {
return new utils.BigNumber(Date.now() + TEN_MINUTES_MS).div(ONE_SECOND_MS).integerValue(utils.BigNumber.ROUND_CEIL);
};
const createSignedOrderAsync = async () => {
const makerAddress = PUBLIC_KEY;
const order = {
chainId: CHAIN_ID,
exchangeAddress: addresses.exchange,
makerAddress,
takerAddress: NULL_ADDRESS,
senderAddress: NULL_ADDRESS,
feeRecipientAddress: NULL_ADDRESS,
expirationTimeSeconds: getRandomFutureDateInSeconds(),
salt: orderUtils.generatePseudoRandomSalt(),
makerAssetAmount: new utils.BigNumber(7),
takerAssetAmount: new utils.BigNumber(7),
makerAssetData: orderUtils.assetDataUtils.encodeERC20AssetData(addresses.zrxToken),
takerAssetData: orderUtils.assetDataUtils.encodeERC20AssetData(addresses.etherToken),
makerFeeAssetData: NULL_BYTES,
takerFeeAssetData: NULL_BYTES,
makerFee: ZERO,
takerFee: ZERO,
};
const signedOrder = await orderUtils.signatureUtils.ecSignOrderAsync(provider, order, makerAddress);
return signedOrder
};
const connectWebsocket = async () => {
try {
const channel = await connect.ordersChannelFactory.createWebSocketOrdersChannelAsync(WS_URL, handler);
channel.subscribe({});
console.log('Successfully connected');
} catch (e) {
console.log(e);
}
}
const postOrderAsync = async () => {
try {
const client = new connect.HttpClient(HTTP_URL);
const signedOrder = await createSignedOrderAsync();
console.log(signedOrder);
await client.submitOrderAsync(signedOrder);
} catch (err) {
console.log(err);
}
};
// createSignedOrderAsync().then(a => console.log(JSON.stringify(a)))
postOrderAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment