-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathaction.go
73 lines (61 loc) · 1.44 KB
/
action.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
package fswap
import (
"encoding/base64"
"time"
"github.com/fox-one/4swap-sdk-go/v2/route"
"github.com/google/uuid"
"github.com/pandodao/mtg/mtgpack"
"github.com/pandodao/mtg/protocol"
"github.com/pandodao/mtg/protocol/checksum"
"github.com/shopspring/decimal"
)
const (
ActionAdd uint16 = 1
ActionRemove uint16 = 2
ActionSwap uint16 = 3
ProtocolVersion uint8 = 2
)
func BuildAdd(followID string, oppositeAsset string, slippage decimal.Decimal, expireDuration time.Duration) string {
return buildAction(
ActionAdd,
followID,
uuid.MustParse(oppositeAsset),
slippage,
int16(expireDuration.Seconds()),
)
}
func BuildRemove(followID string) string {
return buildAction(
ActionRemove,
followID,
)
}
func BuildSwap(followID string, fillAsset string, paths route.Paths, minAmount decimal.Decimal) string {
return buildAction(
ActionSwap,
followID,
uuid.MustParse(fillAsset),
paths,
minAmount,
)
}
func buildAction(action uint16, followID string, args ...interface{}) string {
h := protocol.Header{
Version: ProtocolVersion,
ProtocolID: protocol.ProtocolFswap,
Action: action,
}
if followID != "" {
h.FollowID = uuid.MustParse(followID)
}
enc := mtgpack.NewEncoder()
if err := enc.EncodeValue(h); err != nil {
panic(err)
}
if err := enc.EncodeValues(args...); err != nil {
panic(err)
}
b := enc.Bytes()
sum := checksum.Sha256(b)
return base64.StdEncoding.EncodeToString(append(b, sum...))
}