async def build_transfer( self, to: str, amount: Decimal, token: TokenLike, fee: Decimal = None, valid_from=DEFAULT_VALID_FROM, valid_until=DEFAULT_VALID_UNTIL ) -> Tuple[Transfer, TxEthSignature]: account_id, nonce = await self.zk_provider.get_account_nonce( self.address()) token = await self.resolve_token(token) if fee is None: fee = await self.zk_provider.get_transaction_fee( FeeTxType.transfer, to, token.id) fee = fee.total_fee else: fee = token.from_decimal(fee) transfer = Transfer(account_id=account_id, from_address=self.address(), to_address=to, amount=token.from_decimal(amount), fee=fee, nonce=nonce, valid_from=valid_from, valid_until=valid_until, token=token) eth_signature = await self.eth_signer.sign_tx(transfer) zk_signature = self.zk_signer.sign_tx(transfer) transfer.signature = zk_signature return transfer, eth_signature
async def build_transfer( self, to: str, amount: int, token: Token, fee: int, nonce: Optional[int] = None, valid_from: int = DEFAULT_VALID_FROM, valid_until: int = DEFAULT_VALID_UNTIL, ) -> Tuple[Transfer, TxEthSignature]: """ This function takes as a parameter the integer amount/fee of lowest token denominations (wei, satoshi, etc.) """ if nonce is None: nonce = await self.zk_provider.get_account_nonce(self.address()) account_id = await self.get_account_id() transfer = Transfer(account_id=account_id, from_address=self.address(), to_address=to.lower(), amount=amount, fee=fee, nonce=nonce, valid_from=valid_from, valid_until=valid_until, token=token) eth_signature = self.eth_signer.sign_tx(transfer) zk_signature = self.zk_signer.sign_tx(transfer) transfer.signature = zk_signature return transfer, eth_signature
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
async def _process_transfer(self, obj): if not obj[self.IS_ENCODED_TRANSACTION]: account_id = await self.wallet.get_account_id() token = await self.wallet.resolve_token(obj["token"]) fee = obj["fee"] if fee is None: fee = await self.wallet.zk_provider.get_transaction_fee( FeeTxType.transfer, obj["to_address"], token.id) fee = fee.total_fee else: fee = token.from_decimal(fee) amount = token.from_decimal(obj["amount"]) transfer = Transfer(account_id=account_id, from_address=obj["from_address"].lower(), to_address=obj["to_address"].lower(), token=token, amount=amount, fee=fee, nonce=self.nonce, valid_from=obj["valid_from"], valid_until=obj["valid_until"]) zk_signature = self.wallet.zk_signer.sign_tx(transfer) transfer.signature = zk_signature else: token = await self.wallet.resolve_token(obj["token"]) transfer = Transfer(account_id=obj["accountId"], from_address=obj["from"], to_address=obj["to"], token=token, amount=obj["amount"], fee=obj["fee"], nonce=self.nonce, valid_from=obj["validFrom"], valid_until=obj["validUntil"], signature=obj["signature"]) self.nonce += 1 return transfer
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'
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'