-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add getTokenBalance operation to token Module
- Loading branch information
1 parent
b655a68
commit 1adb978
Showing
6 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
packages/js/src/plugins/tokenModule/operations/getTokenBalance.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { PublicKey } from '@solana/web3.js'; | ||
|
||
import BN from 'bn.js'; | ||
import type { Convergence } from '../../../Convergence'; | ||
import { Operation, OperationHandler, useOperation } from '../../../types'; | ||
|
||
const Key = 'GetTokenBalance' as const; | ||
|
||
/** | ||
* Get token Balance. | ||
* | ||
* ```ts | ||
* await convergence | ||
* .tokens() | ||
* .getTokenBalance({ | ||
* mintAddress, | ||
* owner, | ||
* mintDecimals | ||
* }; | ||
* ``` | ||
* | ||
* @group Operations | ||
* @category Constructors | ||
*/ | ||
export const getTokenBalanceOperation = | ||
useOperation<GetTokenBalanceOperation>(Key); | ||
|
||
/** | ||
* @group Operations | ||
* @category Types | ||
*/ | ||
export type GetTokenBalanceOperation = Operation< | ||
typeof Key, | ||
GetTokenBalanceInput, | ||
GetTokenBalanceOutput | ||
>; | ||
|
||
/** | ||
* @group Operations | ||
* @category Inputs | ||
*/ | ||
export type GetTokenBalanceInput = { | ||
/** The address of the mint account. */ | ||
mintAddress: PublicKey; | ||
/** The address of ATA owner */ | ||
owner: PublicKey; | ||
/** mint decimals*/ | ||
mintDecimals: number; | ||
}; | ||
|
||
/** | ||
* @group Operations | ||
* @category Outputs | ||
*/ | ||
export type GetTokenBalanceOutput = { | ||
/** The blockchain response from sending and confirming the transaction. */ | ||
tokenBalance: number; | ||
}; | ||
|
||
/** | ||
* @group Operations | ||
* @category Handlers | ||
*/ | ||
export const getTokenBalanceOperationHandler: OperationHandler<GetTokenBalanceOperation> = | ||
{ | ||
async handle( | ||
operation: GetTokenBalanceOperation, | ||
convergence: Convergence | ||
): Promise<GetTokenBalanceOutput> { | ||
const { mintAddress, owner, mintDecimals } = operation.input; | ||
|
||
const ataAddress = convergence.tokens().pdas().associatedTokenAccount({ | ||
mint: mintAddress, | ||
owner, | ||
}); | ||
try { | ||
const ata = await convergence | ||
.tokens() | ||
.findTokenByAddress({ address: ataAddress }); | ||
return { | ||
tokenBalance: ata.amount.basisPoints | ||
.div(new BN(Math.pow(10, mintDecimals))) | ||
.toNumber(), | ||
}; | ||
} catch (e) { | ||
return { | ||
tokenBalance: 0, | ||
}; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters