Ejemplo n.º 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"])
Ejemplo n.º 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"])
Ejemplo n.º 3
0
def test_participate_with_signed_address(chain, crowdsale, customer,
                                         customer_id, token, private_key):
    """Buy tokens with a proper signed address."""

    address_bytes = get_address_as_bytes(customer)
    sign_data = sign(address_bytes, private_key)

    time_travel(chain, crowdsale.functions.startsAt().call() + 1)
    wei_value = to_wei(1, "ether")
    assert crowdsale.functions.getState().call() == CrowdsaleState.Funding
    crowdsale.functions.buyWithSignedAddress(customer_id, sign_data["v"],
                                             sign_data["r_bytes"],
                                             sign_data["s_bytes"]).transact({
                                                 "from":
                                                 customer,
                                                 "value":
                                                 wei_value
                                             })

    # We got credited
    assert token.functions.balanceOf(customer).call() > 0

    # We have tracked the investor id
    events = crowdsale.events.Invested().createFilter(
        fromBlock=0).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
Ejemplo n.º 4
0
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
Ejemplo n.º 5
0
def test_erc865(chain, security_token, team_multisig, customer, private_key, signer_address):
    check_gas(chain, security_token.transact({"from": team_multisig}).transfer(signer_address, 1234))

    token_addr = int(security_token.address, 16).to_bytes(20, byteorder="big")
    to_addr = int(team_multisig, 16).to_bytes(20, byteorder="big")
    value = int(123).to_bytes(32, byteorder="big")
    fee = int(123).to_bytes(32, byteorder="big")
    nonce = int(123).to_bytes(32, byteorder="big")
    prefix = int(0x48664c16).to_bytes(4, byteorder="big")
    payload = prefix + token_addr + to_addr + value + fee + nonce
    signed_data = sign(payload, private_key, hash_function=keccak)
    key_raw = signed_data["r_bytes"] + signed_data["s_bytes"] + signed_data["v"].to_bytes(1, byteorder="big")

    security_token.transact({"from": customer}).transferPreSigned(key_raw, team_multisig, 123, 123, 123)
Ejemplo n.º 6
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
Ejemplo n.º 7
0
def test_participate_bad_signature(chain, crowdsale, customer, customer_id,
                                   token):
    """Investment does not happen with a bad signature.."""

    address_bytes = get_address_as_bytes(customer)
    sign_data = sign(address_bytes, private_key)

    time_travel(chain, crowdsale.call().startsAt() + 1)
    wei_value = to_wei(1, "ether")
    assert crowdsale.call().getState() == CrowdsaleState.Funding

    sign_data["s_bytes"] = b'ABC'  # Corrupt signature data

    with pytest.raises(TransactionFailed):
        crowdsale.transact({
            "from": customer,
            "value": wei_value
        }).buyWithSignedAddress(customer_id, sign_data["v"],
                                sign_data["r_bytes"], sign_data["s_bytes"])
Ejemplo n.º 8
0
def test_kyc_participate_bad_signature(chain, kyc_crowdsale, customer,
                                       customer_id, kyc_token, private_key,
                                       preico_starts_at, pricing,
                                       team_multisig):
    """Investment does not happen with a bad signature.."""

    # 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(1, "ether")

    # KYC limits for this participant: 0...1 ETH
    kyc_payload = pack_kyc_dataframe(customer, customer_id, 0, 1 * 10000)
    signed_data = sign(kyc_payload, private_key + "x")  # 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"])
Ejemplo n.º 9
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"])
Ejemplo n.º 10
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.functions.buyWithKYCData(
            kyc_payload, signed_data["v"], signed_data["r_bytes"],
            signed_data["s_bytes"]).transact({
                "from": customer,
                "value": wei_value
            })
Ejemplo n.º 11
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