diff --git a/pretix_eth/network/helpers.py b/pretix_eth/network/helpers.py index 8e1e4c03..bd7e0859 100644 --- a/pretix_eth/network/helpers.py +++ b/pretix_eth/network/helpers.py @@ -24,12 +24,17 @@ def make_erc_681_url( ) -def make_uniswap_url(output_currency, recipient_address, exact_amount): +def make_uniswap_url(output_currency, recipient_address, exact_amount, network_name=""): """ Build uniswap url to swap exact_amount of one currency to the required output currency and send to a recipient address. """ - return f"https://uniswap.exchange/send?exactField=output&exactAmount={exact_amount}&outputCurrency={output_currency}&recipient={recipient_address}" # noqa: E501 + uniswap_link = f"https://uniswap.exchange/send?exactField=output&exactAmount={exact_amount}&outputCurrency={output_currency}&recipient={recipient_address}" # noqa: E501 + + if not network_name == "": + uniswap_link += f"&chain={network_name}" + + return uniswap_link def make_checkout_web3modal_url( diff --git a/pretix_eth/network/tokens.py b/pretix_eth/network/tokens.py index a4247961..5917fba4 100644 --- a/pretix_eth/network/tokens.py +++ b/pretix_eth/network/tokens.py @@ -28,6 +28,11 @@ class IToken(object): NETWORK_IDENTIFIER = None # E.g. "L1" NETWORK_VERBOSE_NAME = None # E.g. "Ethereum Mainnet" + NETWORK_UNISWAP_NAME = None + # ^ this is the url param in uniswap url to identify the network + # can find this name here: + # https://github.com/Uniswap/interface/blob/main/src/constants/chains.ts#L21 + TOKEN_SYMBOL = None # E.g. "ETH" TOKEN_VERBOSE_NAME = None # {TOKEN_SYMBOL}-{NETWORK_VERBOSE_NAME} TOKEN_VERBOSE_NAME_TRANSLATED = None # using django translation module @@ -41,11 +46,15 @@ def __init__(self): self._set_other_token_constants() def _validate_class_variables(self): - if not ( - self.NETWORK_IDENTIFIER and self.NETWORK_VERBOSE_NAME and self.TOKEN_SYMBOL - ): + if not all([ + self.NETWORK_IDENTIFIER, + self.NETWORK_VERBOSE_NAME, + self.NETWORK_UNISWAP_NAME, + self.TOKEN_SYMBOL + ]): raise ValueError( - "Please provide network_identifier, verbose name, token symbol for this class" + "Please provide network_identifier, verbose name, " + + "uniswap url name, token symbol for this class" ) if not self.IS_NATIVE_ASSET and not self.ADDRESS: raise ValueError( @@ -132,6 +141,7 @@ class L1(IToken): NETWORK_IDENTIFIER = "L1" NETWORK_VERBOSE_NAME = "Ethereum Mainnet" CHAIN_ID = 1 + NETWORK_UNISWAP_NAME = "mainnet" def payment_instructions( self, wallet_address, payment_amount, amount_in_token_base_unit @@ -153,6 +163,7 @@ def payment_instructions( output_currency=self.TOKEN_SYMBOL if self.IS_NATIVE_ASSET else self.ADDRESS, recipient_address=wallet_address, exact_amount=amount_in_token_base_unit, + network_name=self.NETWORK_UNISWAP_NAME, ) amount_manual = f"{amount_in_token_base_unit} {self.TOKEN_SYMBOL}" web3modal_url = make_checkout_web3modal_url( @@ -179,6 +190,7 @@ class RinkebyL1(L1): NETWORK_IDENTIFIER = "Rinkeby" NETWORK_VERBOSE_NAME = "Rinkeby Ethereum Testnet" CHAIN_ID = 4 + NETWORK_UNISWAP_NAME = "rinkeby" class GoerliL1(L1): @@ -189,6 +201,7 @@ class GoerliL1(L1): NETWORK_IDENTIFIER = "Goerli" NETWORK_VERBOSE_NAME = "Goerli Ethereum Testnet" CHAIN_ID = 5 + NETWORK_UNISWAP_NAME = "goerli" class EthRinkebyL1(RinkebyL1): @@ -256,6 +269,7 @@ class Optimism(L1): NETWORK_IDENTIFIER = "Optimism" NETWORK_VERBOSE_NAME = "Optimism Mainnet" CHAIN_ID = 10 + NETWORK_UNISWAP_NAME = "optimism" class KovanOptimism(Optimism): @@ -266,6 +280,7 @@ class KovanOptimism(Optimism): NETWORK_IDENTIFIER = "KovanOptimism" NETWORK_VERBOSE_NAME = "Kovan Optimism Testnet" CHAIN_ID = 69 + NETWORK_UNISWAP_NAME = "optimistic_kovan" class EthKovanOptimism(KovanOptimism): @@ -315,38 +330,7 @@ class Arbitrum(L1): NETWORK_IDENTIFIER = "Arbitrum" NETWORK_VERBOSE_NAME = "Arbitrum Mainnet" CHAIN_ID = 42161 - - def payment_instructions( - self, wallet_address, payment_amount, amount_in_token_base_unit - ): - """ - Generic instructions for paying on all Arbitrum networks (eg Rinkeby and Mainnet), - both for native tokens and custom tokens. - - Pay via a web3 modal, ERC 681 (QR Code) or manually. - """ - erc_681_url = make_erc_681_url( - to_address=wallet_address, - payment_amount=payment_amount, - chain_id=self.CHAIN_ID, - is_token=not self.IS_NATIVE_ASSET, - token_address=self.ADDRESS, - ) - amount_manual = f"{amount_in_token_base_unit} {self.TOKEN_SYMBOL}" - web3modal_url = make_checkout_web3modal_url( - currency_type=self.TOKEN_SYMBOL, - amount_in_ether_or_token=amount_in_token_base_unit, - wallet_address=wallet_address, - chainId=self.CHAIN_ID, - ) - - return { - "erc_681_url": erc_681_url, - # "uniswap_url": None, - "web3modal_url": web3modal_url, - "amount_manual": amount_manual, - "wallet_address": wallet_address, - } + NETWORK_UNISWAP_NAME = "arbitrum" class RinkebyArbitrum(Arbitrum): @@ -357,6 +341,7 @@ class RinkebyArbitrum(Arbitrum): NETWORK_IDENTIFIER = "RinkebyArbitrum" NETWORK_VERBOSE_NAME = "Rinkeby Arbitrum Testnet" CHAIN_ID = 421611 + NETWORK_UNISWAP_NAME = "arbitrum_rinkeby" class ETHRinkebyArbitrum(RinkebyArbitrum): diff --git a/tests/tokens/test_token_and_registries_setup.py b/tests/tokens/test_token_and_registries_setup.py index 642ce4c9..917befc2 100644 --- a/tests/tokens/test_token_and_registries_setup.py +++ b/tests/tokens/test_token_and_registries_setup.py @@ -12,11 +12,11 @@ def assert_error_when_init_variables_not_passed(Test: IToken): Test() assert ( execinfo.value.args[0] - == "Please provide network_identifier, verbose name, token symbol for this class" + == "Please provide network_identifier, verbose name, uniswap url name, token symbol for this class" # noqa: E501 ) -def test_error_if_no_network_constants_not_set(): +def test_error_if_no_network_constants_set(): class Test(IToken): pass @@ -24,32 +24,41 @@ class Test(IToken): def test_error_if_some_network_constants_not_set(): - # Network ID, but no network verbose name, token symbol + # Network ID, but no network verbose name, token symbol, uniswap network name class Test(IToken): NETWORK_IDENTIFIER = "Test" assert_error_when_init_variables_not_passed(Test) - # Network ID and token symbol but no network verbose name, + # Network ID and token symbol but no network verbose name, uniswap network name class Test(IToken): NETWORK_IDENTIFIER = "Test" TOKEN_SYMBOL = "T" assert_error_when_init_variables_not_passed(Test) - # Network ID and network verbose name, but no token symbol + # Network ID and network verbose name, but no token symbol, uniswap network name class Test(IToken): NETWORK_IDENTIFIER = "Test" NETWORK_VERBOSE_NAME = "Test Network" assert_error_when_init_variables_not_passed(Test) + # Network ID, network verbose name and token symbol, but no uniswap network name + class Test(IToken): + NETWORK_IDENTIFIER = "Test" + NETWORK_VERBOSE_NAME = "Test Network" + TOKEN_SYMBOL = "T" + + assert_error_when_init_variables_not_passed(Test) + def test_network_creation_when_basic_constants_set(): class Test(IToken): NETWORK_IDENTIFIER = "Test" NETWORK_VERBOSE_NAME = "Test Network" TOKEN_SYMBOL = "T" + NETWORK_UNISWAP_NAME = "test" try: testToken = Test() diff --git a/tests/tokens/test_token_methods.py b/tests/tokens/test_token_methods.py index 79cdac36..7d187483 100644 --- a/tests/tokens/test_token_methods.py +++ b/tests/tokens/test_token_methods.py @@ -10,6 +10,7 @@ class Test(IToken): NETWORK_VERBOSE_NAME = "Test Network" TOKEN_SYMBOL = "T" CHAIN_ID = 1 + NETWORK_UNISWAP_NAME = "mainnet" return Test() @@ -82,7 +83,7 @@ def test_make_erc_681_url_for_L2(): assert erc681_url == "ethereum:0xtoken@3/transfer?address=0xtest1&uint256=10" -def test_uniswap_link(): +def test_uniswap_link_default_chain(): uniswap_link = helpers.make_uniswap_url("ETH", "0xtest1", 1000) assert ( uniswap_link @@ -90,6 +91,14 @@ def test_uniswap_link(): ) +def test_uniswap_link_with_chain_parameter(): + uniswap_link = helpers.make_uniswap_url("ETH", "0xtest1", 1000, "arbitrum") + assert ( + uniswap_link + == "https://uniswap.exchange/send?exactField=output&exactAmount=1000&outputCurrency=ETH&recipient=0xtest1&chain=arbitrum" # noqa: E501 + ) + + def test_web3modal_checkout_link(): web3modal_checkout_link = helpers.make_checkout_web3modal_url( "ETH", 10000, "0xtest1"