-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.js
59 lines (54 loc) · 1.58 KB
/
example.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
let io = require('socket.io-client')
const BASE_URL = 'wss://api-v4.zerion.io/';
function verify(request, response) {
// each value in request payload must be found in response meta
return Object.keys(request.payload).every(key => {
const requestValue = request.payload[key];
const responseMetaValue = response.meta[key];
if (typeof requestValue === 'object') {
return JSON.stringify(requestValue) === JSON.stringify(responseMetaValue);
}
return responseMetaValue === requestValue;
});
}
const addressSocket = {
namespace: 'address',
socket: io(`${BASE_URL}address`, {
transports: ['websocket'],
timeout: 60000,
query: {
api_token:
'Demo.ukEVQp6L5vfgxcz4sBke7XvS873GMYHy',
},
}),
};
function get(socketNamespace, requestBody) {
console.log("in get")
return new Promise(resolve => {
const { socket, namespace } = socketNamespace;
function handleReceive(data) {
console.log("in handle receive")
if (verify(requestBody, data)) {
unsubscribe();
resolve(data);
}
}
const model = requestBody.scope[0];
function unsubscribe() {
socket.off(`received ${namespace} ${model}`, handleReceive);
socket.emit('unsubscribe', requestBody);
}
socket.emit('get', requestBody);
socket.on(`received ${namespace} ${model}`, handleReceive);
});
}
get(addressSocket, {
scope: ['portfolio'],
payload: {
address: '0x7e5ce10826ee167de897d262fcc9976f609ecd2b',
currency: 'usd',
portfolio_fields: 'all'
},
}).then(response => {
console.log(response.payload.portfolio);
});