npm install
node src/test.js
- web3
- [email protected]
-
Deploy loopring protocol2 to an ethereum testnet or private net. The protocol was already deployed to Kovan: https://github.com/Loopring/protocol2/blob/master/deployment-kovan.md
-
While developing, use a local testnet using ganache:
git clone https://github.com/Loopring/protocol2
cd protocol2
npm install
npm run ganache
- Open another terminal window, and run:
npm run migrate
. This will deploy the loopring protocol in the ganache node and will create a file calleddeployedAddresses.json
. - Copy
deployedAddresses.json
to your project, you will need to use it later.
All sample code below is in src/test.js
.
For all type definitions (orders, rings,...) see https://github.com/Loopring/protocol2-js/blob/master/src/types.ts For now we only support signing with EIP-712. Make sure to submit rings from the same address as the miner.
const order1 = {
tokenS: "WETH",
tokenB: "LRC",
amountS: 1e18,
amountB: 1000e18,
feeToken: LrcAddress,
feeAmount: 2e18,
signAlgorithm: pjs.SignAlgorithm.EIP712,
};
const order2 = {
tokenS: "LRC",
tokenB: "WETH",
amountS: 1000e18,
amountB: 1e18,
feeToken: LrcAddress,
feeAmount: 3e18,
signAlgorithm: pjs.SignAlgorithm.EIP712,
};
const ringsInfo = {
transactionOrigin: miner,
miner: miner,
rings: [[0, 1]],
orders: [order1, order2],
};
const ringsGenerator = new pjs.RingsGenerator(context);
await ringsGenerator.setupRingsAsync(ringsInfo); // sign orders, and ringsInfo.
const bs = ringsGenerator.toSubmitableParam(ringsInfo);
const submitter = new web3.eth.Contract(JSON.parse(submitterABI), ringSubmitterAddress);
const txData = submitter.methods.simulateAndReport(web3.utils.hexToBytes(bs), {from: ringsInfo.transactionOrigin}).encodeABI();
await sendTransaction(miner, ringSubmitterAddress, 0, txData);
watchAndPrintEvent(submitter, "RingMined");
If you want to know more about how loopring protocol2 works, run the unit tests in loopring protocol2 project:
clone protocol2
npm install
npm run ganache
npm run compile
npm run transpile
npm run test -- transpiled/test/testSubmitRingsSimple.js -x // run single test.