Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@metaplex-foundation/beet": "^0.4.0",
"@metaplex-foundation/beet-solana": "^0.3.0",
"@solana/spl-token-registry": "^0.2.4574",
"@solana/web3.js": "^1.47.3",
"bn.js": "^5.2.1"
}
Expand Down
46 changes: 0 additions & 46 deletions src/step05-TODO.ts

This file was deleted.

94 changes: 94 additions & 0 deletions src/step05.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
Connection,
GetProgramAccountsFilter,
PublicKey,
clusterApiUrl,
} from "@solana/web3.js";

import { TokenAccountStruct } from "./structs/TokenAccount";
import { MintAccountStruct } from "./structs/MintAccount";

import { TokenListProvider } from "@solana/spl-token-registry";

const myWallet = new PublicKey("664vpd2BcmQxeidfCa2a4R7r6dBxg5SgJcdecssZeazh");
const tokenProgramId = new PublicKey(
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
);

const connection = new Connection(clusterApiUrl("mainnet-beta"));

async function getProgramAccounts() {
const filters: GetProgramAccountsFilter[] = [
{
dataSize: 165,
},
{
memcmp: {
bytes: myWallet.toString(),
offset: 32,
},
},
];
const tokenAccounts = await connection.getProgramAccounts(tokenProgramId, {
filters,
});

const allOwnedMint = tokenAccounts
.map((tokenAccount) =>
TokenAccountStruct.read(tokenAccount.account.data, 0)
)
.map((data) => {
return {
mint: data.mint,
amount: data.amount,
};
});

// (use connection.getMultipleAccountsInfo)
// https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getMultipleAccountsInfo
const mintsAccount = await connection.getMultipleAccountsInfo(
allOwnedMint.map((dt) => dt.mint)
);

// TODO: Decode mints accounts to get decimals
const mintsDecimals = mintsAccount
.flatMap((mintAccount) =>
mintAccount ? MintAccountStruct.read(mintAccount.data, 0) : []
)
.map((data) => {
return {
decimal: data.decimals,
};
});

// TODO: Divide token accounts amounts by 10 ** mintDecimal
const zip = (a, b) => a.map((k, i) => [k, b[i]]);

const rebaseBalance = (decimal: number, amount: number) => {
const balance = decimal === 0 ? amount : amount / 10 ** decimal;
return balance.toFixed(2);
};

// BONUS: convert mints to symbol
const tokenProvider = await new TokenListProvider().resolve();
const tokenList = tokenProvider.filterByClusterSlug("mainnet-beta").getList();

const getSymbol = (address: string) => {
return tokenList.filter((x) => x.address === address).map((x) => x.symbol);
};

// TODO: Display all your token amounts !
const result = zip(allOwnedMint, mintsDecimals).map((dt) => {
return {
mint: dt[0].mint.toBase58(),
symbol: getSymbol(dt[0].mint.toBase58()),
amount: rebaseBalance(dt[1].decimal, dt[0].amount.toNumber()),
};
});

console.log(result);
}

getProgramAccounts();
20 changes: 4 additions & 16 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
"module": "commonjs",
"moduleResolution": "node",
"declaration": true,
"strict": true,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true /* Enable strict null checks. */,
"strictFunctionTypes": true /* Enable strict checking of function types. */,
"noUnusedLocals": false /* Report errors on unused locals. */,
Expand All @@ -19,19 +17,9 @@
"experimentalDecorators": true,
"sourceMap": true,
"outDir": "./dist/tsc/",
"types": [
"node"
],
"lib": [
"ES6",
"DOM"
]
"types": ["node"],
"lib": ["ES6", "DOM"]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.test.ts"
]
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "**/*.test.ts"]
}
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@
dependencies:
buffer "~6.0.3"

"@solana/spl-token-registry@^0.2.4574":
version "0.2.4574"
resolved "https://registry.yarnpkg.com/@solana/spl-token-registry/-/spl-token-registry-0.2.4574.tgz#13f4636b7bec90d2bb43bbbb83512cd90d2ce257"
integrity sha512-JzlfZmke8Rxug20VT/VpI2XsXlsqMlcORIUivF+Yucj7tFi7A0dXG7h+2UnD0WaZJw8BrUz2ABNkUnv89vbv1A==
dependencies:
cross-fetch "3.0.6"

"@solana/web3.js@^1.44.0", "@solana/web3.js@^1.47.3":
version "1.47.3"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.47.3.tgz#ec13f2cf4f9f54cc4fbd26d20be1e026c6e2279c"
Expand Down Expand Up @@ -590,6 +597,13 @@ create-require@^1.1.0:
resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333"
integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==

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"

cross-spawn@^7.0.2:
version "7.0.3"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
Expand Down Expand Up @@ -1318,6 +1332,11 @@ node-fetch@2:
dependencies:
whatwg-url "^5.0.0"

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

node-gyp-build@^4.2.0, node-gyp-build@^4.3.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.5.0.tgz#7a64eefa0b21112f89f58379da128ac177f20e40"
Expand Down