@@ -5,13 +5,19 @@ import {
55 Signature ,
66 SignedTransaction ,
77} from '@near-js/transactions'
8+ import { baseDecode , baseEncode } from '@near-js/utils'
89import type { AssetId , ChainId } from '@shapeshiftoss/caip'
9- import { ASSET_REFERENCE , nearAssetId , nearChainId } from '@shapeshiftoss/caip'
10+ import {
11+ ASSET_NAMESPACE ,
12+ ASSET_REFERENCE ,
13+ nearAssetId ,
14+ nearChainId ,
15+ toAssetId ,
16+ } from '@shapeshiftoss/caip'
1017import type { HDWallet } from '@shapeshiftoss/hdwallet-core'
1118import type { Bip44Params , RootBip44Params } from '@shapeshiftoss/types'
1219import { KnownChainIds } from '@shapeshiftoss/types'
1320import { TransferType , TxStatus } from '@shapeshiftoss/unchained-client'
14- import bs58 from 'bs58'
1521
1622import type { ChainAdapter as IChainAdapter } from '../api'
1723import { ChainAdapterError , ErrorHandler } from '../error/ErrorHandler'
@@ -41,6 +47,25 @@ interface NearWallet extends HDWallet {
4147 } ) : Promise < { signature : string ; publicKey : string } | null >
4248}
4349
50+ interface NearBlocksFtMeta {
51+ name : string
52+ symbol : string
53+ decimals : number
54+ icon : string | null
55+ reference : string | null
56+ price : string | null
57+ }
58+
59+ interface NearBlocksFtBalance {
60+ contract : string
61+ amount : string
62+ ft_meta : NearBlocksFtMeta
63+ }
64+
65+ interface NearBlocksInventoryResponse {
66+ fts : NearBlocksFtBalance [ ]
67+ }
68+
4469const supportsNear = ( wallet : HDWallet ) : wallet is NearWallet => {
4570 return '_supportsNear' in wallet && ( wallet as any ) . _supportsNear === true
4671}
@@ -139,6 +164,8 @@ interface NearFullTxResult {
139164 } [ ]
140165}
141166
167+ const NEARBLOCKS_API_URL = 'https://api.nearblocks.io/v1'
168+
142169export class ChainAdapter implements IChainAdapter < KnownChainIds . NearMainnet > {
143170 static readonly rootBip44Params : RootBip44Params = {
144171 purpose : 44 ,
@@ -244,21 +271,60 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.NearMainnet> {
244271 }
245272 }
246273
274+ private async fetchTokenBalances ( accountId : string ) : Promise <
275+ {
276+ assetId : AssetId
277+ balance : string
278+ name : string
279+ precision : number
280+ symbol : string
281+ } [ ]
282+ > {
283+ try {
284+ const response = await fetch ( `${ NEARBLOCKS_API_URL } /account/${ accountId } /inventory` )
285+ if ( ! response . ok ) {
286+ console . warn ( `Failed to fetch NEAR token balances: ${ response . status } ` )
287+ return [ ]
288+ }
289+
290+ const data = ( await response . json ( ) ) as { inventory : NearBlocksInventoryResponse }
291+ const fts = data . inventory ?. fts ?? [ ]
292+
293+ return fts . map ( ft => ( {
294+ assetId : toAssetId ( {
295+ chainId : this . chainId ,
296+ assetNamespace : ASSET_NAMESPACE . nep141 ,
297+ assetReference : ft . contract ,
298+ } ) ,
299+ balance : ft . amount ,
300+ name : ft . ft_meta . name ,
301+ precision : ft . ft_meta . decimals ,
302+ symbol : ft . ft_meta . symbol ,
303+ } ) )
304+ } catch ( err ) {
305+ console . warn ( 'Failed to fetch NEAR token balances:' , err )
306+ return [ ]
307+ }
308+ }
309+
247310 async getAccount ( pubkey : string ) : Promise < Account < KnownChainIds . NearMainnet > > {
248311 try {
249- const accountResult = await this . rpcCall < NearAccountResult > ( 'query' , {
250- request_type : 'view_account' ,
251- finality : 'final' ,
252- account_id : pubkey ,
253- } )
312+ const [ accountResult , tokens ] = await Promise . all ( [
313+ this . rpcCall < NearAccountResult > ( 'query' , {
314+ request_type : 'view_account' ,
315+ finality : 'final' ,
316+ account_id : pubkey ,
317+ } ) ,
318+ this . fetchTokenBalances ( pubkey ) ,
319+ ] )
254320
255321 return {
256322 balance : accountResult . amount ,
257323 chainId : this . chainId ,
258324 assetId : this . assetId ,
259325 chain : this . getType ( ) ,
260326 chainSpecific : {
261- tokens : [ ] ,
327+ tokens,
262328 } ,
263329 pubkey,
264330 }
@@ -311,7 +377,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.NearMainnet> {
311377
312378 // Convert hex public key to bytes and create PublicKey using standard NEAR format
313379 const pubKeyBytes = Buffer . from ( from , 'hex' )
314- const pubKeyBase58 = bs58 . encode ( pubKeyBytes )
380+ const pubKeyBase58 = baseEncode ( pubKeyBytes )
315381 const publicKey = PublicKey . fromString ( `ed25519:${ pubKeyBase58 } ` )
316382
317383 // Get current nonce from access key
@@ -330,7 +396,7 @@ export class ChainAdapter implements IChainAdapter<KnownChainIds.NearMainnet> {
330396 [ ] ,
331397 )
332398 const blockHashBase58 = statusResult . sync_info . latest_block_hash
333- const blockHash = bs58 . decode ( blockHashBase58 )
399+ const blockHash = baseDecode ( blockHashBase58 )
334400
335401 // Build transfer action
336402 const actions = [ actionCreators . transfer ( BigInt ( value ) ) ]
0 commit comments