Skip to content

Commit

Permalink
feat!(amplifier): Return both transaction and result promises (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
espendk authored Mar 12, 2024
1 parent 856244e commit 31b7fe4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 50 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
127 changes: 87 additions & 40 deletions src/amplifier/mangroveAmplifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class MangroveAmplifier {
*/
public async addBundle(
data: z.input<typeof addBundleParams>,
): Promise<ethers.ContractReceipt> {
): Promise<Transaction<BigNumber>> {
const {
outboundToken,
outboundVolume,
Expand Down Expand Up @@ -140,22 +140,31 @@ 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,
{},
[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);
}),
};
}

/**
Expand Down Expand Up @@ -245,35 +254,40 @@ class MangroveAmplifier {
*/
public async updateBundle(
data: z.input<typeof updateBundleParams>,
): Promise<ethers.ContractReceipt> {
): Promise<Transaction<void>> {
const {
bundleId,
outboundToken,
outboundVolume,
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<typeof updateBundleOfferParams>,
): Promise<ethers.ContractReceipt | undefined> {
): Promise<Transaction<any>[]> {
const { bundleId, inboundToken, newTick, newInboundLogic, outboundToken } =
updateBundleOfferParams.parse(data);

Expand All @@ -290,7 +304,7 @@ class MangroveAmplifier {

const olKeyHash = this.mgv.getOlKeyHash(olKey);

let receipt: ethers.ContractReceipt | undefined;
const transactions: Transaction<any>[] = [];

if (newTick) {
const base = await this.mgv.tokenFromAddress(inboundToken);
Expand All @@ -314,33 +328,61 @@ class MangroveAmplifier {
existingLogic?.gasOverhead ?? 0,
);

const response = await createTxWithOptionalGasEstimation(
const responsePromise = createTxWithOptionalGasEstimation(
this.amplifier.updateOffer,
this.amplifier.estimateGas.updateOffer,
0,
{},
[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(
Expand Down Expand Up @@ -381,22 +423,27 @@ class MangroveAmplifier {
*/
public async retractBundle(
data: z.input<typeof retractBundleParams>,
): Promise<ethers.ContractReceipt> {
): Promise<Transaction<void>> {
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 },
});
}),
};
}
}

Expand Down
15 changes: 8 additions & 7 deletions test/integration/amplifier/amplifier.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -114,14 +113,16 @@ 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,
expiryDate: 0n,
inboundTokens: inboundTokens,
});

const bundleId = await result;

const bundleData = await amplifier.getBundle({
bundleId,
outboundToken: mgv.getAddress("TokenC"),
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 31b7fe4

Please sign in to comment.