-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
174 additions
and
16 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 @@ | ||
import { UniTokenInfo } from '@sonarwatch/portfolio-core'; | ||
|
||
export default abstract class Fetcher { | ||
abstract fetch(address: string): Promise<UniTokenInfo | null>; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { UniTokenInfo } from '@sonarwatch/portfolio-core'; | ||
import axios from 'axios'; | ||
import Fetcher from '../Fetcher'; | ||
|
||
export default class EvmFetcher extends Fetcher { | ||
private rpc: string; | ||
|
||
constructor(rpc: string) { | ||
super(); | ||
this.rpc = rpc; | ||
} | ||
|
||
async fetch(address: string): Promise<UniTokenInfo | null> { | ||
const res = await axios.post(this.rpc, { | ||
jsonrpc: '2.0', | ||
id: 'text', | ||
method: 'getAsset', | ||
params: { id: address }, | ||
}); | ||
return res.data; | ||
} | ||
} |
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,4 @@ | ||
import SolanaFetcher from './solana'; | ||
import EvmFetcher from './evm'; | ||
|
||
export { SolanaFetcher, EvmFetcher }; |
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,22 @@ | ||
import { UniTokenInfo } from '@sonarwatch/portfolio-core'; | ||
import axios from 'axios'; | ||
import Fetcher from '../Fetcher'; | ||
|
||
export default class SolanaFetcher extends Fetcher { | ||
private dasUrl: string; | ||
|
||
constructor(dasUrl: string) { | ||
super(); | ||
this.dasUrl = dasUrl; | ||
} | ||
|
||
async fetch(address: string): Promise<UniTokenInfo | null> { | ||
const res = await axios.post(this.dasUrl, { | ||
jsonrpc: '2.0', | ||
id: 'text', | ||
method: 'getAsset', | ||
params: { id: address }, | ||
}); | ||
return res.data; | ||
} | ||
} |
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,25 @@ | ||
import { NetworkIdType } from '@sonarwatch/portfolio-core'; | ||
import Fetcher from './Fetcher'; | ||
import { EvmFetcher, SolanaFetcher } from './fetchers'; | ||
|
||
export type GetDefaultFetchersConfig = { | ||
solana: { | ||
dasUrl: string; | ||
}; | ||
ethereum: { | ||
rpc: string; | ||
}; | ||
}; | ||
|
||
export function getDefaultFetchers( | ||
config: GetDefaultFetchersConfig | ||
): Partial<Record<NetworkIdType, Fetcher>> { | ||
return { | ||
solana: new SolanaFetcher(config.solana.dasUrl), | ||
ethereum: new EvmFetcher(config.ethereum.rpc), | ||
}; | ||
} | ||
|
||
export function stringToBoolean(str: string): boolean { | ||
return str === 'true'; | ||
} |
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,9 +1,4 @@ | ||
import axios from 'axios'; | ||
|
||
const foo = 'bar'; | ||
function baz() { | ||
return axios.get(''); | ||
} | ||
|
||
export { foo, baz }; | ||
export { TokenRegistry } from './TokenRegistry'; | ||
export * from './Logger'; | ||
export * from './Fetcher'; | ||
export * from './TokenRegistry'; | ||
export * from './fetchers'; |
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,37 @@ | ||
import { TokenRegistry } from '../src/TokenRegistry'; | ||
import { getDefaultFetchers } from '../src/helpers'; | ||
|
||
describe('TokenRegistry', () => { | ||
it('sould be instantiable', async () => { | ||
const s3Endpoint = process.env.S3_ENDPOINT; | ||
const s3AccessKeyId = process.env.S3_ACCESS_KEY_ID; | ||
const s3SecretAccessKey = process.env.S3_SECRET_ACCESS_KEY; | ||
const s3Bucket = process.env.S3_BUCKET; | ||
const s3Region = process.env.S3_REGION; | ||
if (!s3Endpoint) return; | ||
if (!s3AccessKeyId) return; | ||
if (!s3SecretAccessKey) return; | ||
if (!s3Bucket) return; | ||
|
||
const fetchers = getDefaultFetchers({ | ||
solana: { dasUrl: '' }, | ||
ethereum: { rpc: '' }, | ||
}); | ||
const tokenRegistry = new TokenRegistry({ | ||
fetchers, | ||
redisOptions: {}, | ||
s3Config: { | ||
bucket: s3Bucket || '', | ||
endpoint: s3Endpoint, | ||
credentials: { | ||
accessKeyId: s3AccessKeyId || '', | ||
secretAccessKey: s3SecretAccessKey || '', | ||
}, | ||
forcePathStyle: true, | ||
region: s3Region, | ||
}, | ||
}); | ||
await tokenRegistry.checkS3Client(); | ||
expect(tokenRegistry).toBeDefined(); | ||
}); | ||
}); |
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,50 @@ | ||
import axios from 'axios'; | ||
import { UniTokenInfo } from '@sonarwatch/portfolio-core'; | ||
import SolanaFetcher from '../../src/fetchers/solana'; | ||
|
||
// Mock the axios module | ||
jest.mock('axios'); | ||
const mockedAxios = axios as jest.Mocked<typeof axios>; | ||
|
||
describe('SolanaFetcher', () => { | ||
const dasUrl = 'https://example.com'; | ||
let fetcher: SolanaFetcher; | ||
|
||
beforeEach(() => { | ||
fetcher = new SolanaFetcher(dasUrl); | ||
}); | ||
|
||
it('should call the correct URL and return data when fetch is called', async () => { | ||
const address = 'sample-address'; | ||
const mockResponse: UniTokenInfo = { | ||
name: 'Sample Token', | ||
symbol: 'STK', | ||
decimals: 6, | ||
address, | ||
chainId: 101, | ||
}; | ||
|
||
// Mocking axios.post to return a resolved promise with mock data | ||
mockedAxios.post.mockResolvedValueOnce({ data: mockResponse }); | ||
|
||
// Call the fetch method | ||
const result = await fetcher.fetch(address); | ||
|
||
// Check if axios.post was called with correct parameters | ||
expect(mockedAxios.post).toHaveBeenCalledWith(dasUrl, { | ||
jsonrpc: '2.0', | ||
id: 'text', | ||
method: 'getAsset', | ||
params: { id: address }, | ||
}); | ||
|
||
// Check if the fetch method returned the expected data | ||
expect(result).toEqual(mockResponse); | ||
}); | ||
|
||
it('should throw an error if axios.post fails', async () => { | ||
const address = 'sample-address'; | ||
mockedAxios.post.mockRejectedValueOnce(new Error('Network error')); | ||
await expect(fetcher.fetch(address)).rejects.toThrow('Network error'); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.