Skip to content

Commit

Permalink
make the new zapper contract a feature flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jankjr committed Jan 23, 2025
1 parent a200016 commit 001555b
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 62 deletions.
11 changes: 11 additions & 0 deletions src.ts/Universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,9 +749,15 @@ export class Universe<const UniverseConf extends Config = Config> {
return [...out]
}
get execAddress() {
if (this.config.useNewZapperContract && this.config.addresses.executorAddress2 != null) {
return this.config.addresses.executorAddress2
}
return this.config.addresses.executorAddress
}
get zapperAddress() {
if (this.config.useNewZapperContract && this.config.addresses.zapper2Address != null) {
return this.config.addresses.zapper2Address
}
return this.config.addresses.zapperAddress
}

Expand Down Expand Up @@ -1097,6 +1103,11 @@ export class Universe<const UniverseConf extends Config = Config> {
config: DeployFolioConfig | DeployFolioConfigJson,
opts?: ToTransactionArgs,
) {
if (this.config.useNewZapperContract === false) {
throw new Error(
`To enable deployZap, you must set useNewZapperContract to true in your config`
)
}
if (!(config instanceof DeployFolioConfig)) {
config = await DeployFolioConfig.create(this, config)
}
Expand Down
2 changes: 1 addition & 1 deletion src.ts/action/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const plannerUtils = {
amount: gen.Value,
destination: Address
) {
if (destination == universe.config.addresses.executorAddress) {
if (destination == universe.execAddress) {
return
}
plannerUtils.erc20.transfer(universe, planner, amount, token, destination)
Expand Down
4 changes: 2 additions & 2 deletions src.ts/action/ERC4626.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ export const ERC4626WithdrawAction = (proto: string) =>
planner.add(
lib.redeem(
inputBal,
this.universe.config.addresses.executorAddress.address,
this.universe.config.addresses.executorAddress.address
this.universe.execAddress.address,
this.universe.execAddress.address
)
)
return this.outputBalanceOf(this.universe, planner)
Expand Down
8 changes: 7 additions & 1 deletion src.ts/configuration/ChainConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const defaultSearcherOptions = {
minimiseDustPhase1Steps: 10,
minimiseDustPhase2Steps: 5,

cacheResolution: 4
cacheResolution: 4,

// Use new contract for all zaps
useNewZapperContract: false
}
export const getDefaultSearcherOptions = () => {
return defaultSearcherOptions
Expand Down Expand Up @@ -76,8 +79,10 @@ export const makeConfig = <
facadeAddress: string
oldFacadeAddress: string
executorAddress: string
executorAddress2?: string
emitId: string
zapperAddress: string
zapper2Address?: string
wrappedNative: string
rtokenLens: string

Expand All @@ -89,6 +94,7 @@ export const makeConfig = <
curveStableSwapNGHelper: string
curveCryptoFactoryHelper: string


usdc: string
},
options: {
Expand Down
6 changes: 4 additions & 2 deletions src.ts/configuration/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ export const baseConfig = makeConfig(
emitId: contractAddress('EmitId'),
facadeAddress: '0xEb2071e9B542555E90E6e4E1F83fa17423583991',
oldFacadeAddress: '0xe1aa15DA8b993c6312BAeD91E0b470AE405F91BF',
zapperAddress: contractAddress('Zapper2'),
executorAddress: contractAddress('ZapperExecutor'),
zapperAddress: '0xaa560d5c2fade67cf6836ab793e56a79f09d4282',
zapper2Address: contractAddress('Zapper2'),
executorAddress: '0x560740052f380669c90811f711f80b21306d4713',
executorAddress2: contractAddress('ZapperExecutor'),
wrappedNative: '0x4200000000000000000000000000000000000006',
rtokenLens: contractAddress('RTokenLens'),

Expand Down
42 changes: 36 additions & 6 deletions src.ts/searcher/ToTransactionArgs.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { Token, TokenQuantity } from '../entities/Token'
import { Planner } from '../tx-gen/Planner'
import { Zapper2__factory } from '../contracts'
import { Zapper2__factory, Zapper__factory } from '../contracts'
import { parseHexStringIntoBuffer } from '../base/utils'
import { Address } from '../base/Address'
import { Universe } from '../Universe'
import { TransactionRequest } from '@ethersproject/providers'
import { ZapParamsStruct } from '../contracts/contracts/Zapper2'
import { ZapERC20ParamsStruct } from '../contracts/contracts/Zapper'

export type ToTransactionArgs = Partial<{
recipient?: Address
dustRecipient?: Address
}>

export const encodeProgramToZapERC20Params = (
export const encodeZapParamsStruct = (
planner: Planner,
input: TokenQuantity,
outputToken: Address,
Expand All @@ -32,16 +33,45 @@ export const encodeProgramToZapERC20Params = (
recipient: recipient.address,
}
}
export const encodeZapERC20ParamsStruct = (
planner: Planner,
input: TokenQuantity,
outputToken: Address,
minOutput: bigint,
tokens: Token[]
): ZapERC20ParamsStruct => {
const plan = planner.plan()
return {
tokenIn: input.token.address.address,
amountIn: input.amount,
commands: plan.commands,
state: plan.state,
amountOut: minOutput,
tokenOut: outputToken.address,
tokens: tokens.map((i) => i.address.address),
}
}

const zapperInterface = Zapper2__factory.createInterface()
const zapperInterface = Zapper__factory.createInterface()
const zapper2Interface = Zapper2__factory.createInterface()

export const encodeCalldata = (
export const encodeZapper2Calldata = (
payload: ZapParamsStruct,
options: { isDeployZap: boolean }
) => {
const data = options.isDeployZap
? zapperInterface.encodeFunctionData('zapDeploy', [payload])
: zapperInterface.encodeFunctionData('zap', [payload])
? zapper2Interface.encodeFunctionData('zapDeploy', [payload])
: zapper2Interface.encodeFunctionData('zap', [payload])

return data
}
export const encodeZapperCalldata = (
payload: ZapERC20ParamsStruct,
options: { ethInput: boolean }
) => {
const data = options.ethInput
? zapperInterface.encodeFunctionData('zapETH', [payload])
: zapperInterface.encodeFunctionData('zapERC20', [payload])

return data
}
Expand Down
1 change: 1 addition & 0 deletions src.ts/searcher/TokenFlowGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,7 @@ const optimise = async (
}

const dustFraction = bestSoFar.result.dustValue / bestSoFar.result.totalValue
console.log('dustFraction', dustFraction)
if (dustFraction > maxDustFraction) {
console.log(bestSoFar.result.outputs.join(', '))
console.log(bestSoFar.result.dustValue)
Expand Down
95 changes: 54 additions & 41 deletions src.ts/searcher/TxGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import {
Value,
} from '../tx-gen/Planner'
import {
encodeCalldata,
encodeProgramToZapERC20Params,
encodeZapper2Calldata,
encodeZapERC20ParamsStruct,
encodeZapParamsStruct,
encodeZapperCalldata,
} from './ToTransactionArgs'
import { Universe } from '../Universe'
import { SimulateParams } from '../configuration/ZapSimulation'
Expand All @@ -25,6 +27,8 @@ import { NodeProxy, TFGResult } from './TokenFlowGraph'
import { Approval } from '../base/Approval'
import { constants } from 'ethers/lib/ethers'
import { DeployFolioConfig } from '../action/DeployFolioConfig'
import { ZapParamsStruct } from '../contracts/contracts/Zapper2'
import { ZapERC20ParamsStruct } from '../contracts/contracts/Zapper'

const iface = Zapper__factory.createInterface()
const simulateAndParse = async (
Expand Down Expand Up @@ -72,37 +76,56 @@ const evaluateProgram = async (
) => {
const outputTokenAddress =
outputToken instanceof Address ? outputToken : outputToken.address
// console.log(printPlan(planner, universe).join('\n'))
const tx = encodeProgramToZapERC20Params(
planner,
inputs[0],
outputTokenAddress,
minOutput,
dustTokens,
opts.recipient
)
let data: string
let params: ZapParamsStruct | ZapERC20ParamsStruct
if (universe.config.useNewZapperContract) {
const p = encodeZapParamsStruct(
planner,
inputs[0],
outputTokenAddress,
minOutput,
dustTokens,
opts.recipient
)
params = p
data = encodeZapper2Calldata(p, {
isDeployZap: opts.deployFolio != null,
})
} else {
const p = encodeZapERC20ParamsStruct(
planner,
inputs[0],
outputTokenAddress,
minOutput,
dustTokens
)
params = p
data = encodeZapperCalldata(p, {
ethInput: opts.ethereumInput,
})
}

let to: Address
if (
opts.deployFolio ||
(universe.config.useNewZapperContract &&
opts.deployFolio == null &&
universe.config.addresses.zapper2Address != null)
) {
if (universe.config.addresses.zapper2Address == null) {
throw new Error('Zapper2 address not set, cannot generate deployZap')
}
to = universe.config.addresses.zapper2Address
} else {
to = universe.config.addresses.zapperAddress
}

// console.log(
// JSON.stringify({
// amountIn: tx.amountIn.toString(),
// amountOut: tx.amountOut.toString(),
// tokens: tx.tokens.map((t) => t.toString()).join(', '),
// tokenIn: tx.tokenIn.toString(),
// tokenOut: tx.tokenOut.toString(),
// state: tx.state.join('\n'),
// commands: tx.commands.map((c) => c.toString()).join('\n'),
// }),
// JSON.stringify(opts)
// )
const data = encodeCalldata(tx, {
isDeployZap: opts.deployFolio != null,
})
let value = 0n
if (opts.ethereumInput) {
value = inputs[0].amount
}
const simulationPayload = {
to: universe.config.addresses.zapperAddress.address,
to: to.address,
from: signer.address,
data,
value,
Expand All @@ -112,19 +135,9 @@ const evaluateProgram = async (
},
}

// console.log(
// JSON.stringify(
// {
// block: universe.currentBlock,
// to: simulationPayload.to.toString(),
// from: simulationPayload.from.toString(),
// data: simulationPayload.data.toString(),
// value: simulationPayload.value.toString(),
// },
// null,
// 2
// )
// )
console.log('to', to.address)
console.log('data', data)

try {
return {
res: await simulateAndParse(universe, simulationPayload, dustTokens),
Expand All @@ -135,7 +148,7 @@ const evaluateProgram = async (
data,
value,
},
params: tx,
params,
},
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion src.ts/tx-gen/Planner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ const formatAddress = (address: string, universe: Universe): string => {
if (universe.tokens.has(addr)) {
return universe.tokens.get(addr)!.symbol
}
if (universe.config.addresses.executorAddress === addr) {
if (universe.execAddress === addr) {
return 'this'
}
const addrObj = Address.from(
Expand Down
20 changes: 12 additions & 8 deletions test/integration/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
} from '../createActionTestCase'
import { createZapTestCase } from '../createZapTestCase'
import { DefaultMap } from '../../src.ts/base/DefaultMap'
import { getDefaultSearcherOptions } from '../../src.ts/configuration/ChainConfiguration'
import {
getDefaultSearcherOptions,
SearcherOptions,
} from '../../src.ts/configuration/ChainConfiguration'
import { getProvider } from './providerUtils'
import { ONE } from '../../src.ts/action/Action'
dotenv.config()
Expand All @@ -26,12 +29,13 @@ if (process.env.BASE_PROVIDER == null) {
process.exit(0)
}

const searcherOptions = {
const searcherOptions: SearcherOptions = {
...getDefaultSearcherOptions(),
optimisationSteps: 15,
minimiseDustPhase1Steps: 5,
minimiseDustPhase2Steps: 15,
cacheResolution: 4,
useNewZapperContract: true,
}

/** !!
Expand Down Expand Up @@ -100,14 +104,14 @@ const testUser = Address.from(
process.env.TEST_USER ?? '0xF2d98377d80DADf725bFb97E91357F1d81384De2'
)
const issueanceCases = [
makeTestCase(10000, t.USDC, rTokens.bsd),
makeTestCase(100, t.WETH, rTokens.bsd),
makeTestCase(10000, t.USDC, rTokens.hyUSD),
makeTestCase(10000, t.USDbC, rTokens.hyUSD),
makeTestCase(10000, t.DAI, rTokens.hyUSD),
// makeTestCase(10000, t.USDC, rTokens.bsd),
// makeTestCase(100, t.WETH, rTokens.bsd),
// makeTestCase(10000, t.USDC, rTokens.hyUSD),
// makeTestCase(10000, t.USDbC, rTokens.hyUSD),
// makeTestCase(10000, t.DAI, rTokens.hyUSD),
makeTestCase(5, t.WETH, rTokens.hyUSD),

makeTestCase(50, t.WETH, rTokens.BSDX),
// makeTestCase(50, t.WETH, rTokens.BSDX),
]

const redeemCases = [
Expand Down

0 comments on commit 001555b

Please sign in to comment.