-
Notifications
You must be signed in to change notification settings - Fork 2
/
cs-api.js
115 lines (96 loc) · 2.8 KB
/
cs-api.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
const fetch = typeof window !== 'undefined' && window.fetch ? window.fetch : require('node-fetch');
const cache_lifetime = 10 * 60 * 1000; // 10 minutes
class Cache {
// data = {};
get(key) {
const record = this.data[key];
if (!record)
return null;
if (record.ts < Date.now() - cache_lifetime) // expired
return null;
return record.value;
}
put(key, value) {
this.data[key] = { value, ts: Date.now() };
}
constructor() {
this.data = {};
}
}
const cache = new Cache();
function cachify(func, count_args) {
return async function() {
const bForceUpdate = arguments[count_args]; // the last arg is optional
const args = [];
for (let i = 0; i < count_args; i++) // not including the 'cached' arg
args[i] = arguments[i];
const key = func.name + '_' + args.join(',');
if (!bForceUpdate) { // use cached value if available
const value = cache.get(key);
if (value !== null) {
// console.log(`using cached value ${value} for`, func.name, arguments)
return value;
}
}
const value = await func.apply(null, args);
cache.put(key, value);
return value;
}
}
const request = async (url, options) => {
const response = await fetch(url, {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
...options
});
if (!response.ok) {
const error = await response.text();
console.error('-- error', error);
throw new Error(error);
}
const data = await response.json();
return data;
}
function getBaseUrl(testnet) {
return `https://${testnet ? 'testnet-bridge.' : ''}counterstake.org/api`;
}
const fetchBridges = async (testnet) => {
console.log(`fetching bridges`);
const data = await request(`${getBaseUrl(testnet)}/bridges`);
if (data.status !== 'success')
throw Error(`getting bridges failed ${JSON.stringify(data)}`);
const bridges = data.data;
// console.log('bridges', bridges)
return bridges;
}
/**
* Query the status of a transfer you previously sent
* @memberOf counterstake-sdk
* @param {string} txid
* @param {boolean} testnet
* @return {Promise<Object>}
* @example
* const transfer = await getTransfer(txid, testnet);
*/
async function getTransfer(txid, testnet) {
const data = await request(`${getBaseUrl(testnet)}/transfer/?txid=${encodeURIComponent(txid)}`);
if (data.status !== 'success')
throw Error(`getting transfer ${txid} failed ${JSON.stringify(data)}`);
const transfer = data.data;
// console.log('transfer', transfer)
return transfer;
}
/**
* Get the list of all bridges and information about them
* @memberOf counterstake-sdk
* @param {boolean} testnet
* @param {boolean} bForceUpdate
* @return {Promise<Array<Object>>}
* @example
* const bridges = await getBridges(testnet, bForceUpdate);
*/
const getBridges = cachify(fetchBridges, 1)
exports.getBridges = getBridges;
exports.getTransfer = getTransfer;