From c34a4256f712441044e31de6c9a2092fdc9027a3 Mon Sep 17 00:00:00 2001 From: Nagaprasadvr Date: Tue, 31 Oct 2023 18:17:13 +0530 Subject: [PATCH] add latest repsonse expiration to sdk --- packages/js/package.json | 10 ++--- .../rfqModule/operations/respondToRfq.ts | 17 ++++++++ packages/js/tests/helpers.ts | 5 +++ packages/js/tests/unit/response.spec.ts | 20 ++++++++++ yarn.lock | 40 +++++++++---------- 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/packages/js/package.json b/packages/js/package.json index 89b544d9f..4f5bbcb0e 100644 --- a/packages/js/package.json +++ b/packages/js/package.json @@ -51,11 +51,11 @@ "@bundlr-network/client": "^0.8.8", "@convergence-rfq/beet": "0.7.10", "@convergence-rfq/beet-solana": "0.4.11", - "@convergence-rfq/psyoptions-american-instrument": "2.2.13", - "@convergence-rfq/psyoptions-european-instrument": "2.2.13", - "@convergence-rfq/rfq": "2.2.13", - "@convergence-rfq/risk-engine": "2.2.13", - "@convergence-rfq/spot-instrument": "2.2.13", + "@convergence-rfq/psyoptions-american-instrument": "2.2.14", + "@convergence-rfq/psyoptions-european-instrument": "2.2.14", + "@convergence-rfq/rfq": "2.2.14", + "@convergence-rfq/risk-engine": "2.2.14", + "@convergence-rfq/spot-instrument": "2.2.14", "@coral-xyz/borsh": "^0.26.0", "@mithraic-labs/psy-american": "^0.2.3", "@mithraic-labs/tokenized-euros": "^0.2.3", diff --git a/packages/js/src/plugins/rfqModule/operations/respondToRfq.ts b/packages/js/src/plugins/rfqModule/operations/respondToRfq.ts index 529505697..3b6fe5d08 100644 --- a/packages/js/src/plugins/rfqModule/operations/respondToRfq.ts +++ b/packages/js/src/plugins/rfqModule/operations/respondToRfq.ts @@ -1,6 +1,7 @@ import { createRespondToRfqInstruction } from '@convergence-rfq/rfq'; import { PublicKey, AccountMeta, ComputeBudgetProgram } from '@solana/web3.js'; +import BN from 'bn.js'; import { SendAndConfirmTransactionResponse } from '../../rpcModule'; import { assertResponse, Response } from '../models/Response'; import { Convergence } from '../../../Convergence'; @@ -99,6 +100,11 @@ export type RespondToRfqInput = { */ ask?: Quote; + /** + * The optional response expirationTimestamp in milliseconds. + */ + expirationTimestamp?: number; + /** * The address of the RFQ account. */ @@ -239,6 +245,7 @@ export const respondToRfqBuilder = async ( user: maker.publicKey, programs, }), + expirationTimestamp, } = params; if (!bid && !ask) { @@ -247,6 +254,15 @@ export const respondToRfqBuilder = async ( const rfqModel = await convergence.rfqs().findRfqByAddress({ address: rfq }); + let expirationTimestampBn: BN; + if (!expirationTimestamp) { + const rfqExpirationTimestamp = + rfqModel.creationTimestamp / 1_000 + rfqModel.activeWindow; + expirationTimestampBn = new BN(rfqExpirationTimestamp); + } else { + expirationTimestampBn = new BN(expirationTimestamp / 1_000); + } + const { response, pdaDistinguisher } = await getNextResponsePdaAndDistinguisher( convergence, @@ -323,6 +339,7 @@ export const respondToRfqBuilder = async ( bid: bid && toSolitaQuote(bid, rfqModel.quoteAsset.getDecimals()), ask: ask && toSolitaQuote(ask, rfqModel.quoteAsset.getDecimals()), pdaDistinguisher, + expirationTimestamp: expirationTimestampBn, } ), signers: [maker], diff --git a/packages/js/tests/helpers.ts b/packages/js/tests/helpers.ts index aa52d4ade..369c997fc 100644 --- a/packages/js/tests/helpers.ts +++ b/packages/js/tests/helpers.ts @@ -404,9 +404,11 @@ export const createRfq = async ( cvg: Convergence, amount: number, orderType: OrderType, + activeWindow?: number, rfqType: 'open' | 'fixed-base' | 'fixed-quote' = 'fixed-base', quoteMintPk = QUOTE_MINT_PK, baseMintPk = BASE_MINT_BTC_PK + // 10 minutes ) => { let instrumentAmount = 1; let fixedSizeAmount = 1; @@ -429,6 +431,7 @@ export const createRfq = async ( orderType, fixedSize: { type: rfqType, amount: fixedSizeAmount }, quoteAsset: await SpotQuoteInstrument.create(cvg, quoteMint), + activeWindow, }); return { rfq, response }; }; @@ -438,6 +441,7 @@ export const respondToRfq = async ( rfq: Rfq, bid?: number, ask?: number, + responseExpirationTimestamp?: number, legsMultiplier?: number ) => { if (!bid && !ask) { @@ -448,6 +452,7 @@ export const respondToRfq = async ( rfq: rfq.address, bid: bid ? { price: bid, legsMultiplier } : undefined, ask: ask ? { price: ask, legsMultiplier } : undefined, + expirationTimestamp: responseExpirationTimestamp, }); }; diff --git a/packages/js/tests/unit/response.spec.ts b/packages/js/tests/unit/response.spec.ts index 08085665e..41d4bd76c 100644 --- a/packages/js/tests/unit/response.spec.ts +++ b/packages/js/tests/unit/response.spec.ts @@ -219,4 +219,24 @@ describe('unit.response', () => { expect(r.rfq.toBase58()).toEqual(rfq0.address.toBase58()) ); }); + + it('Cannot confirm Response if response is expired', async () => { + const rfq = await createRfq(takerCvg, amount0, 'buy', 3); + + const res = await respondToRfq( + makerCvg, + rfq.rfq, + undefined, + amount1, + Date.now() / 1_000 + 2 + ); + + expect( + await makerCvg.rfqs().confirmResponse({ + response: res.rfqResponse.address, + rfq: rfq.rfq.address, + side: 'ask', + }) + ).toThrowError('Response is expired'); + }); }); diff --git a/yarn.lock b/yarn.lock index 4327fc8d7..dd9ebc26b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1240,10 +1240,10 @@ bn.js "^5.2.0" debug "^4.3.3" -"@convergence-rfq/psyoptions-american-instrument@2.2.13": - version "2.2.13" - resolved "https://registry.yarnpkg.com/@convergence-rfq/psyoptions-american-instrument/-/psyoptions-american-instrument-2.2.13.tgz#c4d0ab251ecc325a382e0e75ca743711a49fc7c7" - integrity sha512-DAsERQdjTxPhayB3GElWKlKsFPrd42XnG+CLg6+7Q1SZGDQFB19G7UafcwJJmik5wwrxcWJHLCONhmPZEmLCAg== +"@convergence-rfq/psyoptions-american-instrument@2.2.14": + version "2.2.14" + resolved "https://registry.yarnpkg.com/@convergence-rfq/psyoptions-american-instrument/-/psyoptions-american-instrument-2.2.14.tgz#25e2da5709ae4dd3d031d54112f10016003cd2ae" + integrity sha512-8uEDCLPqc26nCwPWpm43xFN9wv3x9BSWf2h0kUhfmGUFnyVH7VfIrMNlxJwC2S6329TtlnTyl5bDbO2a+aMPQw== dependencies: "@convergence-rfq/beet" "^0.7.10" "@convergence-rfq/beet-solana" "^0.4.11" @@ -1251,10 +1251,10 @@ "@solana/web3.js" "^1.56.2" bn.js "^5.2.0" -"@convergence-rfq/psyoptions-european-instrument@2.2.13": - version "2.2.13" - resolved "https://registry.yarnpkg.com/@convergence-rfq/psyoptions-european-instrument/-/psyoptions-european-instrument-2.2.13.tgz#1915a640040011eb193d6b96dc26ca3ed992941e" - integrity sha512-S7jM0mrhjx3QiT62fJAKVWSBlqcXCpk+KESNvy7mdSnJZGgpVTnyQeLWSqwO2cDElDOLn4Z40yfRTRgxssuPnw== +"@convergence-rfq/psyoptions-european-instrument@2.2.14": + version "2.2.14" + resolved "https://registry.yarnpkg.com/@convergence-rfq/psyoptions-european-instrument/-/psyoptions-european-instrument-2.2.14.tgz#eff403b346cc28424b7fc34404615b29292b341e" + integrity sha512-6Ga6iRaP2aQmrCcwT7G15Oucuvq6ySqAYHijS1MIUfw/iP9uqbpYdb2eDdUBMY8lY9CTvwm3zZ9C4i1NLDQJog== dependencies: "@convergence-rfq/beet" "^0.7.10" "@convergence-rfq/beet-solana" "^0.4.11" @@ -1262,10 +1262,10 @@ "@solana/web3.js" "^1.56.2" bn.js "^5.2.0" -"@convergence-rfq/rfq@2.2.13": - version "2.2.13" - resolved "https://registry.yarnpkg.com/@convergence-rfq/rfq/-/rfq-2.2.13.tgz#d78cd575ee176935c42ee178a51d6ee8e7637d89" - integrity sha512-0/QzK8roBoc/oCKVj/Z0gHeBYxjLvxlb1SqvmVlqlQPWjpoZfAojse04+g5c1LnUO9X0STU0Cn5tKDFbt0Jerg== +"@convergence-rfq/rfq@2.2.14": + version "2.2.14" + resolved "https://registry.yarnpkg.com/@convergence-rfq/rfq/-/rfq-2.2.14.tgz#7b2ad2d33908f6cc17c776b7d2ce2ab80c70af8a" + integrity sha512-MGGVOrJ7zjoJrjd3WNpCX2upzrw6XipJuRU2MAuffVV53p5t7xS896MONW5eCt/Zp4ffvWO9PYh5s0dZkcs17g== dependencies: "@convergence-rfq/beet" "^0.7.10" "@convergence-rfq/beet-solana" "^0.4.11" @@ -1273,10 +1273,10 @@ "@solana/web3.js" "^1.56.2" bn.js "^5.2.0" -"@convergence-rfq/risk-engine@2.2.13": - version "2.2.13" - resolved "https://registry.yarnpkg.com/@convergence-rfq/risk-engine/-/risk-engine-2.2.13.tgz#0449366fe33f31711f1c484e7e95d9871ffb9b23" - integrity sha512-koUzlOGm20/sH42IYkiM34NCL4lsNX7YO93rILgdmm68I31njx7gNxLneLvyebfMcvJiSF3NKdmbu3dpn/U8Qg== +"@convergence-rfq/risk-engine@2.2.14": + version "2.2.14" + resolved "https://registry.yarnpkg.com/@convergence-rfq/risk-engine/-/risk-engine-2.2.14.tgz#1ecefeb27ea9233df8bab970953f2b41d1a538e4" + integrity sha512-mAhALsP5w/UEfwz4wn6p/6niTkOvjG4Ae5KXQH36g0KMwCHdFipr45ZPkqmiGAR0cT1PvOiz376qa566BUyt2Q== dependencies: "@convergence-rfq/beet" "^0.7.10" "@convergence-rfq/beet-solana" "^0.4.11" @@ -1284,10 +1284,10 @@ "@solana/web3.js" "^1.56.2" bn.js "^5.2.0" -"@convergence-rfq/spot-instrument@2.2.13": - version "2.2.13" - resolved "https://registry.yarnpkg.com/@convergence-rfq/spot-instrument/-/spot-instrument-2.2.13.tgz#06550c962f0f4e68ed277c551586b138fec5b329" - integrity sha512-C2l0/vALFR7SxPrVu0GvzlyYrLH/Uy2wZ5IyDHnn74Xt/SBUOx4VdB2F2mHyLl/KG+yLaMREfcmBB7ZV2FHEyQ== +"@convergence-rfq/spot-instrument@2.2.14": + version "2.2.14" + resolved "https://registry.yarnpkg.com/@convergence-rfq/spot-instrument/-/spot-instrument-2.2.14.tgz#5b7596022245045f44d3565d94deda658e034438" + integrity sha512-NTeiltgcM36akuXIwrXSAavoec61rQq1Z2siLM0TXG/wC9B8LGvDkXQdUfu0jqrgXe5auf2cPOxsq3E80AAEZg== dependencies: "@convergence-rfq/beet" "^0.7.10" "@convergence-rfq/beet-solana" "^0.4.11"