How to Integrate with the Loopring Blockchain with Magic
How to Integrate with the Loopring Blockchain with Magic
#Installation
Magic interacts with the Loopring blockchain via Magic-js SDK.
note
You can skip straight to our GitHub example directly:
Via Magic SDK, you can interact with Loopring. Loopring (formerly Matic) is EVM compatible so you can directly follow the Ethereum installation and Ethereum Network Configuration.
You also need to install looping-idk.
#NPM
Bash
01npm install --save magic-sdk @loopring-web/loopring-sdk
#Yarn
Bash
01yarn add magic-sdk @loopring-web/loopring-sdk
#Send Transaction
#ES Modules/TypeScript
Typescript
01import { Magic } from "magic-sdk";
02import * as sdk from '@loopring-web/loopring-sdk';
03import {LOOPRING_EXPORTED_ACCOUNT, LoopringAPI, signatureKeyPairMock, TOKEN_INFO, web3} from "./Loopring";
04
05const customNodeOptions = {
06 // Your own ethereum node URL
07 rpcUrl: 'https://goerli.infura.io/v3/a06ed9c6b5424b61beafff27ecc3abf3',
08 chainId: 5, // chainId
09};
10
11const magic = new Magic("YOUR API KEY", {network: customNodeOptions});
12
13const web3 = new Web3(magic.rpcProvider)
14
15// Step1. get account info payerAddress should be magic wallet public address
16const { accInfo } = await LoopringAPI.exchangeAPI.getAccount({owner: payerAddress});
17console.log("accInfo:", accInfo);
18
19// Step 2. eddsaKey
20const eddsaKey = await signatureKeyPairMock(accInfo, web3);
21console.log("eddsaKey:", eddsaKey.sk);
22// Step 3. get apikey
23const { apiKey } = await LoopringAPI.userAPI.getUserApiKey({
24 accountId: accInfo.accountId,
25 },
26 eddsaKey.sk);
27console.log("apiKey:", apiKey);
28
29// Step 4. get storageId
30const storageId = await LoopringAPI.userAPI.getNextStorageId({
31 accountId: accInfo.accountId,
32 sellTokenId: TOKEN_INFO.tokenMap["LRC"].tokenId
33 },
34 apiKey);
35console.log("storageId:", storageId);
36
37const fee = await LoopringAPI.userAPI.getOffchainFeeAmt({
38 accountId: accInfo.accountId,
39 requestType: sdk.OffchainFeeReqType.TRANSFER_AND_UPDATE_ACCOUNT
40 },
41 apiKey);
42console.log("fee:", fee);
43
44const request = {
45 exchange: LOOPRING_EXPORTED_ACCOUNT.exchangeAddress,
46 payerAddr: accInfo.owner,
47 payerId: accInfo.accountId,
48 payeeAddr: payeeAddress,
49 payeeId: payeeAccountId,
50 storageId: storageId.offchainId,
51 token: {
52 tokenId: TOKEN_INFO.tokenMap.LRC.tokenId,
53 volume: amount.toString()
54 },
55 maxFee: {
56 tokenId: TOKEN_INFO.tokenMap["LRC"].tokenId,
57 volume: fee.fees["LRC"].fee ?? "9400000000000000000"
58 },
59 validUntil: LOOPRING_EXPORTED_ACCOUNT.validUntil,
60 payPayeeUpdateAccount: true
61 };
62
63const transactionResult = await LoopringAPI.userAPI.submitInternalTransfer({
64 request,
65 web3,
66 chainId: sdk.ChainId.GOERLI,
67 walletType: sdk.ConnectorNames.Unknown,
68 eddsaKey: eddsaKey.sk,
69 apiKey: apiKey,});
70
71console.log(transactionResult)