Skip to content

Commit

Permalink
Merge pull request #11 from ultraio/feature/BLOCK-1831-make-user-grou…
Browse files Browse the repository at this point in the history
…p-integration-user-friendly

BLOCK-1973 dfuse support for user group integraiton query
  • Loading branch information
Adam-Ultra authored Dec 13, 2023
2 parents 2019fa6 + b5580ea commit d0b9f9f
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions abidecoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"
"time"

// "reflect"
"go.uber.org/zap"
)

Expand All @@ -29,6 +29,10 @@ func (a *ABI) DecodeAction(data []byte, actionName ActionName) ([]byte, error) {

}

/*ultra-Adam---BLOCK-1831 make user group integration user-friendly ---start/end*/
const OR uint64 = 0X1000_0000_0000_0000 // 0: AND, 1: OR
const NEGATION uint64 = 0X2000_0000_0000_0000 // 0: no negation, 1: Negation

func (a *ABI) DecodeTableRow(tableName TableName, data []byte) ([]byte, error) {
binaryDecoder := NewDecoder(data)
tbl := a.TableForName(tableName)
Expand All @@ -40,6 +44,7 @@ func (a *ABI) DecodeTableRow(tableName TableName, data []byte) ([]byte, error) {
if err != nil {
return nil, err
}

return json.Marshal(builtStruct)

}
Expand All @@ -49,6 +54,9 @@ func (a *ABI) DecodeTableRowTyped(tableType string, data []byte) ([]byte, error)
if err != nil {
return nil, err
}



return json.Marshal(builtStruct)

}
Expand Down Expand Up @@ -94,7 +102,62 @@ func (a *ABI) decode(binaryDecoder *Decoder, structName string) (map[string]inte
}
}

return a.decodeFields(binaryDecoder, structure.Fields, builtStruct)
finalStruct, err := a.decodeFields(binaryDecoder, structure.Fields, builtStruct)

// only handle the group_restriction field from the token factory purchase table
if structName == "token_factory_purchase_v0" {
zlog.Info("purchase option table : ", zap.Any("value: ", finalStruct), zap.Any("type: ", structure), zap.Any("struct name: ", structName) )

/*ultra-Adam---BLOCK-1831 make user group integration user-friendly ---start/end*/
groupRestrictionValue, exists := finalStruct["group_restriction"]
zlog.Info("group_restriction value and type: ", zap.Any("value: ", groupRestrictionValue))
if exists {
// todo: check tmr, why the cast failed?
groupRestrictionSlice, isSlice := groupRestrictionValue.([]interface{});
if !isSlice {
zlog.Info("group_restriction is not slice")
}

if isSlice && len(groupRestrictionSlice) > 0 {
groupRestrictionStr := ""
for i,item := range groupRestrictionSlice{
vtype := fmt.Sprintf("%T", item)
zlog.Info("var type: ", zap.Any("vtype: ", vtype) )

// First, assert to eos.Int64
uint64Val, ok := item.(Uint64)
if !ok {
zlog.Info("Failed to assert to Int64 type")
return nil, fmt.Errorf("Failed to assert to Int64 type")
}

v := uint64(uint64Val)

if (v&OR) == OR { // OR
if i != 0 { // Ignore first OR
groupRestrictionStr += "|"
}
} else { // AND
if i != 0 { // Ignore first AND
groupRestrictionStr += "&"
}
}

if (v & NEGATION) == NEGATION { // NEGATION
groupRestrictionStr += "~"
}

// Extract group ID
groupID := v & ^(NEGATION + OR)
groupRestrictionStr += strconv.FormatUint(groupID, 10)
}
builtStruct["group_restriction"] = groupRestrictionStr;
}
}
/*ultra-Adam---BLOCK-1831 make user group integration user-friendly ---end*/
}

return finalStruct,err
}

func (a *ABI) decodeFields(binaryDecoder *Decoder, fields []FieldDef, builtStruct map[string]interface{}) (out map[string]interface{}, err error) {
Expand Down

0 comments on commit d0b9f9f

Please sign in to comment.