-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcoingecko.spec.mts
More file actions
146 lines (129 loc) · 4.38 KB
/
coingecko.spec.mts
File metadata and controls
146 lines (129 loc) · 4.38 KB
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
import * as chai from "chai";
import chaiAsPromised from "chai-as-promised";
chai.use(chaiAsPromised);
const assert: Chai.Assert = chai.assert;
import { FakeCryptoAsset } from "../../support/cryptoasset.fake.mjs";
import {
CoinGeckoOracle,
getCoinGeckoId,
} from "../../../src/services/oracles/coingecko.mjs";
import { FiatCurrency } from "../../../src/fiatcurrency.mjs";
import {
CryptoMetadata,
CryptoRegistryNG,
} from "../../../src/cryptoregistry.mjs";
import { PriceMap } from "../../../src/services/oracle.mjs";
import { prepare } from "../../support/register.helper.mjs";
import { when } from "../../support/test.helper.mjs";
const MOCHA_TEST_TIMEOUT = 60000;
const INTERNAL_TO_COINGECKO_ID = {
bitcoin: "bitcoin",
};
describe("CoinGecko", function () {
when("COINGECKO_API_KEY", describe)("Live tests", function () {
const API_KEY = process.env["COINGECKO_API_KEY"];
this.timeout(MOCHA_TEST_TIMEOUT);
this.slow(MOCHA_TEST_TIMEOUT);
let coingecko: CoinGeckoOracle;
let cryptoRegistry: CryptoRegistryNG;
let cryptoMetadata: CryptoMetadata;
beforeEach(function () {
coingecko = CoinGeckoOracle.create(API_KEY, INTERNAL_TO_COINGECKO_ID);
cryptoRegistry = CryptoRegistryNG.create();
cryptoMetadata = CryptoMetadata.create();
});
describe("API", () => {});
describe("getPrice", function () {
describe("should return historical prices", function () {
const register = prepare(this);
const bitcoin = FakeCryptoAsset.bitcoin;
const test_cases: [string, string, Record<string, string>][] = [
[
"bitcoin",
"2025-12-30",
{
BTC: "1.000000", // 1,
USD: "87156.562660", // 87156.56266080117,
EUR: "74039.325667", // 74039.32566722526,
},
],
];
for (const [id, date, expected] of test_cases) {
register(`case ${id} ${date}`, async () => {
const priceMap = new Map() as PriceMap;
await coingecko!.getPrice(
cryptoRegistry,
cryptoMetadata,
bitcoin,
new Date(date),
new Set(Object.keys(expected).map(FiatCurrency)),
priceMap,
);
const expectedCurrencies = Object.keys(expected).map((code) =>
FiatCurrency(code),
);
assert.includeMembers(
Array.from(priceMap.keys()),
expectedCurrencies,
);
for (const [currency, value] of Object.entries(expected)) {
const fiatCurrency = FiatCurrency(currency);
const price = priceMap.get(fiatCurrency);
assert.exists(price, `Price for ${currency} should exist`);
if (price) {
assert.equal(price.rate.toDecimalString(6), value);
}
}
});
}
});
it("should properly URI encode the crypto id", async function () {
// If the URI is not properly encoded, the price of the crypto below
// will be resolved as if it was the "plain" bitcoin.
const maliciousCrypto = cryptoRegistry.createCryptoAsset(
"/bitcoin",
"fake bitcoin",
"/BTC",
18,
);
const priceMap = new Map() as PriceMap;
await coingecko.getPrice(
cryptoRegistry,
cryptoMetadata,
maliciousCrypto,
new Date("2024-12-30"),
new Set([FiatCurrency("eur")]),
priceMap,
);
assert.equal(priceMap.size, 0);
});
describe("should convert from internal id to CoinGecko id", function () {
const register = prepare(this);
const idMapping = {
usdc: "usd-coin",
};
// prettier-ignore
const testcases = [
[ "usdc", "usd-coin" ]
];
for (const [internalId, expected] of testcases) {
register(`case ${internalId} ⏵ ${expected}`, () => {
const crypto = cryptoRegistry.createCryptoAsset(
"usdc",
"USDC",
"USDC",
6,
);
const coinGeckoId = getCoinGeckoId(
cryptoRegistry,
cryptoMetadata,
crypto,
idMapping,
);
assert.equal(coinGeckoId, expected);
});
}
});
});
});
});