diff --git a/CHANGELOG.md b/CHANGELOG.md index 36dcdcea2..da35ae3ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,10 @@ # Next version -# 2.0.5-40 +- Return both transaction (`ethers.ContractTransaction`) and result promises from `MangroveAmplifier` methods. -- add tx receipt in addbunddle +# 2.0.5-40 -# 2.0.5-39 +- add tx receipt in `MangroveAmplifier.addbunddle` # 2.0.5-39 diff --git a/src/amplifier/mangroveAmplifier.ts b/src/amplifier/mangroveAmplifier.ts index 79a98a3cf..509bca3a3 100644 --- a/src/amplifier/mangroveAmplifier.ts +++ b/src/amplifier/mangroveAmplifier.ts @@ -99,7 +99,7 @@ class MangroveAmplifier { */ public async addBundle( data: z.input, - ): Promise { + ): Promise> { const { outboundToken, outboundVolume, @@ -140,7 +140,7 @@ class MangroveAmplifier { total = total.add(provision).add(BigNumber.from(64_000)); } - const response = await createTxWithOptionalGasEstimation( + const responsePromise = createTxWithOptionalGasEstimation( this.amplifier.newBundle, this.amplifier.estimateGas.newBundle, 1, @@ -148,14 +148,23 @@ class MangroveAmplifier { [fx, vr, { value: total }], ); - const receipt = await response.wait(); + return { + response: responsePromise, + result: responsePromise.then(async (response) => { + const receipt = await response.wait(); - logger.debug("Amplified order raw receipt", { - contextInfo: "amplifiedOrder.addBundle", - data: { receipt }, - }); + logger.debug("Amplified order raw receipt", { + contextInfo: "amplifiedOrder.addBundle", + data: { receipt }, + }); + + const bundleId = receipt.events?.filter( + (e) => e.event === "InitBundle", + )[0].args?.bundleId; - return receipt; + return BigNumber.from(bundleId); + }), + }; } /** @@ -245,7 +254,7 @@ class MangroveAmplifier { */ public async updateBundle( data: z.input, - ): Promise { + ): Promise> { const { bundleId, outboundToken, @@ -253,27 +262,32 @@ class MangroveAmplifier { updateExpiry, expiryDate, } = updateBundleParams.parse(data); - const response = await createTxWithOptionalGasEstimation( + const responsePromise = createTxWithOptionalGasEstimation( this.amplifier.updateBundle, this.amplifier.estimateGas.updateBundle, 0, {}, [bundleId, outboundToken, outboundVolume, updateExpiry, expiryDate], ); - const receipt = await response.wait(); - logger.debug("Amplified order update raw receipt", { - contextInfo: "amplifiedOrder.updateBundle", - data: { receipt }, - }); - return receipt; + return { + response: responsePromise, + result: responsePromise.then(async (response) => { + const receipt = await response.wait(); + + logger.debug("Amplified order update raw receipt", { + contextInfo: "amplifiedOrder.updateBundle", + data: { receipt }, + }); + }), + }; } /** */ public async updateOfferInBundle( data: z.input, - ): Promise { + ): Promise[]> { const { bundleId, inboundToken, newTick, newInboundLogic, outboundToken } = updateBundleOfferParams.parse(data); @@ -290,7 +304,7 @@ class MangroveAmplifier { const olKeyHash = this.mgv.getOlKeyHash(olKey); - let receipt: ethers.ContractReceipt | undefined; + const transactions: Transaction[] = []; if (newTick) { const base = await this.mgv.tokenFromAddress(inboundToken); @@ -314,7 +328,7 @@ class MangroveAmplifier { existingLogic?.gasOverhead ?? 0, ); - const response = await createTxWithOptionalGasEstimation( + const responsePromise = createTxWithOptionalGasEstimation( this.amplifier.updateOffer, this.amplifier.estimateGas.updateOffer, 0, @@ -322,25 +336,53 @@ class MangroveAmplifier { [olKey, newTick, gives.gives.toString(), gasReq, offerId], ); - receipt = await response.wait(); + transactions.push({ + response: responsePromise, + result: responsePromise.then(async (response) => { + const receipt = await response.wait(); - logger.debug("Amplified order update tick receipt", { - contextInfo: "amplifiedOrder.updateOfferInBundle", - data: { receipt }, + logger.debug("Amplified order update tick receipt", { + contextInfo: "amplifiedOrder.updateOfferInBundle", + data: { receipt }, + }); + }), }); } - const newRoutingLogicParams = { - token: inboundToken, - logic: newInboundLogic!, - offerId: offerId.toNumber(), - olKeyHash, - }; - if (newInboundLogic) { - await this.setRoutingLogic(newRoutingLogicParams, {}); + const newRoutingLogicParams = { + token: inboundToken, + logic: newInboundLogic, + offerId: offerId.toNumber(), + olKeyHash, + }; + + if (transactions.length === 0) { + transactions.push( + await this.setRoutingLogic(newRoutingLogicParams, {}), + ); + return transactions; + } + + const { result: prevTxResult } = transactions[0]; + + const responsePromise = prevTxResult.then(async () => { + const { response } = await this.setRoutingLogic( + newRoutingLogicParams, + {}, + ); + return response; + }); + + transactions.push({ + response: responsePromise, + result: responsePromise.then(async (response) => { + await response.wait(); + }), + }); } - return receipt; + + return transactions; } private async setRoutingLogic( @@ -381,22 +423,27 @@ class MangroveAmplifier { */ public async retractBundle( data: z.input, - ): Promise { + ): Promise> { const { bundleId, outboundToken } = retractBundleParams.parse(data); - const response = await createTxWithOptionalGasEstimation( + const responsePromise = createTxWithOptionalGasEstimation( this.amplifier.retractBundle, this.amplifier.estimateGas.retractBundle, 0, {}, [bundleId, outboundToken], ); - const receipt = await response.wait(); - logger.debug("Amplified order update raw receipt", { - contextInfo: "amplifiedOrder.retractBundle", - data: { receipt }, - }); - return receipt; + return { + response: responsePromise, + result: responsePromise.then(async (response) => { + const receipt = await response.wait(); + + logger.debug("Amplified order update raw receipt", { + contextInfo: "amplifiedOrder.retractBundle", + data: { receipt }, + }); + }), + }; } } diff --git a/test/integration/amplifier/amplifier.integration.test.ts b/test/integration/amplifier/amplifier.integration.test.ts index f169e54fc..ef9ac7536 100644 --- a/test/integration/amplifier/amplifier.integration.test.ts +++ b/test/integration/amplifier/amplifier.integration.test.ts @@ -3,7 +3,6 @@ import { afterEach, beforeEach, describe, it } from "mocha"; import { toWei } from "../../util/helpers"; import * as mgvTestUtil from "../../../src/util/test/mgvIntegrationTestUtil"; import MangroveAmplifier from "../../../src/amplifier/mangroveAmplifier"; -import { typechain } from "../../../src/types"; import { Mangrove } from "../../../src"; @@ -114,7 +113,7 @@ describe("Amplifier integration tests suite", () => { it("Creates a bundle across 2 markets", async function () { const inboundTokens = [simpleToken("TokenA"), simpleToken("TokenB")]; - const bundleId = await amplifier.addBundle({ + const { result } = await amplifier.addBundle({ outboundToken: mgv.getAddress("TokenC"), outboundVolume: 10n ** 18n, outboundLogic: mgv.logics.simple, @@ -122,6 +121,8 @@ describe("Amplifier integration tests suite", () => { inboundTokens: inboundTokens, }); + const bundleId = await result; + const bundleData = await amplifier.getBundle({ bundleId, outboundToken: mgv.getAddress("TokenC"), @@ -144,7 +145,7 @@ describe("Amplifier integration tests suite", () => { describe("Succeeds", () => { it("Retracts a bundle", async function () { - const bundleId = await addBundle(); + const bundleId = await (await addBundle()).result; await amplifier.retractBundle({ bundleId, @@ -165,7 +166,7 @@ describe("Amplifier integration tests suite", () => { describe("Succeeds", () => { it("Updates a bundle (not date)", async function () { - const bundleId = await addBundle(); + const bundleId = await (await addBundle()).result; await amplifier.updateBundle({ bundleId, @@ -183,7 +184,7 @@ describe("Amplifier integration tests suite", () => { }); it("Updates a bundle (date)", async function () { - const bundleId = await addBundle(); + const bundleId = await (await addBundle()).result; await amplifier.updateBundle({ bundleId, @@ -206,7 +207,7 @@ describe("Amplifier integration tests suite", () => { describe("Succeeds", () => { it("Updates an offer in a bundle (tick)", async function () { - const bundleId = await addBundle(); + const bundleId = await (await addBundle()).result; await amplifier.updateOfferInBundle({ bundleId, @@ -223,7 +224,7 @@ describe("Amplifier integration tests suite", () => { }); it("Updates an offer in a bundle (logic)", async function () { - const bundleId = await addBundle(); + const bundleId = await (await addBundle()).result; await amplifier.updateOfferInBundle({ bundleId,