-
Notifications
You must be signed in to change notification settings - Fork 1
/
abis.go
109 lines (91 loc) · 3.2 KB
/
abis.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2016, 2024 The TrueBlocks Authors. All rights reserved.
// Use of this source code is governed by a license that can
// be found in the LICENSE file.
/*
* Parts of this file were auto generated. Edit only those parts of
* the code inside of 'EXISTING_CODE' tags.
*/
package sdk
import (
// EXISTING_CODE
"encoding/json"
"fmt"
"sort"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
// EXISTING_CODE
)
type AbisOptions struct {
Addrs []string `json:"addrs,omitempty"`
Known bool `json:"known,omitempty"`
ProxyFor base.Address `json:"proxyFor,omitempty"`
Hint []string `json:"hint,omitempty"`
RenderCtx *output.RenderCtx `json:"-"`
Globals
}
// String implements the stringer interface
func (opts AbisOptions) String() string {
bytes, _ := json.Marshal(opts)
return string(bytes)
}
// Abis implements the chifra abis command.
func (opts *AbisOptions) Abis() ([]types.Function, *types.MetaData, error) {
in := opts.toInternal()
return queryAbis[types.Function](in)
}
// AbisList implements the chifra abis --list command.
func (opts *AbisOptions) AbisList() ([]types.Abi, *types.MetaData, error) {
in := opts.toInternal()
in.List = true
return queryAbis[types.Abi](in)
}
// AbisCount implements the chifra abis --count command.
func (opts *AbisOptions) AbisCount() ([]types.Count, *types.MetaData, error) {
in := opts.toInternal()
in.Count = true
return queryAbis[types.Count](in)
}
// AbisFind implements the chifra abis --find command.
func (opts *AbisOptions) AbisFind(val []string) ([]types.Function, *types.MetaData, error) {
in := opts.toInternal()
in.Find = val
return queryAbis[types.Function](in)
}
// AbisEncode implements the chifra abis --encode command.
func (opts *AbisOptions) AbisEncode(val string) ([]types.Function, *types.MetaData, error) {
in := opts.toInternal()
in.Encode = val
return queryAbis[types.Function](in)
}
// No enums
func SortAbis(abis []types.Abi, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Abi) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidAbiField(field) {
return fmt.Errorf("%s is not an Abi sort field", field)
}
sorts[i] = types.AbiBy(types.AbiField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(abis, types.AbiCmp(abis, sorts...))
return nil
}
func SortFunctions(functions []types.Function, sortSpec SortSpec) error {
if len(sortSpec.Fields) != len(sortSpec.Order) {
return fmt.Errorf("fields and order must have the same length")
}
sorts := make([]func(p1, p2 types.Function) bool, len(sortSpec.Fields))
for i, field := range sortSpec.Fields {
if !types.IsValidFunctionField(field) {
return fmt.Errorf("%s is not an Function sort field", field)
}
sorts[i] = types.FunctionBy(types.FunctionField(field), types.SortOrder(sortSpec.Order[i]))
}
sort.Slice(functions, types.FunctionCmp(functions, sorts...))
return nil
}
// EXISTING_CODE
// EXISTING_CODE