-
Notifications
You must be signed in to change notification settings - Fork 0
/
parseDCTTx.go
73 lines (62 loc) · 1.85 KB
/
parseDCTTx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package scenjsonparse
import (
"errors"
"fmt"
oj "github.com/Dharitri-org/drtg-scenario/orderedjson"
scenmodel "github.com/Dharitri-org/drtg-scenario/scenario/model"
)
func (p *Parser) processTxDCT(txDctRaw oj.OJsonObject) ([]*scenmodel.DCTTxData, error) {
allDctData := make([]*scenmodel.DCTTxData, 0)
switch txDct := txDctRaw.(type) {
case *oj.OJsonMap:
if !p.AllowDctTxLegacySyntax {
return nil, fmt.Errorf("wrong DCT Multi-Transfer format, list expected")
}
entry, err := p.parseSingleTxDctEntry(txDct)
if err != nil {
return nil, err
}
allDctData = append(allDctData, entry)
case *oj.OJsonList:
for _, txDctListItem := range txDct.AsList() {
txDctMap, isMap := txDctListItem.(*oj.OJsonMap)
if !isMap {
return nil, fmt.Errorf("wrong DCT Multi-Transfer format")
}
entry, err := p.parseSingleTxDctEntry(txDctMap)
if err != nil {
return nil, err
}
allDctData = append(allDctData, entry)
}
default:
return nil, fmt.Errorf("wrong DCT transfer format, expected list")
}
return allDctData, nil
}
func (p *Parser) parseSingleTxDctEntry(dctTxEntry *oj.OJsonMap) (*scenmodel.DCTTxData, error) {
dctData := scenmodel.DCTTxData{}
var err error
for _, kvp := range dctTxEntry.OrderedKV {
switch kvp.Key {
case "tokenIdentifier":
dctData.TokenIdentifier, err = p.processStringAsByteArray(kvp.Value)
if err != nil {
return nil, fmt.Errorf("invalid DCT token name: %w", err)
}
case "nonce":
dctData.Nonce, err = p.processUint64(kvp.Value)
if err != nil {
return nil, errors.New("invalid account nonce")
}
case "value":
dctData.Value, err = p.processBigInt(kvp.Value, bigIntUnsignedBytes)
if err != nil {
return nil, fmt.Errorf("invalid DCT balance: %w", err)
}
default:
return nil, fmt.Errorf("unknown transaction DCT data field: %s", kvp.Key)
}
}
return &dctData, nil
}