Создание транзакции

В данной статье мы будем использоваться этот кошелек для примера:

Address: 0x026A25EfbcEFb2e481d005E4F00Ccced0AF511FF
Private Key: 0x1c1a49fea9a4ede1dc8e582639f498d41fa3c4a9e2ab2b9d740a4a3ec14e1cbf

Уровень-1 (Layer-1)

Единственная транзакция, которая совершается на Уровне-1 – это депозит. Она имеет два подтипа DepositETH и DepositERC20.

ABIs

[
  {
    "constant": false,
    "inputs": [
      {
        "internalType": "contract IERC20",
        "name": "_token",
        "type": "address"
      },
      {
        "internalType": "uint104",
        "name": "_amount",
        "type": "uint104"
      },
      {
        "internalType": "address",
        "name": "_franklinAddr",
        "type": "address"
      }
    ],
    "name": "depositERC20",
    "outputs": [],
    "payable": false,
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "constant": false,
    "inputs": [
      {
        "internalType": "address",
        "name": "_franklinAddr",
        "type": "address"
      }
    ],
    "name": "depositETH",
    "outputs": [],
    "payable": true,
    "stateMutability": "payable",
    "type": "function"
  }
]

Депозит ETH

Пример кода:

Депозит ERC20

Как и в других проектах вы должны подтвердить основной адрес контракта ZKSwap, чтобы внести свои токенты ERC20 на депозит. Будьте осторожны с токенами, у которых есть ограничения (Подробнее см. здесь).

Пример кода:

Уровень-2

При отправке транзакции на ZKSwap через API необходимо заполнить три поля: tx, signature и fastProcessing. Далее по тексту Signed Transaction относиться к полю tx, а ETH Signature относится к полю signature.

Приватный ключ

Подпишите следующее сообщение приватным ключом Уровня 1, чтобы получить сид фразу Уровня 2. И используйте криптографические библиотеки zkSync, чтобы получить приватный ключ.

Вот полный пример получения приватного ключа с помощью zksync-crypto в JavaScript.

Хеш открытого ключа

После получения приватного ключа вам необходимо зарегистрировать хэш открытого ключа в ZKSwap, чтобы он мог верифицировать отправленные вами транзакции.

Подпись

Перед отправкой транзакции в ZKSwap вам необходимо подписать данные транзакции.

Замена открытого ключа

После того, как учетная запись была «зарегистрирована» в ZKSwap (посредством перевода или депозита), вы можете изменить pubKeyHash учетной записи, чтобы совершать любую транзакцию на Уровне 2. Это особая транзакция, которую не нужно подписывать приватным ключом Уровня 2.

Поля транзакции

Field

Type

Comment

type

string

ChangePubKey

accountId

number

Your account ID.

account

address

Your address.

newPkHash

number

The public key hash.

nonce

number

Your current nonce.

ethSignature

string

Layer-1 signature

Пример подписанной транзакции

ETH подпись

Перевод

Поля транзакции

Bytes Order

Field

Type

Bytes Length

Comment

Bytes Example

0

type

string

1

Transfer

0x05

1

accountId

number

4

Your account ID.

0x0000053a

2

from

address

20

Your address.

0x026a25efbcefb2e481d005e4f00ccced0af511ff

3

to

address

20

The target address you want to transfer.

0x961b513dfd3e363c238e0f98219ee02552a847bd

4

token

number

2

The ID of the token.

0x000a

5

amount

string

5

The amount you want to transfer.

0x4a817c8008

6

feeToken

number

1

The ID of fee token.

0x0a

7

fee

string

2

0x0000

8

chainId

number

1

11 for mainnet, 128 for ropsten.

0x0b

9

nonce

number

4

Your current nonce.

0x00000001

-

signature

object

Contains your pubKey and signature.

Пример Full Bytes

Пример подписанной транзакции

ETH подпись

Вывод

Поля транзакции

Bytes Order

Field

Type

Bytes Length

Comment

Bytes Example

0

type

string

1

Withdraw

0x03

1

accountId

number

4

Your account ID.

0x0000053a

2

from

address

20

Your address.

0x026a25efbcefb2e481d005e4f00ccced0af511ff

3

to

address

20

The target address you want to withdraw to.

0x026a25efbcefb2e481d005e4f00ccced0af511ff

4

token

number

2

The ID of the token you want to withdraw.

0x000a

5

amount

string

16

The amount you want to withdraw.

0x00000000000000000de0b6b3a7640000

6

feeToken

number

1

The ID of fee token.

0x0a

7

fee

string

