-
Notifications
You must be signed in to change notification settings - Fork 13
loopring.js v2.0.0 English Developer’s Documentation
This developer documentation introduces the use of loopring.js to access Loopring’s Protocol. The Document consists of two sections: Ethereum and the Relay.
The Ethereum section focuses on the functionality of the wallet. The wallet functions include: the creation of Ethereum accounts, unlocking of private keys, unlocking and generation of mnemonics, unlocking and generation of keystores, access to MetaMask, and access to hardware wallets such as Trezor and Ledger. Signatures of Ethereum transactions can be realized. In the creation of Ethereum contracts, the following functions are done: the signing of the information, the signing of Loopring orders, and an analyses of the abi function and abi information. Also, this section includes some Ethereum JSON-RPC interfaces, including eth_getTransactionCount,eth_sendRawTransaction, eth_gasPrice, eth_estimateGas, eth_getBalance,eth_getTransactionByHash, eth_call.
The Relay section focuses on the access of the Loopring Relay interfaces, including the JSON-RPC and SocketIO interfaces. See the Loopring Relay access documentation for details on the specific interfaces.
path is constant used in Loopring wallet,(m/44'/60'/0'/0)
Get the address using the private key
- privatekey | hex string | Buffer
- address
const pKey = "07ae9ee56203d29171ce3de536d7742e0af4df5b7f62d298a0445d11e466bf9e";
privateKeytoAddress(pkey); //address:0x48ff2269e58a373120FFdBBdEE3FBceA854AC30AGet the address using the public key
- publicKey | hex string | Buffer
- sanitize | bool, the default is false
- address
const publicKey = "0895b915149d15148ac4171f453060d6b53a9ebb694689351df8e3a49c109c7a65374b5c196ce8cc35ff79cb3ce54ea1695704dd5b3cfc6864bd110b62cfd509";
publicKeytoAddress(publicKey)//address:0x48ff2269e58a373120FFdBBdEE3FBceA854AC30AGet public key using the private key
- privateKey | hex string | Buffer
- publickey | hex string, without prefix
const privateKey = '07ae9ee56203d29171ce3de536d7742e0af4df5b7f62d298a0445d11e466bf9e';
const publicKey = privateKeytoPublic(privateKey);
//publicKey:"0895b915149d15148ac4171f453060d6b53a9ebb694689351df8e3a49c109c7a65374b5c196ce8cc35ff79cb3ce54ea1695704dd5b3cfc6864bd110b62cfd509"An example account is generated using mnemonics, dpath, and passwords.
- mnemonic | string
- dpath | string
- password | string - can be empty
- account KeyAccount example
const mnemonic = "seven museum glove patrol gain dumb dawn bridge task alone lion check interest hair scare cash sentence diary better kingdom remember nerve sunset move";
const dpath = "m/44'/60'/0'/0/0";
const password = "1111111";
const account = fromMnemonic(mnemonic,dpath,password);Get keyAccount using the keystore and password
- keystore | string
- password | string - can be empty, depending on whether the keystore requires a password to unlock it.
- KeyAccount | account
const keystore = "{"version":3,"id":"e603b01d-9aa9-4ddf-a165-1b21630237a5","address":"2131b0816b3ef8fe2d120e32b20d15c866e6b4c1","Crypto":{"ciphertext":"7e0c30a985cf29493486acaf86259a2cb0eb45befb367ab59a0baa7738adf49e","cipherparams":{"iv":"54bbb6e77719a13c3fc2072bb88a708c"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"50c31e2a99f231b09201494cac1cf0943246edcc6864a91cc931563cd11eb0ce","n":1024,"r":8,"p":1},"mac":"13d3566174d20d93d2fb447167c21a127190d4b9b4843fe7cbebeb6054639a4f"}}";
const password = "1111111";
const account = fromKeystore(keystore,password);Get the keyAccount using the privateKey
- privateKey | hex string | Buffer
- KeyAccount | account
const privateKey = "07ae9ee56203d29171ce3de536d7742e0af4df5b7f62d298a0445d11e466bf9e";
const account = fromPrivateKey(privateKey);Generate a set of 24 English word mnemonics
- mnemonic string
const mnemonic = createMnemonic();
// mnemonic: "seven museum glove patrol gain dumb dawn bridge task alone lion check interest hair scare cash sentence diary better kingdom remember nerve sunset move"Get the specified address using the publicKey, chainCode, and pageSize number.
- publicKey | hex string | Buffer
- chainCode | hex string | Buffer
- pageSize | number
- pageNum | number
- Address[pageSize]
const publicKey = "029a29b250b48fb317b81717d405f8fcf54a6bcd25d5a4f9e446db01d14c84bf9d";
const chainCode = "9bc5b6e72a92ba96f97f7b16296268c1c5b06a1ddaa22a4d821986a06b2ae16e";
const pageSize = 5;
const pageNum=0;
const addresses = getAddresses({publicKey, chainCode, pageSize, pageNum});
//addresses:["0xc57bb1cd690e7483ee2d2b4ecee060145a33fa3c","0x7d0749db3013f6e4c949d7810630aebe0e2e8756","0x7a9bbf63e8cc2bd7dba83884b6ed2d1e2e764409","0xef803e1d485fe9ae9ea6308d5c5d874cc011ac9a","0xfdffa0f9d72f7766b204bb8d6166990548419d96"]This account type implements the sendTransaction method.
Send Ethereum transactions
- host url
- signTx, which is the serialized signature on an Ethereum transaction
- txHash
const host = 'localhost:8545';
const signTx="0xd46e8dd67c5d32be8d46e8dd67c5d32be8058bb8eb970870f072445675058bb8eb970870f072445675";
const response = await sendTransaction(host,{signTx});
//response
{
"id":1,
"jsonrpc": "2.0",
"result": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331"
}The class, which extends the Account class, implements the toV3Keystore, getPublicKey, getAddress, sign, signMessage, signEthereumTx, and signOrder methods based on the Account.
- privateKey | hex string | Buffer
const privateKey = '07ae9ee56203d29171ce3de536d7742e0af4df5b7f62d298a0445d11e466bf9e';
const account = new KeyAccount(privateKey);Get the address of the account
- address
account.getAddress();//address:0x48ff2269e58a373120FFdBBdEE3FBceA854AC30AGet the publicKey of the account
- publicKey | hex string
const publicKey = account.getPublicKey();
//publicKey:"0895b915149d15148ac4171f453060d6b53a9ebb694689351df8e3a49c109c7a65374b5c196ce8cc35ff79cb3ce54ea1695704dd5b3cfc6864bd110b62cfd509"The Json Keystore is converted to v3
- password | string
- keystore | object
const keystore = account.toV3Keystore('123456789');
//keystore:{ version: 3,
id: '2f76ed18-76dd-4186-90c6-c95c71fcff09',
address: '48ff2269e58a373120ffdbbdee3fbcea854ac30a',
crypto:
{ ciphertext: 'ad086c4b4bed193ae4ed2103160829c5c64027b2258110bae86d78be18905463',
cipherparams: { iv: '32dd303feab25da5e612ffa3676c946f' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams:
{ dklen: 32,
salt: '765aa6c750c0a511da0184e9914484a7293a63f5e5817b57ce9bef8ef559b8df',
n: 262144,
r: 8,
p: 1 },
mac: '33fb274ba8eb91674f0e5957e86784358cf65d9593c4b1e55333299a94249565' } }Sign the hash
- hash | Buffer
- sig {r, s, v}
- r | hex string
- s | hex string
- v | number
const hash = toBuffer('loopring');
const sig = account.sign(hash);
/sig :{ r: '0x87b7472c6116f5fd931b05f7b57c8b87192eb006f0e1376d997e68629d66bde7',
s: '0x501f9eb08cde1a241595afd91e14d53568365d0124b72cc18fefd2b8ea1223ac',
v: 28 }Signing the Message automatically adds Ethereum-defined prefix information: ("\x19Ethereum Signed Message:\n" + len(keccak256(message)).
- Message | string | Buffer
- sig {r, s, v}
- r | hex string
- s | hex string
- v | number
const message = 'loopring';
const sig = account.signMessage(messsage);
//sig : { r: '0x83a812a468e90106038ba4f409b2702d14e373c40ad377c92935c61d09f12e53',
s: '0x666425e6e769c3bf4378408488cd920aeda964d7995dac748529dab396cbaca4',
v: 28 }Sign the Ethereum transaction to obtain Tx in the form of serialized hexadecimal characters.
- rawTx
- chainId | number - For example, the chainId of Ethereum is 1
- nonce | hex string | Buffer
- value | hex string | Buffer
- data | hex string | Buffer
- gasPrice | hex string | Buffer
- gasLimit | hex string | Buffer
- to | address | Buffer
- tx | hex string, serialized tx
const rawTx = {
"gasPrice": "0x4e3b29200",
"gasLimit": "0x15f90",
"to": "0x88699e7fee2da0462981a08a15a3b940304cc516",
"value": "0x56bc75e2d63100000",
"data": "",
"chainId": 1,
"nonce": "0x9b"
};
const tx = account.signEthereumTx(rawTx)
//tx:0xf86f819b8504e3b2920083015f909488699e7fee2da0462981a08a15a3b940304cc51689056bc75e2d631000008025a0d75c34cf2236bf632126f10d9ee8e963bf94623f8ec2dedb59c6d13342dbe3bea0644afdfa9812f494eee21adafc1b268c5b88bc47905880577876a8a293bd9c66Sign the Loopring order, and return the signed order back
- order
- protocol | address, here is an example version 1.5.1 protocol address: 0x8d8812b72d1e4ffCeC158D25f56748b7d67c1e78
- delegate | address, the Loopring protocol authorization address, here is an example version 1.5.1 address: 0x17233e07c67d086464fD408148c3ABB56245FA64
- owner | address, the address of the ordering user
- tokenS | address, the contract address that is selling currency
- tokenB | address, the contract address that is buying currency
- authAddr | address, randomly generated account address
- authPriavteKey | privatekey, randomly generated privatekey corresponding to the account
- validSince | hex string, the order effective time, timestamp in seconds
- validUntil | hex string, the order expiration time, timestamp in seconds
- amountB | hex string, the amount of tokenB to buy (here in units of the smallest unit)
- amountS | hex string, the amount of tokenS to sell (here in units of the smallest unit)
- walletAddress | address, address of the wallet that receives the tokens from an order
- buyNoMoreThanAmountB | bool, true if amountB is greater than tokenB
- lrcFee | hex string, orders fully match the maximum amount of fees that need to be paid. (Here the unit of LRC is the smallest unit)
- marginSplitPercentage | number(0–100), the proportion of funds used to pay for the reconciliation
- signedOrder, add the order signature r, s, v to the original order
const order = {
"delegateAddress": "0x17233e07c67d086464fD408148c3ABB56245FA64",
"protocol": "0x8d8812b72d1e4ffCeC158D25f56748b7d67c1e78",
"owner": "0xb94065482ad64d4c2b9252358d746b39e820a582",
"tokenB": "0xEF68e7C694F40c8202821eDF525dE3782458639f",
"tokenS": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"amountB": "0x15af1d78b58c400000",
"amountS": "0x4fefa17b7240000",
"lrcFee": "0xa8c0ff92d4c0000",
"validSince": "0x5af6ce85",
"validUntil": "0x5af82005",
"marginSplitPercentage": 50,
"buyNoMoreThanAmountB": true,
"walletAddress": "0xb94065482ad64d4c2b9252358d746b39e820a582",
"authAddr": "0xf65bf0b63cf812ab1a979a8e54c070674a849344",
"authPrivateKey": "95f373ce0c34872db600017d506b90f7fbbb6433496640228cc7a8e00f27b23e"
};
const signedOrder = account.signOrder(order);
//signedOrder:{
"delegateAddress": "0x17233e07c67d086464fD408148c3ABB56245FA64",
"protocol": "0x8d8812b72d1e4ffCeC158D25f56748b7d67c1e78",
"owner": "0xb94065482ad64d4c2b9252358d746b39e820a582",
"tokenB": "0xEF68e7C694F40c8202821eDF525dE3782458639f",
"tokenS": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"amountB": "0x15af1d78b58c400000",
"amountS": "0x4fefa17b7240000",
"lrcFee": "0xa8c0ff92d4c0000",
"validSince": "0x5af6ce85",
"validUntil": "0x5af82005",
"marginSplitPercentage": 50,
"buyNoMoreThanAmountB": true,
"walletAddress": "0xb94065482ad64d4c2b9252358d746b39e820a582",
"authAddr": "0xf65bf0b63cf812ab1a979a8e54c070674a849344",
"authPrivateKey": "95f373ce0c34872db600017d506b90f7fbbb6433496640228cc7a8e00f27b23e",
"v": 27,
"r": "0xb657f82ee339555e622fc60fefd4089c40057bdb6d4976b19de2a88177129ed4",
"s": "0x0d4ac4e1fbc05421f59b365f53c229a4b3cb9d75b4e53b7f3f0ffe3cdb85dfde"
}The class, connecting to the TREZOR account, extends the Account class, and implements getAddress, signMessage, and signEthereumTx. TREZOR's signMessage method is different from other account signatures and can only be verified by TREZOR itself. Therefore, TREZOR does not support the signing of a Loopring order, which results in TREZOR users being unable to place orders through TREZOR unless they were unlocked in Loopr by TREZOR mnemonics and placed orders.
- dpath | string
- TrezorAccount | account
const dpath = "m/44'/60'/0'/0/0";
const account = new TrezorAccount(dpath);Get account address
- address | address
const address = await account.getAddress();
//address:0x48ff2269e58a373120FFdBBdEE3FBceA854AC30ASigning messages can only be verified by TREZOR
- message | string
- sig
- r | hex string
- s | hex string
- v | number
const message = 'loopring';
const sig = account.signMessage(message);
//sig:{
r:"0xcd2d7bb6ca215d4f7faf637da0db43d2ff6d2be095db0961c94b1e5f364dedc4",
s:"0x42d5d8a55dc56e06dee07fbea65949092dd4b98a928de426d60c55d16e045141",
v:28
}See KeyAccount.signEthereumTx
See KeyAccount.signEthereumTx
See KeyAccount.signEthereumTx
See KeyAccount.signEthereumTx
The class connects the Ledger to the account. The account is expanded to implement the getAddress, signMessage, signEthereumTx, and signOrder.
- ledger | Ledger, connection example
- dpath | string
- LedgerAccount | account
Get the account address
- address
const address = await account.getAddress();
//address:0x48ff2269e58a373120FFdBBdEE3FBceA854AC30ASigning the message automatically adds Ethereum-defined prefix information ("\x19Ethereum Signed Message:\n" + len(keccak256(message)).
- message | string
- sig {r, s, v}
- r | hex string
- s | hex string
- v | number
const message = 'loopring';
const sig = account.signMessage(messsage);
//sig : { r: '0x83a812a468e90106038ba4f409b2702d14e373c40ad377c92935c61d09f12e53',
s: '0x666425e6e769c3bf4378408488cd920aeda964d7995dac748529dab396cbaca4',
v: 28 }See KeyAccount.signEthereumTx
- rawTx
- chainId | number - for example, the chainId of the Ethereum main network is 1.
- nonce | hex string
- value | hex string
- data | hex string
- gasPrice | hex string
- gasLimit | hex string
- to address | Buffer
See KeyAccount.signEthereumTx
Reference KeyAccount.signEthereumTx
Reference KeyAccount.signOrder
See KeyAccount.signOrder
Reference KeyAccount.signOrder
Reference KeyAccount.signOrder
This is an account class that connects the MetaMask wallet to your account. It extends the Account by implementing getAddress, sign, signMessage, signEthereumTx, and signOrder.
- web3
- account MetaMaskAccount
getAddress, sign(hash), signMessage(message), signEthereumTx(rawTx), signOrder(order) See the corresponding method of KeyAccount
Decrypt the private key using the keystore and password
- keystore string
- password string
- privatekey Buffer
const keystore = "{"version":3,"id":"e603b01d-9aa9-4ddf-a165-1b21630237a5","address":"2131b0816b3ef8fe2d120e32b20d15c866e6b4c1","Crypto":{"ciphertext":"7e0c30a985cf29493486acaf86259a2cb0eb45befb367ab59a0baa7738adf49e","cipherparams":{"iv":"54bbb6e77719a13c3fc2072bb88a708c"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"50c31e2a99f231b09201494cac1cf0943246edcc6864a91cc931563cd11eb0ce","n":1024,"r":8,"p":1},"mac":"13d3566174d20d93d2fb447167c21a127190d4b9b4843fe7cbebeb6054639a4f"}}";
const password = "1111111";
const privatekey = decryptKeystoreToPkey(keystore,password);Keystore obtained using the privatekey and password
- privateKey Buffer
- password string
- keystore Object
const privateKey = toBuffer('0x07ae9ee56203d29171ce3de536d7742e0af4df5b7f62d298a0445d11e466bf9e');
const password = "1111111";
const keystore = pkeyToKeystore(privateKey,password);
//keystore:{ version: 3,
id: '2f76ed18-76dd-4186-90c6-c95c71fcff09',
address: '48ff2269e58a373120ffdbbdee3fbcea854ac30a',
crypto:
{ ciphertext: 'ad086c4b4bed193ae4ed2103160829c5c64027b2258110bae86d78be18905463',
cipherparams: { iv: '32dd303feab25da5e612ffa3676c946f' },
cipher: 'aes-128-ctr',
kdf: 'scrypt',
kdfparams:
{ dklen: 32,
salt: '765aa6c750c0a511da0184e9914484a7293a63f5e5817b57ce9bef8ef559b8df',
n: 262144,
r: 8,
p: 1 },
mac: '33fb274ba8eb91674f0e5957e86784358cf65d9593c4b1e55333299a94249565' } }Decrypt the utc-type keystore using the keystore and password to get the privatekey
- keystore string
- password string
- privatekey Buffer
See decryptKeystoreToPkey
Analyze the keystore to get the keystore type
- keystore string
- type string
const keystore = "{"version":3,"id":"e603b01d-9aa9-4ddf-a165-1b21630237a5","address":"2131b0816b3ef8fe2d120e32b20d15c866e6b4c1","Crypto":{"ciphertext":"7e0c30a985cf29493486acaf86259a2cb0eb45befb367ab59a0baa7738adf49e","cipherparams":{"iv":"54bbb6e77719a13c3fc2072bb88a708c"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"50c31e2a99f231b09201494cac1cf0943246edcc6864a91cc931563cd11eb0ce","n":1024,"r":8,"p":1},"mac":"13d3566174d20d93d2fb447167c21a127190d4b9b4843fe7cbebeb6054639a4f"}}";
const type = determineKeystoreType(keystore);
//type:v2-v3-utcDecrypt the presale keystore type using the keystore and password
- keystore string
- password string
- privatekey Buffer
See decryptKeystoreToPkey
Decrypt the v-1-encrypted privatekey using the keystore and password
- keystore string
- password string
- privatekey Buffer
See decryptKeystoreToPkey
By analyzing the keystore, we determine if the keystore unlocks need a password
- keystone string
- isPasswordRequired bool
const keystore = "{"version":3,"id":"e603b01d-9aa9-4ddf-a165-1b21630237a5","address":"2131b0816b3ef8fe2d120e32b20d15c866e6b4c1","Crypto":{"ciphertext":"7e0c30a985cf29493486acaf86259a2cb0eb45befb367ab59a0baa7738adf49e","cipherparams":{"iv":"54bbb6e77719a13c3fc2072bb88a708c"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"dklen":32,"salt":"50c31e2a99f231b09201494cac1cf0943246edcc6864a91cc931563cd11eb0ce","n":1024,"r":8,"p":1},"mac":"13d3566174d20d93d2fb447167c21a127190d4b9b4843fe7cbebeb6054639a4f"}}";
const isPasswordRequired = isKeystorePassRequired(keystore);
// trueGet the keystore file name of the V3 specification
- address Address
- fileName string
const address = "0x48ff2269e58a373120FFdBBdEE3FBceA854AC30A";
const fileName = getFileName(address);
//fileName:"UTC--2018-03-07T07-03-45.764Z--48ff2269e58a373120ffdbbdee3fbcea854ac30a.json"Connect Ledger wallet to get a connection.
const response = await connect();
if(!response.error){
const ledger = response.result
}Get the publicKey and chainCode of the specified dpath
- dpath string
- ledgerConnect Ledger, real connection example
- response
- publicKey hex string
- chainCode hex string
const dpath = "m/44'/60'/0'/0";
const response = await getXPubKey(dpath,ledger)
if(!response.error){
const result = response.result
}
//result:{ "chainCode":"e755d0397636c4ab0a2c8aaf3b624f668d7c01de7afab2628fe498adf1d38c4c",
"publicKey": "027b9dca5697352f06f9f696757c5849460fd030322e086419467f9ccf146520bd",
}Sign the specified message
- dpath string
- message string
- ledgerConnnect Ledger, connection example
- sig
- r | hex string
- s | hex string
- v | number
const dpath = "m/44'/60'/0'/0";
const message = 'loopring';
const sig = await signMessage(dpath,message,ledger);Signing the specified rawTx
- dpath string
- rawTx object
- chainId number - For example, the chainId of Ethereum main network is 1.
- nonce hex string
- value hex string
- data hex string
- gasPrice hex string
- gasLimit hex string
- to address
Reference KeyAccount.signEthereumTx(rawTx)
const dpath = "m/44'/60'/0'/0";
const rawTx = {
"gasPrice": "0x4e3b29200",
"gasLimit": "0x15f90",
"to": "0x88699e7fee2da0462981a08a15a3b940304cc516",
"value": "0x56bc75e2d63100000",
"data": "",
"chainId": 1,
"nonce": "0x9b"
};
const response = await signEthereumTx(dpath,rawTx,ledger);
if(!response.error){
const signedTx = response.result;
}Using MetaMask, the hash is signed with the specified account. MetaMask signs the hash and does not add Ethereum header information.
- web3
- account | address
- hash | string
- sig
- r | hex string
- s | hex string
- v | number
const web3 = window.web3 // MetaMask
const message = 'loopring';
const account = '0x48ff2269e58a373120FFdBBdEE3FBceA854AC30A';
const sig = await sign(web3,account,toBuffer(message))Signing the Message automatically adds Ethereum-defined prefix information ("\x19Ethereum Signed Message:\n" + len(keccak256(message)).
-
web3
-
account | address
-
hash | string
- sig
- r | hex string
- s | hex string
- v | number
const message = 'loopring';
const web3 = window.web3
const account = '0x48ff2269e58a373120FFdBBdEE3FBceA854AC30A';
const sig = await signMessage(web3,account,message)Sign Ethereum tx, returning signed serialized tx
- web3
- account | address
- rawTx | object
- chainId | number - for example, the chainId of the Ethereum main network is 1.
- nonce | hex string
- value | hex string
- data | hex string
- gasPrice | hex string
- gasLimit | hex string
- to | address
Reference KeyAccount.signEthereumTx(rawTx)
const web3 = window.web3
const account = "0x48ff2269e58a373120FFdBBdEE3FBceA854AC30A";
const rawTx = {
"gasPrice": "0x4e3b29200",
"gasLimit": "0x15f90",
"to": "0x88699e7fee2da0462981a08a15a3b940304cc516",
"value": "0x56bc75e2d63100000",
"data": "",
"chainId": 1,
"nonce": "0x9b"
};
const response = await signEthereumTx(web3,account,rawTx)
if(!response.error){
const signedTx = response.result;
}Send Ethereum Trading via MetaMask
- web3
- tx
- nonce | hex string
- value | hex string
- data | hex string
- gasPrice | hex string
- gasLimit | hex string
- to | address
- txHash | hex string
const rawTx = {
"gasPrice": "0x4e3b29200",
"gasLimit": "0x15f90",
"to": "0x88699e7fee2da0462981a08a15a3b940304cc516",
"value": "0x56bc75e2d63100000",
"data": "",
"nonce": "0x9b"
};
const web3 = window.web3
const response = await sendTransaction(web3,rawTx);
if(!response.error){
const txHash = response.result;
}Decrypt mnemonics to get private keys
- mnemonic | string
- dpath | string
- password | string (optional)
- privateKey | Buffer
const mnemonic = "seven museum glove patrol gain dumb dawn bridge task alone lion check interest hair scare cash sentence diary better kingdom remember nerve sunset move";
const dpath = "m/44'/60'/0'/0/0";
const privateKey = mnemonictoPrivatekey(mnemonic,dpath);Determine the validity of mnemonic
- mnemonic | string
- isValid | bool
const menmonic = "seven museum glove patrol gain dumb dawn bridge task alone lion check interest hair scare cash sentence diary better kingdom remember nerve sunset move";
const isValid = isValidateMnemonic(mnemonic);
//isValid trueGet the address through the specified dpath
Reference ledger.getAddress
Reference ledger.getAddress
Reference ledger.getAddress
Specify the account using dpath and sign the message. The header information is added to the signature, but it is different from Ethereum's rules, because TREZOR's signature information can only be verified by TREZOR.
- dpath | string
- message | string
- sig
- r | hex string
- s | hex string
- v | number
const message = 'loopring';
const dpath = "m/44'/60'/0'/0/0";
const response = await signMessage(dpath,message);
if(!response.error){
const sig = response.result;
}
//sig:{
r:"0xcd2d7bb6ca215d4f7faf637da0db43d2ff6d2be095db0961c94b1e5f364dedc4",
s:"0x42d5d8a55dc56e06dee07fbea65949092dd4b98a928de426d60c55d16e045141",
v:28
}Specify the account using dpath and sign the rawTx.
- dpath | string
- rawTx
- nonce | hex string
- value | hex string
- data | hex string
- gasPrice | hex string
- gasLimit | hex string
- to | address
- chainId | number
- signTx | hex string
const rawTx = {
"gasPrice": "0x4e3b29200",
"gasLimit": "0x15f90",
"to": "0x88699e7fee2da0462981a08a15a3b940304cc516",
"value": "0x56bc75e2d63100000",
"data": "",
"chainId": 1,
"nonce": "0x9b"
};
const dpath = "m/44'/60'/0'/0/0";
const response = await signEthereumTx(dpath,rawTx)
if(!response.error){
const tx = response.tx
}
//tx:0xf86f819b8504e3b2920083015f909488699e7fee2da0462981a08a15a3b940304cc51689056bc75e2d631000008025a0d75c34cf2236bf632126f10d9ee8e963bf94623f8ec2dedb59c6d13342dbe3bea0644afdfa9812f494eee21adafc1b268c5b88bc47905880577876a8a293bd9c66PublicKey and chainCode of the account that obtained the specified dpath
- dpath | string
-
publicKey
-
chainCode
const dpath = "m/44'/60'/0'/0";
const response = await getXPubKey(dpath);
if(!response.error){
const result = response.result;
}
// {publicKey:"029a29b250b48fb317b81717d405f8fcf54a6bcd25d5a4f9e446db01d14c84bf9d",
chainCode:"9bc5b6e72a92ba96f97f7b16296268c1c5b06a1ddaa22a4d821986a06b2ae16e"};According to Ethereum abi, the contract method is implemented.
This function implements the encoded abi method, which is the decoding of the abi output data, and the decoding of the abi input data.
- abiMethod object
- inputs | Array, a list of incoming parameters
- name | string, the parameter name
- type, the parameter type
- name | string, the abi method name
- outputs | Array abi method value list
- inputs | Array, a list of incoming parameters
const abiMethod = {
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
};
const abiFunction = new AbiFunction(abiMethod);Encode the input data
Parameters
- inputs | object, abi method corresponding parameter. The key of the inputs is the name of the abi method.
- data | hex string (methodId + parameters), corresponds to the Ethereum TX data
const _to = "0x88699e7fee2da0462981a08a15a3b940304cc516";
const _value = "0xde0b6b3a7640000";
const data = abiFunction.encodeInputs({_to,_value});
//data: "0xa9059cbb000000000000000000000000d91a7cb8efc59f485e999f02019bf2947b15ee1d0000000000000000000000000000000000000000000008ac7230489e80000";Decode input parameters that have been encoded
encodedInputs | hex string
- inputs | Array
const data = "0x00000000000000000000000088699e7fee2da0462981a08a15a3b940304cc5160000000000000000000000000000000000000000000000de0b6b3a7640000";
const inputs = abiFunction.decodeEncodedInputs(data);
//inputs ['88699e7fee2da0462981a08a15a3b940304cc516','0xde0b6b3a7640000']decode abi method and return output values
- data | hex string
- outputs | Array
const abiMethod = {
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
};
const abiFunction = new AbiFunctions(abiMethod);
const data = '0x000000000000000000000000000000000000000000e6cbc4f6ec6156401801fc';
const outputs = abiFunction.decodeOutputs(data);
//outputs:['0xe6cbc4f6ec6156401801fc']● abi contract abi
const abi = [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}];
const contract = new Contract(abi);This method specifies that the inputs must be encoded
- Method | string, if there is no method with the same methodName in the abi, it can be called methodName. Otherwise, the argument will pass on this type of methodName + methodId.
- data | hex string
const method='transfer' //(transfer(address,uint256) | '0xa9059cbb')
const _to = "0x88699e7fee2da0462981a08a15a3b940304cc516";
const _value = "0xde0b6b3a7640000";
const data = contract.encodeInputs(method,{_to,_value})
//data:"0xa9059cbb000000000000000000000000d91a7cb8efc59f485e999f02019bf2947b15ee1d0000000000000000000000000000000000000000000008ac7230489e80000"Decode the encoded input parameter data of the specified method.
- data | hex string (methodId + parameters)
- inputs | Array
const data = "0xa9059cbb000000000000000000000000d91a7cb8efc59f485e999f02019bf2947b15ee1d0000000000000000000000000000000000000000000008ac7230489e80000";
const inputs = contract.decodeEncodeInputs(data);
//inputs:['88699e7fee2da0462981a08a15a3b940304cc516','0xde0b6b3a7640000']Decode the output data of the specified method
- Method | string, if there is no method with the same methodName in the abi, it can be called methodName. Otherwise, you should pass the methodName + inputs type or methodId.
- outputs | Array
const method = 'balanceOf'
const data = '0x000000000000000000000000000000000000000000e6cbc4f6ec6156401801fc';
const outputs = contract.decodeOutputs(method, data);
//outputs:['0xe6cbc4f6ec6156401801fc']Multiple access to Loopring protocol’s commonly used contracts. Including ERC-20, WETH, AirdropContract, and Loopring Protocol.
LoopringProtocol captures the encodECancelOrder and the encodeSubmitRing
There is a specified number of orders that can be assigned at once. If the amount exceeds the order availability, the order is marked as complete cancellation.
-
signedOrder
- protocol | address, here is an example version 1.5.1 protocol address: 0x8d8812b72d1e4ffCeC158D25f56748b7d67c1e78
- delegate | address, the Loopring protocol authorization address, here is an example version 1.5.1 address: 0x17233e07c67d086464fD408148c3ABB56245FA64
- owner | address, the address of the ordering user
- tokenS | address, the contract address that is selling currency
- tokenB | address, the contract address that is buying currency
- authAddr | address, randomly generated account address
- authPriavteKey | privatekey, randomly generated privatekey corresponding to the account
- validSince | hex string, the order effective time, timestamp in seconds
- validUntil | hex string, the order expiration time, timestamp in seconds
- amountB | hex string, the amount of tokenB to buy (here in units of the smallest unit)
- amountS | hex string, the amount of tokenS to sell (here in units of the smallest unit)
- walletAddress | address, address of the wallet that receives the tokens from an order
- buyNoMoreThanAmountB | bool, true if amountB is greater than tokenB
- lrcFee | hex string, orders fully match the maximum amount of fees that need to be paid. (Here the unit of LRC is the smallest unit)
- marginSplitPercentage | number(0–100), the proportion of funds used to pay for the reconciliation
- r | hex string
- s | hex string
- v | number
- powNonce | number, a random number that satisfies the degree of difficulty
-
amount | number, the quantity to be cancelled is defaulted to the full quantity of the order
- data | hex string
const signedOrder = {
"delegateAddress": "0x17233e07c67d086464fD408148c3ABB56245FA64",
"protocol": "0x8d8812b72d1e4ffCeC158D25f56748b7d67c1e78",
"owner": "0xb94065482ad64d4c2b9252358d746b39e820a582",
"tokenB": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"tokenS": "0xEF68e7C694F40c8202821eDF525dE3782458639f",
"amountB": "0x429d069189e0000",
"amountS": "0xad78ebc5ac6200000",
"lrcFee": "0x928ca80cfc20000",
"validSince": "0x5b038122",
"validUntil": "0x5b04d2a2",
"marginSplitPercentage": 50,
"buyNoMoreThanAmountB": false,
"walletAddress": "0xb94065482ad64d4c2b9252358d746b39e820a582",
"authAddr": "0x5b98dac691be2f2882bfb79067ee50c221d20203",
"authPrivateKey": "89fb80ba23d355686ff0b2093100af2d6a2ec071fe98c33252878caeab738e37",
"v": 28,
"r": "0xbdf3c5bdeeadbddc0995d7fb51471e2166774c8ad5ed9cc315635985c190e573",
"s": "0x4ab135ff654c3f5e87183865175b6180e342565525eefc56bf2a0d5d5c564a73",
"powNonce": 100
};
const data = LoopringProtocol.encodeCancelOrder(signedOrder);
//data :
"0x8c59f7ca000000000000000000000000b94065482ad64d4c2b9252358d746b39e820a582000000000000000000000000ef68e7c694f40c8202821edf525de3782458639f000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000b94065482ad64d4c2b9252358d746b39e820a5820000000000000000000000005b98dac691be2f2882bfb79067ee50c221d2020300000000000000000000000000000000000000000000000ad78ebc5ac62000000000000000000000000000000000000000000000000000000429d069189e0000000000000000000000000000000000000000000000000000000000005b038122000000000000000000000000000000000000000000000000000000005b04d2a20000000000000000000000000000000000000000000000000928ca80cfc2000000000000000000000000000000000000000000000000000ad78ebc5ac620000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032000000000000000000000000000000000000000000000000000000000000001cbdf3c5bdeeadbddc0995d7fb51471e2166774c8ad5ed9cc315635985c190e5734ab135ff654c3f5e87183865175b6180e342565525eefc56bf2a0d5d5c564a73"-
orders | order
-
feeRecipient | address
-
feeSelections 0 |1 (0 means select sub-run, 1 means select lrcFee, the default is 1)
- data | hex string
Implement part of the Ethereum jsonrpc interface
- host | string
const host = 'localhost:8545';
const ethNode = new Eth(host);Get the transactionCount at the specified address
For details, reference: Ethereum Jsonrpc eth_getTransactionCount interface
Send signature transactions to Ethereum nodes
For details, reference: Ethereum JSON-RPC eth_sendRawTransaction interface
Get the average gas price of the Ethereum network
For details, reference: Ethereum JSON-RPC eth_gasPrice interface
Get the Ethereum balance for the specified address
For details, reference: Ethereum JSON-RPC eth_getBalance interface
Get the transaction details for the specified hash
For details, reference: Ethereum JSON-RPC eth_getTransactionByHash interface
Simulate a tx
For details, reference: Ethereum JSON-RPC eth_call interface
Implement Loopring Relay's JSON-RPC interface and Socket interface. See the Loopring Relay interface for more details. Loopring Relay Documents
- host Loopring Relay host
Implement the relevant JSON-RPC interface of the Loopring Relay.
Get the account balance of the specified owner and the authorized value of the Loopring address delegateAddress.
For details, reference: Loopring Relay getBalance interface
const owner = "0x88699e7fee2da0462981a08a15a3b940304cc516";
const delegataAddress = "0x17233e07c67d086464fD408148c3ABB56245FA64";
const response = relay.account.getBalance({owner,delegataAddress});Registers the specified owner address to therelay. After registration, relay will analyze the Ethereum txs that stores the address.
For details, reference: Loopring Relay register interface
Informs the Ethereum tx that the Relay has sent. The Relay will track the state of tx.
For details, reference: Loopring Relay notifyTransactionSubmitted interface
Get the Ethereum txs for the specified owner
For details, reference: Loopring Relay getTransactions interface
The sum of the LRC Fee required to obtain a valid order for the specified Owner.
For details, reference: Loopring Relay getFrozenLrcFee interface
Get the specified owner address of the portfolio
For details, reference: Loopring Relay getPortfolio interface
Implement Loopring Relay Market related JSON-RPC interface
Get the price of the specified Currency for all tokens supported by the Relay
For details, reference: Loopring Relay getPriceQuote interface
Get all the markets supported by the Relay
For details, reference: Loopring Relay getSupportedMarket inteface
Get all tokens supported by the Relay
For details, reference: Loopring Relay getSupportedTokens interface
Get the depth of the market
For details, reference: Loopring Relay getDepth interface
Get 24-hour combined trade statistics for all Loopring markets
For details, reference: Loopring Relay getTicker interface
Get statistics on the 24-hour merger of multiple exchanges destined for the market
For details, reference: Loopring Relay getTickers
Obtain trend information such as price changes for specific markets at multiple specified exchanges
For details, reference: Loopring Relay getTrend interface
Implement Loopring Relay
Get a Loopring order list
For details, reference: Loopring Relay getOrders interface
Get the destined address and the cutoff timestamp of the delegateAddress of the corresponding loopring. Orders before the corresponding cutoff time will be cancelled.
For details, reference: Loopring Relay getCutoff interface
Submit Order to the Loopring Relay
For details, reference: Loopring Relay placeOrder interface
Calculate orderHash
Implementing the Loopring Relay JSON-RPC interface
Get a ring that has been matched
For details, reference: Loopring Relay getRings interface
Get ring details
For details, reference: Loopring Relay getRingMinedDetail interface
Get order match history
For details, reference: Loopring Relay getFills interface
The Loopring Relay implements Web Sockets using socket.io. See the Loopring Relay socket event list for details: Loopring Relay socket interface
Connect to the socket server for the specified url. For details, reference: socket.io_client api documents
-
url string
-
options object (Optional)
const url = 'ws://relay.loopring'
const options= {transports: ['websocket']};
const socket = new Socket(url,options)Send a message to the relay, then start monitoring the specified event or update the conditions of the specified event
-
event string
-
options string(json) event, the parameters
const event = 'portfolio_req';
const options = '{"owner" : "0x847983c3a34afa192cfee860698584c030f4c9db1"}';
socket.emit(event,options)Focus on the data of the specified event
- event string
- handle function, processing data returned
const event = "portfolio_res"
const handle = (data)=> {console.log(data)}
socket.on(event,handle);Manually disconnect the socket connection
socket.close()Loopring is the protocol for decentralized token exchange.