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 - fix options rfq underlying asset token mapping during #242

Closed
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
2 changes: 1 addition & 1 deletion packages/js/src/plugins/instrumentModule/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ declare module '../../Convergence' {
programAddress: PublicKey,
factory: LegInstrumentParser
): void;
parseLegInstrument(leg: SolitaLeg): LegInstrument;
parseLegInstrument(leg: SolitaLeg): Promise<LegInstrument>;
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/js/src/plugins/instrumentModule/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Convergence } from '../../Convergence';
import { LegSide } from '../rfqModule/models/LegSide';

export interface LegInstrumentParser {
parseFromLeg(convergence: Convergence, leg: Leg): LegInstrument;
parseFromLeg(convergence: Convergence, leg: Leg): Promise<LegInstrument>;
}

export interface LegInstrument {
Expand All @@ -17,6 +17,7 @@ export interface LegInstrument {
getSide: () => LegSide;
serializeInstrumentData: () => Buffer;
getValidationAccounts(): Promise<AccountMeta[]>;
getBaseAssetMint(): PublicKey;
}

// TODO add registration of quote instruments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as psyoptionsAmerican from '@mithraic-labs/psy-american';

import { BN } from 'bn.js';
import { PublicKey } from '@solana/web3.js';
import { Leg } from '@convergence-rfq/rfq';
import { Convergence } from '../../Convergence';

import { ATAExistence, getOrCreateATA } from '../../utils/ata';
Expand All @@ -13,6 +14,7 @@ import {
} from '../../utils/TransactionBuilder';
import { PsyoptionsAmericanInstrument } from './types';
import { createAmericanProgram } from './instrument';
import { psyoptionsAmericanInstrumentProgram } from './programs';

export const mintAmericanOptions = async (
convergence: Convergence,
Expand Down Expand Up @@ -185,3 +187,9 @@ export const getOrCreateAmericanOptionATAs = async (
}
return ATAExistence.NOTEXISTS;
};

export function hasPsyoptionsAmericanLeg(legs: Leg[]): boolean {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

return legs.some((leg) =>
leg.instrumentProgram.equals(psyoptionsAmericanInstrumentProgram.address)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ export class PsyoptionsAmericanInstrument implements LegInstrument {
readonly baseAssetIndex: BaseAssetIndex,
readonly amount: number,
readonly side: LegSide,
private optionMeta?: OptionMarketWithKey
public optionMeta: OptionMarketWithKey
) {}

getBaseAssetIndex = () => this.baseAssetIndex;
getAmount = () => this.amount;
getDecimals = () => PsyoptionsAmericanInstrument.decimals;
getSide = () => this.side;
getBaseAssetMint = () => this.optionMeta.underlyingAssetMint;

static async create(
convergence: Convergence,
Expand Down Expand Up @@ -196,10 +197,10 @@ export class PsyoptionsAmericanInstrument implements LegInstrument {
}

export const psyoptionsAmericanInstrumentParser = {
parseFromLeg(
async parseFromLeg(
convergence: Convergence,
leg: Leg
): PsyoptionsAmericanInstrument {
): Promise<PsyoptionsAmericanInstrument> {
const { side, instrumentAmount, instrumentData, baseAssetIndex } = leg;
const [
{
Expand All @@ -215,7 +216,10 @@ export const psyoptionsAmericanInstrumentParser = {
] = psyoptionsAmericanInstrumentDataSerializer.deserialize(
Buffer.from(instrumentData)
);

const optionMeta = await PsyoptionsAmericanInstrument.fetchMeta(
convergence,
metaKey
);
return new PsyoptionsAmericanInstrument(
convergence,
optionType,
Expand All @@ -231,7 +235,8 @@ export const psyoptionsAmericanInstrumentParser = {
metaKey,
baseAssetIndex,
removeDecimals(instrumentAmount, PsyoptionsAmericanInstrument.decimals),
fromSolitaLegSide(side)
fromSolitaLegSide(side),
optionMeta
);
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import * as psyoptionsEuropean from '@mithraic-labs/tokenized-euros';
import * as anchor from '@project-serum/anchor';
import { Keypair, PublicKey } from '@solana/web3.js';
import { BN } from 'bn.js';
import { Leg } from '@convergence-rfq/rfq';
import { Mint } from '../tokenModule';
import { ATAExistence, getOrCreateATA } from '../../utils/ata';
import { addDecimals } from '../../utils/conversions';
import { TransactionBuilder } from '../../utils/TransactionBuilder';
import { Convergence } from '../../Convergence';
import { PsyoptionsEuropeanInstrument } from './instrument';
import { psyoptionsEuropeanInstrumentProgram } from './programs';
import { Pda } from '@/types/Pda';
import { makeConfirmOptionsFinalizedOnMainnet } from '@/types/Operation';
import { toBigNumber } from '@/types/BigNumber';
Expand Down Expand Up @@ -264,3 +266,9 @@ export const getOrCreateEuropeanOptionATAs = async (
}
return ATAExistence.NOTEXISTS;
};

export function hasPsyoptionsEuropeanLeg(legs: Leg[]): boolean {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

return legs.some((leg) =>
leg.instrumentProgram.equals(psyoptionsEuropeanInstrumentProgram.address)
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ export class PsyoptionsEuropeanInstrument implements LegInstrument {
readonly baseAssetIndex: BaseAssetIndex,
readonly amount: number,
readonly side: LegSide,
private optionMeta?: EuroMeta
public optionMeta: EuroMeta
) {}

getBaseAssetIndex = () => this.baseAssetIndex;
getAmount = () => this.amount;
getDecimals = () => PsyoptionsEuropeanInstrument.decimals;
getSide = () => this.side;
getBaseAssetMint = () => this.optionMeta.underlyingMint;

static async create(
convergence: Convergence,
Expand Down Expand Up @@ -214,10 +215,10 @@ export class PsyoptionsEuropeanInstrument implements LegInstrument {
}

export const psyoptionsEuropeanInstrumentParser = {
parseFromLeg(
async parseFromLeg(
convergence: Convergence,
leg: Leg
): PsyoptionsEuropeanInstrument {
): Promise<PsyoptionsEuropeanInstrument> {
const { side, instrumentAmount, instrumentData, baseAssetIndex } = leg;
const [
{
Expand All @@ -233,7 +234,10 @@ export const psyoptionsEuropeanInstrumentParser = {
] = psyoptionsEuropeanInstrumentDataSerializer.deserialize(
Buffer.from(instrumentData)
);

const optionMeta = await PsyoptionsEuropeanInstrument.fetchMeta(
convergence,
metaKey
);
return new PsyoptionsEuropeanInstrument(
convergence,
optionType,
Expand All @@ -249,7 +253,8 @@ export const psyoptionsEuropeanInstrumentParser = {
metaKey,
baseAssetIndex,
removeDecimals(instrumentAmount, PsyoptionsEuropeanInstrument.decimals),
fromSolitaLegSide(side)
fromSolitaLegSide(side),
optionMeta
);
},
};
7 changes: 6 additions & 1 deletion packages/js/src/plugins/rfqModule/models/Rfq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export const toRfq = async (
);
const collateralMint = await collateralMintCache.get(convergence);
const collateralDecimals = collateralMint.decimals;
const { legs } = account.data;
const parsedLegs = await Promise.all(
legs.map((leg) => convergence.parseLegInstrument(leg))
);

return {
model: 'rfq',
address: account.publicKey,
Expand All @@ -130,6 +135,6 @@ export const toRfq = async (
totalResponses: account.data.totalResponses,
clearedResponses: account.data.clearedResponses,
confirmedResponses: account.data.confirmedResponses,
legs: account.data.legs.map((leg) => convergence.parseLegInstrument(leg)),
legs: parsedLegs,
};
};
7 changes: 5 additions & 2 deletions packages/js/src/plugins/spotInstrumentModule/instruments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class SpotLegInstrument implements LegInstrument {
getDecimals = () => this.decimals;
getAmount = () => this.amount;
getBaseAssetIndex = () => this.baseAssetIndex;

getBaseAssetMint = () => this.mintAddress;
static async create(
convergence: Convergence,
mint: Mint,
Expand Down Expand Up @@ -92,7 +92,10 @@ export class SpotLegInstrument implements LegInstrument {
}

export const spotLegInstrumentParser = {
parseFromLeg(convergence: Convergence, leg: Leg): SpotLegInstrument {
async parseFromLeg(
convergence: Convergence,
leg: Leg
): Promise<SpotLegInstrument> {
const {
side,
instrumentAmount,
Expand Down
1 change: 0 additions & 1 deletion packages/js/tests/integration/psyoptionsAmerican.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('integration.psyoptionsAmerican', () => {
quoteMint
);
expect(rfq).toHaveProperty('address');

const { rfqResponse } = await respondToRfq(makerCvg, rfq, 12.1);
expect(rfqResponse).toHaveProperty('address');

Expand Down
1 change: 0 additions & 1 deletion packages/js/tests/integration/psyoptionsEuropean.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ describe('integration.psyoptionsEuropean', () => {
baseMint,
quoteMint
);

expect(rfq).toHaveProperty('address');
expect(response.signature).toBeDefined();

Expand Down
1 change: 1 addition & 0 deletions packages/js/tests/unit/rfq.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('unit.rfq', () => {
quoteAsset: await SpotQuoteInstrument.create(takerCvg, quoteMint),
fixedSize,
});

// @ts-ignore
expect(fixedSize.amount).toBeCloseTo(rfq.size.amount);
});
Expand Down