|
| 1 | +from swaps.utils.transaction_data import setup_tokens_addresses |
| 2 | +from swaps.utils.transaction_data import setup_for_liq |
| 3 | +from loguru import logger |
| 4 | +from web3 import Web3 |
| 5 | +import random |
| 6 | + |
| 7 | +from utils.transaction_data import ( |
| 8 | + get_wallet_balance, |
| 9 | + approve_token, |
| 10 | + create_amount, |
| 11 | + load_abi, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +class MuteSwap: |
| 16 | + def __init__(self, private_key: str, |
| 17 | + from_token: str, |
| 18 | + to_token: str, |
| 19 | + amount_from: float, |
| 20 | + amount_to: float |
| 21 | + ) -> None: |
| 22 | + self.private_key = private_key |
| 23 | + self.from_token = from_token |
| 24 | + self.to_token = to_token |
| 25 | + self.amount_to_swap = random.uniform(amount_from, amount_to) |
| 26 | + self.web3 = Web3(Web3.HTTPProvider('https://mainnet.era.zksync.io')) |
| 27 | + self.account = self.web3.eth.account.from_key(private_key) |
| 28 | + self.address_wallet = self.account.address |
| 29 | + |
| 30 | + async def swap(self) -> None: |
| 31 | + |
| 32 | + to_token_address, from_token_address = await setup_tokens_addresses(from_token=self.from_token, |
| 33 | + to_token=self.to_token) |
| 34 | + mute_contract = self.web3.eth.contract( |
| 35 | + address=Web3.to_checksum_address('0x8B791913eB07C32779a16750e3868aA8495F5964'), |
| 36 | + abi=await load_abi('mute_abi')) |
| 37 | + |
| 38 | + value, token_contract = await create_amount(self.from_token, self.web3, from_token_address, self.amount_to_swap) |
| 39 | + value = int(value) |
| 40 | + |
| 41 | + balance = await get_wallet_balance(self.from_token, self.web3, self.address_wallet, token_contract, 'ERA') |
| 42 | + |
| 43 | + if value > balance: |
| 44 | + raise Exception(f'Not enough balance for wallet {self.address_wallet}') |
| 45 | + |
| 46 | + deadline = int(self.web3.eth.get_block('latest').timestamp) + 1200 |
| 47 | + |
| 48 | + if self.from_token.lower() != 'eth': |
| 49 | + await approve_token(amount=value, |
| 50 | + private_key=self.private_key, |
| 51 | + chain='ERA', |
| 52 | + from_token_address=from_token_address, |
| 53 | + spender='0x8B791913eB07C32779a16750e3868aA8495F5964', |
| 54 | + address_wallet=self.address_wallet, |
| 55 | + web3=self.web3) |
| 56 | + |
| 57 | + if self.from_token.lower() == 'eth': |
| 58 | + tx = mute_contract.functions.swapExactETHForTokensSupportingFeeOnTransferTokens( |
| 59 | + 0, |
| 60 | + [Web3.to_checksum_address(from_token_address), Web3.to_checksum_address(to_token_address)], |
| 61 | + self.address_wallet, |
| 62 | + deadline, |
| 63 | + [True, False] |
| 64 | + ).build_transaction({ |
| 65 | + 'value': value if self.from_token.lower() == 'eth' else 0, |
| 66 | + 'nonce': self.web3.eth.get_transaction_count(self.address_wallet), |
| 67 | + 'from': self.address_wallet, |
| 68 | + 'maxFeePerGas': 0, |
| 69 | + 'maxPriorityFeePerGas': 0, |
| 70 | + 'gas': 0 |
| 71 | + }) |
| 72 | + |
| 73 | + else: |
| 74 | + tx = mute_contract.functions.swapExactTokensForETHSupportingFeeOnTransferTokens( |
| 75 | + value, |
| 76 | + 0, |
| 77 | + [Web3.to_checksum_address(from_token_address), Web3.to_checksum_address(to_token_address)], |
| 78 | + self.address_wallet, |
| 79 | + deadline, |
| 80 | + [False, False] |
| 81 | + ).build_transaction({ |
| 82 | + 'value': value if self.from_token.lower() == 'eth' else 0, |
| 83 | + 'nonce': self.web3.eth.get_transaction_count(self.address_wallet), |
| 84 | + 'from': self.address_wallet, |
| 85 | + 'maxFeePerGas': 0, |
| 86 | + 'maxPriorityFeePerGas': 0, |
| 87 | + 'gas': 0 |
| 88 | + }) |
| 89 | + |
| 90 | + tx.update({'maxFeePerGas': self.web3.eth.gas_price}) |
| 91 | + tx.update({'maxPriorityFeePerGas': self.web3.eth.gas_price}) |
| 92 | + |
| 93 | + gasLimit = self.web3.eth.estimate_gas(tx) |
| 94 | + tx.update({'gas': gasLimit}) |
| 95 | + signed_tx = self.web3.eth.account.sign_transaction(tx, self.private_key) |
| 96 | + raw_tx_hash = self.web3.eth.send_raw_transaction(signed_tx.rawTransaction) |
| 97 | + tx_hash = self.web3.to_hex(raw_tx_hash) |
| 98 | + logger.success( |
| 99 | + f'Swapped {self.amount_to_swap} {self.from_token} tokens => {self.to_token} | TX: https://explorer.zksync.io/tx/{tx_hash}') |
| 100 | + |
| 101 | + |
| 102 | +class MuteLiq: |
| 103 | + def __init__(self, private_key: str, |
| 104 | + token: str, |
| 105 | + amount_from: float, |
| 106 | + amount_to: float |
| 107 | + ) -> None: |
| 108 | + self.private_key = private_key |
| 109 | + self.token = token |
| 110 | + self.amount = random.uniform(amount_from, amount_to) |
| 111 | + self.web3 = Web3(Web3.HTTPProvider('https://mainnet.era.zksync.io')) |
| 112 | + self.account = self.web3.eth.account.from_key(private_key) |
| 113 | + self.address_wallet = self.account.address |
| 114 | + |
| 115 | + async def add_liquidity(self) -> None: |
| 116 | + to_token_address, from_token_address = await setup_for_liq(self.token) |
| 117 | + mute_contract = self.web3.eth.contract( |
| 118 | + address=Web3.to_checksum_address('0x8B791913eB07C32779a16750e3868aA8495F5964'), |
| 119 | + abi=await load_abi('mute_abi')) |
| 120 | + |
| 121 | + value, token_contract = await create_amount(self.token, self.web3, from_token_address, self.amount) |
| 122 | + value = int(value) |
| 123 | + |
| 124 | + balance = await get_wallet_balance(self.token, self.web3, self.address_wallet, token_contract, 'ERA') |
| 125 | + |
| 126 | + if value > balance: |
| 127 | + raise Exception(f'Not enough balance for wallet {self.address_wallet}') |
| 128 | + |
| 129 | + deadline = int(self.web3.eth.get_block('latest').timestamp) + 1200 |
| 130 | + if self.token.lower() != 'eth': |
| 131 | + await approve_token(amount=value, |
| 132 | + private_key=self.private_key, |
| 133 | + chain='ERA', |
| 134 | + from_token_address=from_token_address, |
| 135 | + spender='0x8B791913eB07C32779a16750e3868aA8495F5964', |
| 136 | + address_wallet=self.address_wallet, |
| 137 | + web3=self.web3) |
| 138 | + |
| 139 | + tx = mute_contract.functions.addLiquidityETH( |
| 140 | + Web3.to_checksum_address('0x3355df6D4c9C3035724Fd0e3914dE96A5a83aaf4'), |
| 141 | + value, |
| 142 | + int(0), |
| 143 | + int(0), |
| 144 | + self.address_wallet, |
| 145 | + int(deadline), |
| 146 | + int(50), |
| 147 | + False |
| 148 | + ).build_transaction({ |
| 149 | + 'value': value if self.token.lower() == 'eth' else 0, |
| 150 | + 'nonce': self.web3.eth.get_transaction_count(self.address_wallet), |
| 151 | + 'from': self.address_wallet, |
| 152 | + 'maxFeePerGas': 0, |
| 153 | + 'maxPriorityFeePerGas': 0, |
| 154 | + 'gas': 0 |
| 155 | + }) |
| 156 | + |
| 157 | + tx.update({'maxFeePerGas': self.web3.eth.gas_price}) |
| 158 | + tx.update({'maxPriorityFeePerGas': self.web3.eth.gas_price}) |
| 159 | + |
| 160 | + gasLimit = self.web3.eth.estimate_gas(tx) |
| 161 | + tx.update({'gas': gasLimit}) |
| 162 | + |
| 163 | + signed_tx = self.web3.eth.account.sign_transaction(tx, self.private_key) |
| 164 | + raw_tx_hash = self.web3.eth.send_raw_transaction(signed_tx.rawTransaction) |
| 165 | + tx_hash = self.web3.to_hex(raw_tx_hash) |
| 166 | + logger.success( |
| 167 | + f'Added {self.amount} {self.token} tokens to liquidity pool on Mute | TX: https://explorer.zksync.io/tx/{tx_hash}') |
0 commit comments