Skip to content

Commit

Permalink
Merge pull request #132 from TokenMarketNet/feat/calculate-tokens-wit…
Browse files Browse the repository at this point in the history
…h-more-accuracy

The raw weis per token is too rough for generic pricing purposes. Ada…
  • Loading branch information
villesundell committed Jun 5, 2018
2 parents 0e7a5f3 + d9e290b commit 43b081f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
13 changes: 10 additions & 3 deletions contracts/CrowdsaleBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,22 @@ contract CrowdsaleBase is Haltable {
}

/**
* @dev Calculate tokens user will have for theirr purchase
* @dev Calculate tokens user will have for their purchase
*
* @param weisTotal How much ethers (in wei) the user putssssss in
* @param pricePerToken What is the price for one token
*
* @return tokensTotal which is weisTotal divided by pricePerToken
* @return tokensTotal which is received tokens, token decimals included
*/
function calculateTokens(uint256 weisTotal, uint256 pricePerToken) public constant returns(uint tokensTotal) {
return weisTotal/pricePerToken;
// pricePerToken is how many full tokens, token decimal place included, you get for wei amount.
// Because, in theory, decimal amount can vary, we do the exponent calculation here,
// though gas wise using 10**18 constant would be much simpler.
// Furthermore we could use rough amounts and take in raw wei per tokens amount,
// but we lose too much accuracy for generic calculations, thus all these are
// practically implemented as 10**18 fixed points.
uint multiplier = 10 ** token.decimals();
return weisTotal.times(multiplier)/pricePerToken;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions ico/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@ def _deploy_contracts(project, chain, web3, yaml_filename, chain_data, deploy_ad

def deploy_crowdsale_from_file(project: Project, yaml_filename: str, deployment_name: str, deploy_address: str):
"""Deploy crowdsale plan."""

if not yaml_filename.endswith(".yml"):
# A stop gap fix
# Otherwise our backup filename generator may get confused
raise RuntimeError("YAML files must have .yml extension")

chain_data = load_crowdsale_definitions(yaml_filename, deployment_name)
chain_name = chain_data["chain"]

Expand Down
8 changes: 4 additions & 4 deletions ico/tests/contracts/test_kyccrowdsale.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_kyc_participate_with_signed_address(chain, kyc_crowdsale, customer, cus
assert not kyc_crowdsale.call().isBreakingCap(wei_value, tokens_per_eth, wei_value, tokens_per_eth)

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

kyc_crowdsale.transact({"from": customer, "value": wei_value, "gas": 2222333}).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"], signed_data["s_bytes"])
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_kyc_participate_over_payment(chain, kyc_crowdsale, customer, customer_i
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, 1)
kyc_payload = pack_kyc_pricing_dataframe(customer, customer_id, 0, 10*10000, 1*10**18)
signed_data = sign(kyc_payload, private_key) # Use bad private key

kyc_crowdsale.transact({"from": customer, "value": wei_value, "gas": 2222333}).buyWithKYCData(kyc_payload, signed_data["v"], signed_data["r_bytes"], signed_data["s_bytes"])
Expand Down Expand Up @@ -246,7 +246,7 @@ def test_kyc_participate_with_different_price(chain, web3, kyc_crowdsale, custom
# Do a test buy for 1 ETH
wei_value = to_wei(1, "ether")
excepted_token_value = int(0.5 * 10**18)
price = 2 # wei per token
price = 2*10**18 # wei per token

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

Expand Down Expand Up @@ -274,7 +274,7 @@ def test_kyc_participate_with_different_price(chain, web3, kyc_crowdsale, custom
# More tokens, different price this time
wei_value = to_wei(1, "ether")
new_excepted_token_value = int(0.25 * 10**18)
price = 4 # wei per token
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)
Expand Down
2 changes: 1 addition & 1 deletion populus.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"provider": {
"class": "web3.providers.rpc.HTTPProvider",
"settings": {
"endpoint_uri": "http://127.0.0.1:8547",
"endpoint_uri": "http://localhost:8547",
"request_kwargs": {
"timeout": 180
}
Expand Down

0 comments on commit 43b081f

Please sign in to comment.