Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(helpful suggestion) Add Relay.link Bridge #5

Open
B0R9F3D9 opened this issue Feb 18, 2024 · 0 comments
Open

(helpful suggestion) Add Relay.link Bridge #5

B0R9F3D9 opened this issue Feb 18, 2024 · 0 comments

Comments

@B0R9F3D9
Copy link

  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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant