So I am having difficulty with multiplying in Wei.
======================================================================= Here is the short version:
I want to multiply 7000 ether by 1.25 to get 8750 ether as the expected result.
=======================================================================
Here is the long confusion version:
I have a Smart Contract that pulls the USD value of ETH then performs a conversion of the USD value at 75 cents on the dollar an issues the tokens appropriately.
My issue is multiplying in Wei.
I would have loved to do all the calculations in Wei but I need to have a USD conversion as part of the formula.
I do want to continue using SafeMath in the calculations.
Say I have 7 ETH sent to the contract The ETH price is 1,000 USD at the time Which makes the USD value at 7,000 The exchange rate is 0.001 USD to ETH
The tokens are issued at .75 to 1 dollar so I multiply by 1.25 to get the 8,750 tokens as the final number in regular math
I have all of it except multiplying by 1.25
I assumed I would multiply by 1250000000000000000 Wei for 1.25 but my final number it way to large.
Whats the right way to perform that math in solidity?
Here is my code:
pragma solidity ^0.4.18;
import "./SafeMath.sol";
import "./Ownable.sol";
import "./usingOraclize.sol";
contract Test is Ownable, usingOraclize {
using SafeMath for uint256;
uint public ETHUSDPriceUINT = 1000000000000000000; // this is a place holder for the Oraclize API call back variable
function Test(address _tokenAddress, address _multiSigAddress) {
}
function buyTokens(string _ETHprice) payable returns (uint) {
result = _ETHprice; // provide the current price of ETH in USD
uint ETHContributionInWei = msg.value;
ETHUSDPriceUINT = parseInt(result, 2); // let's use two decimal points
uint exchangeRate = ETHUSDPriceUINT.div(ETHUSDPriceUINT);
uint USD_Value = ETHContributionInWei.mul(ETHUSDPriceUINT);
uint tokenFactor = 1250000000000000000 wei;
uint numTokensIssued = USD_Value.mul(tokenFactor);
return numTokensIssued;
}
function () payable {
}
}