1
1
import fetch from 'cross-fetch' ;
2
2
import { Interface } from '@ethersproject/abi' ;
3
3
import { Contract } from '@ethersproject/contracts' ;
4
- import { StaticJsonRpcProvider } from '@ethersproject/providers ' ;
4
+ import { namehash } from '@ethersproject/hash ' ;
5
5
import { jsonToGraphQLQuery } from 'json-to-graphql-query' ;
6
6
import Ajv from 'ajv' ;
7
7
import addFormats from 'ajv-formats' ;
@@ -19,8 +19,6 @@ export const SNAPSHOT_SUBGRAPH_URL = {
19
19
'42' : 'https://api.thegraph.com/subgraphs/name/snapshot-labs/snapshot-kovan'
20
20
} ;
21
21
22
- export const SNAPSHOT_SCORE_API = 'https://score.snapshot.org/api/scores' ;
23
-
24
22
export async function call ( provider , abi : any [ ] , call : any [ ] , options ?) {
25
23
const contract = new Contract ( call [ 0 ] , abi , provider ) ;
26
24
try {
@@ -116,9 +114,9 @@ export async function getScores(
116
114
space : string ,
117
115
strategies : any [ ] ,
118
116
network : string ,
119
- provider : StaticJsonRpcProvider | string ,
120
117
addresses : string [ ] ,
121
- snapshot : number | string = 'latest'
118
+ snapshot : number | string = 'latest' ,
119
+ scoreApiUrl = 'https://score.snapshot.org/api/scores'
122
120
) {
123
121
try {
124
122
const params = {
@@ -128,7 +126,7 @@ export async function getScores(
128
126
strategies,
129
127
addresses
130
128
} ;
131
- const res = await fetch ( SNAPSHOT_SCORE_API , {
129
+ const res = await fetch ( scoreApiUrl , {
132
130
method : 'POST' ,
133
131
headers : { 'Content-Type' : 'application/json' } ,
134
132
body : JSON . stringify ( { params } )
@@ -149,6 +147,36 @@ export function validateSchema(schema, data) {
149
147
return valid ? valid : validate . errors ;
150
148
}
151
149
150
+ export async function getSpaceUri ( id ) {
151
+ const abi =
152
+ 'function text(bytes32 node, string calldata key) external view returns (string memory)' ;
153
+ const address = '0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41' ;
154
+
155
+ let uri : any = false ;
156
+ try {
157
+ const hash = namehash ( id ) ;
158
+ const provider = getProvider ( '1' ) ;
159
+ uri = await call (
160
+ provider ,
161
+ [ abi ] ,
162
+ [ address , 'text' , [ hash , 'snapshot' ] ]
163
+ ) ;
164
+ } catch ( e ) {
165
+ console . log ( 'getSpaceUriFromTextRecord failed' , id , e ) ;
166
+ }
167
+ return uri ;
168
+ }
169
+
170
+ export function clone ( item ) {
171
+ return JSON . parse ( JSON . stringify ( item ) ) ;
172
+ }
173
+
174
+ export async function sleep ( time ) {
175
+ return new Promise ( resolve => {
176
+ setTimeout ( resolve , time ) ;
177
+ } ) ;
178
+ }
179
+
152
180
export default {
153
181
call,
154
182
multicall,
@@ -158,6 +186,9 @@ export default {
158
186
sendTransaction,
159
187
getScores,
160
188
validateSchema,
189
+ getSpaceUri,
190
+ clone,
191
+ sleep,
161
192
getProvider,
162
193
signMessage,
163
194
getBlockNumber,
0 commit comments