-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoken.go
94 lines (83 loc) · 2.74 KB
/
token.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
package fio
import (
"github.com/fioprotocol/fio-go/eos"
)
const FioSymbol = "ᵮ"
// Tokens is a convenience function for converting from a float for human readability.
// Example 1 FIO Token: Tokens(1.0) == uint64(1000000000)
func Tokens(tokens float64) uint64 {
return uint64(tokens * 1000000000.0)
}
// TransferTokensPubKey is used to send FIO tokens to a public key
type TransferTokensPubKey struct {
PayeePublicKey string `json:"payee_public_key"`
Amount uint64 `json:"amount"`
MaxFee uint64 `json:"max_fee"`
Actor eos.AccountName `json:"actor"`
Tpid string `json:"tpid"`
}
// NewTransferTokensPubKey builds an eos.Action for sending FIO tokens
func NewTransferTokensPubKey(actor eos.AccountName, recipientPubKey string, amount uint64) *Action {
return NewAction(
"fio.token", "trnsfiopubky", actor,
TransferTokensPubKey{
PayeePublicKey: recipientPubKey,
Amount: amount,
MaxFee: Tokens(GetMaxFee(FeeTransferTokensPubKey)),
Actor: actor,
Tpid: CurrentTpid(),
},
)
}
// Transfer is a privileged call, and not normally used for sending tokens, use TransferTokensPubKey instead
type Transfer struct {
From eos.AccountName `json:"from"`
To eos.AccountName `json:"to"`
Quantity eos.Asset `json:"quantity"`
Memo string `json:"memo"`
}
// NewTransfer is unlikely to be called, this is a privileged action
//
// deprecated: internal action, user cannot call.
func NewTransfer(actor eos.AccountName, recipient eos.AccountName, amount uint64) *Action {
return NewAction(
eos.AccountName("fio.token"), "transfer", actor,
Transfer{
From: actor,
To: recipient,
Quantity: eos.Asset{
Amount: eos.Int64(amount),
Symbol: eos.Symbol{
Precision: 9,
Symbol: "FIO",
},
},
},
)
}
// GetBalance gets an account's balance
func (api *API) GetBalance(account eos.AccountName) (float64, error) {
a, err := api.GetCurrencyBalance(account, "FIO", eos.AccountName("fio.token"))
if err != nil {
return 0.0, err
}
if len(a) > 0 {
if a[0].Amount > 0 {
return float64(a[0].Amount) / 1000000000.0, nil
}
}
return 0.0, nil
}
type GetFioBalanceResp struct {
Balance uint64 `json:"balance"`
Available uint64 `json:"available"`
}
type getFioBalanceReq struct {
FioPublicKey string `json:"fio_public_key"`
}
// GetFioBalance is the preferred way to get an account's balance, it will include the number of available
// tokens in the case that an account holds locked/staked tokens.
func (api *API) GetFioBalance(pubkey string) (fiobalance *GetFioBalanceResp, err error) {
err = api.call("chain", "get_fio_balance", &getFioBalanceReq{FioPublicKey: pubkey}, &fiobalance)
return fiobalance, err
}