Skip to content

(helpful suggestion) Add Relay.link Bridge #5

@B0R9F3D9

Description

@B0R9F3D9
  1. Add relay.py file tomodules folder and paste this code:
from loguru import logger
import aiohttp

from utils.gas_checker import check_gas
from utils.helpers import retry
from .account import Account

CHAIN_IDS = {
    'Ethereum': 1,
    'Optimism': 10,
    'Arbitrum': 42161,
    'Base': 8453,
    'Zora': 7777777
}


class Relay(Account):
    def __init__(self, account_id: int, private_key: str, proxy: str | None = None) -> None:
        super().__init__(account_id=account_id, private_key=private_key, proxy=proxy, chain="zksync")
        self.proxy = f"http://{proxy}" if proxy else None

    async def get_info(self, dest_chain_id: int, amount: int) -> tuple[str, int, str]:
        'return (address_to, amount_wei, tx_data)'
        json_data = {
            'destinationChainId': dest_chain_id,
            'originChainId': 324, # zksync-era chain id
            'source': "relay.link",
            'txs': [{
                'data': '0x',
                'to': self.address,
                'value': amount
            }],
            'user': self.address
        }
        headers = {
            "Accept": "application/json",
            "Content-Type": "application/json",
            "Host": "api.relay.link",
            "Origin": "https://www.relay.link",
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                'https://api.relay.link/execute/call',
                headers=headers,
                json=json_data,
                proxy=self.proxy
            ) as response:
                if response.status != 200:
                    raise Exception(f'Relay response error: CODE - {response.status} | RESP - {await response.text()}')
                data = (await response.json())['steps'][0]['items'][0]['data']
        return self.w3.to_checksum_address(data['to']), int(data['value']), data['data']
            
    @check_gas
    @retry
    async def bridge(self, dest_chain: str, amount: float) -> None:
        dest_chain_id = CHAIN_IDS[dest_chain]

        address_to, amount_send_wei, data = await self.get_info(dest_chain_id, int(amount * 10**18))

        logger.info(f'[{self.account_id}][{self.address}] Bridging via Relay - {amount:.5f} ETH ZkSync Era -> ' + 
                    f'{amount:.5f} ETH {dest_chain} | Cost: {amount_send_wei/10**18:.5f} ETH')
        
        tx_data = await self.get_tx_data(amount_send_wei) | {'to': address_to, 'data': data}

        signed_txn = await self.sign(tx_data)
        txn_hash = await self.send_raw_transaction(signed_txn)
        await self.wait_until_tx_finished(txn_hash.hex())

  1. Add to modules_settingsy file this code:
async def relay_bridge(account_id, key, proxy):
    '''
    Make bridge from zksync-era via Relay.link
    Available chains: 'Ethereum', 'Optimism', 'Arbitrum', 'Base', 'Zora'
    '''

    amount = 0.000001

    chain = 'Arbitrum'

    relay = Relay(account_id, key, proxy)
    await relay.bridge(chain, amount)

  1. Add to def get_module() in main.py file this code:
result = questionary.select(
        "Select a method to get started",
        choices=[
            ...
            Choice("52) MultiApprove", multi_approve),
            Choice("53) Make bridge on Relay", relay_bridge),
            Choice("54) Check transaction count", "tx_checker"),
            Choice("55) Exit", "exit"),
        ],
        ...

  1. Enjoy

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions