示例#1
0
def test_halted(chain, web3, kyc_presale, customer, customer_id, private_key,
                preico_starts_at, team_multisig, pricing_info, signer_address):
    """We cannot buy after a halt."""

    # Check KYC crowdsale is good to go
    time_travel(chain, kyc_presale.call().startsAt() + 1)

    # Check the setup looks good
    assert kyc_presale.call().getState() == CrowdsaleState.Funding
    assert kyc_presale.call().signerAddress().lower() == signer_address.lower()

    # Do a test buy for 0.5 ETH and check it is good token wise
    wei_value = to_wei(1.0, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0,
                                             1 * 10000, pricing_info)
    signed_data = sign(kyc_payload, private_key)

    kyc_presale.transact({"from": team_multisig}).halt()

    with pytest.raises(TransactionFailed):
        kyc_presale.transact({
            "from": customer,
            "value": wei_value,
            "gas": 2222333
        }).buyWithKYCData(kyc_payload, signed_data["v"],
                          signed_data["r_bytes"], signed_data["s_bytes"])
示例#2
0
def test_kyc_participate_over_payment(chain, kyc_presale, customer,
                                      customer_id, private_key,
                                      preico_starts_at, team_multisig,
                                      pricing_info):
    """KYC'ed participant does not fulfill his minimum limit."""

    # Check KYC crowdsale is good to go
    time_travel(chain, kyc_presale.call().startsAt() + 1)

    wei_value = to_wei(1, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0,
                                             10 * 10000, pricing_info)
    signed_data = sign(kyc_payload, private_key)  # Use bad private key

    kyc_presale.transact({
        "from": customer,
        "value": wei_value,
        "gas": 2222333
    }).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"],
                      signed_data["s_bytes"])

    with pytest.raises(TransactionFailed):
        wei_value = to_wei(10, "ether")
        kyc_presale.transact({
            "from": customer,
            "value": wei_value,
            "gas": 2222333
        }).buyWithKYCData(kyc_payload, signed_data["v"],
                          signed_data["r_bytes"], signed_data["s_bytes"])
def test_kyc_participate_with_signed_address(chain, kyc_crowdsale, customer,
                                             customer_id, kyc_token,
                                             private_key, preico_starts_at,
                                             pricing, team_multisig):
    """Buy tokens with a proper KYC payload."""

    # Check KYC crowdsale is good to go
    time_travel(chain, kyc_crowdsale.call().startsAt() + 1)
    event_filter = kyc_crowdsale.events.Invested().createFilter(fromBlock=0)
    # Check the setup looks good
    assert kyc_crowdsale.functions.getState().call() == CrowdsaleState.Funding
    assert kyc_crowdsale.functions.isFinalizerSane().call()
    assert kyc_crowdsale.functions.isPricingSane().call()
    assert kyc_crowdsale.functions.beneficiary().call() == team_multisig
    assert kyc_token.functions.transferAgents(team_multisig).call() == True

    # Do a test buy for 1 ETH and check it is good token wise
    wei_value = to_wei(1, "ether")
    tokens_per_eth = pricing.functions.calculatePrice(wei_value, wei_value, 0,
                                                      customer, 18).call()
    assert tokens_per_eth == 10**18
    assert kyc_crowdsale.functions.getTokensLeft().call() >= tokens_per_eth
    assert kyc_token.functions.balanceOf(
        team_multisig).call() >= tokens_per_eth
    assert not kyc_crowdsale.functions.isBreakingCap(
        wei_value, tokens_per_eth, wei_value, tokens_per_eth).call()

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0,
                                             1 * 10000, 1 * 10**18)
    signed_data = sign(kyc_payload, private_key)

    kyc_crowdsale.functions.buyWithKYCData(kyc_payload, signed_data["v"],
                                           signed_data["r_bytes"],
                                           signed_data["s_bytes"]).transact({
                                               "from":
                                               customer,
                                               "value":
                                               wei_value
                                           })
    # We got credited
    assert kyc_token.functions.balanceOf(customer).call() > 0
    assert kyc_crowdsale.functions.investedAmountOf(
        customer).call() == 1 * 10**18

    # We have tracked the investor id
    events = event_filter.get_all_entries()
    assert len(events) == 1
    e = events[0]
    assert e["args"]["investor"] == customer
    assert e["args"]["weiAmount"] == wei_value
    assert e["args"]["customerId"] == customer_id.int
