Skip to content

Commit

Permalink
arithmetic operators, no full commented, no tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Sep 27, 2023
1 parent 7d53eb6 commit fb0612c
Show file tree
Hide file tree
Showing 8 changed files with 705 additions and 291 deletions.
11 changes: 4 additions & 7 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,23 +181,20 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str
ID: common.BytesToAddress(token.ID).String(),
ChainID: token.ChainID,
MinBalance: new(big.Int).SetBytes(token.MinBalance).String(),
Decimals: token.Decimals,
}
}
// init the operators and the predicate evaluator
operators := strategyoperators.InitOperators(capi.db.QueriesRO, tokensInfo)
eval := lexer.NewEval[map[string]string](operators.Map())
eval := lexer.NewEval[*strategyoperators.StrategyIteration](operators.Map())
// execute the evaluation of the predicate
res, err := eval.EvalToken(validPredicate)
if err != nil {
return 0, ErrEvalStrategyPredicate.WithErr(err)
}
// parse the evaluation results
for strAddress, strValue := range res {
value, ok := new(big.Int).SetString(strValue, 10)
if !ok {
return 0, ErrCantCreateCensus.With("error decoding calculated balance")
}
strategyHolders[common.HexToAddress(strAddress)] = value
for address, value := range res.Data {
strategyHolders[common.HexToAddress(address)] = value
censusWeight = new(big.Int).Add(censusWeight, value)
}
}
Expand Down
60 changes: 60 additions & 0 deletions api/strategyoperators/combinators.go
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
}
34 changes: 34 additions & 0 deletions api/strategyoperators/combinators_test.go
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)
}
}
Loading

0 comments on commit fb0612c

Please sign in to comment.