-
Notifications
You must be signed in to change notification settings - Fork 77
/
nep5.go
173 lines (152 loc) · 5.46 KB
/
nep5.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package client
import (
"errors"
"fmt"
"github.com/nspcc-dev/neo-go/pkg/core"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
"github.com/nspcc-dev/neo-go/pkg/io"
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)
// NEP5Decimals invokes `decimals` NEP5 method on a specified contract.
func (c *Client) NEP5Decimals(tokenHash util.Uint160) (int64, error) {
result, err := c.InvokeFunction(tokenHash.StringLE(), "decimals", []smartcontract.Parameter{}, nil)
if err != nil {
return 0, err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return 0, errors.New("invalid VM state")
}
return topIntFromStack(result.Stack)
}
// NEP5Name invokes `name` NEP5 method on a specified contract.
func (c *Client) NEP5Name(tokenHash util.Uint160) (string, error) {
result, err := c.InvokeFunction(tokenHash.StringLE(), "name", []smartcontract.Parameter{}, nil)
if err != nil {
return "", err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return "", errors.New("invalid VM state")
}
return topStringFromStack(result.Stack)
}
// NEP5Symbol invokes `symbol` NEP5 method on a specified contract.
func (c *Client) NEP5Symbol(tokenHash util.Uint160) (string, error) {
result, err := c.InvokeFunction(tokenHash.StringLE(), "symbol", []smartcontract.Parameter{}, nil)
if err != nil {
return "", err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return "", errors.New("invalid VM state")
}
return topStringFromStack(result.Stack)
}
// NEP5TotalSupply invokes `totalSupply` NEP5 method on a specified contract.
func (c *Client) NEP5TotalSupply(tokenHash util.Uint160) (int64, error) {
result, err := c.InvokeFunction(tokenHash.StringLE(), "totalSupply", []smartcontract.Parameter{}, nil)
if err != nil {
return 0, err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return 0, errors.New("invalid VM state")
}
return topIntFromStack(result.Stack)
}
// NEP5BalanceOf invokes `balanceOf` NEP5 method on a specified contract.
func (c *Client) NEP5BalanceOf(tokenHash util.Uint160) (int64, error) {
result, err := c.InvokeFunction(tokenHash.StringLE(), "balanceOf", []smartcontract.Parameter{}, nil)
if err != nil {
return 0, err
} else if result.State != "HALT" || len(result.Stack) == 0 {
return 0, errors.New("invalid VM state")
}
return topIntFromStack(result.Stack)
}
// NEP5TokenInfo returns full NEP5 token info.
func (c *Client) NEP5TokenInfo(tokenHash util.Uint160) (*wallet.Token, error) {
name, err := c.NEP5Name(tokenHash)
if err != nil {
return nil, err
}
symbol, err := c.NEP5Symbol(tokenHash)
if err != nil {
return nil, err
}
decimals, err := c.NEP5Decimals(tokenHash)
if err != nil {
return nil, err
}
return wallet.NewToken(tokenHash, name, symbol, decimals), nil
}
// TransferNEP5 creates an invocation transaction that invokes 'transfer' method
// on a given token to move specified amount of NEP5 assets (in FixedN format
// using contract's number of decimals) to given account.
func (c *Client) TransferNEP5(acc *wallet.Account, to util.Uint160, token *wallet.Token, amount int64, gas util.Fixed8) (util.Uint256, error) {
from, err := address.StringToUint160(acc.Address)
if err != nil {
return util.Uint256{}, fmt.Errorf("bad account address: %v", err)
}
// Note: we don't use invoke function here because it requires
// 2 round trips instead of one.
w := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(w.BinWriter, token.Hash, "transfer", from, to, amount)
emit.Opcode(w.BinWriter, opcode.THROWIFNOT)
tx := transaction.NewInvocationTX(w.Bytes(), gas)
tx.Attributes = append(tx.Attributes, transaction.Attribute{
Usage: transaction.Script,
Data: from.BytesBE(),
})
if err := request.AddInputsAndUnspentsToTx(tx, acc.Address, core.UtilityTokenID(), gas, c); err != nil {
return util.Uint256{}, fmt.Errorf("can't add GAS to transaction: %v", err)
}
if err := acc.SignTx(tx); err != nil {
return util.Uint256{}, fmt.Errorf("can't sign tx: %v", err)
}
if err := c.SendRawTransaction(tx); err != nil {
return util.Uint256{}, err
}
return tx.Hash(), nil
}
func topIntFromStack(st []smartcontract.Parameter) (int64, error) {
index := len(st) - 1 // top stack element is last in the array
var decimals int64
switch typ := st[index].Type; typ {
case smartcontract.IntegerType:
var ok bool
decimals, ok = st[index].Value.(int64)
if !ok {
return 0, errors.New("invalid Integer item")
}
case smartcontract.ByteArrayType:
data, ok := st[index].Value.([]byte)
if !ok {
return 0, errors.New("invalid ByteArray item")
}
decimals = emit.BytesToInt(data).Int64()
default:
return 0, fmt.Errorf("invalid stack item type: %s", typ)
}
return decimals, nil
}
func topStringFromStack(st []smartcontract.Parameter) (string, error) {
index := len(st) - 1 // top stack element is last in the array
var s string
switch typ := st[index].Type; typ {
case smartcontract.StringType:
var ok bool
s, ok = st[index].Value.(string)
if !ok {
return "", errors.New("invalid String item")
}
case smartcontract.ByteArrayType:
data, ok := st[index].Value.([]byte)
if !ok {
return "", errors.New("invalid ByteArray item")
}
s = string(data)
default:
return "", fmt.Errorf("invalid stack item type: %s", typ)
}
return s, nil
}