Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sdk 372 preparesettlement should calculate needed atas create atas #245

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.putOptionMint
: euroMeta.callOptionMint,
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
Loading