Compare commits
9 Commits
Author | SHA1 | Date |
---|---|---|
armaniferrante | d41271f9c9 | |
armaniferrante | 1aae052784 | |
armaniferrante | 9cc0495d4a | |
Shardul Aeer | 3f213aa5ae | |
Shardul Aeer | 80b0fa4605 | |
Armani Ferrante | 4a92cad65a | |
armaniferrante | 8333da03ad | |
armaniferrante | 318801a876 | |
Armani Ferrante | 879c212da6 |
|
@ -11,6 +11,7 @@ import {
|
|||
Connection,
|
||||
Transaction,
|
||||
TransactionSignature,
|
||||
PublicKey,
|
||||
} from "@solana/web3.js";
|
||||
import {
|
||||
TokenListContainer,
|
||||
|
@ -118,6 +119,13 @@ function AppInner() {
|
|||
);
|
||||
}
|
||||
|
||||
// Cast wallet to AnchorWallet in order to be compatible with Anchor's Provider class
|
||||
interface AnchorWallet {
|
||||
signTransaction(tx: Transaction): Promise<Transaction>;
|
||||
signAllTransactions(txs: Transaction[]): Promise<Transaction[]>;
|
||||
publicKey: PublicKey;
|
||||
}
|
||||
|
||||
// Custom provider to display notifications whenever a transaction is sent.
|
||||
//
|
||||
// Note that this is an Anchor wallet/network provider--not a React provider,
|
||||
|
@ -137,7 +145,8 @@ class NotifyingProvider extends Provider {
|
|||
opts: ConfirmOptions,
|
||||
onTransaction: (tx: TransactionSignature | undefined, err?: Error) => void
|
||||
) {
|
||||
super(connection, wallet, opts);
|
||||
const newWallet = wallet as AnchorWallet;
|
||||
super(connection, newWallet, opts);
|
||||
this.onTransaction = onTransaction;
|
||||
}
|
||||
|
||||
|
@ -151,7 +160,9 @@ class NotifyingProvider extends Provider {
|
|||
this.onTransaction(txSig);
|
||||
return txSig;
|
||||
} catch (err) {
|
||||
if (err instanceof Error || err === undefined) {
|
||||
this.onTransaction(undefined, err);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
@ -167,7 +178,9 @@ class NotifyingProvider extends Provider {
|
|||
});
|
||||
return txSigs;
|
||||
} catch (err) {
|
||||
if (err instanceof Error || err === undefined) {
|
||||
this.onTransaction(undefined, err);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
66
package.json
66
package.json
|
@ -1,23 +1,23 @@
|
|||
{
|
||||
"name": "@project-serum/swap-ui",
|
||||
"version": "0.2.0",
|
||||
"version": "0.2.5",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"homepage": "https://github.com/project-serum/swap-ui",
|
||||
"license": "Apache-2.0",
|
||||
"dependencies": {
|
||||
"@project-serum/serum": "^0.13.34",
|
||||
"@project-serum/swap": "^0.1.0-alpha.31",
|
||||
"@solana/spl-token": "^0.1.4"
|
||||
"@project-serum/serum": "^0.13.58",
|
||||
"@project-serum/swap": "^0.1.0-alpha.35",
|
||||
"@solana/spl-token": "^0.1.8"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@material-ui/core": "^4.11.4",
|
||||
"@material-ui/core": "^4.12.3",
|
||||
"@material-ui/icons": "^4.11.2",
|
||||
"@material-ui/lab": "^4.0.0-alpha.58",
|
||||
"@project-serum/anchor": "^0.7.0",
|
||||
"@solana/spl-token-registry": "^0.2.86",
|
||||
"@material-ui/lab": "^4.0.0-alpha.60",
|
||||
"@project-serum/anchor": "^0.14.0",
|
||||
"@solana/spl-token-registry": "^0.2.229",
|
||||
"@solana/web3.js": "^1.17.1",
|
||||
"material-ui-popup-state": "^1.8.3",
|
||||
"material-ui-popup-state": "^1.9.3",
|
||||
"react": "^17.0.2",
|
||||
"react-async-hook": "^3.6.2"
|
||||
},
|
||||
|
@ -35,39 +35,39 @@
|
|||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"@fontsource/roboto": "4.3.0",
|
||||
"@material-ui/core": "^4.11.4",
|
||||
"@fontsource/roboto": "4.5.0",
|
||||
"@material-ui/core": "^4.12.3",
|
||||
"@material-ui/icons": "^4.11.2",
|
||||
"@material-ui/lab": "^4.0.0-alpha.58",
|
||||
"@project-serum/anchor": "^0.7.0",
|
||||
"@project-serum/serum": "^0.13.34",
|
||||
"@project-serum/sol-wallet-adapter": "^0.2.0",
|
||||
"@project-serum/swap": "^0.1.0-alpha.31",
|
||||
"@solana/spl-token": "^0.1.4",
|
||||
"@solana/spl-token-registry": "^0.2.86",
|
||||
"@solana/web3.js": "^1.17.0",
|
||||
"@testing-library/jest-dom": "^5.11.4",
|
||||
"@testing-library/react": "^11.1.0",
|
||||
"@testing-library/user-event": "^12.1.10",
|
||||
"@material-ui/lab": "^4.0.0-alpha.60",
|
||||
"@project-serum/anchor": "^0.14.0",
|
||||
"@project-serum/serum": "^0.13.58",
|
||||
"@project-serum/sol-wallet-adapter": "^0.2.5",
|
||||
"@project-serum/swap": "^0.1.0-alpha.35",
|
||||
"@solana/spl-token": "^0.1.8",
|
||||
"@solana/spl-token-registry": "^0.2.229",
|
||||
"@solana/web3.js": "^1.24.1",
|
||||
"@testing-library/jest-dom": "^5.14.1",
|
||||
"@testing-library/react": "^12.0.0",
|
||||
"@testing-library/user-event": "^13.2.1",
|
||||
"@types/bs58": "^4.0.1",
|
||||
"@types/jest": "^26.0.15",
|
||||
"@types/node": "^12.0.0",
|
||||
"@types/react": "^17.0.0",
|
||||
"@types/react-dom": "^17.0.0",
|
||||
"@types/jest": "^27.0.1",
|
||||
"@types/node": "^16.7.10",
|
||||
"@types/react": "^17.0.19",
|
||||
"@types/react-dom": "^17.0.9",
|
||||
"bs58": "^4.0.1",
|
||||
"gh-pages": "^3.1.0",
|
||||
"material-ui-popup-state": "^1.8.3",
|
||||
"notistack": "^1.0.7",
|
||||
"prettier": "^2.3.0",
|
||||
"gh-pages": "^3.2.3",
|
||||
"material-ui-popup-state": "^1.9.3",
|
||||
"notistack": "^1.0.10",
|
||||
"prettier": "^2.3.2",
|
||||
"react": "^17.0.2",
|
||||
"react-app-rewire-alias": "^1.0.3",
|
||||
"react-app-rewired": "^2.1.8",
|
||||
"react-async-hook": "^3.6.2",
|
||||
"react-dom": "^17.0.2",
|
||||
"react-scripts": "4.0.3",
|
||||
"typedoc": "^0.20.36",
|
||||
"typescript": "^4.1.2",
|
||||
"web-vitals": "^1.0.1"
|
||||
"typedoc": "^0.21.9",
|
||||
"typescript": "^4.4.2",
|
||||
"web-vitals": "^2.1.0"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
|
|
|
@ -9,7 +9,10 @@ import {
|
|||
Token,
|
||||
TOKEN_PROGRAM_ID,
|
||||
} from "@solana/spl-token";
|
||||
import { getOwnedTokenAccounts, parseTokenAccountData } from "../utils/tokens";
|
||||
import {
|
||||
getOwnedAssociatedTokenAccounts,
|
||||
parseTokenAccountData,
|
||||
} from "../utils/tokens";
|
||||
import { SOL_MINT } from "../utils/pubkeys";
|
||||
|
||||
export type TokenContext = {
|
||||
|
@ -29,14 +32,16 @@ export function TokenContextProvider(props: any) {
|
|||
return;
|
||||
}
|
||||
// Fetch SPL tokens.
|
||||
getOwnedTokenAccounts(provider.connection, provider.wallet.publicKey).then(
|
||||
(accs) => {
|
||||
getOwnedAssociatedTokenAccounts(
|
||||
provider.connection,
|
||||
provider.wallet.publicKey
|
||||
).then((accs) => {
|
||||
if (accs) {
|
||||
// @ts-ignore
|
||||
_OWNED_TOKEN_ACCOUNTS_CACHE.push(...accs);
|
||||
setRefresh((r) => r + 1);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
// Fetch SOL balance.
|
||||
provider.connection
|
||||
.getAccountInfo(provider.wallet.publicKey)
|
||||
|
@ -123,6 +128,8 @@ export function useOwnedTokenAccount(
|
|||
listener = provider.connection.onAccountChange(
|
||||
tokenAccount.publicKey,
|
||||
(info) => {
|
||||
if (info.data.length !== 0) {
|
||||
try {
|
||||
const token = parseTokenAccountData(info.data);
|
||||
if (token.amount !== tokenAccount.account.amount) {
|
||||
const index = _OWNED_TOKEN_ACCOUNTS_CACHE.indexOf(tokenAccount);
|
||||
|
@ -130,6 +137,10 @@ export function useOwnedTokenAccount(
|
|||
_OWNED_TOKEN_ACCOUNTS_CACHE[index].account = token;
|
||||
setRefresh((r) => r + 1);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("Failed to decode token AccountInfo");
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -58,6 +58,8 @@ export default function Swap(props: SwapProps): ReactElement {
|
|||
toAmount,
|
||||
referral,
|
||||
} = props;
|
||||
|
||||
// @ts-ignore
|
||||
const swapClient = new SwapClient(provider, tokenList);
|
||||
const theme = createMuiTheme(
|
||||
materialTheme || {
|
||||
|
|
|
@ -5,62 +5,58 @@ import * as BufferLayout from "buffer-layout";
|
|||
import { BN } from "@project-serum/anchor";
|
||||
import {
|
||||
TOKEN_PROGRAM_ID,
|
||||
ASSOCIATED_TOKEN_PROGRAM_ID,
|
||||
Token,
|
||||
AccountInfo as TokenAccount,
|
||||
} from "@solana/spl-token";
|
||||
import { Connection, PublicKey } from "@solana/web3.js";
|
||||
import * as bs58 from "bs58";
|
||||
|
||||
export async function getOwnedTokenAccounts(
|
||||
export async function getOwnedAssociatedTokenAccounts(
|
||||
connection: Connection,
|
||||
publicKey: PublicKey
|
||||
) {
|
||||
let filters = getOwnedAccountsFilters(publicKey);
|
||||
// @ts-ignore
|
||||
let resp = await connection._rpcRequest("getProgramAccounts", [
|
||||
TOKEN_PROGRAM_ID.toBase58(),
|
||||
{
|
||||
let resp = await connection.getProgramAccounts(TOKEN_PROGRAM_ID, {
|
||||
commitment: connection.commitment,
|
||||
filters,
|
||||
},
|
||||
]);
|
||||
if (resp.error) {
|
||||
throw new Error(
|
||||
"failed to get token accounts owned by " +
|
||||
publicKey.toBase58() +
|
||||
": " +
|
||||
resp.error.message
|
||||
);
|
||||
}
|
||||
return resp.result
|
||||
});
|
||||
|
||||
const accs = resp
|
||||
.map(({ pubkey, account: { data, executable, owner, lamports } }: any) => ({
|
||||
publicKey: new PublicKey(pubkey),
|
||||
accountInfo: {
|
||||
data: bs58.decode(data),
|
||||
data,
|
||||
executable,
|
||||
owner: new PublicKey(owner),
|
||||
lamports,
|
||||
},
|
||||
}))
|
||||
.filter(({ accountInfo }: any) => {
|
||||
// TODO: remove this check once mainnet is updated
|
||||
return filters.every((filter) => {
|
||||
if (filter.dataSize) {
|
||||
return accountInfo.data.length === filter.dataSize;
|
||||
} else if (filter.memcmp) {
|
||||
let filterBytes = bs58.decode(filter.memcmp.bytes);
|
||||
return accountInfo.data
|
||||
.slice(
|
||||
filter.memcmp.offset,
|
||||
filter.memcmp.offset + filterBytes.length
|
||||
)
|
||||
.equals(filterBytes);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
})
|
||||
.map(({ publicKey, accountInfo }: any) => {
|
||||
return { publicKey, account: parseTokenAccountData(accountInfo.data) };
|
||||
});
|
||||
|
||||
return (
|
||||
(
|
||||
await Promise.all(
|
||||
accs
|
||||
// @ts-ignore
|
||||
.map(async (ta) => {
|
||||
const ata = await Token.getAssociatedTokenAddress(
|
||||
ASSOCIATED_TOKEN_PROGRAM_ID,
|
||||
TOKEN_PROGRAM_ID,
|
||||
ta.account.mint,
|
||||
publicKey
|
||||
);
|
||||
return [ta, ata];
|
||||
})
|
||||
)
|
||||
)
|
||||
// @ts-ignore
|
||||
.filter(([ta, ata]) => ta.publicKey.equals(ata))
|
||||
// @ts-ignore
|
||||
.map(([ta]) => ta)
|
||||
);
|
||||
}
|
||||
|
||||
const ACCOUNT_LAYOUT = BufferLayout.struct([
|
||||
|
|
Loading…
Reference in New Issue