Skip to content

Commit

Permalink
Adjusting RouterSimulator interface
Browse files Browse the repository at this point in the history
RouterSimulator now supports several input/output assets.
  • Loading branch information
pgbrandao committed Dec 4, 2023
1 parent 32214d3 commit 094fcb1
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 134 deletions.
26 changes: 13 additions & 13 deletions core/src/abis/RouterSimulator.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export default {
networks: {
137: {
routerAddress: "0x5F5Ef893d65b91c5A060119fcBe8F0EB964dB5a8",
routerSimulatorAddress: "0xf12093D00E70D0AF5a3d16950d515cbC8232d46F",
routerAddress: "0x36CF9f3FC82C3b04f8273402abFF73fC208a809f",
routerSimulatorAddress: "0xc7ab8F80B8FCBFD2d493C0d131B6EaB6691dfBb9",
gammaRatiosCalculator: "0x0F306e004258dc4c9Ecc0373b6E3dC59A3f0dB58",
},
},
Expand Down
57 changes: 35 additions & 22 deletions core/src/path/tx-simulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ export async function simulateAssetSwapTransaction({

const populatedTx = await routerSimulator.simulateJamTx.populateTransaction(
config.networks[chainId].routerAddress,
sellAsset.address,
amountIn,
buyAsset.address,
[sellAsset.address],
[amountIn],
[buyAsset.address],
routerTransactionData.steps,
routerTransactionData.stores
);
Expand All @@ -71,12 +71,16 @@ export async function simulateAssetSwapTransaction({
};

try {
const ret = await (provider as any).send("eth_call", [
const rawCallResult = await (provider as any).send("eth_call", [
callData,
"latest",
stateOverrides,
]);
return BigInt(ret);
const decodedCallResult = routerSimulator.interface.decodeFunctionResult(
"simulateJamTx",
rawCallResult
);
return BigInt(decodedCallResult[0]);
} catch (e) {
console.error("Failed to simulate transaction");
console.dir(
Expand All @@ -102,17 +106,17 @@ export async function simulateRouterOperation({
chainId,
routerOperation,
provider,
sellAsset,
amountIn,
buyAsset,
sellAssets,
amountsIn,
buyAssets,
}: {
chainId: number;
routerOperation: RouterOperation;
provider: Provider;
sellAsset: Asset;
amountIn: string;
buyAsset: Asset;
}): Promise<BigNumberish | null> {
sellAssets: Asset[];
amountsIn: BigNumberish[];
buyAssets: Asset[];
}): Promise<BigNumberish[] | null> {
const config = await loadConfig();

// const txSimulator = getTxSimulatorContract(provider) as Contract;
Expand All @@ -138,20 +142,24 @@ export async function simulateRouterOperation({

const populatedTx = await routerSimulator.simulateJamTx.populateTransaction(
config.networks[chainId].routerAddress,
sellAsset.address,
amountIn,
buyAsset.address,
sellAssets.map((a) => a.address),
amountsIn,
buyAssets.map((a) => a.address),
routerTransactionData.steps,
routerTransactionData.stores
);

const from = "0x6D763ee17cEA70cB1026Fa0F272dd620546A9B9F";

const stateOverrides = generateTokenApprovalStateDiff(
sellAsset,
from,
config.networks[chainId].routerSimulatorAddress
);
const stateOverrides = {};
for (const sellAsset of sellAssets) {
const approvalStateDiff = generateTokenApprovalStateDiff(
sellAsset,
from,
config.networks[chainId].routerSimulatorAddress
);
Object.assign(stateOverrides, approvalStateDiff);
}

const callData = {
from: from,
Expand All @@ -168,12 +176,17 @@ export async function simulateRouterOperation({
);

try {
const ret = await (provider as any).send("eth_call", [
const rawCallResult = await (provider as any).send("eth_call", [
callData,
"latest",
stateOverrides,
]);
return BigInt(ret);
const decodedCallResult = routerSimulator.interface.decodeFunctionResult(
"simulateJamTx",
rawCallResult
);

return decodedCallResult.map((b) => BigInt(b));
} catch (e) {
console.error("Failed to simulate transaction");
throw e;
Expand Down
30 changes: 21 additions & 9 deletions core/tests/protocols/aave.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { test } from "vitest";
import { simulateSingleAssetToSingleAsset } from "./utils";
import { simulateRouterOperationHelper } from "./utils";

test("generateTransaction: USDC to aPolUSDC (aaveV3Deposit)", async () => {
await simulateSingleAssetToSingleAsset({
await simulateRouterOperationHelper({
chainId: 137,
inputAssetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
outputAssetId: "371b83f1-3301-4c69-b3ad-8d199c6d1774",
amountIn: "1000000000",
inputAllocation: [
{
assetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
amountStr: "1000000000",
},
],
outputAllocation: [
{ assetId: "371b83f1-3301-4c69-b3ad-8d199c6d1774", fraction: 1.0 },
],
});
});

test("generateTransaction: aPolUSDC (aaveV3Deposit) to USDC", async () => {
await simulateSingleAssetToSingleAsset({
await simulateRouterOperationHelper({
chainId: 137,
inputAssetId: "371b83f1-3301-4c69-b3ad-8d199c6d1774",
outputAssetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
amountIn: "1000000000",
inputAllocation: [
{
assetId: "371b83f1-3301-4c69-b3ad-8d199c6d1774",
amountStr: "1000000000",
},
],
outputAllocation: [
{ assetId: "e251ecf6-48c2-4538-afcd-fbb92424054d", fraction: 1.0 },
],
});
});
34 changes: 25 additions & 9 deletions core/tests/protocols/balancer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { test } from "vitest";
import { simulateSingleAssetToSingleAsset } from "./utils";
import { simulateRouterOperationHelper } from "./utils";

test("generateTransaction: USDC to jBRL/BRZ (balancerDeposit)", async () => {
await simulateSingleAssetToSingleAsset({
await simulateRouterOperationHelper({
chainId: 137,
inputAssetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
outputAssetId: "03f36f17-bbc2-4d8d-b0b2-9ce0f534d708",
amountIn: "1000000000",

inputAllocation: [
{
assetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
amountStr: "1000000000",
},
],
outputAllocation: [
{ assetId: "03f36f17-bbc2-4d8d-b0b2-9ce0f534d708", fraction: 1 },
],
});
});

test("generateTransaction: maticX/WMATIC (balancerDeposit) to USDC", async () => {
await simulateSingleAssetToSingleAsset({
await simulateRouterOperationHelper({
chainId: 137,
inputAssetId: "9b09afe5-c740-4cd7-a247-4fe4950a7f33",
outputAssetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
amountIn: "10000000000000000000000",
inputAllocation: [
{
assetId: "9b09afe5-c740-4cd7-a247-4fe4950a7f33",
amountStr: "10000000000000000000000",
},
],
outputAllocation: [
{
assetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
fraction: 1,
},
],
});
});
34 changes: 25 additions & 9 deletions core/tests/protocols/beefy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { test } from "vitest";
import { simulateSingleAssetToSingleAsset } from "./utils";
import { simulateRouterOperationHelper } from "./utils";

test("generateTransaction: USDC to QUICK (beefyDeposit)", async () => {
await simulateSingleAssetToSingleAsset({
await simulateRouterOperationHelper({
chainId: 137,
inputAssetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
outputAssetId: "37819023-9c6a-4848-8cf5-24a95350f001",
amountIn: "1000000000",
inputAllocation: [
{
assetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
amountStr: "1000000000",
},
],

outputAllocation: [
{
assetId: "37819023-9c6a-4848-8cf5-24a95350f001",
fraction: 1.0,
},
],
});
});

test("generateTransaction: beefy.finance (beefyDeposit) to USDC", async () => {
await simulateSingleAssetToSingleAsset({
await simulateRouterOperationHelper({
chainId: 137,
inputAssetId: "fecfd33d-e6a7-476b-89cb-910a0058fa48",
outputAssetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
amountIn: "1000000",
inputAllocation: [
{ assetId: "fecfd33d-e6a7-476b-89cb-910a0058fa48", amountStr: "1000000" },
],
outputAllocation: [
{
assetId: "e251ecf6-48c2-4538-afcd-fbb92424054d",
fraction: 1.0,
},
],
});
});
Loading

0 comments on commit 094fcb1

Please sign in to comment.