Solidity converting decimal value to zero - solidity

I'm building a function that divides 40% of revenue with current supply and multiply that with holder's balance to calculate the token amount sent to token holder on the basis of balance.
revenue = 100000, totalSupply = 500000000, holdersBalance = 1000
It should return 0.08 as distribution amount, while I'm getting 0. Is there any way to fix this? any help will be appreciated, thanks
uint256 public revenue;
uint256 public distributionAmount; //Get final amount to distribute
function calculate(uint256 _revenue, uint256 _totalSupply, uint256 holdersBalance) public {
revenue = _revenue;
uint256 amount = (revenue * 40)/100;
amount = ((amount / _totalSupply) * holdersBalance);
distributionAmount = amount; //should get 0.08 in distributionAmount, but getting zero
}

Related

How do I increase the token amount? (BEP20)

I created BEP20 token via remix, now I want to increase this token amount. How can I make 100 thousand tokens 200 thousand?
you need add a mint function, like this blow
function issue(uint amount) public onlyOwner {
require(_totalSupply + amount > _totalSupply);
require(balances[owner] + amount > balances[owner]);
balances[owner] += amount;
_totalSupply += amount;
Issue(amount);
}

How can I update the price of Matic on realtime inside a smart contract

I'm working on a smart contract that allows users to pay for monthly subscriptions like Netflix, Amazon, etc...
Assuming that each subscription costs 20$ per month which is equivalent to 22 Matic at the time of writing this post.
During the time of the subscription, the user should pay 20$ per month with Matic, and its value will vary.
How can I update the monthly payment inside the smart contract so the subscriber will be able to pay each with the current value of MATIC not more not less? It's possible in solidity?
Or should I let the user pay his subscription manually each month based on the price of each subscription in dollars but with MATIC?
I tried to implement a solution with Tellor in my smart contract but I got stuck.
// smart contract subscription(part of it)
struct Subscription {
address payable subscriber;
uint start;
uint nextPayment;
bool activated;
}
/* nested mapping from address to id to Subscription */
mapping(address => mapping(uint => Subscription)) private AllSubscriptions;
/* Pay subcription every Month*/
function pay(uint256 planId)
external payable
onlyUsers()
{
Subscription storage submitSubscription = AllSubscriptions[msg.sender][planId];
require(block.timestamp > submitSubscription.nextPayment, " Payement not due yet");
Plan storage plan = idToPlan[planId];
require(msg.value >= plan.monthlyPayment, " Monthly payment not correct");
emit PaymentSent(
payable(msg.sender),
payable(address(this)),
plan.monthlyPayment,
planId,
block.timestamp);
totalPaymentsPerWallet[msg.sender] += 1;
submitSubscription.nextPayment = submitSubscription.nextPayment + 4 weeks;
}
// UsingTellor functionnalities
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "usingtellor/contracts/UsingTellor.sol";
contract PriceContract is UsingTellor {
uint256 public btcPrice;
//This Contract now has access to all functions in UsingTellor
constructor(address payable _tellorAddress) UsingTellor(_tellorAddress) public {}
function setBtcPrice() public {
bytes memory _b = abi.encode("SpotPrice",abi.encode("MATIC","USD"));
bytes32 _queryID = keccak256(_b);
bool _didGet;
uint256 _timestamp;
bytes _value;
(_didGet, _value, _timestamp) = getDataBefore(_queryID);
uint256 maticPrice = abi.decode(_value,(uint256));
require(token.transferFrom(subscriber,address(this),cost/maticPrice));
}
}
You can use any of the Chainlink data feeds that return the current price of MATIC in USD. It's a free service, there's no LINK payment for data feeds.
Docs and code example: https://docs.chain.link/docs/get-the-latest-price/

Confused setting price/ratio per token in solidity

I am trying to set the price ratio 1 token per 0.01 USDT
Code
uint256 public priceTokenPerDai = 10000000000000000;
function calculateAmountTokensPurchased(uint256 _amountPaid)
public
view
returns (uint256)
{
console.log("_amountPaid %s", _amountPaid);
console.log("_____priceTokenPerDai %s", priceTokenPerDai);
return priceTokenPerDai / _amountPaid;
}
Ouput
_amountPaid 10000000000000000000000000000000000
_____priceTokenPerDai 10000000000000000
BigNumber { value: "1" }
_amountPaid 1
_____priceTokenPerDai 10000000000000000
I'm a bit confused because decimals in solidity don't exist. I want to make a purchase of 1 cent for 1 token
well in solidity usually most tokens use 18 decimals (this isn't always true so is better to check it), so if you want to set the price of 1 per 0.1, the amount paid should also include the decimals if not you are "sending" 0.000000000000000001 tokens instead of 1

solidity global variables uint256 in division its not work

example:
uint256 unit = 10000000000000000 * (100 / 1000);
it is work.
global variables:
uint256 ratio = 100; //in global
uint256 unit = 10000000000000000 * (ratio / 1000); //in function
not work, the result always be 0.
In Solidity fractional division is not supported. As to how the division works you can refer to the documentation. What is happening in your code is the result of the division rounding down to 0.
You can find information about how fixed point variables work in Solidity here. You can try to use these but you should really try to avoid using fractional numbers in your code.
In your case you can replace the expression with uint256 unit = 10000000000000000 / (1000 / ratio); as long as ratio < 1000

Smart contract Token distribution solution

I have a question regard to the token sale: How token distribution work?
An example:
10% for private sale
30% for public sale
20% for dev team
40% for rewards
The number of token will be transferred to these address at the time the token created or it will be transferred to these address after that?
It depends on the token contract implementation, can be both.
Example that mints 100 tokens to public sale available right after token creation, and reserves another 50 for later vesting by a dev team address:
pragma solidity ^0.8;
contract MyToken {
mapping (address => uint256) _balances;
mapping (address => Vesting) _vesting;
struct Vesting {
uint256 amount;
uint64 unlockTime;
}
constructor() {
address publicSaleAddress = address(0x123);
address devTeamAddress = address(0x456);
// set the `publicSaleAddress` balance right away
_balances[publicSaleAddress] = 100;
// don't set the `devTeamAddress` balance now
// instead, allow them to claim it after some time
uint64 deadline = uint64(block.timestamp) + 365 days;
_vesting[devTeamAddress] = Vesting(
50,
deadline
);
}
function unvest() external {
// check if they have something to unvest
require(_vesting[msg.sender].amount > 0, 'Nothing to unvest');
// check if they are allowed to unvest yet
require(_vesting[msg.sender].unlockTime >= block.timestamp, 'Not yet');
// increase their balance
_balances[msg.sender] += _vesting[msg.sender].amount;
// prevent them from claiming the balance again
_vesting[msg.sender] = Vesting(0, 0);
}
}