• Stars
    star
    268
  • Rank 153,144 (Top 4 %)
  • Language
    Solidity
  • License
    GNU General Publi...
  • Created over 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Safe arithmetic

DSMath

Safe Arithmetic

DS-Math provides arithmetic functions for the common numerical primitive types of Solidity. You can safely add, subtract, multiply, and divide uint numbers without fear of integer overflow. You can also find the minimum and maximum of two numbers.

Additionally, this package provides arithmetic functions for new two higher level numerical concepts called wad (18 decimals) and ray (27 decimals). These are used to represent fixed-point decimal numbers.

A wad is a decimal number with 18 digits of precision and a ray is a decimal number with 27 digits of precision. These functions are necessary to account for the difference between how integer arithmetic behaves normally, and how decimal arithmetic should actually work. A brief example using wmul, which returns the product of a wad and another number:

1.1 * 2.2 == 2.42

//Regular integer arithmetic adds orders of magnitude:

110 * 220 == 24200

// Wad arithmetic does not add orders of magnitude:

wmul(1.1 ether, 2.2 ether) == 2.42 ether

Naming Convention

The standard functions are the uint set, so their function names are not prefixed: add, sub, mul, min, and max. There is no div function, as divide-by-zero checking is built into the Solidity compiler.

The int functions have an i prefix: imin, and imax.

Wad functions have a w prefix: wmul, wdiv.

Ray functions have a r prefix: rmul, rdiv, rpow.

API Reference

add

Return x + y or an exception in case of uint overflow.

sub

Return x - y or an exception in case of uint overflow.

mul

Return x * y or an exception in case of uint overflow.

min

Return the smaller number of x and y.

max

Return the larger number of x and y.

imin

Return the smaller number of x and y.

imax

Return the larger number of x and y.

wmul

Multiply two Wads and return a new Wad with the correct level of precision. A Wad is a decimal number with 18 digits of precision that is being represented as an integer.

wdiv

Divide two Wads and return a new Wad with the correct level of precision. A Wad is a decimal number with 18 digits of precision that is being represented as an integer.

rmul

Multiply two Rays and return a new Ray with the correct level of precision. A Ray is a decimal number with 27 digits of precision that is being represented as an integer.

rdiv

Divide two Rays and return a new Ray with the correct level of precision. A Ray is a decimal number with 27 digits of precision that is being represented as an integer.

rpow

Raise a Ray to the n^th power and return a new Ray with the correct level of precision. A Ray is a decimal number with 27 digits of precision that is being represented as an integer.

More Repositories

1

dapptools

Dapp, Seth, Hevm, and more
Haskell
2,086
star
2

dappsys

Composable building blocks for Ethereum contracts
Nix
580
star
3

ds-proxy

a proxy object that can compose transactions on owner's behalf
Solidity
311
star
4

dapple

EVM contract system developer multitool
JavaScript
298
star
5

dapp

This repository has been moved to dapphub/dapptools
249
star
6

ds-token

A simple and sufficient ERC20 implementation
Solidity
225
star
7

ds-test

Assertions, equality checks and other test helpers
Solidity
208
star
8

chai

ERC20 wrapper over the Dai Savings Rate
Solidity
147
star
9

seth

(OLD REPO) The command-line Ethereum army knife by DappHub
147
star
10

ds-auth

Updatable, unobtrusive Solidity authorization pattern
Solidity
138
star
11

klab

K framework proof explorer & smart contract specification format
JavaScript
124
star
12

hevm

(OLD REPO) A debug-oriented Ethereum VM (EVM)
114
star
13

dmap

registry contract with no time to argue
JavaScript
58
star
14

ds-dach

Dai Automated Clearing House
Shell
52
star
15

dpack

simple lockfile for your dapp's addresses and artifacts
TypeScript
46
star
16

k-dss

formal verification of multicollateral dai in the K framework
GCC Machine Description
45
star
17

ds-feeds

Simple data access pattern for dumb, durable software objects
Makefile
32
star
18

ds-chief

approval voting to select who wears the hat by consensus
Solidity
29
star
19

ds-weth

ETH->ERC20 with extra opinions
Solidity
27
star
20

ds-pause

Schedule function calls that can only be executed once some delay has elapsed
Solidity
26
star
21

ethrun

Directly run EVM bytecode (using Parity technology)
Rust
24
star
22

ds-vault

DSAuth-protected ERC20 token vault
Solidity
24
star
23

erc20

erc20 interface definition container package
Solidity
24
star
24

ds-guard

Whitelist DSAuthority for use with DSAuth
Solidity
22
star
25

ds-group

Multisig with a command-line interface
Shell
22
star
26

dapp-tools

Code for the dapp.tools website
Nix
21
star
27

dappsys-monolithic

Dappsys for all
Solidity
21
star
28

ds-cabal

Simple m-of-n multisig proxy written in handcrafted EVM bytcode
Makefile
21
star
29

keeper

Admin toolkit for incentive-following software daemons
Shell
19
star
30

ds-roles

A DSAuthority for up to 256 roles
Solidity
17
star
31

ds-note

Log function calls as events
Solidity
17
star
32

ds-value

Set and get a value
Solidity
14
star
33

fv-tutorial

Materials for the devcon4 workshop
GCC Machine Description
14
star
34

ds-eth-token

ERC20 ETH token wrapper. No more special case logic for Ether.
Makefile
13
star
35

ds-exec

Better exception handling
Solidity
9
star
36

ll

Linear Logic references
HTML
8
star
37

awesome-dmap

awesome dmap links
7
star
38

nixpkgs-dapphub

Dapp development overlay for Nix
Nix
7
star
39

locktopus

sqlite file of locked values in the dmap
JavaScript
6
star
40

ds-warp

Time travel for Ethereum
Nix
6
star
41

token

(OLD REPO) Command-line utility for ERC20 tokens
6
star
42

ACS

Applied Computer Science Working Group
6
star
43

ds-pain

Shell
5
star
44

erc20-golf

GCC Machine Description
5
star
45

chronobank-contracts

audit branch of chronobank contracts
JavaScript
5
star
46

ds-items

Ownable, transferrable, non-fungible unsigned integers
Makefile
4
star
47

dapphub.com

HTML
4
star
48

dapphub-registry

JavaScript
4
star
49

ds-forkable

Forkable Datastore Service
4
star
50

ds-cache

DSValue with expiry
Solidity
3
star
51

dapple-script

DappleScript - A simple ethereum interaction language.
JavaScript
3
star
52

ds-thing

Your things should be DSThings
Solidity
3
star
53

dapp-wizard

Racket
3
star
54

remappings-test

dummy project for testing dapp remappings
Solidity
2
star
55

kyo

know-your-origin, don't use this terrible anti-pattern you fool
2
star
56

k

k-fork
Java
2
star
57

ethsign

(OLD REPO) Simple Ethereum transaction signer using Geth as a library
2
star
58

ds-spell

An object that does one thing once.
Makefile
2
star
59

dappsys-graphics

dappsys visualizations
JavaScript
2
star
60

ethkey

Create Ethereum accounts using Geth as a library
Go
2
star
61

ds-store

Dappsys data management components
Makefile
2
star
62

qrtx

JavaScript
2
star
63

dapple-quicktest

Blazingly fast EVM test runner
JavaScript
2
star
64

cage

Nix
2
star
65

maker-docs

Reference documentation for Maker and the Dai Credit System
HTML
2
star
66

libethjet-haskell

2
star
67

tr-base

dummy repo for testing some dapp-remappings stuff
Solidity
1
star
68

lockpack

1
star
69

LLPoster

TeX
1
star
70

dapp.org-old

HTML
1
star
71

guts

1
star
72

ds-delay

Makefile
1
star
73

dapp.tools

Unused - obsolete github pages site for dapp.tools.
HTML
1
star
74

fv-tutorial-solutions

GCC Machine Description
1
star
75

celf-evm

EVM written in celf
JavaScript
1
star
76

llmotivation

Motivational Paper on Linear Logic as a blockchain programming language
TeX
1
star
77

ds-read

Makefile
1
star
78

dapp.org

1
star
79

dapp.coop

HTML
1
star
80

exp-squaring-percise-dapp

Makefile
1
star
81

mod-blockchain

JavaScript
1
star
82

wiki.dapp.coop

JavaScript
1
star
83

klab-explorer.github.io

klab explorer webpage
1
star
84

ds-rpow

Makefile
1
star
85

k-gas-analyser

Haskell
1
star
86

chronobank-review

review of chronobank contracts
HTML
1
star
87

ds-base

building up a reasonable base class
Makefile
1
star
88

ds-stop

DSAuth-protected stop and start
Solidity
1
star
89

dappsys-docs

Python
1
star
90

klab-multipleCalls

proving that one contract can call another one, wow
GCC Machine Description
1
star
91

tr0

dummy repo for testing dapp-remappings
Solidity
1
star
92

k-ds-rpow

SMT
1
star
93

manny

Manny O'Malley, Malicious Oracle
1
star
94

bat-rewrite

Makefile
1
star
95

hubot-channel-welcome

JavaScript
1
star
96

dappsys-nix

Shell
1
star
97

dapple-wevm

Wallet side ethereum virtual machine
JavaScript
1
star
98

dpath

dpath syntax and semantics
1
star
99

dai-semantics

K specification of the dai stablecoin system
Lua
1
star
100

dapple-pkg

Dapple packages module
JavaScript
1
star