@@ -27,6 +27,13 @@ func (e *CustomError) Error() string {
27
27
return e .Message
28
28
}
29
29
30
+ type NonceInfo struct {
31
+ LastExecutedTxNonce * int64 `json:"last_executed_tx_nonce"`
32
+ LastMempoolTxNonce * int64 `json:"last_mempool_tx_nonce"`
33
+ PossibleNextNonce int64 `json:"possible_next_nonce"`
34
+ DetectedMissingNonces []int64 `json:"detected_missing_nonces"`
35
+ }
36
+
30
37
func makeRequest (url string , method string , payload interface {}) ([]byte , error ) {
31
38
client := resty .New ()
32
39
var resp * resty.Response
@@ -55,23 +62,25 @@ func makeRequest(url string, method string, payload interface{}) ([]byte, error)
55
62
return resp .Body (), nil
56
63
}
57
64
58
- func getNonce (address string , network stacks.StacksNetwork ) (* big.Int , error ) {
59
- url := network .GetAccountAPIURL (address )
65
+ func getNextNonce (address string , network stacks.StacksNetwork ) (* big.Int , error ) {
66
+ url := network .GetNonceAPIURL (address )
67
+
60
68
body , err := makeRequest (url , "GET" , nil )
61
69
if err != nil {
62
- return nil , & CustomError {Message : "Error fetching nonce" , Err : err }
70
+ return nil , & CustomError {Message : "Error fetching nonce info " , Err : err }
63
71
}
64
72
65
- var result struct {
66
- Nonce uint64 `json:"nonce"`
73
+ var nonceInfo NonceInfo
74
+ err = json .Unmarshal (body , & nonceInfo )
75
+ if err != nil {
76
+ return nil , & CustomError {Message : "Error parsing nonce info" , Err : err }
67
77
}
68
78
69
- err = json .Unmarshal (body , & result )
70
- if err != nil {
71
- return nil , & CustomError {Message : "Error parsing JSON response" , Err : err }
79
+ if len (nonceInfo .DetectedMissingNonces ) > 0 {
80
+ fmt .Printf ("Warning: Detected missing nonces: %v\n " , nonceInfo .DetectedMissingNonces )
72
81
}
73
82
74
- return big .NewInt (int64 ( result . Nonce ) ), nil
83
+ return big .NewInt (nonceInfo . PossibleNextNonce ), nil
75
84
}
76
85
77
86
func estimateTransactionFee (tx StacksTransaction , network stacks.StacksNetwork ) (* big.Int , error ) {
@@ -154,7 +163,7 @@ func createAndSignTransaction(tx StacksTransaction, network stacks.StacksNetwork
154
163
}
155
164
156
165
if nonce == nil {
157
- nonce , err = getNonce (senderAddress , network )
166
+ nonce , err = getNextNonce (senderAddress , network )
158
167
if err != nil {
159
168
return & CustomError {Message : "Failed to fetch nonce" , Err : err }
160
169
}
@@ -254,7 +263,6 @@ func BroadcastTransaction(tx StacksTransaction, network *stacks.StacksNetwork) (
254
263
SetHeader ("Content-Type" , "application/octet-stream" ).
255
264
SetBody (serializedTx ).
256
265
Post (url )
257
-
258
266
if err != nil {
259
267
return "" , & CustomError {Message : "Failed to send transaction" , Err : err }
260
268
}
0 commit comments