Make Transaction
In the following article, we will use this wallet as example:
Address: 0x026A25EfbcEFb2e481d005E4F00Ccced0AF511FF
Private Key: 0x1c1a49fea9a4ede1dc8e582639f498d41fa3c4a9e2ab2b9d740a4a3ec14e1cbfLayer-1
The only transaction that made on Layer-1 is deposit. It has two sub-types which are DepositETH and 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"
}
]Deposit ETH
Example Code:
Deposit ERC20
Like other projects, you must approve ZKSwap's main contract address to spend your ERC20 tokens in order to deposit. Be careful for those tokens that has limit on changing allowance (See https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729).
Example code:
Layer-2
When submitting transaction to ZKSwap, the API requires 3 fields: tx, signature and fastProcessing.In the following docs, Signed Transaction refers to the tx field and ETH Signature refers to the signature field.
Private Key
Sign the following message with Layer-1's private key to get Layer-2's seed. And use zkSync's crypto libraries to get private key.
Here's a full example of getting private key using zksync-crypto in JavaScript.
Public Key Hash
After getting private key, you will need to register you publick key hash to ZKSwap so that it can verify transations that you sent.
Signature
Before submit transaction to ZKSwap, you need to sign the the transaction data.
Change Public Key
After the account has been "registered" on ZKSwap (via transfer or deposit), you can change the account's pubKeyHash in order to make any transaction on Layer-2. This is a special transaction that you don't need to sign it with Layer-2's private key.
Transaction Fields
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
Signed Transaction Example
ETH Signature
NOTE: The {pubKeyHash} in the above message doesn't contain 0x as prefix. The {hexlifiedNonce} and {hexlifiedAccountId} has 4-bytes length with 0 padding in the start and 0x as prefx. See example for reference.
Transfer
Transaction Fields
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 packed 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 Example
Signed Transaction Example
ETH Signature
Withdraw
Transaction Fields
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 Example
Signed Transaction Example
ETH Signature
Swap
Fee
Let's take swap token A for token B as example. There are two cases:
If A is a fee token, the fee token of swap is A. The number of fee is calculated by
amountIn * 5 / 9995If A is not a fee token, the fee token of swap is B. The number of fee is
amountOut * 5 /10000. It's notamountOutMin.amountOutequalsamountOutMinthen slipplage is 0.
Transaction Fields
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 Example
Signed Transaction Example
ETH Signature
Add Liquidity
Transaction Fields
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 Example
Signed Transaction Example
ETH Signature
Remove Liquidity
Transaction Fields
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 Example
Signed Transaction Example
ETH Signature
Last updated