-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Remove coingecko-api-v3 library and de-dupe fetching utils (#4837)
### Description - Remove coingecko-api-v3 lib which is unnecessary and breaks bundling - Remove `getCoingeckoTokenPrices` function which is redundant with `CoinGeckoTokenPriceGetter` - Simplify `CoinGeckoTokenPriceGetter` and remove need for Mock class Related: #4787 See discussion here: https://discord.com/channels/935678348330434570/1304125818817220653 ### Drive-by changes - Fix bundling issue with Storybook in widgets lib ### Backward compatibility No ### Testing Rewrote unit tests and tested Storybook
- Loading branch information
Showing
11 changed files
with
141 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@hyperlane-xyz/sdk': major | ||
--- | ||
|
||
Remove getCoingeckoTokenPrices (use CoinGeckoTokenPriceGetter instead) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,83 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
|
||
import { ethereum, solanamainnet } from '@hyperlane-xyz/registry'; | ||
|
||
import { TestChainName, testChainMetadata } from '../consts/testChains.js'; | ||
import { MockCoinGecko } from '../test/MockCoinGecko.js'; | ||
|
||
import { CoinGeckoTokenPriceGetter } from './token-prices.js'; | ||
|
||
const MOCK_FETCH_CALLS = true; | ||
|
||
describe('TokenPriceGetter', () => { | ||
let tokenPriceGetter: CoinGeckoTokenPriceGetter; | ||
let mockCoinGecko: MockCoinGecko; | ||
const chainA = TestChainName.test1, | ||
chainB = TestChainName.test2, | ||
priceA = 1, | ||
priceB = 5.5; | ||
before(async () => { | ||
mockCoinGecko = new MockCoinGecko(); | ||
// Origin token | ||
mockCoinGecko.setTokenPrice(chainA, priceA); | ||
// Destination token | ||
mockCoinGecko.setTokenPrice(chainB, priceB); | ||
tokenPriceGetter = new CoinGeckoTokenPriceGetter( | ||
mockCoinGecko, | ||
testChainMetadata, | ||
undefined, | ||
0, | ||
); | ||
|
||
const chainA = TestChainName.test1; | ||
const chainB = TestChainName.test2; | ||
const priceA = 2; | ||
const priceB = 5; | ||
let stub: sinon.SinonStub; | ||
|
||
beforeEach(() => { | ||
tokenPriceGetter = new CoinGeckoTokenPriceGetter({ | ||
chainMetadata: { ethereum, solanamainnet, ...testChainMetadata }, | ||
apiKey: 'test', | ||
expirySeconds: 10, | ||
sleepMsBetweenRequests: 10, | ||
}); | ||
|
||
if (MOCK_FETCH_CALLS) { | ||
stub = sinon | ||
.stub(tokenPriceGetter, 'fetchPriceData') | ||
.returns(Promise.resolve([priceA, priceB])); | ||
} | ||
}); | ||
|
||
describe('getTokenPrice', () => { | ||
it('returns a token price', async () => { | ||
expect(await tokenPriceGetter.getTokenPrice(chainA)).to.equal(priceA); | ||
afterEach(() => { | ||
if (MOCK_FETCH_CALLS && stub) { | ||
stub.restore(); | ||
} | ||
}); | ||
|
||
describe('getTokenPriceByIds', () => { | ||
it('returns token prices', async () => { | ||
// stubbed results | ||
expect( | ||
await tokenPriceGetter.getTokenPriceByIds([ | ||
ethereum.name, | ||
solanamainnet.name, | ||
]), | ||
).to.eql([priceA, priceB]); | ||
}); | ||
}); | ||
|
||
it('caches a token price', async () => { | ||
mockCoinGecko.setFail(chainA, true); | ||
expect(await tokenPriceGetter.getTokenPrice(chainA)).to.equal(priceA); | ||
mockCoinGecko.setFail(chainA, false); | ||
describe('getTokenPrice', () => { | ||
it('returns a token price', async () => { | ||
// hardcoded result of 1 for testnets | ||
expect( | ||
await tokenPriceGetter.getTokenPrice(TestChainName.test1), | ||
).to.equal(1); | ||
// stubbed result for non-testnet | ||
expect(await tokenPriceGetter.getTokenPrice(ethereum.name)).to.equal( | ||
priceA, | ||
); | ||
}); | ||
}); | ||
|
||
describe('getTokenExchangeRate', () => { | ||
it('returns a value consistent with getTokenPrice()', async () => { | ||
const exchangeRate = await tokenPriceGetter.getTokenExchangeRate( | ||
chainA, | ||
chainB, | ||
); | ||
// Should equal 1 because testnet prices are always forced to 1 | ||
expect(exchangeRate).to.equal(1); | ||
// hardcoded result of 1 for testnets | ||
expect( | ||
await tokenPriceGetter.getTokenExchangeRate(chainA, chainB), | ||
).to.equal(1); | ||
|
||
// stubbed result for non-testnet | ||
expect( | ||
await tokenPriceGetter.getTokenExchangeRate( | ||
ethereum.name, | ||
solanamainnet.name, | ||
), | ||
).to.equal(priceA / priceB); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.