Skip to main content
deleted 4 characters in body
Source Link

I am trying to write a smart contract which will do the below.

contract MyContract is ERC20{
IERC20 market;
IERC20 coin;
uint256 feesFactor = 0;
uint256 conversionRatio = 0;

    function setConversion(uint256 ratio) public {
        conversionRatio = ratio/100;ratio;
    }

    function getConversion() public view returns (uint256) {
        return conversionRatio;
    }

    function setFeesFactor(uint256 fees) public {
        feesFactor = fees;
    }


    function deposit(uint256 _amount) external onlyPositive(_amount) {
        

        coin.transferFrom(msg.sender, address(this), _amount);

        uint256 fees = _amount * feesFactor;

        market.mint(_amount - fees);

        uint256 tokens = (_amount - swapFees) * getConversion();
        _mint(msg.sender, tokens);

    }
}

Using web3js I attempt to pass .03 for the feeFactor and .04 for the conversionRatio using web3.utils.toWei(). This works and i can display these values after using web3.utils.fromWei().

However my deposit function fails silently; the _amount value is also sent using web3.utils.toWei().

What changes do i need to make on both the smart contract and the web3js code so that i can use decimals like .03 and .04 and get the deposit function to work.

Yes approval has been done on the coin before deposit is called. Thanks.

I am trying to write a smart contract which will do the below.

contract MyContract is ERC20{
IERC20 market;
IERC20 coin;
uint256 feesFactor = 0;
uint256 conversionRatio = 0;

    function setConversion(uint256 ratio) public {
        conversionRatio = ratio/100;
    }

    function getConversion() public view returns (uint256) {
        return conversionRatio;
    }

    function setFeesFactor(uint256 fees) public {
        feesFactor = fees;
    }


    function deposit(uint256 _amount) external onlyPositive(_amount) {
        

        coin.transferFrom(msg.sender, address(this), _amount);

        uint256 fees = _amount * feesFactor;

        market.mint(_amount - fees);

        uint256 tokens = (_amount - swapFees) * getConversion();
        _mint(msg.sender, tokens);

    }
}

Using web3js I attempt to pass .03 for the feeFactor and .04 for the conversionRatio using web3.utils.toWei(). This works and i can display these values after using web3.utils.fromWei().

However my deposit function fails silently; the _amount value is also sent using web3.utils.toWei().

What changes do i need to make on both the smart contract and the web3js code so that i can use decimals like .03 and .04 and get the deposit function to work.

Yes approval has been done on the coin before deposit is called. Thanks.

I am trying to write a smart contract which will do the below.

contract MyContract is ERC20{
IERC20 market;
IERC20 coin;
uint256 feesFactor = 0;
uint256 conversionRatio = 0;

    function setConversion(uint256 ratio) public {
        conversionRatio = ratio;
    }

    function getConversion() public view returns (uint256) {
        return conversionRatio;
    }

    function setFeesFactor(uint256 fees) public {
        feesFactor = fees;
    }


    function deposit(uint256 _amount) external onlyPositive(_amount) {
        

        coin.transferFrom(msg.sender, address(this), _amount);

        uint256 fees = _amount * feesFactor;

        market.mint(_amount - fees);

        uint256 tokens = (_amount - swapFees) * getConversion();
        _mint(msg.sender, tokens);

    }
}

Using web3js I attempt to pass .03 for the feeFactor and .04 for the conversionRatio using web3.utils.toWei(). This works and i can display these values after using web3.utils.fromWei().

However my deposit function fails silently; the _amount value is also sent using web3.utils.toWei().

What changes do i need to make on both the smart contract and the web3js code so that i can use decimals like .03 and .04 and get the deposit function to work.

Yes approval has been done on the coin before deposit is called. Thanks.

Source Link

Need to send decimal number to smart contract for calculations

I am trying to write a smart contract which will do the below.

contract MyContract is ERC20{
IERC20 market;
IERC20 coin;
uint256 feesFactor = 0;
uint256 conversionRatio = 0;

    function setConversion(uint256 ratio) public {
        conversionRatio = ratio/100;
    }

    function getConversion() public view returns (uint256) {
        return conversionRatio;
    }

    function setFeesFactor(uint256 fees) public {
        feesFactor = fees;
    }


    function deposit(uint256 _amount) external onlyPositive(_amount) {
        

        coin.transferFrom(msg.sender, address(this), _amount);

        uint256 fees = _amount * feesFactor;

        market.mint(_amount - fees);

        uint256 tokens = (_amount - swapFees) * getConversion();
        _mint(msg.sender, tokens);

    }
}

Using web3js I attempt to pass .03 for the feeFactor and .04 for the conversionRatio using web3.utils.toWei(). This works and i can display these values after using web3.utils.fromWei().

However my deposit function fails silently; the _amount value is also sent using web3.utils.toWei().

What changes do i need to make on both the smart contract and the web3js code so that i can use decimals like .03 and .04 and get the deposit function to work.

Yes approval has been done on the coin before deposit is called. Thanks.