Skip to content

Commit

Permalink
feat: update to spl-toke-regsitry
Browse files Browse the repository at this point in the history
  • Loading branch information
bartosz-lipinski committed Mar 5, 2021
1 parent 1f1d5a0 commit 81a6800
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 58 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@project-serum/serum": "^0.13.11",
"@project-serum/sol-wallet-adapter": "^0.1.1",
"@solana/spl-token": "0.0.11",
"@solana/spl-token-registry": "^0.2.0",
"@solana/spl-token-swap": "0.0.2",
"@solana/web3.js": "^0.86.2",
"@testing-library/jest-dom": "^4.2.4",
Expand Down
16 changes: 8 additions & 8 deletions src/components/currencyInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ export const CurrencyInput = (props: {
const renderPopularTokens = tokens.map((item) => {
return (
<Option
key={item.mintAddress}
value={item.mintAddress}
name={item.tokenSymbol}
title={item.mintAddress}
key={item.address}
value={item.address}
name={item.symbol}
title={item.address}
>
<TokenDisplay
key={item.mintAddress}
name={item.tokenSymbol}
mintAddress={item.mintAddress}
key={item.address}
name={item.symbol}
mintAddress={item.address}
showBalance={true}
/>
</Option>
Expand Down Expand Up @@ -124,7 +124,7 @@ export const CurrencyInput = (props: {

const additionalAccounts = [...grouppedUserAccounts.keys()];
if (
tokens.findIndex((t) => t.mintAddress === props.mint) < 0 &&
tokens.findIndex((t) => t.address === props.mint) < 0 &&
props.mint &&
!grouppedUserAccounts.has(props?.mint)
) {
Expand Down
69 changes: 39 additions & 30 deletions src/utils/connection.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KnownToken, useLocalStorageState } from "./utils";
import { useLocalStorageState } from "./utils";
import {
Account,
clusterApiUrl,
Expand All @@ -10,17 +10,31 @@ import React, { useContext, useEffect, useMemo, useState } from "react";
import { setProgramIds } from "./ids";
import { notify } from "./notifications";
import { ExplorerLink } from "../components/explorerLink";
import { TokenListProvider, ENV as ChainID, TokenInfo } from "@solana/spl-token-registry";

export type ENV = "mainnet-beta" | "testnet" | "devnet" | "localnet";

export const ENDPOINTS = [
{
name: "mainnet-beta" as ENV,
endpoint: "https://solana-api.projectserum.com/",
chainID: ChainID.MainnetBeta
},
{
name: "testnet" as ENV,
endpoint: clusterApiUrl("testnet"),
chainID: ChainID.Testnet
},
{
name: "devnet" as ENV,
endpoint: clusterApiUrl("devnet"),
chainID: ChainID.Devnet
},
{
name: "localnet" as ENV,
endpoint: "http://127.0.0.1:8899",
chainID: ChainID.Devnet
},
{ name: "testnet" as ENV, endpoint: clusterApiUrl("testnet") },
{ name: "devnet" as ENV, endpoint: clusterApiUrl("devnet") },
{ name: "localnet" as ENV, endpoint: "http://127.0.0.1:8899" },
];

const DEFAULT = ENDPOINTS[0].endpoint;
Expand All @@ -34,8 +48,8 @@ interface ConnectionConfig {
setSlippage: (val: number) => void;
env: ENV;
setEndpoint: (val: string) => void;
tokens: KnownToken[];
tokenMap: Map<string, KnownToken>;
tokens: TokenInfo[];
tokenMap: Map<string, TokenInfo>;
}

const ConnectionContext = React.createContext<ConnectionConfig>({
Expand All @@ -47,7 +61,7 @@ const ConnectionContext = React.createContext<ConnectionConfig>({
sendConnection: new Connection(DEFAULT, "recent"),
env: ENDPOINTS[0].name,
tokens: [],
tokenMap: new Map<string, KnownToken>(),
tokenMap: new Map<string, TokenInfo>(),
});

export function ConnectionProvider({ children = undefined as any }) {
Expand All @@ -68,31 +82,26 @@ export function ConnectionProvider({ children = undefined as any }) {
endpoint,
]);

const env =
ENDPOINTS.find((end) => end.endpoint === endpoint)?.name ||
ENDPOINTS[0].name;
const chain =
ENDPOINTS.find((end) => end.endpoint === endpoint) ||
ENDPOINTS[0];

const env = chain.name;

const [tokens, setTokens] = useState<KnownToken[]>([]);
const [tokenMap, setTokenMap] = useState<Map<string, KnownToken>>(new Map());
const [tokens, setTokens] = useState<TokenInfo[]>([]);
const [tokenMap, setTokenMap] = useState<Map<string, TokenInfo>>(new Map());
useEffect(() => {
// fetch token files
window
.fetch(
`https://raw.githubusercontent.com/solana-labs/token-list/main/src/tokens/${env}.json`
)
.then((res) => {
return res.json();
})
.then((list: KnownToken[]) => {
const knownMints = list.reduce((map, item) => {
map.set(item.mintAddress, item);
return map;
}, new Map<string, KnownToken>());

setTokenMap(knownMints);
setTokens(list);
});
}, [env]);
new TokenListProvider().resolve().then((res) => {
const list = res.filterByChainId(chain.chainID).excludeByTag('nft').getList();
const knownMints = list.reduce((map, item) => {
map.set(item.address, item);
return map;
}, new Map<string, TokenInfo>());

setTokenMap(knownMints);
setTokens(list);
});
}, [chain]);

setProgramIds(env);

Expand Down
19 changes: 10 additions & 9 deletions src/utils/currencyPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import {
TokenAccount,
DEFAULT_DENOMINATOR,
} from "../models";
import { convert, getTokenIcon, getTokenName, KnownToken } from "./utils";
import { convert, getTokenIcon, getTokenName } from "./utils";
import { useHistory, useLocation } from "react-router-dom";
import bs58 from "bs58";
import { TokenInfo } from "@solana/spl-token-registry";

export interface CurrencyContextState {
mintAddress: string;
Expand Down Expand Up @@ -131,10 +132,10 @@ export function CurrencyPairProvider({ children = null as any }) {

useEffect(() => {
const base =
tokens.find((t) => t.mintAddress === mintAddressA)?.tokenSymbol ||
tokens.find((t) => t.address === mintAddressA)?.symbol ||
mintAddressA;
const quote =
tokens.find((t) => t.mintAddress === mintAddressB)?.tokenSymbol ||
tokens.find((t) => t.address === mintAddressB)?.symbol ||
mintAddressB;

document.title = `Swap | Serum (${base}/${quote})`;
Expand All @@ -144,10 +145,10 @@ export function CurrencyPairProvider({ children = null as any }) {
useEffect(() => {
// set history
const base =
tokens.find((t) => t.mintAddress === mintAddressA)?.tokenSymbol ||
tokens.find((t) => t.address === mintAddressA)?.symbol ||
mintAddressA;
const quote =
tokens.find((t) => t.mintAddress === mintAddressB)?.tokenSymbol ||
tokens.find((t) => t.address === mintAddressB)?.symbol ||
mintAddressB;

if (base && quote && location.pathname.indexOf("info") < 0) {
Expand Down Expand Up @@ -180,12 +181,12 @@ export function CurrencyPairProvider({ children = null as any }) {
}

setMintAddressA(
tokens.find((t) => t.tokenSymbol === defaultBase)?.mintAddress ||
tokens.find((t) => t.symbol === defaultBase)?.address ||
(isValidAddress(defaultBase) ? defaultBase : "") ||
""
);
setMintAddressB(
tokens.find((t) => t.tokenSymbol === defaultQuote)?.mintAddress ||
tokens.find((t) => t.symbol === defaultQuote)?.address ||
(isValidAddress(defaultQuote) ? defaultQuote : "") ||
""
);
Expand Down Expand Up @@ -271,12 +272,12 @@ const isValidAddress = (address: string) => {
}
};

function getDefaultTokens(tokens: KnownToken[], search: string) {
function getDefaultTokens(tokens: TokenInfo[], search: string) {
let defaultBase = "SOL";
let defaultQuote = "USDC";

const nameToToken = tokens.reduce((map, item) => {
map.set(item.tokenSymbol, item);
map.set(item.symbol, item);
return map;
}, new Map<string, any>());

Expand Down
14 changes: 4 additions & 10 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import { useCallback, useState } from "react";
import { MintInfo } from "@solana/spl-token";

import { PoolInfo, TokenAccount } from "./../models";
import { TokenInfo } from "@solana/spl-token-registry";

export interface KnownToken {
tokenSymbol: string;
tokenName: string;
icon: string;
mintAddress: string;
}

export type KnownTokenMap = Map<string, KnownToken>;
export type KnownTokenMap = Map<string, TokenInfo>;

export function useLocalStorageState(key: string, defaultState?: string) {
const [state, setState] = useState(() => {
Expand Down Expand Up @@ -52,7 +46,7 @@ export function getTokenName(
shorten = true,
length = 5
): string {
const knownSymbol = map.get(mintAddress)?.tokenSymbol;
const knownSymbol = map.get(mintAddress)?.symbol;
if (knownSymbol) {
return knownSymbol;
}
Expand All @@ -64,7 +58,7 @@ export function getTokenIcon(
map: KnownTokenMap,
mintAddress: string
): string | undefined {
return map.get(mintAddress)?.icon;
return map.get(mintAddress)?.logoURI;
}

export function getPoolName(
Expand Down
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,13 @@
resolved "https://registry.yarnpkg.com/@sheerun/mutationobserver-shim/-/mutationobserver-shim-0.3.3.tgz#5405ee8e444ed212db44e79351f0c70a582aae25"
integrity sha512-DetpxZw1fzPD5xUBrIAoplLChO2VB8DlL5Gg+I1IR9b2wPqYIca2WSUxL5g1vLeR4MsQq1NeWriXAVffV+U1Fw==

"@solana/spl-token-registry@^0.2.0":
version "0.2.0"
resolved "https://registry.yarnpkg.com/@solana/spl-token-registry/-/spl-token-registry-0.2.0.tgz#cd32d13c6056b0b6d722880e240663ebc5b39ad7"
integrity sha512-DC1gQDthODgCa0Nd5sPuoGpgEG3xdxQRoggfFFLQpwX689zILabvmVj6v6hF8bN8fGEpADZK1Fp8pwD427jkUQ==
dependencies:
cross-fetch "^3.0.6"

"@solana/[email protected]":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@solana/spl-token-swap/-/spl-token-swap-0.0.2.tgz#b7afa943d27fc4c5100ea3ab6fcd518bcc05fed3"
Expand Down Expand Up @@ -3813,6 +3820,13 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

cross-fetch@^3.0.6:
version "3.0.6"
resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.0.6.tgz#3a4040bc8941e653e0e9cf17f29ebcd177d3365c"
integrity sha512-KBPUbqgFjzWlVcURG+Svp9TlhA5uliYtiNx/0r8nv0pdypeQCRJ9IaSIc3q/x3q8t3F75cHuwxVql1HFGHCNJQ==
dependencies:
node-fetch "2.6.1"

[email protected]:
version "7.0.1"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.1.tgz#0ab56286e0f7c24e153d04cc2aa027e43a9a5d14"
Expand Down Expand Up @@ -7968,7 +7982,7 @@ node-addon-api@^2.0.0:
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-2.0.2.tgz#432cfa82962ce494b132e9d72a15b29f71ff5d32"
integrity sha512-Ntyt4AIXyaLIuMHF6IOoTakB3K+RWxwtsHNRxllEoA6vPwP9o4866g6YWDLUdnucilZhmkxiHwHr11gAENw+QA==

node-fetch@^2.2.0:
node-fetch@2.6.1, node-fetch@^2.2.0:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
Expand Down

0 comments on commit 81a6800

Please sign in to comment.