Skip to content
This repository has been archived by the owner on Jul 3, 2022. It is now read-only.

Commit

Permalink
feat: add extensions for website and other metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
oJshua committed Mar 4, 2021
1 parent e177d34 commit c113b02
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 60 deletions.
27 changes: 14 additions & 13 deletions README.md
@@ -1,9 +1,9 @@
# @solana/spl-token-registry

[![npm](https://img.shields.io/npm/v/@solana/spl-token-registry)](https://unpkg.com/@solana/spl-token-registry@latest/) [![GitHub license](https://img.shields.io/badge/license-APACHE-blue.svg)](https://github.com/solana-labs/token-list/blob/b3fa86b3fdd9c817139e38641d46c5a892542a52/LICENSE)
[![npm](https://img.shields.io/npm/v/@solana/spl-token-registry)](https://unpkg.com/@solana/spl-token-registry@latest/) [![GitHub license](https://img.shields.io/badge/license-APACHE-blue.svg)](https://github.com/solana-labs/token-list/blob/b3fa86b3fdd9c817139e38641d46c5a892542a52/LICENSE)

Solana Token Registry is a package that allows application to query for list of tokens.
JSON schema for the tokens includes: name, symbol, logo and mint account address
JSON schema for the tokens includes: chainId, address, name, decimals, symbol, logoURI (optional), tags (optional), and custom extensions metadata.

## Installation

Expand All @@ -19,12 +19,11 @@ yarn install @solana/spl-token-registry

### Query available tokens

```typescript

new TokenListProvider().resolve("mainnet-beta").then(tokens => {
console.log(tokens);
```typescript
new TokenListProvider().resolve().then((tokens) => {
const tokenList = tokens.filterByClusterSlug('mainnet-beta').getList();
console.log(tokenList);
});

```

### Render icon for token in React
Expand All @@ -38,21 +37,23 @@ export const Icon = (props: { mint: string }) => {
const [tokenMap, setTokenMap] = useState<Map<string, KnownToken>>(new Map());

useEffect(() => {
new TokenListProvider().resolve("mainnet-beta").then(tokens => {
setTokenMap(tokens.reduce((map, item) => {
map.set(item.mintAddress, item);
new TokenListProvider().resolve().then(tokens => {
const tokenList = tokens.filterByChain(ENV.MainnetBeta).getList();

setTokenMap(tokenList.reduce((map, item) => {
map.set(item.address, item);
return map;
},new Map()));
});
}, [setTokenMap]);

const token = tokenMap.get(props.mint);
if (!token) return null;
if (!token || !token.logoURI) return null;

return (<img src={token.icon} />);
return (<img src={token.logoURI} />);

```
## Adding new token
Submit PR with changes to JSON file `src/tokens/<env>.json`
Submit PR with changes to JSON file `src/tokens/solana.tokenlist.json`
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/index.ts
@@ -1 +1 @@
export * from './lib/tokens';
export * from './lib/tokenlist';
6 changes: 3 additions & 3 deletions src/lib/tokenlist.spec.ts
Expand Up @@ -3,7 +3,7 @@ import { ENV, Strategy, TokenListProvider } from './tokenlist';

test('Token list is filterable by a tag', async (t) => {
const list = (await new TokenListProvider().resolve(Strategy.Static))
.filterByChain(ENV.MainnetBeta)
.filterByChainId(ENV.MainnetBeta)
.filterByTag('nft')
.getList();

Expand All @@ -12,7 +12,7 @@ test('Token list is filterable by a tag', async (t) => {

test('Token list can exclude by a tag', async (t) => {
const list = (await new TokenListProvider().resolve(Strategy.Static))
.filterByChain(ENV.MainnetBeta)
.filterByChainId(ENV.MainnetBeta)
.excludeByTag('nft')
.getList();

Expand All @@ -21,7 +21,7 @@ test('Token list can exclude by a tag', async (t) => {

test('Token list can exclude by a chain', async (t) => {
const list = (await new TokenListProvider().resolve(Strategy.Static))
.excludeByChain(ENV.MainnetBeta)
.excludeByChainId(ENV.MainnetBeta)
.getList();

t.false(list.some((item) => item.chainId === ENV.MainnetBeta));
Expand Down
26 changes: 23 additions & 3 deletions src/lib/tokenlist.ts
Expand Up @@ -21,6 +21,10 @@ export interface TagDetails {
readonly description: string;
}

export interface TokenExtensions {
readonly website?: string;
}

export interface TokenInfo {
readonly chainId: number;
readonly address: string;
Expand All @@ -29,10 +33,17 @@ export interface TokenInfo {
readonly symbol: string;
readonly logoURI?: string;
readonly tags?: string[];
readonly extensions?: TokenExtensions;
}

export type TokenInfoMap = Map<string, TokenInfo>;

export const CLUSTER_SLUGS: { [id: string]: ENV } = {
'mainnet-beta': ENV.MainnetBeta,
testnet: ENV.Testnet,
devnet: ENV.Devnet,
};

export class GitHubTokenListResolutionStrategy {
repositories = [
'https://raw.githubusercontent.com/solana-labs/token-list/main/src/tokens/solana.tokenlist.json',
Expand Down Expand Up @@ -101,7 +112,9 @@ export class TokenListProvider {
[Strategy.CDN]: new CDNTokenListResolutionStrategy(),
};

resolve = async (strategy: Strategy = Strategy.CDN) => {
resolve = async (
strategy: Strategy = Strategy.CDN
): Promise<TokenListContainer> => {
return new TokenListContainer(
await TokenListProvider.strategies[strategy].resolve()
);
Expand All @@ -118,12 +131,12 @@ export class TokenListContainer {
return this;
};

filterByChain = (chainId: number | ENV) => {
filterByChainId = (chainId: number | ENV) => {
this.tokenList = this.tokenList.filter((item) => item.chainId === chainId);
return this;
};

excludeByChain = (chainId: number | ENV) => {
excludeByChainId = (chainId: number | ENV) => {
this.tokenList = this.tokenList.filter((item) => item.chainId !== chainId);
return this;
};
Expand All @@ -135,6 +148,13 @@ export class TokenListContainer {
return this;
};

filterByClusterSlug = (slug: string) => {
if (slug in CLUSTER_SLUGS) {
this.filterByChainId(CLUSTER_SLUGS[slug]);
}
return this;
};

getList() {
return this.tokenList;
}
Expand Down

0 comments on commit c113b02

Please sign in to comment.