-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
arithmetic operators, no full commented, no tests
- Loading branch information
1 parent
7d53eb6
commit fb0612c
Showing
8 changed files
with
705 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package strategyoperators | ||
|
||
import "math/big" | ||
|
||
func normalize(a, b *big.Int, aDecimals, bDecimals uint64) (*big.Int, *big.Int, uint64) { | ||
if aDecimals > bDecimals { | ||
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(aDecimals-bDecimals)), nil) | ||
return a, new(big.Int).Mul(b, exp), aDecimals | ||
} | ||
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(bDecimals-aDecimals)), nil) | ||
return new(big.Int).Mul(a, exp), b, bDecimals | ||
} | ||
|
||
func reduceNormalized(a *big.Int, aDecimals uint64) *big.Int { | ||
exp := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(aDecimals)), nil) | ||
return new(big.Int).Div(a, exp) | ||
} | ||
|
||
func sumBalancesCombinator(balances map[string][2]*big.Int) map[string]*big.Int { | ||
res := make(map[string]*big.Int) | ||
for address, balances := range balances { | ||
res[address] = new(big.Int).Add(balances[0], balances[1]) | ||
} | ||
return res | ||
} | ||
|
||
func mulBalancesCombinator(balances map[string][2]*big.Int, decimals uint64, forceNotZero bool) map[string]*big.Int { | ||
res := make(map[string]*big.Int) | ||
for address, balances := range balances { | ||
if balances[0].Cmp(big.NewInt(0)) == 0 { | ||
if forceNotZero { | ||
continue | ||
} | ||
res[address] = balances[1] | ||
continue | ||
} | ||
if balances[1].Cmp(big.NewInt(0)) == 0 { | ||
if forceNotZero { | ||
continue | ||
} | ||
res[address] = balances[0] | ||
continue | ||
} | ||
|
||
value := new(big.Int).Mul(balances[0], balances[1]) | ||
if value.Cmp(big.NewInt(0)) == 0 { | ||
continue | ||
} | ||
res[address] = reduceNormalized(value, decimals) | ||
} | ||
return res | ||
} | ||
|
||
func membershipCombinator(balances map[string][2]*big.Int) map[string]*big.Int { | ||
res := make(map[string]*big.Int) | ||
for address := range balances { | ||
res[address] = big.NewInt(1) | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package strategyoperators | ||
|
||
import ( | ||
"log" | ||
"math/big" | ||
"testing" | ||
) | ||
|
||
func Test_normalize(t *testing.T) { | ||
a, _ := new(big.Int).SetString("6000000000000000000", 10) | ||
b, _ := new(big.Int).SetString("6", 10) | ||
aDecimals := uint64(18) | ||
bDecimals := uint64(0) | ||
|
||
expectedA, expectedB := new(big.Int).Set(a), new(big.Int).Set(a) | ||
resultA, resultB, commaPlaces := normalize(a, b, aDecimals, bDecimals) | ||
if expectedA.Cmp(resultA) != 0 || expectedB.Cmp(resultB) != 0 { | ||
log.Fatal(expectedA, resultA, expectedB, resultB) | ||
} | ||
if commaPlaces != aDecimals { | ||
log.Fatal(commaPlaces, aDecimals) | ||
} | ||
|
||
b, _ = new(big.Int).SetString("6000", 10) | ||
bDecimals = uint64(3) | ||
expectedA, expectedB = new(big.Int).Set(a), new(big.Int).Set(a) | ||
resultA, resultB, commaPlaces = normalize(a, b, aDecimals, bDecimals) | ||
if expectedA.Cmp(resultA) != 0 || expectedB.Cmp(resultB) != 0 { | ||
log.Fatal(expectedA, resultA, expectedB, resultB) | ||
} | ||
if commaPlaces != aDecimals { | ||
log.Fatal(commaPlaces, aDecimals) | ||
} | ||
} |
Oops, something went wrong.