Skip to content

Commit

Permalink
add getTokenBalance operation to token Module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Sep 14, 2023
1 parent b655a68 commit 1adb978
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,20 @@ export const prepareAmericanOptions = async (
) {
ataTxBuilderArray.push(underlyingToken.txBuilder);
}
const { tokenBalance } = await convergence.tokens().getTokenBalance({
mintAddress: optionMarket.optionMint,
owner: caller,
mintDecimals: PsyoptionsAmericanInstrument.decimals,
});

const tokensToMint = amount - tokenBalance;
const ixWithSigners =
await psyoptionsAmerican.instructions.mintOptionInstruction(
americanProgram,
optionToken.ataPubKey,
writerToken.ataPubKey,
underlyingToken.ataPubKey,
new BN(amount!),
new BN(tokensToMint!),
optionMarket as psyoptionsAmerican.OptionMarketWithKey
);
ixWithSigners.ix.keys[0] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,26 @@ export const prepareEuropeanOptions = async (
) {
ataTxBuilderArray.push(writerDestination.txBuilder);
}

const { tokenBalance } = await convergence.tokens().getTokenBalance({
mintAddress:
leg.optionType == psyoptionsEuropean.OptionType.PUT
? euroMeta.putWriterMint
: euroMeta.callWriterMint,
owner: caller,
mintDecimals: PsyoptionsEuropeanInstrument.decimals,
});

const tokensToMint = amount - tokenBalance;

const { instruction: ix } = psyoptionsEuropean.instructions.mintOptions(
europeanProgram,
leg.optionMetaPubKey,
euroMeta as psyoptionsEuropean.EuroMeta,
minterCollateralKey,
optionDestination.ataPubKey,
writerDestination.ataPubKey,
addDecimals(amount, PsyoptionsEuropeanInstrument.decimals),
addDecimals(tokensToMint, PsyoptionsEuropeanInstrument.decimals),
leg.optionType
);

Expand Down
9 changes: 9 additions & 0 deletions packages/js/src/plugins/tokenModule/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
findTokenWithMintByMintOperation,
FreezeTokensInput,
freezeTokensOperation,
GetTokenBalanceInput,
getTokenBalanceOperation,
MintTokensInput,
mintTokensOperation,
RevokeTokenDelegateAuthorityInput,
Expand Down Expand Up @@ -233,4 +235,11 @@ export class TokenClient {
.operations()
.execute(revokeTokenDelegateAuthorityOperation(input), options);
}

/** {@inheritDoc getTokenBalanceOperation } */
getTokenBalance(input: GetTokenBalanceInput, options?: OperationOptions) {
return this.convergence
.operations()
.execute(getTokenBalanceOperation(input), options);
}
}
91 changes: 91 additions & 0 deletions packages/js/src/plugins/tokenModule/operations/getTokenBalance.ts
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,
};
}
},
};
1 change: 1 addition & 0 deletions packages/js/src/plugins/tokenModule/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './mintTokens';
export * from './revokeTokenDelegateAuthority';
export * from './sendTokens';
export * from './thawTokens';
export * from './getTokenBalance';
4 changes: 4 additions & 0 deletions packages/js/src/plugins/tokenModule/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
findTokenWithMintByMintOperationHandler,
freezeTokensOperation,
freezeTokensOperationHandler,
getTokenBalanceOperationHandler,
getTokenBalanceOperation,
mintTokensOperation,
mintTokensOperationHandler,
revokeTokenDelegateAuthorityOperation,
Expand Down Expand Up @@ -94,6 +96,8 @@ export const tokenModule = (): ConvergencePlugin => ({
op.register(sendTokensOperation, sendTokensOperationHandler);
op.register(thawTokensOperation, thawTokensOperationHandler);

op.register(getTokenBalanceOperation, getTokenBalanceOperationHandler);

convergence.tokens = function () {
return new TokenClient(this);
};
Expand Down

0 comments on commit 1adb978

Please sign in to comment.