Example #1
0
    def test_is_valid_order_deserialized(self):
        account = Account.from_key(PRIVATE_KEY)
        zksync_signer = ZkSyncSigner.from_account(account, self.library,
                                                  ChainId.MAINNET)
        ethereum_signer = EthereumSignerWeb3(account=account)

        token1 = Token(id=1, symbol='', address='',
                       decimals=0)  # only id matters
        token2 = Token(id=2, symbol='', address='',
                       decimals=0)  # only id matters
        tokens_pool = Tokens(tokens=[token1, token2])

        order = Order(account_id=7,
                      nonce=18,
                      token_sell=token1,
                      token_buy=token2,
                      ratio=Fraction(1, 4),
                      amount=1000000,
                      recipient='0x823b6a996cea19e0c41e250b20e2e804ea72ccdf',
                      valid_from=0,
                      valid_until=4294967295)
        order.signature = zksync_signer.sign_tx(order)
        order.eth_signature = ethereum_signer.sign_tx(order)
        zksync_validator = EncodedTxValidator(self.library)
        serialized_order = json.dumps(order.dict(), indent=4)

        deserialized_order = Order.from_json(json.loads(serialized_order),
                                             tokens_pool)
        ret = zksync_validator.is_valid_signature(deserialized_order)
        self.assertTrue(ret)
        ret = deserialized_order.is_valid_eth_signature(
            ethereum_signer.address())
        self.assertTrue(ret)
Example #2
0
 def test_swap_bytes(self):
     token1 = Token(id=1, symbol='', address='',
                    decimals=0)  # only id matters
     token2 = Token(id=2, symbol='', address='',
                    decimals=0)  # only id matters
     token3 = Token(id=3, symbol='', address='',
                    decimals=0)  # only id matters
     order1 = Order(account_id=6,
                    nonce=18,
                    token_sell=token1,
                    token_buy=token2,
                    ratio=Fraction(1, 2),
                    amount=1000000,
                    recipient='0x823b6a996cea19e0c41e250b20e2e804ea72ccdf',
                    valid_from=0,
                    valid_until=4294967295)
     order2 = Order(account_id=44,
                    nonce=101,
                    token_sell=token2,
                    token_buy=token1,
                    ratio=Fraction(3, 1),
                    amount=2500000,
                    recipient='0x63adbb48d1bc2cf54562910ce54b7ca06b87f319',
                    valid_from=0,
                    valid_until=4294967295)
     swap = Swap(
         orders=(order1, order2),
         nonce=1,
         amounts=(1000000, 2500000),
         submitter_id=5,
         submitter_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a",
         fee_token=token3,
         fee=123)
     res = "f40100000005ede35562d3555e61120a151b3c8e8e91d83a378a000000017b1e76f6f124bae1917435a02cfbf5571d79ddb8380bc4bf4858c9e9969487000000030f600001e848000004c4b400"
     assert swap.encoded_message().hex() == res
Example #3
0
    def test_order_deserialization(self):
        token1 = Token(id=1, symbol='', address='',
                       decimals=0)  # only id matters
        token2 = Token(id=2, symbol='', address='',
                       decimals=0)  # only id matters
        tokens = Tokens(tokens=[token1, token2])

        order = Order(account_id=7,
                      nonce=18,
                      token_sell=token1,
                      token_buy=token2,
                      ratio=Fraction(1, 4),
                      amount=1000000,
                      recipient='0x823b6a996cea19e0c41e250b20e2e804ea72ccdf',
                      valid_from=0,
                      valid_until=4294967295)
        serialized_order = order.dict()
        from_json_order = Order.from_json(serialized_order, tokens)
        self.assertEqual(order.account_id, from_json_order.account_id)
        self.assertEqual(order.nonce, from_json_order.nonce)
        self.assertEqual(order.token_sell, from_json_order.token_sell)
        self.assertEqual(order.token_buy, from_json_order.token_buy)
        self.assertEqual(order.ratio, from_json_order.ratio)
        self.assertEqual(order.recipient, from_json_order.recipient)
        self.assertEqual(order.valid_from, from_json_order.valid_from)
        self.assertEqual(order.valid_until, from_json_order.valid_until)
Example #4
0
 def test_order_bytes(self):
     token1 = Token.eth()
     token2 = Token(id=2, symbol='', address='',
                    decimals=0)  # only id matters
     order = Order(account_id=6,
                   nonce=18,
                   token_sell=token1,
                   token_buy=token2,
                   ratio=Fraction(1, 2),
                   amount=1000000,
                   recipient='0x823b6a996cea19e0c41e250b20e2e804ea72ccdf',
                   valid_from=0,
                   valid_until=4294967295)
     res = '6f0100000006823b6a996cea19e0c41e250b20e2e804ea72ccdf0000001200000000000000020000000000000000000000000000010000000000000000000000000000020001e84800000000000000000000000000ffffffff'
     assert order.encoded_message().hex() == res
Example #5
0
 async def test_approve_deposit(self):
     token = Token(address=Web3.toChecksumAddress(
         '0xeb8f08a975ab53e34d8a0330e0d34de942c95926'),
                   id=20,
                   symbol='USDC',
                   decimals=18)
     assert await self.ethereum_provider.approve_deposit(token, Decimal(10))
Example #6
0
 async def test_is_deposit_approved(self):
     token = Token(address=Web3.toChecksumAddress(
         '0xD2084eA2AE4bBE1424E4fe3CDE25B713632fb988'),
                   id=20,
                   symbol='BAT',
                   decimals=18)
     assert await self.ethereum_provider.is_deposit_approved(token, 1)
Example #7
0
 async def test_full_exit(self):
     token = Token(address=Web3.toChecksumAddress(
         '0xD2084eA2AE4bBE1424E4fe3CDE25B713632fb988'),
                   id=20,
                   symbol='BAT',
                   decimals=18)
     assert await self.ethereum_provider.full_exit(token, 6713)
Example #8
0
 async def test_is_deposit_approved(self):
     token = Token(address=Web3.toChecksumAddress(
         '0xfe1b6abc39e46cec54d275efb4b29b33be176c2a'),
                   id=20,
                   symbol='PHNX',
                   decimals=18)
     assert await self.ethereum_provider.is_deposit_approved(token, 10)
Example #9
0
 async def test_full_exit(self):
     token = Token(address=Web3.toChecksumAddress(
         '0xfe1b6abc39e46cec54d275efb4b29b33be176c2a'),
                   id=20,
                   symbol='PHNX',
                   decimals=18)
     assert await self.ethereum_provider.full_exit(token, 6713)
Example #10
0
 async def get_tokens(self) -> Tokens:
     data = await self.provider.request("tokens", None)
     tokens = [Token(address=Web3.toChecksumAddress(token['address']),
                     id=token['id'],
                     symbol=token['symbol'],
                     decimals=token['decimals']
                     ) for token in data.values()]
     return Tokens(tokens=tokens)
Example #11
0
 def test_forced_exit_bytes(self):
     tr = ForcedExit(target="0x19aa2ed8712072e918632259780e587698ef58df",
                     token=Token.eth(),
                     fee=1000000,
                     nonce=12,
                     valid_from=0,
                     valid_until=4294967295,
                     initiator_account_id=44)
     res = "080000002c19aa2ed8712072e918632259780e587698ef58df00007d030000000c000000000000000000000000ffffffff"
     assert tr.encoded_message().hex() == res
Example #12
0
 def test_mint_nft_bytes(self):
     tr = MintNFT(
         creator_id=44,
         creator_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a",
         content_hash=
         "0000000000000000000000000000000000000000000000000000000000000123",
         recipient="0x19aa2ed8712072e918632259780e587698ef58df",
         fee=1000000,
         fee_token=Token.eth(),
         nonce=12)
     res = "f6010000002cede35562d3555e61120a151b3c8e8e91d83a378a000000000000000000000000000000000000000000000000000000000000012319aa2ed8712072e918632259780e587698ef58df000000007d030000000c"
     assert tr.encoded_message().hex() == res
