def _sigmoid(a, b, l, d, x): assert a > 0 assert b >= 0 assert l >= 0 assert d > 0 if x > l: rate = math.div(math.safeSub(x, l), d) if rate >> (math.PRECISION - math.MIN_PRECISION) < 0x1e00000000: exp = math.exp(rate) addexp = math.add(math.float(1), exp) divexp = math.div(math.float(1), addexp) mulexp = math.mul(a, divexp) y = math.add(mulexp, b) else: y = b elif (x < l) and (x >= 0): rate = math.div(math.safeSub(l, x), d) if rate >> (math.PRECISION - math.MIN_PRECISION) < 0x1e00000000: exp = math.exp(rate) addexp = math.add(math.float(1), exp) divexp = math.div(math.float(1), addexp) mulexp = math.mul(a, divexp) y = math.sub(math.add(a, b * 2), math.add(mulexp, b)) else: y = math.add(a, b) else: y = math.div(math.add(a, b * 2), math.float(2)) return y
def _get_loan_plan(self, supply, circulation): # check over flow and change unit high = math.decimaltofloat(self.high * self.decimal) low = math.decimaltofloat(self.low * self.decimal) supply = math.uint256(supply) circulation = math.uint256(circulation) supply = math.ethertofloat(supply) circulation = math.ethertofloat(circulation) assert 0 < supply assert 0 < circulation return math.floattodecimal( _sigmoid(high - low, low, math.div(supply, math.float(2)), math.div(supply, math.float(8)), circulation) ), self.loandays * 24 * 3600, self.exemptdays * 24 * 3600
def _sigmoid(a, b, l, d, x): assert a > 0 assert b >= 0 assert l >= 0 assert d > 0 if x > l: rate = math.div(math.safeSub(x, l), d) if rate < 0x1e00000000: exp = math.fixedExp(rate) addexp = math.add(1 << 32, exp) divexp = math.div(1 << 32, addexp) mulexp = math.mul(a, divexp) y = math.add(mulexp, b) else: y = b elif (x < l) and (x >= 0): rate = math.div(math.safeSub(l, x), d) if rate < 0x1e00000000: exp = math.fixedExp(rate) addexp = math.add(1 << 32, exp) divexp = math.div(1 << 32, addexp) mulexp = math.mul(a, divexp) y = math.sub(math.add(a, b * 2), math.add(mulexp, b)) else: y = math.add(a, b) else: y = math.div(math.add(a, b * 2), math.float(2)) return y
def _loan(self, cdtamount, interestrate): # check overflow and change unit cdtamount = math.uint256(cdtamount) interestrate = math.uint256(interestrate) cdtamount = math.ethertofloat(cdtamount) interestrate = math.decimaltofloat(interestrate) # loaned ether ethamount = math.mul(cdtamount, math.decimaltofloat(self.CDTL * self.decimal)) # calculate the interest earn = math.mul(ethamount, interestrate) cdtreserve = math.mul( earn, math.decimaltofloat(self.CDT_RESERVE * self.decimal)) interest = math.sub(earn, cdtreserve) issuecdtamount = math.div( cdtreserve, math.mul(math.decimaltofloat(self.CDTIP * self.decimal), math.float(2))) # calculate the new issue CDT to prize loaned user using the interest ethamount = math.sub(ethamount, earn) sctamount = cdtamount return math.floattoether(ethamount), math.floattoether( interest), math.floattoether(issuecdtamount), math.floattoether( sctamount)
def _issue(self, circulation, ethamount): # check over flow and change unit circulation = math.uint256(circulation) ethamount = math.uint256(ethamount) circulation = math.ethertofloat(circulation) ethamount = math.ethertofloat(ethamount) crr = self._get_crr(circulation) ethdpt = math.mul(ethamount, crr) dpt = math.div(ethdpt, math.decimaltofloat(self.DPTIP * self.decimal)) cdt = math.div(math.mul(math.sub(math.float(1), crr), ethamount), math.decimaltofloat(self.CDTIP * self.decimal)) crr = self._get_crr(circulation) # Split the new issued tokens to User and Founder F = math.div(math.sub(math.float(1), crr), math.float(2)) U = math.sub(math.float(1), F) udpt = math.mul(dpt, U) ucdt = math.mul(cdt, U) fdpt = math.mul(dpt, F) fcdt = math.mul(cdt, F) return math.floattoether(udpt), math.floattoether( ucdt), math.floattoether(fdpt), math.floattoether( fcdt), math.floattoether(ethdpt), math.floattodecimal(crr)
from solidity.python.dabformula import EasyDABFormula as Formula from solidity.python.dabformula import _sigmoid as _sigmoid from solidity.python.dabformula import sigmoid as sigmoid import solidity.python.solmath as math import cmath formula = Formula() # udpt_expect, ucdt_expect, fdpt_expect, fcdt_expect, crr_expect = formula._issue(30000000000000000570425344,30000000000000000000) # print(crr_expect) acc_float = 1 / math.float(1) for i in range(17): try: s1 = sigmoid(0.6, 0.2, 100000, 100000 / 4, 10000 + acc_float * 10**i) s2 = _sigmoid( math.ethertofloat(0.6 * formula.ether), math.ethertofloat(0.2 * formula.ether), math.float(100000), math.float(100000) / 4, math.ethertofloat(10000 * formula.ether + acc_float * 10**i * formula.ether)) print(s1.real, math.floattoether(s2) / formula.ether, 'accuracy of _sigmoid', s1.real - math.floattoether(s2) / formula.ether, i) except AssertionError as err: continue for i in range(5000): try: s1 = sigmoid(0.6, 0.2, 100000, 100000 / 4, 1000 * i) s2 = _sigmoid(math.ethertofloat(0.6 * formula.ether), math.ethertofloat(0.2 * formula.ether),