# Questions tagged [safemath]

The tag has no usage guidance.

36 questions
Filter by
Sorted by
Tagged with
24 views

### Which compiler version should I use?

// SPDX-License-Identifier: MIT pragma solidity >=0.6.0 <0.9.0; import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; import "@chainlink/contracts/src/v0....
14 views

### broken for loop

can someone let me know what i got wrong in this for loop, if you need the full code let me know. function calculateSplitPercent() public { for(uint256 i = 0; i >= uniqueIdCounter; i++) { ...
29 views

### Can someone please tell me what i have wrong here

function calculateSplitPercent() public { for(uint256 i = 0; i >= uniqueIdCounter; i++) { uint256 spliPercent = balanceOf[uniqueIdToAddress[i]].div(totalFunded).mul(100); ...
21 views

### Percentages in solidity

How would i work out "x is what percent of y" or for example 5 is what percent 10 = 50%. For some reason i just cant wrap my head around it. A clip of my code is below, i want to work out ...
10 views

### Is this contract is safe even without SafeMath?

I'm learning solidity and playing around with smart contracts. I came across this code on github and at first sight it appears to be a super naive implementation that doesn't use SafeMath (or solidity ...
• 1
15 views

### dealing with rounding for share issuance based on incoming investment size, total shares outstanding, and total equity

This is probably a fairly straightforward question about rounding errors, but it may help for me to add the context in which this is meaningful. struct EntityToBeInvestedIn { bool exists; ...
• 25
14 views

### Reflect.finance token-type deployment: change distribution on constructor

I am developing a token based on reflect.finance / safemoon way of work with reflections between holders, but I always reach an error when trying to distribute the initial token supply between ...
• 13
1 vote
179 views

### 'Error: Member "mod" not found or not visible after argument-dependent lookup in uint256' when trying to use Chainlink

I got this error while trying to compile function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override { randomResult = randomness.mod(20).add(1); } I saw it in ...
379 views

### calculate percentage in solidity using SafeMath

I hope this question is not asked, but i googled too much and tried to find out the problem my self. I'm trying to get a dynamic percentage using SafeMath uint256 lowNumber = 316442921; uint256 ...
17 views

### how to change the code of the Smart contract to solidity?

how to change the “safemoon” or “open zeppelin ERC20” contract so that a total of 777T coins are created, transaction fee is 1%, liquidity fee is 1% and burnout is 1% with each transaction
• 123
5k views

### What is the purpose of "unchecked" in Solidity?

According to Solidity documentation: Prior to Solidity 0.8.0, arithmetic operations would always wrap in case of under- or overflow leading to widespread use of libraries that introduce additional ...
• 574
1 vote
143 views

### Multiply number with Pi in solidity

I'm finding a library to implement this formula in solidity. Is there is a library to implement this formula.
• 591
55 views

### Does using SafeMath for uint is herited from child contract

I have this code: contract A { using SafeMath for uint; ... } contract B is A { ... } Does my contract B herits from "using SafeMath for uint;"?
113 views

### Calculate Percentage

I want to calculate user reward per day. uint256 totalBalance = 111; uint256 rewardPerDay = 1000; function calculateReward(uint256 userBalance) public view returns (uint256) { uint256 ...
1 vote
66 views

### Where to find SafeMath pragma solidity ^0.6.6?

While trying to use SafeMath to compute the modulo of a uint256 like shown in this video:https://www.youtube.com/watch?v=JqZWariqh5s (without SafeMath) I'm having some trouble finding a pragma ...
• 33
20 views

### SafeMath Utility Error in Ethereum Document 'Unique Digital Asset' Code

I refer to the example code 'Unique Digital Asset' code in Ethereum Doc (https://ethereum.org/en/developers/docs/smart-contracts/anatomy/#unique-digital-asset) line 60. uint256 id = SafeMath.sub(...
68 views

### OpenZeppelin import problem

I am trying to import a couple of OpenZeppelin contracts to my project, but each time I am given an error related to unsuccessful imports from sources. I'm gonna attach a screenshot of my code. I have ...
• 121
1 vote
37 views

### Loaded Die Algorithm in Solidity?

I am trying to implement a smart contract, that takes in a discreet distribution and returns a loaded random number. The higher the number higher the chance from 1-10000. **anyone has any experience ...
• 43
2k views

### NotFoundError: Key not found in database [hn]

I'm playing Ethernaut (https://ethernaut.openzeppelin.com/). I'm currently on level 3 (Coin Flip). This is the Coin Flip contract: // SPDX-License-Identifier: MIT pragma solidity ^0.6.0; import '@...
182 views

### Round integer down to nearest 10

Is there a good way of floor rounding a number to the nearest 10 in solidity? I have a function that calculates a percentage of a number but I want only to return multiples of 10. e.g. if it is 12% ...
• 341
1 vote
496 views

### Using SafeMath with older Solidity compiler

I've been told SafeMath doesn't matter. Does SafeMath slow things down, cost more ETH, or does it add an extra level of stability? Will SafeMath be unsupported one day? contract SafeMath { function ...
115 views

### How to add three numbers using safemath?

I know this might sound obvious but in solidity you can never be too sure. Let us say I have three numbers 5, 7 and 8. Can I add them like so: using SafeMath for uint256; 5.add(7.add(8)); // or even ...
• 1,243
65 views

### How to make a multiplication and division without overflow and any loss of precision in Vyper

How can I calculate X * Y / Z in vyper uint256 without overflow and any loss of precision? I want to build Vyper FullMath library functions similar to the Uniswap V3 FullMath library written in ...
• 116
2k views

### TypeError: Member "add" not found or not visible after argument-dependent lookup in uint256

i have created a token ....but it gives me error " erc20.sol:94:25: TypeError: Member "add" not found or not visible after argument-dependent lookup in uint256. balances[account] = ...
• 21
1 vote
32 views

### Why is the math code causing transactions to fail

Ok so This is my first time working on solidity contracts and I'm at a loss how to debug this code. This code fails: function claimReturns(address claimant) private { uint otherTotals = ...
• 115
91 views

### I want to store decimal value in a storage variable and later use that variable at same precision in the formula later internally

I want to store uint public MINTING_VELOCITY = 0.000002; And later use it in the formula: A x t x MINTING_VELOCITY ,where A = 2000 ,t(staking_duration_time) = 120 Issue is currently MINTING_VELOCITY ...
8k views

### Is the SafeMath library obsolete in solidity 0.8.0?

SafeMath was commonly used to revert the transaction on overflow and underflow, instead of allowing the number to wrap around. The solidity 0.8.0 release notes state: Code Generator: All arithmetic ...
• 8,907
1 vote
287 views

### How can I use decimal in solidity

how can i calculate: 1083.83 / 8.25 = 131.37. As I see its not possible in simple way. I have tried a lot of math function but not works. Is there any solution for this?
• 41
450 views

### what happens if I do using SafeMath for uint256 but then divide by uint8?

example using SafeMath for uint256; uint8 number8 = 10; uint256 number256 = 1000; function something() { return number256.div(number8); } is it still using safemath even if my number8 is uint8?
• 217
750 views

### Chainlink import causing Truffle console to crash

Related issue on GitHub: https://github.com/trufflesuite/truffle/issues/3329 If I compile my contracts with truffle compile and then open the truffle console with truffle develop then this happens: ...
156 views

### Efficient, safe pow using EXP opcode

I recently stumbled across ds-math's rpow() being a O(lg n) safe implementation of exponentiation. Such a function is ultimately created because the EVM EXP opcode is unsafe wrt. overflows. The ...
• 123
83 views

### Is there a tool to add SafeMath to production code?

There are a lot of tools to prepare JavaScript/CSS code for production, e.g. to minimize, to convert ES8 code to ES5. Is there a tool to automatically apply SafeMath library to a .sol file? The ...
• 163
146 views

### Using Fractions in Leasing contract

I want to create a smart contract that allows people to lend dai stable coin to other or a smart contract at an interest. suppose at 4 % interest I need to calculate (1+0.04/12)^n (n is the period in ...
355 views

### Safemath Library version for Solidity v0.5?

Is there a version of the SafeMath library that is compliant to SOlidity v0.5? Thanks for your inputs in advance
74 views

### Why is my calculation broken?

I have a smart contract that holds a token balance, let's call it Token A, users may send this smart contract Token B to receive Token A, they do so by specifying the amount of Token A they would like ...
• 3
2k views

### Compilation error using SafeMath

I am trying to do subtraction operation using the SafeMath library. The following is my code: pragma solidity ^0.5.1; library SafeMath { function sub(uint256 a, uint256 b) internal pure returns (...
• 1,185