File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,30 @@ import (
13
13
var nonceCounter = time .Now ().UnixMilli ()
14
14
15
15
// Hyperliquid uses timestamps in milliseconds for nonce
16
+ // GetNonce returns a unique nonce that is always at least the current time in milliseconds.
17
+ // It ensures thread-safe updates using atomic operations.
16
18
func GetNonce () uint64 {
17
- return uint64 (atomic .AddInt64 (& nonceCounter , 1 ))
19
+ now := time .Now ().UnixMilli ()
20
+ for {
21
+ // Load the current nonce value atomically.
22
+ current := atomic .LoadInt64 (& nonceCounter )
23
+
24
+ // If the current time is greater than the stored nonce,
25
+ // attempt to update the nonce to the current time.
26
+ if current < now {
27
+ if atomic .CompareAndSwapInt64 (& nonceCounter , current , now ) {
28
+ return uint64 (now )
29
+ }
30
+ // If the swap fails, retry.
31
+ continue
32
+ }
33
+
34
+ // Otherwise, increment the nonce by one.
35
+ newNonce := current + 1
36
+ if atomic .CompareAndSwapInt64 (& nonceCounter , current , newNonce ) {
37
+ return uint64 (newNonce )
38
+ }
39
+ }
18
40
}
19
41
20
42
// Retruns a random cloid (Client Order ID)
You can’t perform that action at this time.
0 commit comments