forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendbot.go
152 lines (128 loc) · 3.47 KB
/
friendbot.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
package internal
import (
"strconv"
"sync"
b "github.com/cowry-network/go/build"
"github.com/cowry-network/go/clients/horizon"
"github.com/cowry-network/go/keypair"
"github.com/cowry-network/go/support/errors"
)
// TxResult is the result from the asynchronous submit transaction method over a channel
type TxResult struct {
maybeTransactionSuccess *horizon.TransactionSuccess
maybeErr error
}
// Bot represents the friendbot subsystem.
type Bot struct {
Horizon *horizon.Client
Secret string
Network string
StartingBalance string
SubmitTransaction func(bot *Bot, channel chan TxResult, signed string)
// uninitialized
sequence uint64
forceRefreshSequence bool
lock sync.Mutex
}
// Pay funds the account at `destAddress`
func (bot *Bot) Pay(destAddress string) (*horizon.TransactionSuccess, error) {
channel := make(chan TxResult)
err := bot.lockedPay(channel, destAddress)
if err != nil {
return nil, err
}
v := <-channel
return v.maybeTransactionSuccess, v.maybeErr
}
func (bot *Bot) lockedPay(channel chan TxResult, destAddress string) error {
bot.lock.Lock()
defer bot.lock.Unlock()
err := bot.checkSequenceRefresh()
if err != nil {
return err
}
signed, err := bot.makeTx(destAddress)
if err != nil {
return err
}
go bot.SubmitTransaction(bot, channel, signed)
return nil
}
// AsyncSubmitTransaction should be passed into the bot
func AsyncSubmitTransaction(bot *Bot, channel chan TxResult, signed string) {
result, err := bot.Horizon.SubmitTransaction(signed)
if err != nil {
switch e := err.(type) {
case *horizon.Error:
bot.checkHandleBadSequence(e)
}
channel <- TxResult{
maybeTransactionSuccess: nil,
maybeErr: err,
}
} else {
channel <- TxResult{
maybeTransactionSuccess: &result,
maybeErr: nil,
}
}
}
func (bot *Bot) checkHandleBadSequence(err *horizon.Error) {
resCode, e := err.ResultCodes()
isTxBadSeqCode := e == nil && resCode.TransactionCode == "tx_bad_seq"
if !isTxBadSeqCode {
return
}
bot.forceRefreshSequence = true
}
// establish initial sequence if needed
func (bot *Bot) checkSequenceRefresh() error {
if bot.sequence != 0 && !bot.forceRefreshSequence {
return nil
}
return bot.refreshSequence()
}
func (bot *Bot) makeTx(destAddress string) (string, error) {
txn, err := b.Transaction(
b.SourceAccount{AddressOrSeed: bot.Secret},
b.Sequence{Sequence: bot.sequence + 1},
b.Network{Passphrase: bot.Network},
b.CreateAccount(
b.Destination{AddressOrSeed: destAddress},
b.NativeAmount{Amount: bot.StartingBalance},
),
)
if err != nil {
return "", errors.Wrap(err, "Error building a transaction")
}
txs, err := txn.Sign(bot.Secret)
if err != nil {
return "", errors.Wrap(err, "Error signing a transaction")
}
base64, err := txs.Base64()
// only increment the in-memory sequence number if we are going to submit the transaction, while we hold the lock
if err == nil {
bot.sequence++
}
return base64, err
}
// refreshes the sequence from the bot account
func (bot *Bot) refreshSequence() error {
botAccount, err := bot.Horizon.LoadAccount(bot.address())
if err != nil {
bot.sequence = 0
return err
}
seq, err := strconv.ParseInt(botAccount.Sequence, 10, 64)
if err != nil {
bot.sequence = 0
return err
}
bot.sequence = uint64(seq)
bot.forceRefreshSequence = false
return nil
}
func (bot *Bot) address() string {
kp := keypair.MustParse(bot.Secret)
return kp.Address()
}