Skip to content

Commit

Permalink
ahead src
Browse files Browse the repository at this point in the history
  • Loading branch information
olivbau committed Oct 30, 2024
1 parent 435f79f commit 328a6fe
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/Fetcher.ts
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>;
}
5 changes: 5 additions & 0 deletions src/TokenRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@ import {
S3Client,
S3ClientConfig,
} from '@aws-sdk/client-s3';
import { NetworkIdType } from '@sonarwatch/portfolio-core';
import { Logger } from './Logger';
import Fetcher from './Fetcher';

export type TokenRegistryConfig = {
logger?: Logger;
redisOptions: RedisOptions;
s3Config: S3ClientConfig & { bucket: string };
fetchers: Partial<Record<NetworkIdType, Fetcher>>;
};

export class TokenRegistry {
private logger: Logger | undefined;
private redisClient: Redis;
private s3Client: S3Client;
private s3Bucket: string;
private fetchers: Partial<Record<NetworkIdType, Fetcher>>;

constructor(config: TokenRegistryConfig) {
this.logger = config.logger;
this.fetchers = config.fetchers;

// Redis
this.redisClient = new Redis(config.redisOptions);
Expand Down
22 changes: 22 additions & 0 deletions src/fetchers/evm.ts
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;
}
}
4 changes: 4 additions & 0 deletions src/fetchers/index.ts
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 };
22 changes: 22 additions & 0 deletions src/fetchers/solana.ts
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;
}
}
25 changes: 25 additions & 0 deletions src/helpers.ts
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';
}
13 changes: 4 additions & 9 deletions src/index.ts
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';
37 changes: 37 additions & 0 deletions tests/TokenRegistry.test.local.ts
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();
});
});
50 changes: 50 additions & 0 deletions tests/fetchers/solana.test.ts
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');
});
});
7 changes: 0 additions & 7 deletions tests/index.test.ts

This file was deleted.

0 comments on commit 328a6fe

Please sign in to comment.