def test_roundtrip_kyc_data(kyc_deserializer, whitelisted_address):
    """We correctly encode data in Python side and decode it back in the smart contract."""

    customer_id = uuid.uuid4()
    dataframe = pack_kyc_pricing_dataframe(whitelisted_address, customer_id,
                                           int(0.1 * 10000), int(9999 * 10000),
                                           1)
    tuple_value = kyc_deserializer.call().getKYCPayload(dataframe)

    #
    # Check that the output looks like what we did expect
    #

    assert tuple_value[0].lower() == whitelisted_address.lower()
    # Do a raw integer comparison, because web3.py and UUID disagree about left padding zeroes
    assert tuple_value[1] == customer_id.int
    assert tuple_value[2] == 1000
    assert tuple_value[3] == 99990000
示例#5
0
def test_kyc_participate_with_signed_address(chain, web3, kyc_presale,
                                             customer, customer_id,
                                             private_key, preico_starts_at,
                                             team_multisig, pricing_info,
                                             signer_address):
    """Buy tokens with a proper KYC payload."""

    # Check KYC crowdsale is good to go
    time_travel(chain, kyc_presale.call().startsAt() + 1)

    # No contributions yet
    original_multisig_balance = web3.eth.getBalance(team_multisig)

    # Check the setup looks good
    assert kyc_presale.call().getState() == CrowdsaleState.Funding
    assert kyc_presale.call().signerAddress().lower() == signer_address.lower()

    # Do a test buy for 0.5 ETH and check it is good token wise
    wei_value = to_wei(1.0, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0,
                                             1 * 10000, pricing_info)
    signed_data = sign(kyc_payload, private_key)

    kyc_presale.transact({
        "from": customer,
        "value": wei_value,
        "gas": 2222333
    }).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"],
                      signed_data["s_bytes"])

    # Money lands in the multisig wallet
    assert kyc_presale.call().investedAmountOf(customer) == 1 * 10**18
    assert web3.eth.getBalance(team_multisig) > original_multisig_balance

    # We have tracked the investor id
    events = kyc_presale.pastEvents("Prepurchased").get()
    assert len(events) == 1
    e = events[0]
    assert e["args"]["investor"].lower() == customer.lower()
    assert e["args"]["weiAmount"] == wei_value
    assert e["args"]["customerId"] == customer_id.int
    assert e["args"]["pricingInfo"] == pricing_info
示例#6
0
def test_kyc_participate_whitelist(chain, kyc_presale, customer, customer_id,
                                   private_key, preico_starts_at,
                                   team_multisig, pricing_info):
    """Early whitelist buyer gets through despite time checks."""

    # Check KYC crowdsale is closed, too early
    time_travel(chain, kyc_presale.call().startsAt() - 1)
    assert kyc_presale.call().getState() == CrowdsaleState.PreFunding

    # Do a test buy for 1 ETH and check it is good token wise
    wei_value = to_wei(1, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0,
                                             1 * 10000, pricing_info)
    signed_data = sign(kyc_payload, private_key)

    with pytest.raises(TransactionFailed):
        kyc_presale.transact({
            "from": customer,
            "value": wei_value,
            "gas": 2222333
        }).buyWithKYCData(kyc_payload, signed_data["v"],
                          signed_data["r_bytes"], signed_data["s_bytes"])

    # Whitelist this participant
    kyc_presale.transact({
        "from": team_multisig
    }).setEarlyParicipantWhitelist(customer, True)

    # Now we can buy despite the time limti
    kyc_presale.transact({
        "from": customer,
        "value": wei_value,
        "gas": 2222333
    }).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"],
                      signed_data["s_bytes"])
示例#7
0
def test_kyc_participate_bad_signature(chain, kyc_presale, customer,
                                       customer_id, private_key,
                                       preico_starts_at, team_multisig,
                                       pricing_info):
    """Investment does not happen with a bad signature.."""

    # Check KYC crowdsale is good to go
    time_travel(chain, kyc_presale.call().startsAt() + 1)

    # Do a test buy for 1 ETH and check it is good token wise
    wei_value = to_wei(1, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0,
                                             1 * 10000, pricing_info)
    signed_data = sign(kyc_payload, private_key + "x")  # Use bad private key

    with pytest.raises(TransactionFailed):
        kyc_presale.transact({
            "from": customer,
            "value": wei_value,
            "gas": 2222333
        }).buyWithKYCData(kyc_payload, signed_data["v"],
                          signed_data["r_bytes"], signed_data["s_bytes"])
示例#8
0
def test_kyc_participate_under_payment(chain, kyc_crowdsale, customer,
                                       customer_id, kyc_token, private_key,
                                       preico_starts_at, pricing,
                                       team_multisig):
    """KYC'ed participant does not fulfill his minimum limit."""

    # Check KYC crowdsale is good to go
    time_travel(chain, kyc_crowdsale.call().startsAt() + 1)

    # Do a test buy for 1 ETH and check it is good token wise
    wei_value = to_wei(0.1, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id,
                                             int(0.5 * 10000), 1 * 10000, 1)
    signed_data = sign(kyc_payload, private_key)  # Use bad private key

    with pytest.raises(TransactionFailed):
        kyc_crowdsale.transact({
            "from": customer,
            "value": wei_value,
            "gas": 2222333
        }).buyWithKYCData(kyc_payload, signed_data["v"],
                          signed_data["r_bytes"], signed_data["s_bytes"])
示例#9
0
def test_kyc_participate_with_different_price(chain, web3, kyc_crowdsale,
                                              customer, customer_id, kyc_token,
                                              private_key, preico_starts_at,
                                              pricing, team_multisig):
    """The same user buys token with two different prices (as given by the server)."""

    # Check KYC crowdsale is good to go
    whitelisted_address = customer
    time_travel(chain, kyc_crowdsale.call().startsAt() + 1)
    start_multisig_total = web3.eth.getBalance(team_multisig)

    # Check the setup looks good
    assert kyc_crowdsale.call().getState() == CrowdsaleState.Funding
    assert kyc_crowdsale.call().isFinalizerSane()
    assert kyc_crowdsale.call().isPricingSane()
    assert kyc_crowdsale.call().beneficiary() == team_multisig
    assert kyc_token.call().transferAgents(team_multisig) == True
    assert kyc_crowdsale.call().investedAmountOf(whitelisted_address) == 0

    # Do a test buy for 1 ETH
    wei_value = to_wei(1, "ether")
    excepted_token_value = int(0.5 * 10**18)
    price = 2 * 10**18  # wei per token

    assert kyc_crowdsale.call().calculateTokens(wei_value,
                                                price) == excepted_token_value

    # Buy with price 1 token = 2 wei
    kyc_payload = pack_kyc_pricing_dataframe(whitelisted_address, customer_id,
                                             0, 1 * 10000, price)
    signed_data = sign(kyc_payload, private_key)
    unpacked = unpack_kyc_pricing_dataframe(kyc_payload)
    assert unpacked["pricing_data"] == price

    kyc_crowdsale.transact({
        "from": whitelisted_address,
        "value": wei_value,
        "gas": 2222333
    }).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"],
                      signed_data["s_bytes"])

    # We got credited
    assert kyc_token.call().balanceOf(
        whitelisted_address) == excepted_token_value
    assert kyc_crowdsale.call().investedAmountOf(
        whitelisted_address) == wei_value

    # We have tracked the investor id
    events = kyc_crowdsale.pastEvents("Invested").get()
    assert len(events) == 1
    e = events[0]
    assert e["args"]["investor"].lower() == whitelisted_address.lower()
    assert e["args"]["weiAmount"] == wei_value
    assert e["args"]["customerId"] == customer_id.int
    assert e["args"]["tokenAmount"] == excepted_token_value

    # More tokens, different price this time
    wei_value = to_wei(1, "ether")
    new_excepted_token_value = int(0.25 * 10**18)
    price = 4 * 10**18  # wei per token

    # New transaction, increased per person cap to 2 ETH
    kyc_payload = pack_kyc_pricing_dataframe(whitelisted_address, customer_id,
                                             0, 2 * 10000, price)
    signed_data = sign(kyc_payload, private_key)
    kyc_crowdsale.transact({
        "from": whitelisted_address,
        "value": wei_value,
        "gas": 333444
    }).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"],
                      signed_data["s_bytes"])

    # We got credited
    total = wei_value * 2
    assert kyc_token.call().balanceOf(
        whitelisted_address) == excepted_token_value + new_excepted_token_value
    assert kyc_crowdsale.call().investedAmountOf(whitelisted_address) == total
    assert web3.eth.getBalance(team_multisig) == start_multisig_total + total

    # We have another event, this time with new price
    events = kyc_crowdsale.pastEvents("Invested").get()
    assert len(events) == 2
    e = events[-1]
    assert e["args"]["investor"].lower() == whitelisted_address.lower()
    assert e["args"]["weiAmount"] == wei_value
    assert e["args"]["customerId"] == customer_id.int
    assert e["args"]["tokenAmount"] == new_excepted_token_value