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

add uniswap url parameter & add uniswap for arbitrum #200

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pretix_eth/network/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
57 changes: 21 additions & 36 deletions pretix_eth/network/tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
19 changes: 14 additions & 5 deletions tests/tokens/test_token_and_registries_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,53 @@ 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

assert_error_when_init_variables_not_passed(Test)


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()
Expand Down
11 changes: 10 additions & 1 deletion tests/tokens/test_token_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Test(IToken):
NETWORK_VERBOSE_NAME = "Test Network"
TOKEN_SYMBOL = "T"
CHAIN_ID = 1
NETWORK_UNISWAP_NAME = "mainnet"

return Test()

Expand Down Expand Up @@ -82,14 +83,22 @@ 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
== "https://uniswap.exchange/send?exactField=output&exactAmount=1000&outputCurrency=ETH&recipient=0xtest1" # noqa: E501
)


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"
Expand Down