Skip to content

Commit

Permalink
add otherAsIfs to make simulation extendable
Browse files Browse the repository at this point in the history
  • Loading branch information
novaliu86 committed Jul 4, 2024
1 parent ddd729f commit 2a37c00
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/ethers-override/permit2AsIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ export class Permit2AsIf extends AddressAsIf {
}
};

export const permit2 = (permit2Address: string): Permit2AsIf => new Permit2AsIf(permit2Address);
export const PERMIT2_ADDRESS = "0x000000000022d473030f116ddee9f6b43ac78ba3";
export const permit2 = (): Permit2AsIf => new Permit2AsIf(PERMIT2_ADDRESS);
12 changes: 9 additions & 3 deletions src/simulation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BlockTag } from "ethers";
import { Interface, JsonRpcProvider, toQuantity } from "ethers";
import { LedgerState, permit2, addressAt, tokenAt } from "./ethers-override/index.js";
import { LedgerState, permit2, addressAt, tokenAt, PERMIT2_ADDRESS, AddressAsIf } from "./ethers-override/index.js";

// @ts-ignore
import erc20 from './abi/erc20.json' assert { type: "json" };
Expand Down Expand Up @@ -56,6 +56,7 @@ export class SettlementSimulation {
wrapInput: boolean,
upwrapOutput: boolean,
calldata: string,
otherAsIfs: AddressAsIf [] = [],
): Promise<{ gas: bigint, amountOut: bigint, }> {
if (this._provider === undefined) {
throw new Error(`No provider is connected with SettlementSimulation.`);
Expand Down Expand Up @@ -92,9 +93,9 @@ export class SettlementSimulation {
.is(amountIn + 1n)
);

if (tokenProxyAddress !== undefined) {
if (tokenProxyAddress !== undefined && tokenProxyAddress.toLowerCase() === PERMIT2_ADDRESS) {
stateBuilder.asif(
permit2(tokenProxyAddress)
permit2()
.allowance(senderAddress, inputTokenAddress, routerAddress)
.is({
nonce: 1n,
Expand All @@ -104,6 +105,11 @@ export class SettlementSimulation {
);
}
}

otherAsIfs.forEach(a => {
stateBuilder.asif(a);
});

const [ blockTag, override ] = await stateBuilder.getStateAsync();

let ethAmountToSend = 0n;
Expand Down

0 comments on commit 2a37c00

Please sign in to comment.