-
Notifications
You must be signed in to change notification settings - Fork 119
/
parser.go
65 lines (57 loc) · 1.22 KB
/
parser.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
package utils
import (
"encoding/json"
"io/ioutil"
"path/filepath"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/pkg/errors"
)
type TempInput struct {
In []types.Input `json:"Input"`
}
type TempOutput struct {
Out []types.Output `json:"Output"`
}
func ParseInput(fp string) ([]types.Input, error) {
var inputs TempInput
file, err := filepath.Abs(fp)
if err != nil {
return nil, err
}
input, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = json.Unmarshal(input, &inputs)
if err != nil {
return nil, err
}
return inputs.In, nil
}
func ParseOutput(fp string) ([]types.Output, error) {
var outputs TempOutput
file, err := filepath.Abs(fp)
if err != nil {
return nil, err
}
o, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
err = json.Unmarshal(o, &outputs)
if err != nil {
return nil, err
}
return outputs.Out, nil
}
func TotalOutput(output []types.Output) (sdk.Coins, error) {
if len(output) == 0 {
return sdk.NewCoins(), errors.Wrapf(types.ErrNoOutputs, "Outputlist is empty")
}
total := output[0].Coins
for i := 1; i < len(output); i++ {
total = total.Add(output[i].Coins...)
}
return total, nil
}