2

0x4bf0

8

chainId

number

1

11 for mainnet, 128 for ropsten.

0x0b

9

nonce

number

4

Your current nonce.

0x00000002

-

signature

object

Contains your pubKey and signature.

Пример Full Bytes

Пример подписанной транзакции

ETH подпись

Обмен

Комиссия

Для примера обменяем токен А на токен В. Есть два способа:

  1. Если А – это токен комиссии, то ее размер рассчитывается как amountIn * 5 / 9995

  2. Если А не выступает в роли токена комиссии, а для комиссии применяется токен В, то ее размер amountOut * 5 /10000. Это не amountOutMin. amountOut равен amountOutMin и тогда проскальзывание равно 0.

Поля транзакции

Bytes Order

Field

Type

Bytes Length

Comment

Bytes Example

0

type

string

1

Swap

0x0b

1

accountId

number

4

Your account ID.

0x0000053a

2

from

address

20

Your address.

0x026a25efbcefb2e481d005e4f00ccced0af511ff

3

to

address

20

The pair's address.

0xaa45c964e21eafb38574e9d000adbaf85acfbb80

4

tokenIn

number

2

The ID of the token you want to swap in.

0x000a

5

amountIn

string

5

The amount you want to swap in.

0x94efe63009

6

tokenOut

number

2

The ID of the token you want to swap out.

0x0000

7

amountOutMin

string

5

The minimum amount you want to swap out.

0x25004d47c6

8

feeToken

number

1

The ID of fee token.

0x0a

9

fee

string

2

0x7d0d

10

chainId

number

1

11 for mainnet, 128 for ropsten.

0x0b

11

nonce

number

4

Your current nonce.

0x00000003

-

signature

object

Contains your pubKey and signature.

Пример Full Bytes

Пример подписанной транзакции

ETH подпись

Добавление ликвидности

Поля транзакции

Bytes Order

Field

Type

Bytes Length

Comment

Bytes Example

0

type

string

1

AddLiquidity

0x09

1

accountId

number

4

Your account ID.

0x0000053a

2

from

address

20

Your address.

0x026a25efbcefb2e481d005e4f00ccced0af511ff

3

to

address

20

The pair's address.

0xaa45c964e21eafb38574e9d000adbaf85acfbb80

4

tokenA

number

2

The ID of token A.

0x0000

5

amountADesired

number

5

The amount of token A you want to add to the pair.

0xc2cf6f3245

6

amountAMin

string

5

The minimum amount of Token A you want to add to the pair.

0xb911dcd625

7

tokenB

number

2

The ID of the token B.

0x000a

8

amountBDesired

string

5

The amount of token B you want to add to the pair.

0x4a817c8009

9

amountBMin

string

5

The minimum amount of Token B you want to add to the pair.

0x46c7cfe009

10

tokenLiquidity

number

2

The pair ID.

0x4002

11

feeToken

number

1

The ID of fee token.

0x0a

12

fee

string

2

0x0000

13

chainId

number

1

11 for mainnet, 128 for ropsten.

0x0b

14

nonce

number

4

Your current nonce.

0x00000004

-

signature

object

Contains your pubKey and signature.

Пример Full Bytes

Пример подписанной транзакции

ETH подпись

Удаление ликвидности

Поля транзакции

Bytes Order

Field

Type

Comment

Bytes Examplle

0

type

string

1

RemoveLiquidity

0x0a

1

accountId

number

4

Your account ID.

0x0000053a

2

from

address

20

Your address.

0x026a25efbcefb2e481d005e4f00ccced0af511ff

3

to

address

20

The pair's address.

0xaa45c964e21eafb38574e9d000adbaf85acfbb80

4

tokenA

number

2

The ID of token A.

0x0000

5

amountAMin

string

5

The minimum amount of Token A you want to remove from the pair.

0xb96855c485

6

tokenB

number

2

The ID of the token B.

0x000a

7

amountBMin

string

5

The minimum amount of Token B you want to remove from the pair.

0x46a6e21049

8

feeToken

number

1

The ID of fee token.

0x0a

9

fee

string

2

0x0000

10

tokenLiquidity

number

2

The pair ID.

0x4002

11

amountLiquidity

string

5

The amount of LP token you want to remove.

0x77e80cdba7

12

chainId

number

2

11 for mainnet, 128 for ropsten.

0x0b

13

nonce

number

4

Your current nonce.

-

signature

object

Contains your pubKey and signature.

Пример Full Bytes

Пример подписанной транзакции

ETH подпись

Last updated

Was this helpful?