Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olivbau committed Dec 3, 2024
1 parent a91bb30 commit 8b9d30b
Show file tree
Hide file tree
Showing 11 changed files with 151 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/fetchers/aptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import Fetcher from '../Fetcher';
import { Token } from '../types';
import urlToUrlWithHeaders from '../helpers/urlToUrlWithHeaders';
import { aptosToken } from '../helpers/constants';

const coinInfo = '0x1::coin::CoinInfo';
function getCoinAddressFromCoinType(coinType: string) {
Expand Down Expand Up @@ -44,6 +45,8 @@ export default class AptosFetcher extends Fetcher {
}

async _fetch(address: string): Promise<Token | null> {
if (address === aptosToken.address) return aptosToken;

const isFungible = address.split('::').length === 1;

let coinMetadata: CoinMetadata | void;
Expand Down
5 changes: 4 additions & 1 deletion src/fetchers/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
bitcoinNetwork,
NetworkId,
uniformBitcoinTokenAddress,
} from '@sonarwatch/portfolio-core';
Expand All @@ -17,14 +18,16 @@ export default class BitcoinFetcher extends Fetcher {

// eslint-disable-next-line class-methods-use-this
async _fetch(address: string): Promise<Token | null> {
if (address !== bitcoinNetwork.native.address) return null;

return {
chainId: 1,
address,
decimals: 8,
name: 'Bitcoin',
symbol: 'BTC',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/BTC.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/BTC.webp',
networkId: NetworkId.bitcoin,
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/fetchers/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { avalanche, bsc, mainnet, polygon } from 'viem/chains';
import Fetcher from '../Fetcher';
import { Token } from '../types';
import urlToUrlWithHeaders from '../helpers/urlToUrlWithHeaders';
import { evmTokensMap } from '../helpers/constants';
import { rawTokensMap } from '../helpers/constants';
import { TokenRegistry } from '../TokenRegistry';

export const viemChainsByNetworkId: Record<EvmNetworkIdType, Chain> = {
Expand Down Expand Up @@ -46,7 +46,7 @@ export default class EvmFetcher extends Fetcher {
}

async _fetch(address: string): Promise<Token | null> {
const constantToken = evmTokensMap.get(
const constantToken = rawTokensMap.get(
TokenRegistry.getKey(address, this.networkId)
);
if (constantToken) return constantToken;
Expand Down
4 changes: 4 additions & 0 deletions src/fetchers/solana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@sonarwatch/portfolio-core';
import Fetcher from '../Fetcher';
import { Token } from '../types';
import { solToken, wsolToken } from '../helpers/constants';

type DasGetAsset = {
error?: unknown;
Expand Down Expand Up @@ -39,6 +40,9 @@ export default class SolanaFetcher extends Fetcher {
}

async _fetch(address: string): Promise<Token | null> {
if (address === solToken.address) return solToken;
if (address === wsolToken.address) return wsolToken;

const res: AxiosResponse<DasGetAsset> = await axios.post(this.dasUrl, {
jsonrpc: '2.0',
id: 'text',
Expand Down
3 changes: 3 additions & 0 deletions src/fetchers/sui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
import { NetworkId, uniformMoveTokenAddress } from '@sonarwatch/portfolio-core';
import Fetcher from '../Fetcher';
import { Token } from '../types';
import { suiToken } from '../helpers/constants';

export default class SuiFetcher extends Fetcher {
private client: MystenSuiClient;
Expand All @@ -26,6 +27,8 @@ export default class SuiFetcher extends Fetcher {
}

async _fetch(address: string): Promise<Token | null> {
if (address === suiToken.address) return suiToken;

const res = await this.client
.getCoinMetadata({ coinType: address })
.catch((e) => {
Expand Down
112 changes: 92 additions & 20 deletions src/helpers/constants.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,162 @@
import { NetworkId } from '@sonarwatch/portfolio-core';
import {
aptosNetwork,
NetworkId,
solanaNativeDecimals,
solanaNetwork,
suiNetwork,
uniformEvmTokenAddress,
uniformMoveTokenAddress,
} from '@sonarwatch/portfolio-core';
import { Token } from '../types';
import { TokenRegistry } from '../TokenRegistry';

export const evmTokens: Token[] = [
export const solToken: Token = {
address: '11111111111111111111111111111111',
chainId: solanaNetwork.chainId,
decimals: solanaNativeDecimals,
name: 'Solana',
networkId: NetworkId.solana,
symbol: 'SOL',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/SOL.webp',
};

export const wsolToken: Token = {
address: 'So11111111111111111111111111111111111111112',
chainId: solanaNetwork.chainId,
decimals: solanaNativeDecimals,
name: 'Wrapped SOL',
networkId: NetworkId.solana,
symbol: 'wSOL',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/SOL.webp',
};

export const suiToken: Token = {
address: uniformMoveTokenAddress(
'0x0000000000000000000000000000000000000000000000000000000000000002::sui::SUI'
),
chainId: suiNetwork.chainId,
decimals: suiNetwork.native.decimals,
name: 'Sui',
networkId: NetworkId.sui,
symbol: 'SUI',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/SUI.webp',
};

export const aptosToken: Token = {
address: uniformMoveTokenAddress('0x1::aptos_coin::AptosCoin'),
chainId: aptosNetwork.chainId,
decimals: aptosNetwork.native.decimals,
name: 'Aptos',
networkId: NetworkId.aptos,
symbol: 'APT',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/APT.webp',
};

export const rawTokens: Token[] = [
{
chainId: 1,
address: '0x0000000000000000000000000000000000000000',
address: uniformEvmTokenAddress(
'0x0000000000000000000000000000000000000000'
),
decimals: 18,
name: 'Ether',
symbol: 'ETH',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/ETH.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/ETH.webp',
networkId: NetworkId.ethereum,
},
{
chainId: 1,
address: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
address: uniformEvmTokenAddress(
'0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'
),
decimals: 18,
name: 'Ether',
symbol: 'ETH',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/ETH.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/ETH.webp',
networkId: NetworkId.ethereum,
},
{
name: 'Polygon Ecosystem Token',
address: '0x0000000000000000000000000000000000001010',
address: uniformEvmTokenAddress(
'0x0000000000000000000000000000000000001010'
),
symbol: 'POL',
decimals: 18,
chainId: 137,
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/MATIC.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/MATIC.webp',
networkId: NetworkId.polygon,
},
{
name: 'Wrapped Polygon Ecosystem Token',
address: '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
address: uniformEvmTokenAddress(
'0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270'
),
symbol: 'WPOL',
decimals: 18,
chainId: 137,
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/MATIC.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/MATIC.webp',
networkId: NetworkId.polygon,
},
{
chainId: 43114,
address: '0x0000000000000000000000000000000000000000',
address: uniformEvmTokenAddress(
'0x0000000000000000000000000000000000000000'
),
decimals: 18,
name: 'Avalanche',
symbol: 'AVAX',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/AVAX.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/AVAX.webp',
networkId: NetworkId.avalanche,
},
{
chainId: 43114,
address: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
address: uniformEvmTokenAddress(
'0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7'
),
decimals: 18,
name: 'Wrapped AVAX',
symbol: 'WAVAX',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/AVAX.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/AVAX.webp',
networkId: NetworkId.avalanche,
},
{
chainId: 56,
address: '0x0000000000000000000000000000000000000000',
address: uniformEvmTokenAddress(
'0x0000000000000000000000000000000000000000'
),
decimals: 18,
name: 'BNB',
symbol: 'BNB',
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/BNB.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/BNB.webp',
networkId: NetworkId.bnb,
},
{
chainId: 56,
address: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
address: uniformEvmTokenAddress(
'0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'
),
name: 'Wrapped BNB',
symbol: 'WBNB',
decimals: 18,
logoURI:
'https://raw.githubusercontent.com/sonarwatch/token-lists/main/images/common/BNB.png',
'https://raw.githubusercontent.com/sonarwatch/token-registry/main/img/BNB.webp',
networkId: NetworkId.bnb,
},
suiToken,
aptosToken,
];

export const evmTokensMap = new Map(
evmTokens.map((t) => [TokenRegistry.getKey(t.address, t.networkId), t])
export const rawTokensMap = new Map(
rawTokens.map((t) => [TokenRegistry.getKey(t.address, t.networkId), t])
);
6 changes: 3 additions & 3 deletions src/jobs/coingeckoJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import axios, { AxiosResponse } from 'axios';
import { sleep } from '../helpers/misc';
import { Token, JobFct, Job } from '../types';
import { evmTokensMap } from '../helpers/constants';
import { rawTokensMap } from '../helpers/constants';
import { TokenRegistry } from '../TokenRegistry';

const platforms: Record<string, string> = {
Expand Down Expand Up @@ -103,8 +103,8 @@ function getCoingeckoJob(networkId: NetworkIdType) {
networkId
);

// Ignore tokens that are in evmTokensMap
if (evmTokensMap.has(TokenRegistry.getKey(address, networkId))) continue;
// Ignore tokens that are in rawTokensMap
if (rawTokensMap.has(TokenRegistry.getKey(address, networkId))) continue;

const coinDetailsRes: AxiosResponse<GeckoCoinDetails> | null = await axios
.get(`https://api.coingecko.com/api/v3/coins/${gCoin.id}`, {
Expand Down
4 changes: 4 additions & 0 deletions src/jobs/jupiterJob.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import { NetworkId } from '@sonarwatch/portfolio-core';
import { Token, JobFct, Job } from '../types';
import { solToken, wsolToken } from '../helpers/constants';

async function getJupTokens() {
const res = await axios.get('https://tokens.jup.ag/tokens', {
Expand All @@ -16,6 +17,9 @@ const jobFct: JobFct = async () => {
const tokens = new Map<string, Token>();
for (let i = 0; i < jupTokens.length; i += 1) {
const jupToken = jupTokens[i];
if (jupToken.address === solToken.address) continue;
if (jupToken.address === wsolToken.address) continue;

tokens.set(jupToken.address, {
address: jupToken.address,
chainId: 101,
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/aptosFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ describe('aptosFetcher', () => {
expect(tokenInfo2).toBe(null);
});

it('should return APT token', async () => {
const address = '0x00001::aptos_coin::AptosCoin';
const tokenInfo = await fetcher.fetch(address);
expect(tokenInfo).not.toBeNull();
expect(tokenInfo?.symbol).toBe('APT');
expect(tokenInfo?.chainId).toBe(1);
expect(tokenInfo?.decimals).toBe(8);
});

it('should return APE token', async () => {
const address =
'0xada35ada7e43e2ee1c39633ffccec38b76ce702b4efc2e60b50f63fbe4f710d8::apetos_token::ApetosCoin';
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/solanaFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ describe('SolanaFetcher', () => {
expect(tokenInfo).toBe(null);
});

it('should return wSOL token', async () => {
const address = 'So11111111111111111111111111111111111111112';
const tokenInfo = await fetcher.fetch(address);
expect(tokenInfo).not.toBeNull();
expect(tokenInfo?.symbol).toBe('wSOL');
expect(tokenInfo?.chainId).toBe(101);
expect(tokenInfo?.decimals).toBe(9);
});

it('should return SOL token', async () => {
const address = '11111111111111111111111111111111';
const tokenInfo = await fetcher.fetch(address);
expect(tokenInfo).not.toBeNull();
expect(tokenInfo?.symbol).toBe('SOL');
expect(tokenInfo?.chainId).toBe(101);
expect(tokenInfo?.decimals).toBe(9);
});

it('should return PYTH token', async () => {
const address = 'HZ1JovNiVvGrGNiiYvEozEVgZ58xaU3RKwX8eACQBCt3';
const tokenInfo = await fetcher.fetch(address);
Expand Down
9 changes: 9 additions & 0 deletions tests/integration/suiFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ describe('suiFetcher', () => {
expect(tokenInfo2).toBe(null);
});

it('should return SUI token', async () => {
const address = '0x002::sui::SUI';
const tokenInfo = await fetcher.fetch(address);
expect(tokenInfo).not.toBeNull();
expect(tokenInfo?.symbol).toBe('SUI');
expect(tokenInfo?.chainId).toBe(1);
expect(tokenInfo?.decimals).toBe(9);
});

it('should return BOB token', async () => {
const address =
'0x5f3a18cdfd7ef0527a65ba5c07dbe0efe276507d4d1a4d1bebe87f5d40df6cf6::bob::BOB';
Expand Down

0 comments on commit 8b9d30b

Please sign in to comment.