Skip to content

Instantly share code, notes, and snippets.

@AlmostEfficient
Last active November 2, 2021 11:31
Show Gist options
  • Save AlmostEfficient/09c431a58e78d7de3b1a673224180313 to your computer and use it in GitHub Desktop.
Save AlmostEfficient/09c431a58e78d7de3b1a673224180313 to your computer and use it in GitHub Desktop.
Get all Solana token accounts for a given address
// https://docs.solana.com/developing/clients/jsonrpc-api#gettokenaccountsbyowner
import axios from "axios";
async function getTokenAccounts(address: string) {
try {
return new Promise(async (resolve, reject) => {
let data = {
jsonrpc: "2.0",
id: 1,
method: "getProgramAccounts",
params: [
// This is the pubkey for the Solana token program, do not replace this.
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
{ encoding: "jsonParsed",
filters: [
{ dataSize: 165 },
{ memcmp: { "offset": 32, "bytes": address } }
]
}
]
}
await axios.post("https://api.mainnet-beta.solana.com", data)
.then((res) => {
// console.log(res.data)
//@ts-ignore
resolve(res.data.result)
})
.catch((err) => {
console.log(err)
reject(err)
})
})
} catch (e) {
console.log(e)
}
}
export default getTokens;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment