|
| 1 | +import { |
| 2 | + DurationInMs, |
| 3 | + Price, |
| 4 | + PriceFeed, |
| 5 | + PriceFeedMetadata, |
| 6 | + PriceServiceConnection, |
| 7 | +} from "../index"; |
| 8 | + |
| 9 | +async function sleep(duration: DurationInMs): Promise<void> { |
| 10 | + return new Promise((res) => setTimeout(res, duration)); |
| 11 | +} |
| 12 | + |
| 13 | +// The endpoint is set to the price service endpoint in Tilt. |
| 14 | +// Please note that if you change it to a mainnet/testnet endpoint |
| 15 | +// some tests might fail due to the huge response size of a request |
| 16 | +// , i.e. requesting latest price feeds or vaas of all price ids. |
| 17 | +const PRICE_SERVICE_ENDPOINT = "http://pyth-price-server:4200"; |
| 18 | + |
| 19 | +describe("Test http endpoints", () => { |
| 20 | + test("Get price feed (without verbose/binary) works", async () => { |
| 21 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT); |
| 22 | + const ids = await connection.getPriceFeedIds(); |
| 23 | + expect(ids.length).toBeGreaterThan(0); |
| 24 | + |
| 25 | + const priceFeeds = await connection.getLatestPriceFeeds(ids); |
| 26 | + expect(priceFeeds).toBeDefined(); |
| 27 | + expect(priceFeeds!.length).toEqual(ids.length); |
| 28 | + |
| 29 | + for (const priceFeed of priceFeeds!) { |
| 30 | + expect(priceFeed.id.length).toBe(64); // 32 byte address has size 64 in hex |
| 31 | + expect(priceFeed).toBeInstanceOf(PriceFeed); |
| 32 | + expect(priceFeed.getPriceUnchecked()).toBeInstanceOf(Price); |
| 33 | + expect(priceFeed.getEmaPriceUnchecked()).toBeInstanceOf(Price); |
| 34 | + expect(priceFeed.getMetadata()).toBeUndefined(); |
| 35 | + expect(priceFeed.getVAA()).toBeUndefined(); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + test("Get price feed with verbose flag works", async () => { |
| 40 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, { |
| 41 | + priceFeedRequestConfig: { verbose: true }, |
| 42 | + }); |
| 43 | + |
| 44 | + const ids = await connection.getPriceFeedIds(); |
| 45 | + expect(ids.length).toBeGreaterThan(0); |
| 46 | + |
| 47 | + const priceFeeds = await connection.getLatestPriceFeeds(ids); |
| 48 | + expect(priceFeeds).toBeDefined(); |
| 49 | + expect(priceFeeds!.length).toEqual(ids.length); |
| 50 | + |
| 51 | + for (const priceFeed of priceFeeds!) { |
| 52 | + expect(priceFeed.getMetadata()).toBeInstanceOf(PriceFeedMetadata); |
| 53 | + expect(priceFeed.getVAA()).toBeUndefined(); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + test("Get price feed with binary flag works", async () => { |
| 58 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, { |
| 59 | + priceFeedRequestConfig: { binary: true }, |
| 60 | + }); |
| 61 | + |
| 62 | + const ids = await connection.getPriceFeedIds(); |
| 63 | + expect(ids.length).toBeGreaterThan(0); |
| 64 | + |
| 65 | + const priceFeeds = await connection.getLatestPriceFeeds(ids); |
| 66 | + expect(priceFeeds).toBeDefined(); |
| 67 | + expect(priceFeeds!.length).toEqual(ids.length); |
| 68 | + |
| 69 | + for (const priceFeed of priceFeeds!) { |
| 70 | + expect(priceFeed.getMetadata()).toBeUndefined(); |
| 71 | + expect(priceFeed.getVAA()?.length).toBeGreaterThan(0); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + test("Get latest vaa works", async () => { |
| 76 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, { |
| 77 | + priceFeedRequestConfig: { binary: true }, |
| 78 | + }); |
| 79 | + |
| 80 | + const ids = await connection.getPriceFeedIds(); |
| 81 | + expect(ids.length).toBeGreaterThan(0); |
| 82 | + |
| 83 | + const vaas = await connection.getLatestVaas(ids); |
| 84 | + expect(vaas.length).toBeGreaterThan(0); |
| 85 | + |
| 86 | + for (const vaa of vaas) { |
| 87 | + expect(vaa.length).toBeGreaterThan(0); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
| 91 | + test("Get vaa works", async () => { |
| 92 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, { |
| 93 | + priceFeedRequestConfig: { binary: true }, |
| 94 | + }); |
| 95 | + |
| 96 | + const ids = await connection.getPriceFeedIds(); |
| 97 | + expect(ids.length).toBeGreaterThan(0); |
| 98 | + |
| 99 | + const publishTime10SecAgo = Math.floor(new Date().getTime() / 1000) - 10; |
| 100 | + const [vaa, vaaPublishTime] = await connection.getVaa( |
| 101 | + ids[0], |
| 102 | + publishTime10SecAgo |
| 103 | + ); |
| 104 | + |
| 105 | + expect(vaa.length).toBeGreaterThan(0); |
| 106 | + expect(vaaPublishTime).toBeGreaterThanOrEqual(publishTime10SecAgo); |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("Test websocket endpoints", () => { |
| 111 | + jest.setTimeout(60 * 1000); |
| 112 | + |
| 113 | + test.concurrent( |
| 114 | + "websocket subscription works without verbose and binary", |
| 115 | + async () => { |
| 116 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT); |
| 117 | + |
| 118 | + const ids = await connection.getPriceFeedIds(); |
| 119 | + expect(ids.length).toBeGreaterThan(0); |
| 120 | + |
| 121 | + const counter: Map<string, number> = new Map(); |
| 122 | + let totalCounter = 0; |
| 123 | + |
| 124 | + await connection.subscribePriceFeedUpdates(ids, (priceFeed) => { |
| 125 | + expect(priceFeed.id.length).toBe(64); // 32 byte address has size 64 in hex |
| 126 | + expect(priceFeed.getMetadata()).toBeUndefined(); |
| 127 | + expect(priceFeed.getVAA()).toBeUndefined(); |
| 128 | + |
| 129 | + counter.set(priceFeed.id, (counter.get(priceFeed.id) ?? 0) + 1); |
| 130 | + totalCounter += 1; |
| 131 | + }); |
| 132 | + |
| 133 | + // Wait for 30 seconds |
| 134 | + await sleep(30000); |
| 135 | + connection.closeWebSocket(); |
| 136 | + |
| 137 | + expect(totalCounter).toBeGreaterThan(30); |
| 138 | + |
| 139 | + for (const id of ids) { |
| 140 | + expect(counter.get(id)).toBeDefined(); |
| 141 | + // Make sure it receives more than 1 update |
| 142 | + expect(counter.get(id)).toBeGreaterThan(1); |
| 143 | + } |
| 144 | + } |
| 145 | + ); |
| 146 | + |
| 147 | + test.concurrent("websocket subscription works with verbose", async () => { |
| 148 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, { |
| 149 | + priceFeedRequestConfig: { verbose: true }, |
| 150 | + }); |
| 151 | + |
| 152 | + const ids = await connection.getPriceFeedIds(); |
| 153 | + expect(ids.length).toBeGreaterThan(0); |
| 154 | + |
| 155 | + const observedFeeds: Set<string> = new Set(); |
| 156 | + |
| 157 | + await connection.subscribePriceFeedUpdates(ids, (priceFeed) => { |
| 158 | + expect(priceFeed.getMetadata()).toBeInstanceOf(PriceFeedMetadata); |
| 159 | + expect(priceFeed.getVAA()).toBeUndefined(); |
| 160 | + observedFeeds.add(priceFeed.id); |
| 161 | + }); |
| 162 | + |
| 163 | + // Wait for 20 seconds |
| 164 | + await sleep(20000); |
| 165 | + await connection.unsubscribePriceFeedUpdates(ids); |
| 166 | + |
| 167 | + for (const id of ids) { |
| 168 | + expect(observedFeeds.has(id)).toBe(true); |
| 169 | + } |
| 170 | + }); |
| 171 | + |
| 172 | + test.concurrent("websocket subscription works with binary", async () => { |
| 173 | + const connection = new PriceServiceConnection(PRICE_SERVICE_ENDPOINT, { |
| 174 | + priceFeedRequestConfig: { binary: true }, |
| 175 | + }); |
| 176 | + |
| 177 | + const ids = await connection.getPriceFeedIds(); |
| 178 | + expect(ids.length).toBeGreaterThan(0); |
| 179 | + |
| 180 | + const observedFeeds: Set<string> = new Set(); |
| 181 | + |
| 182 | + await connection.subscribePriceFeedUpdates(ids, (priceFeed) => { |
| 183 | + expect(priceFeed.getMetadata()).toBeUndefined(); |
| 184 | + expect(priceFeed.getVAA()?.length).toBeGreaterThan(0); |
| 185 | + observedFeeds.add(priceFeed.id); |
| 186 | + }); |
| 187 | + |
| 188 | + // Wait for 20 seconds |
| 189 | + await sleep(20000); |
| 190 | + connection.closeWebSocket(); |
| 191 | + |
| 192 | + for (const id of ids) { |
| 193 | + expect(observedFeeds.has(id)).toBe(true); |
| 194 | + } |
| 195 | + }); |
| 196 | +}); |
0 commit comments