Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Generate order for exchange using API #133

Closed
somish opened this issue Aug 24, 2017 · 21 comments
Closed

Generate order for exchange using API #133

somish opened this issue Aug 24, 2017 · 21 comments

Comments

@somish
Copy link

somish commented Aug 24, 2017

Hi, we have developed a project on Ethereum, currently on testnet. I have a requirement that would require integration with 0x.js. I have successfully fetched results from methods like getBalanceAsync but am unable to understand which API would be used to create an order. Can someone please help.

@LogvinovLeon
Copy link
Contributor

Hi,
An order is essentially a bunch of data, that conforms to the Order type.
There is no function to generate it, cause it's just a plain JS object.
You can generate it yourself and pass it into any function accepting an order.
However - there are some fields, that you can use some helper functions/consts to fill.

const order = {
	exchangeContractAddress: string, // Can be fetched from [zeroEx.exchange.getExchangeContractAddress](https://0xproject.com/docs/0xjs#getContractAddressAsync)
	expirationUnixTimestampSec: BigNumber, // This is up to you
	feeRecipient: string, // Specify the fee recepient address or use `ZeroEx.NULL_ADDRESS` to disable fees
	maker: string, // The address creating an order
	makerFee: BigNumber, // Fee size the maker pays in Base Units or `new BigNumber(0)`
	makerTokenAddress: string,
	makerTokenAmount: BigNumber,
	salt: BigNumber, // Use [ZeroEx.generatePseudoRandomSalt](https://0xproject.com/docs/0xjs#generatePseudoRandomSalt)
	taker: string, // Taker address or `ZeroEx.NULL_ADDRESS` if you want anyone to be able to fill the order (most probably)
	takerFee: BigNumber, // Fee size the taker pays in Base Units or `new BigNumber(0)`
	takerTokenAddress: string,
	takerTokenAmount: BigNumber,
}

This is how you get a plain Order.
In order to make it useful (fillable) you'd like to sign it.

const orderHash = ZeroEx.getOrderHashHex(order);
const ecSignature = zeroEx.signOrderHashAsync(orderHash, signerAddress);
const signedOrder = {
   ...order,
   ecSignature,
};

I'll also recommend to check out the tests for more examples.
Specifically for order creation: https://github.com/0xProject/0x.js/blob/master/test/utils/order_factory.ts

@LogvinovLeon
Copy link
Contributor

@support-somish Can this issue be closed? Did you successfully generate an order?

@somish
Copy link
Author

somish commented Aug 26, 2017 via email

@LogvinovLeon
Copy link
Contributor

You can use 0x.js with Kovan or testrpc. We don't support Rinkeby yet.
You can run and test 0x.js with testrp. This is how we run onr tests and CI.
If you need some kovan ETH just use a faucet available as a part of kovan OTC.

@somish
Copy link
Author

somish commented Aug 29, 2017

We tried creating an order through the ts file but we are unable to execute ZeroEx.generatePseudoRandomSalt as it says that generatePseudoRandomSalt is not a function. We are able to access all other functions of the ZeroEx object except this one.

@LogvinovLeon
Copy link
Contributor

You can try printing the whole ZeroEx object and checking, that this function is there.
Which version of 0x.js are you using?

@somish
Copy link
Author

somish commented Aug 29, 2017

We used command npm install 0x.js to install 0x in our project. The current version is 0.9.3. Please see the object image attached. Is there a specific version we need to use?
0x object

@LogvinovLeon
Copy link
Contributor

generatePresudoRandomSalt is a static function. You're printing an instance, not a constructor.
Are you sure, you're calling it like ZeroEx.generatePresudoRandomSalt, not like: zeroEx.generatePresudoRandomSalt?
The first way is correct.

@somish
Copy link
Author

somish commented Aug 29, 2017

Many thanks, we were able to create an order hash.

Just one more thing, we are trying to sign the order hash using the following:
zeroEx.signOrderHashAsync(orderHash,"0x7266C50F1f461d2748e675B907eF22987F6B5358")
.then(function(ECSignature) {
console.log("Signature is "+ECSignature);
})
.catch(function(error) {
console.log('Caught error: ', error);
});

We get the error: Specified signerAddress 0x7266C50F1f461d2748e675B907eF22987F6B5358 isn't available through the supplied web3 provider. We are using Web3.providers.HttpProvider("https://kovan.infura.io/") as the Web3 Provider.

Can you please help.

@LogvinovLeon
Copy link
Contributor

Convert the address to lover case

@LogvinovLeon
Copy link
Contributor

Newer versions of 0x.js throw a better error message in this case

@LogvinovLeon
Copy link
Contributor

Actually - this will not help, cause in order to sign - you'll need a private key.
Infura obviously doesn't have your private key.

@LogvinovLeon
Copy link
Contributor

You'll need a web3 instance that has access to your accounts. What's your use case? What do you plan to use for signing?

@somish
Copy link
Author

somish commented Aug 31, 2017

We need to build a nodeJS API which will automatically(based on a few system checks), create orders for exchanging ERC20 tokens. Generally we use web3.eth.sendRawTransaction to send data on chain via infura.io.

@LogvinovLeon
Copy link
Contributor

You can use the provider-engine with the HookedWalletSubprovider to sign the transactions and access accounts.
And you can connect it to infura via RpcSubprovider

@LogvinovLeon
Copy link
Contributor

Here is the example subprovider we wrote for a Ledger https://github.com/0xProject/website/blob/development/ts/subproviders/ledger_wallet_subprovider_factory.ts

@LogvinovLeon
Copy link
Contributor

Here is an another example - that calls a signature server to sign transactions: https://github.com/Neufund/server-wallet-provider/blob/master/src/index.js

@LogvinovLeon
Copy link
Contributor

Closing because of inactivity. Feel free to reopen if still valid.

@nitika-goel
Copy link

nitika-goel commented Nov 7, 2017

Hi, pardon for the inactivity. I'm still unable to solve this. Intend to use infura as I cannot run a local geth node. Here is the code I am using:

const Web3 = require('web3');
const Accounts=require('web3-eth-accounts');
const HookedWeb3Provider=require("hooked-web3-provider");
const ZeroEx = require('0x.js').ZeroEx;
const BigNumber = require('bignumber.js');
const KOVAN_ENDPOINT='https://kovan.infura.io/';

(async () => {
    var web3=new Web3(new Web3.providers.HttpProvider(KOVAN_ENDPOINT));
    var accounts =new Accounts(KOVAN_ENDPOINT);
    // Creating a new Account
    var logAcc=web3.eth.accounts.privateKeyToAccount("528d53be9f33de80d68b148aac930b9ab757d798ef251bcbdcfd94b95961144a");
    console.log(logAcc);

   var zeroEx = new ZeroEx(web3.currentProvider);    // Number of decimals to use (for ETH and ZRX)
    const DECIMALS = 18;     // Addresses
    const NULL_ADDRESS = ZeroEx.NULL_ADDRESS;  
    console.log("Null address is "+NULL_ADDRESS);  // Ethereum Null address
    const WETH_ADDRESS = await zeroEx.etherToken.getContractAddressAsync();  
    console.log("ETH token address is "+ WETH_ADDRESS);    // The wrapped ETH token contract
    const ZRX_ADDRESS  = await zeroEx.exchange.getZRXTokenAddressAsync(); 
    console.log("ZeroEx Address is "+ ZRX_ADDRESS)  // The ZRX token contract
    const EXCHANGE_ADDRESS   = await zeroEx.exchange.getContractAddressAsync();  // The Exchange.sol address (0x exchange smart contract)
    console.log("Exchange address is "+ EXCHANGE_ADDRESS);
     var account =  await zeroEx.getAvailableAddressesAsync();
     console.log(account);
})().catch(console.log);

I have created an account with a private key and am able to sign the order hash with web3.eth.accounts.sign() but in that case my vrs signatures generated do not match.

I intend to use the standard ZeroEx functions but the function zeroEx.getAvailableAddressesAsync() returns null value even though my web3 object has accounts json associated with it.

Can you please provide a sample of using your standard method with infura please?

@EeevilPopTart
Copy link

EeevilPopTart commented Feb 15, 2018

@LogvinovLeon How can I go about signing an order using only a private key? If I need to add my private key to a provider, how can I do that programmatically so that I don't have to use something like MetaMask or a Ledger? I have order data and a private key, hash the order data, now I just want to sign it so I can submit it to a relayer.

Edit: I've found a solution. I'm using the 0x node module to generate the hash, then signing it using the private key via the ecsign function from ethereumjs-util.

@fabioberger
Copy link
Contributor

@EeevilPopTart you can use a Web3 Provider Engine custom provider setup with the HookedWalletSubProvider with a similar module passed into it as this: https://github.com/0xProject/0x.js/blob/development/packages/testnet-faucets/src/ts/id_management.ts

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants