def get(self, configuration_file_name): # Returns the highest estimate of ETH needed to get required service token amount configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) account = configuration_file.account w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, account) required = RequiredAmounts.for_network(configuration_file.network.name) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) highest_cost = 0 for exchange in (kyber, uniswap): exchange_costs = exchange.calculate_transaction_costs( required.service_token, account) if not exchange_costs: continue total_cost = exchange_costs["total"].as_wei highest_cost = max(highest_cost, total_cost) estimated_cost = EthereumAmount(Wei(highest_cost)) self.render_json({ "dex_swap_RDN": { "as_wei": estimated_cost.as_wei, "formatted": estimated_cost.formatted, } })
class ExchangeSelectionTestCase(TokenSwapTestCase): TOKEN_AMOUNTS = (50, 75, 100, 150, 200, 300, 500, 1000) def setUp(self): super().setUp() self.kyber = Kyber(w3=self.w3) self.uniswap = Uniswap(w3=self.w3) @override_settings(network="mainnet") def test_can_get_costs(self): print("\t".join([st.rjust(36) for st in ("RDN to buy", "Kyber", "Uniswap")])) for amount in self.TOKEN_AMOUNTS: rdn_amount = TokenAmount(amount, self._get_RDN()) kyber_costs = self.kyber.calculate_transaction_costs(rdn_amount, self.account) uniswap_costs = self.uniswap.calculate_transaction_costs(rdn_amount, self.account) print( "\t".join( [ st.rjust(36) for st in ( f"{rdn_amount.formatted}", f"{kyber_costs['total'].formatted}", f"{uniswap_costs['total'].formatted}", ) ] ) )
def get(self, configuration_file_name, token_ticker): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) token = Erc20Token.find_by_ticker(token_ticker, configuration_file.network.name) network = configuration_file.network settings = network_settings[network.name] swap_amounts = SwapAmounts.from_settings(settings) if token_ticker == settings.service_token.ticker: swap_amount_1 = swap_amounts.service_token_1 swap_amount_2 = swap_amounts.service_token_2 swap_amount_3 = swap_amounts.service_token_3 elif token_ticker == settings.transfer_token.ticker: swap_amount_1 = swap_amounts.transfer_token_1 swap_amount_2 = swap_amounts.transfer_token_2 swap_amount_3 = swap_amounts.transfer_token_3 self.render( "swap.html", configuration_file=configuration_file, kyber=kyber, uniswap=uniswap, token=token, swap_amount_1=swap_amount_1, swap_amount_2=swap_amount_2, swap_amount_3=swap_amount_3, )
def get(self, configuration_file_name, token_ticker): configuration_file = RaidenConfigurationFile.get_by_filename( configuration_file_name) if get_passphrase() is None: self.render( "account_unlock.html", keystore_file_path=configuration_file.account. keystore_file_path, return_to=f"/swap/{configuration_file_name}/{token_ticker}", ) return w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) token = Erc20Token.find_by_ticker(token_ticker, configuration_file.network.name) swap_amounts = SwapAmounts.from_settings(self.installer_settings) if token_ticker == self.installer_settings.service_token.ticker: swap_amount = swap_amounts.service_token elif token_ticker == self.installer_settings.transfer_token.ticker: swap_amount = swap_amounts.transfer_token self.render( "swap.html", configuration_file=configuration_file, kyber=kyber, uniswap=uniswap, token=token, swap_amount=swap_amount, )
def get(self, configuration_file_name, token_ticker): configuration_file = RaidenConfigurationFile.get_by_filename(configuration_file_name) w3 = make_web3_provider( configuration_file.ethereum_client_rpc_endpoint, configuration_file.account ) kyber = Kyber(w3=w3) uniswap = Uniswap(w3=w3) token = Erc20Token.find_by_ticker(token_ticker) self.render( "swap_options.html", configuration_file=configuration_file, kyber=kyber, uniswap=uniswap, token=token, )
def setUp(self): super().setUp() self.kyber = Kyber(w3=self.w3) self.uniswap = Uniswap(w3=self.w3)
def test_swap(self): self._execute_rdn_swap(Kyber(w3=self.w3))
def kyber(test_account: Account, kyber_chain, patch_network): snapshot_id = take_snapshot() w3 = make_web3_provider(f"http://localhost:{PORT}", test_account) yield Kyber(w3) revert_to_snapshot(snapshot_id)