-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
126 lines (105 loc) · 2.7 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"strconv"
"time"
"github.com/machinebox/graphql"
"github.com/sirupsen/logrus"
_ "go-demo-1/util/math"
)
var timeDeltaTmp = strconv.FormatInt(time.Now().Unix()-60*60, 10)
type PoolOperate struct {
charges []string
withdraws []string
}
type PoolDepositResp struct {
PoolCreateEntities []PoolDepositEntity `json:"poolDepositEntities"`
}
type PoolDepositEntity struct {
UserAddr string `json:"userAddr"`
Share string `json:"share"`
Amount string `json:"amount"`
PoolAddr string `json:"poolAddr"`
Timestamp string `json:"timestamp"`
}
type PoolWithdrawResp struct {
PoolWithdrawEntities []PoolWithdrawEntity `json:"poolWithdrawEntities"`
}
type PoolWithdrawEntity struct {
UserAddr string `json:"userAddr"`
Share string `json:"share"`
Amount string `json:"amount"`
PoolAddr string `json:"poolAddr"`
Timestamp string `json:"timestamp"`
}
var client *graphql.Client
func main() {
call, err := PoolOperateCall("0x65dcfd504c4b5078005257827744539c282cb029")
if err != nil {
return
}
fmt.Println(call)
}
func PoolOperateCall(poolAddress string) (*PoolOperate, error) {
client = graphql.NewClient("http://localhost:8000/subgraphs/name/perpetual/spidex")
poolDeposits, err := poolDeposit(poolAddress, timeDeltaTmp)
if err != nil {
logrus.Error(err)
return nil, err
}
var charges []string
for _, val := range poolDeposits.PoolCreateEntities {
charges = append(charges, val.Amount)
}
poolWithdraws, err := poolWithdraw(poolAddress, timeDeltaTmp)
if err != nil {
logrus.Error(err)
return nil, err
}
var withdraws []string
for _, val := range poolWithdraws.PoolWithdrawEntities {
withdraws = append(withdraws, val.Amount)
}
return &PoolOperate{
charges,
withdraws,
}, nil
}
func poolDeposit(poolAddress string, timeDelta string) (*PoolDepositResp, error) {
query := graphql.NewRequest(`
{
poolDepositEntities(where: {poolAddr: "` + poolAddress + `", timestamp_gt: "` + timeDelta + `"}) {
timestamp
share
userAddr
amount
poolAddr
}
}
`)
var poolDepositResp PoolDepositResp
if err := client.Run(context.Background(), query, &poolDepositResp); err != nil {
log.Fatal(err)
}
return &poolDepositResp, nil
}
func poolWithdraw(poolAddress string, timeDelta string) (*PoolWithdrawResp, error) {
query := graphql.NewRequest(`
{
poolWithdrawEntities(where: {poolAddr: "` + poolAddress + `", timestamp_gt: "` + timeDelta + `"}){
share
timestamp
userAddr
amount
poolAddr
}
}
`)
var poolWithdrawResp PoolWithdrawResp
if err := client.Run(context.Background(), query, &poolWithdrawResp); err != nil {
log.Fatal(err)
}
return &poolWithdrawResp, nil
}