Skip to content

Commit

Permalink
feat: solana getAccount (#1055)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaladinlight authored Oct 8, 2024
1 parent ee9390d commit 3704b5e
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 82 deletions.
2 changes: 1 addition & 1 deletion node/coinstacks/solana/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"dependencies": {
"@shapeshiftoss/common-api": "^10.0.0",
"@solana/web3.js": "^1.95.3",
"helius-sdk": "^1.3.5"
"helius-sdk": "^1.3.6"
}
}
48 changes: 45 additions & 3 deletions node/coinstacks/solana/api/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
ValidationError,
handleError,
} from '../../../common/api/src' // unable to import models from a module with tsoa
import { Account, API, EstimateFeesBody, PriorityFees, Tx, TxHistory } from './models'
import { EnrichedTransaction, Helius } from 'helius-sdk'
import { Account, API, EstimateFeesBody, PriorityFees, TokenBalance, Tx, TxHistory } from './models'
import { DAS, EnrichedTransaction, Helius, Interface } from 'helius-sdk'
import { VersionedMessage } from '@solana/web3.js'
import { validatePageSize } from '@shapeshiftoss/common-api'
import axios from 'axios'
import { NativeBalance } from './types'

const RPC_URL = process.env.RPC_URL
const RPC_API_KEY = process.env.RPC_API_KEY
Expand Down Expand Up @@ -64,7 +65,48 @@ export class Solana implements BaseAPI, API {
@Response<InternalServerError>(500, 'Internal Server Error')
@Get('account/{pubkey}')
async getAccount(@Path() pubkey: string): Promise<Account> {
return { pubkey } as Account
try {
let page = 1
let balance = '0'
let hasMore = false
let tokens: Array<TokenBalance> = []

do {
const { total, limit, items, nativeBalance } = (await heliusSdk.rpc.getAssetsByOwner({
ownerAddress: pubkey,
page: page++,
displayOptions: { showFungible: true, showNativeBalance: true, showZeroBalance: true },
})) as DAS.GetAssetResponseList & { nativeBalance: NativeBalance }

hasMore = total === limit
balance = BigInt(nativeBalance.lamports).toString()

tokens = items.reduce<Array<TokenBalance>>((prev, item) => {
if (!item.content) return prev
if (item.interface !== Interface.FUNGIBLE_TOKEN) return prev

prev.push({
id: item.id,
name: item.content.metadata.name,
symbol: item.content.metadata.symbol,
decimals: item.token_info?.decimals ?? 0,
balance: item.token_info?.balance ? BigInt(item.token_info.balance).toString() : '0',
type: item.interface,
})

return prev
}, tokens)
} while (hasMore)

return {
pubkey,
balance,
unconfirmedBalance: '0',
tokens,
}
} catch (err) {
throw handleError(err)
}
}

/**
Expand Down
22 changes: 21 additions & 1 deletion node/coinstacks/solana/api/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,30 @@ export interface Address {
pubkey: string
}

/**
* Contains info about a token
*/
export interface Token {
id: string
decimals: number
name: string
symbol: string
type: string
}

/**
* Contains info about a token including balance for an address
*/
export interface TokenBalance extends Token {
balance: string
}

/**
* Contains info about an address or extended public key account
*/
export interface Account extends BaseAccount {}
export interface Account extends BaseAccount {
tokens: Array<TokenBalance>
}

/**
* Extended coin specific functionality
Expand Down
67 changes: 61 additions & 6 deletions node/coinstacks/solana/api/src/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,40 @@
"type": "object",
"additionalProperties": false
},
"TokenBalance": {
"description": "Contains info about a token including balance for an address",
"properties": {
"id": {
"type": "string"
},
"decimals": {
"type": "number",
"format": "double"
},
"name": {
"type": "string"
},
"symbol": {
"type": "string"
},
"type": {
"type": "string"
},
"balance": {
"type": "string"
}
},
"required": [
"id",
"decimals",
"name",
"symbol",
"type",
"balance"
],
"type": "object",
"additionalProperties": false
},
"Account": {
"description": "Contains info about an address or extended public key account",
"properties": {
Expand All @@ -30,12 +64,19 @@
},
"pubkey": {
"type": "string"
},
"tokens": {
"items": {
"$ref": "#/components/schemas/TokenBalance"
},
"type": "array"
}
},
"required": [
"balance",
"unconfirmedBalance",
"pubkey"
"pubkey",
"tokens"
],
"type": "object",
"additionalProperties": false
Expand Down Expand Up @@ -596,17 +637,31 @@
"type": "string"
},
"Token": {
"description": "Contains info about a token",
"properties": {
"mint": {
"id": {
"type": "string"
},
"tokenStandard": {
"$ref": "#/components/schemas/TokenStandard"
"decimals": {
"type": "number",
"format": "double"
},
"name": {
"type": "string"
},
"symbol": {
"type": "string"
},
"type": {
"type": "string"
}
},
"required": [
"mint",
"tokenStandard"
"id",
"decimals",
"name",
"symbol",
"type"
],
"type": "object",
"additionalProperties": false
Expand Down
5 changes: 5 additions & 0 deletions node/coinstacks/solana/api/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type NativeBalance = {
lamports: number
price_per_sol: number
total_price: number
}
Loading

0 comments on commit 3704b5e

Please sign in to comment.