Setup a Loopring Account

Open an account via the Loopring DEX.

Please update SDK after v3.1.5 and use UnlockAccount with `account.publicKey or `generateKeyPair` with `account.publicKey`

Step 1: Register a wallet address in the Loopring DEX to get an account ID

There are two ways to register an account on the Loopring DEX to get an account ID:

  1. via API - Make a deposit to your address by calling the Loopring exchange address. Once the Loopring exchange receives your deposit (8 confirmations), it will generate an account id for your address.

  2. via Website App - Use an account in Loopring Layer 2 to transfer some tokens to your address, after the transfer is successful, this will generate an account id for the payee address.

Step 2: Retrieve account info using GET Account info by address

Using the newly acquired account id, retrieve the account information using the getAccount call.

const {accInfo} = await LoopringAPI.exchangeAPI.getAccount({
  owner: LOOPRING_EXPORTED_ACCOUNT.address,
});
Step 3: Use keySeed or CUSTOMER_KEY_SEED generateKeyPair

Generate an EdDSA key-pair using ecdsa to sign message. An EdDSA key-pair is needed for making changes to an account in the SDK. You can create a key-seed phrase using the default recommendation or a custom value.

  • Default keySeed

    Default keySeed string format we use looks like the following:

Sign this message to access Loopring Exchange: %s with key nonce: %d'.

You will need to replace %s with the Loopring exchange address, and replace %d with the value of the nonce received in the account information from step 2.

const keySeed = sdk.BaseAPI.KEY_MESSAGE.replace(
      "${exchangeAddress}",
      LOOPRING_EXPORTED_ACCOUNT.exchangeAddress
    ).replace("${nonce}", accInfo.nonce.toString());
const eddsaKey = await sdk.generateKeyPair({
  web3,
  address: accInfo.owner,
  keySeed,
  walletType: sdk.ConnectorNames.MetaMask,
  chainId: sdk.ChainId.GOERLI,
});
console.log("eddsakey:", eddsaKey.sk);
  • Custom keySeed

    Custom keySeed string must end with with key nonce: ${nonce} Example:

Sign this message to access ${customString} with key nonce: %d

You can define the customString yourself, %d is the nonce received in the account information from step 2.

// CUSTOMER_KEY_SEED = "XXXXXX" + " with key nonce: " + "${nonce}";
const keySeed = CUSTOMER_KEY_SEED.replace(
  "${nonce}",
  accInfo.nonce.toString()
const eddsaKey = await sdk.generateKeyPair({
  web3,
  address: accInfo.owner,
  keySeed,
  walletType: sdk.ConnectorNames.MetaMask,
  chainId: sdk.ChainId.GOERLI,
});
console.log("eddsakey:", eddsaKey.sk);
Step 4: Retrieve fees from the exchange get fee

In order to update your account, you'll need to retrieve the list of available fees from the exchange.

Get updateAccount fee

const fee = await LoopringAPI.globalAPI.getActiveFeeInfo({
  accountId: accInfo.accountId,
});
console.log("fee:", fee);
Step 5: updateAccount (activate or reset)
 const result = await LoopringAPI.userAPI.updateAccount({
  request: {
    exchange: LOOPRING_EXPORTED_ACCOUNT.exchangeAddress,
    owner: accInfo.owner,
    accountId: accInfo.accountId,
    publicKey: {x: eddsaKey.formatedPx, y: eddsaKey.formatedPy},
    maxFee: {
      tokenId: TOKEN_INFO.tokenMap["LRC"].tokenId,
      volume: fee.fees["LRC"].fee ?? "9400000000000000000",
    },
    keySeed,
    validUntil: LOOPRING_EXPORTED_ACCOUNT.validUntil,
    nonce: accInfo.nonce as number,
  },
  web3,
  chainId: sdk.ChainId.GOERLI,
  walletType: sdk.ConnectorNames.Unknown,
  isHWAddr: false,
});

const {accInfo: updateAccountInfo} =
  await LoopringAPI.exchangeAPI.getAccount({
    owner: LOOPRING_EXPORTED_ACCOUNT.address,
  });
console.log(
  "updateAccount Result: ",
  result,
  "updateAccountInfo:",
  updateAccountInfo
);
Step 6: Get account to check update complete

Check that the publicKey is the same as the value set in the previous step and frozen is false.

'frozen' will be true until updateAccount completes and then it will be set to false.

You can call this API every 10s until updateAccount is complete.

const {accInfo} = await LoopringAPI.exchangeAPI.getAccount({
  owner: LOOPRING_EXPORTED_ACCOUNT.address,
});
Step 7: Retrieve account API key Get apiKey

Get the apiKey by eddsa signature and then the apiKey can be used to get user assets or to call other APIs

const apiKey = (
      await LoopringAPI.userAPI.getUserApiKey(
        {
          accountId: accInfo.accountId,
        },
        eddsaKey.sk
      )
    ).apiKey;

Last updated