Example #13
0
 def test_withdraw_nft_bytes(self):
     tr = WithdrawNFT(
         account_id=44,
         from_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a",
         to_address="0x19aa2ed8712072e918632259780e587698ef58df",
         fee_token=Token.eth(),
         fee=1000000,
         nonce=12,
         valid_from=0,
         valid_until=4294967295,
         token_id=100000)
     res = "f5010000002cede35562d3555e61120a151b3c8e8e91d83a378a19aa2ed8712072e918632259780e587698ef58df000186a0000000007d030000000c000000000000000000000000ffffffff"
     assert tr.encoded_message().hex() == res
Example #14
0
 def test_transfer_bytes(self):
     tr = Transfer(
         from_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a",
         to_address="0x19aa2ed8712072e918632259780e587698ef58df",
         token=Token.eth(),
         amount=1000000000000,
         fee=1000000,
         nonce=12,
         valid_from=0,
         valid_until=4294967295,
         account_id=44)
     res = "050000002cede35562d3555e61120a151b3c8e8e91d83a378a19aa2ed8712072e918632259780e587698ef58df00004a817c80027d030000000c000000000000000000000000ffffffff"
     assert tr.encoded_message().hex() == res
Example #15
0
 def test_signature(self):
     account = Account.from_key(PRIVATE_KEY)
     signer = ZkSyncSigner.from_account(account, self.library,
                                        ChainId.MAINNET)
     tr = Transfer(
         from_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a",
         to_address="0x19aa2ed8712072e918632259780e587698ef58df",
         token=Token.eth(),
         amount=1000000000000,
         fee=1000000,
         nonce=12,
         valid_from=0,
         valid_until=4294967295,
         account_id=44)
     res = signer.sign_tx(tr)
     assert res.signature == '849281ea1b3a97b3fe30fbd25184db3e7860db96e3be9d53cf643bd5cf7805a30dbf685c1e63fd75968a61bd83d3a1fb3a0b1c68c71fe87d96f1c1cb7de45b05'
Example #16
0
 def test_signature(self):
     account = Account.from_key(PRIVATE_KEY)
     signer = ZkSyncSigner.from_account(account, self.library,
                                        ChainId.MAINNET)
     tr = Transfer(
         from_address="0xedE35562d3555e61120a151B3c8e8e91d83a378a",
         to_address="0x19aa2ed8712072e918632259780e587698ef58df",
         token=Token.eth(),
         amount=1000000000000,
         fee=1000000,
         nonce=12,
         valid_from=0,
         valid_until=4294967295,
         account_id=44)
     res = signer.sign_tx(tr)
     assert res.signature == 'b3211c7e15d31d64619e0c7f65fce8c6e45637b5cfc8711478c5a151e6568d875ec7f48e040225fe3cc7f1e7294625cad6d98b4595d007d36ef62122de16ae01'
Example #17
0
 async def test_full_exit_nft(self):
     """
     INFO: made by getting all NFT by corresponded address & dumping,
             Symbol: 'NFT-70848'
             '70848'
             address: '0x5e71f0f9b891f22d79ff8697dd4e3e0db371cda5'
             creator_address: '0x995a8b7f96cb837533b79775b6209696d51f435c'
             id: 70848
             account_id: 36357
     """
     account_id = 36357
     token = Token(
         address=Web3.toChecksumAddress('0x5e71f0f9b891f22d79ff8697dd4e3e0db371cda5'),
         id=70848,
         symbol='NFT-70848',
         decimals=0
     )
     assert await self.ethereum_provider.full_exit_nft(token, account_id)
 async def approve_deposit(self, token: Token, limit: Decimal):
     contract = ERC20Contract(self.web3, self.zksync.contract_address,
                              token.address, self.zksync.account)
     return contract.approve_deposit(token.from_decimal(limit))
 async def deposit(self, token: Token, amount: Decimal, address: str):
     if token.is_eth():
         return self.zksync.deposit_eth(address, token.from_decimal(amount))
     else:
         return self.zksync.deposit_erc20(token.address, address,
                                          token.from_decimal(amount))