-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.js
164 lines (153 loc) · 4.8 KB
/
snapshot.js
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
153
154
155
156
157
158
159
160
161
162
163
164
const { ethers } = require('ethers')
const web3 = require('web3')
const axios = require("axios")
const constants = require('./constants')
const provider = new ethers.providers.JsonRpcProvider(constants.ETH_NODE_URL)
async function get_signature(account, signData, provider) {
const { domain, types, message } = signData.data
const signer = new ethers.Wallet(account.privateKey, provider)
return await signer._signTypedData(domain, types, message)
}
async function send_vote_request(data) {
const config = {
method: 'post',
url: 'https://hub.snapshot.org/api/msg',
headers: {
'authority': 'hub.snapshot.org',
'accept': 'application/json',
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8',
'content-type': 'application/json',
'origin': 'https://snapshot.org',
'referer': 'https://snapshot.org/',
'sec-ch-ua': '"Google Chrome";v="105", "Not)A;Brand";v="8", "Chromium";v="105"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"macOS"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-site',
'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
},
data: JSON.stringify(data)
};
try {
const res = await axios(config)
if (res.data.id) {
console.log(`${data.address} 投票成功`);
}
} catch (error) {
console.log(`${data.address} 投票失败`, JSON.stringify(error.response.data));
}
}
async function vote(account, space, proposal, choice) {
const checksum_address = web3.utils.toChecksumAddress(account.address)
const data = {
"address": checksum_address,
"data": {
"domain": {
"name": "snapshot",
"version": "0.1.4"
},
"types": {
"Vote": [
{
"name": "from",
"type": "address"
},
{
"name": "space",
"type": "string"
},
{
"name": "timestamp",
"type": "uint64"
},
{
"name": "proposal",
"type": "string"
},
{
"name": "choice",
"type": "uint32" // TypeError: value.map is not a function, for uint32[]
},
{
"name": "reason",
"type": "string"
},
{
"name": "app",
"type": "string"
}
]
},
"message": {
"space": space,
"proposal": proposal,
"choice": 1, //{"0":"1","1":"2","2":"3","3":"4","4":"5"},
//data types: see here https://docs.soliditylang.org/en/v0.8.17/abi-spec.html
//object
//no type called tuple or uint32[5], dict
//choice, need to change types definition above
// [0:1,1:2,2:3,3:4,4:5]
// {"0":"1","1":"2","2":"3","3":"4","4":"5"},
// uint[] [1,2,3,4,5] => ,"error_description":"invalid choice"
// string[] ["0:1","1:2","2:3","3:4","4:5"] => ,"error_description":"invalid choice"
//string "0:1,1:2,2:3,3:4,4:5" =>,"error_description":{}}
/* https://github.com/snapshot-labs/snapshot/blob/077a71d085cfa1a814a93c51693b487e3ddf04c7/src/components/ModalVote.vue
https://snapshot.org/assets/index.542daadc.js */
"app": "snapshot",
"reason": "",
"from": checksum_address,
"timestamp": Math.floor(Date.now() / 1000)
}
}
}
data.sig = await get_signature(account, data, provider)
await send_vote_request(data)
}
async function get_active_proposals(space, simple) {
const url = 'https://hub.snapshot.org/graphql?'
const data = {
query: `query Proposals {
proposals(first: 20, skip: 0, where: {space_in: ["${space}"], state: "active"}, orderBy: "created", orderDirection: desc) {
id
title
body
choices
start
end
snapshot
state
author
type
app
space {
id
name
}
}
}`
}
const res = await axios.post(url, data)
if (simple) {
return res.data.data.proposals.map(item => {
return {
id: item.id,
title: item.title,
type: item.type,
choices: item.choices,
}
})
}
return res.data.data.proposals
}
async function vote_on(account, space, choice) {
const proposals = await get_active_proposals(space)
for (const proposal of proposals) {
console.log(`Account: ${account.id} 投票进行中: ${space} - ${proposal.title} `);
// - ${proposal.choices[choice - 1]}
await vote(account, space, proposal.id, choice)
}
}
module.exports = {
vote_on
}