Skip to content

Commit

Permalink
fix issues with base mints and redeems
Browse files Browse the repository at this point in the history
  • Loading branch information
jankjr committed Jan 29, 2025
1 parent 691b49f commit 5ef6888
Show file tree
Hide file tree
Showing 19 changed files with 1,078 additions and 894 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src.ts/Universe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -897,13 +897,13 @@ export class Universe<const UniverseConf extends Config = Config> {

this.weirollZapperExec = Contract.createLibrary(
ZapperExecutor__factory.connect(
this.config.addresses.executorAddress.address,
this.execAddress.address,
this.provider
)
)
this.weirollZapperExecContract = Contract.createContract(
ZapperExecutor__factory.connect(
this.config.addresses.executorAddress.address,
this.execAddress.address,
this.provider
)
)
Expand Down
10 changes: 10 additions & 0 deletions src.ts/action/Aerodrome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ class AeropoolAddLiquidity extends BaseV2AerodromeAction {
public constructor(public readonly pool: AerodromeStablePool) {
super(pool, [pool.token0, pool.token1], [pool.lpToken])
}

public async inputProportions(): Promise<TokenQuantity[]> {
const prices = await Promise.all(
(
await this.pool.quoteRemoveLiquidity(this.pool.lpToken.one)
).map((i) => i.price().then((i) => i.asNumber()))
)
const sum = prices.reduce((a, b) => a + b, 0)
return prices.map((i, index) => this.inputToken[index].from(i / sum))
}
}

class AeropoolRemoveLiquidity extends BaseV2AerodromeAction {
Expand Down
53 changes: 32 additions & 21 deletions src.ts/action/Lido.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,21 @@ export class LidoDeployment {
)
),
}
const burnRate = universe.createCachedProducer(async () => {
return await this.quoteBurn_(wsteth.one)
}, 12000)
const mintRate = universe.createCachedProducer(async () => {
return await this.quoteMint_(steth.one)
}, 12000)
this.rateFn = async (qty) => {
if (qty.token === wsteth) {
return (await burnRate()).mul(qty.into(steth))
return this.quoteBurn_(qty)
} else {
return (await mintRate()).mul(qty.into(wsteth))
return this.quoteMint_(qty)
}
}

const wrap = new STETHToWSTETH(this)
const unwrap = new WSTETHToSTETH(this)
const stake = wrapGasToken(this.universe, new ETHToSTETH(this))
universe.defineMintable(wrap, unwrap, true)
universe.addAction(stake, steth.address)
universe.defineMintable(wrap, unwrap)
universe.addAction(stake)
universe.addAction(unwrap)
universe.addAction(wrap)
universe.mintableTokens.set(steth, stake)

this.actions = {
Expand Down Expand Up @@ -154,7 +150,7 @@ abstract class BaseLidoAction extends Action('Lido') {
__: TokenQuantity[]
) {
const input = inputs[0]
const out = planner.add(this.planAction(input))
const out = this.planAction(planner, input)
if (out == null) {
throw new Error('Failed to plan action')
}
Expand All @@ -165,7 +161,7 @@ abstract class BaseLidoAction extends Action('Lido') {
return [await this.quoteAction(amountsIn)]
}

abstract planAction(inputs: gen.Value): gen.FunctionCall
abstract planAction(planner: gen.Planner, inputs: gen.Value): gen.Value
abstract quoteAction(inputs: TokenQuantity): Promise<TokenQuantity>

toString(): string {
Expand Down Expand Up @@ -215,10 +211,19 @@ class ETHToSTETH extends BaseStETHAction {
get oneUsePrZap() {
return false
}
planAction(input: gen.Value) {
return this.lido.weiroll.stethInstance
.submit(constants.AddressZero)
.withValue(input)
planAction(planner: gen.Planner, input: gen.Value) {
planner.add(
this.lido.weiroll.stethInstance
.submit(constants.AddressZero)
.withValue(input)
)
return this.genUtils.sub(
this.lido.universe,
planner,
input,
1n,
'ETHToSTETH'
)
}
constructor(readonly lido: LidoDeployment) {
super(lido, lido.universe.nativeToken, lido.steth)
Expand All @@ -242,7 +247,6 @@ abstract class BaseWSTETHAction extends BaseLidoAction {
return BigInt(175000n)
}

abstract planAction(inputs: gen.Value): gen.FunctionCall
abstract quoteAction(inputs: TokenQuantity): Promise<TokenQuantity>

async quote([amountsIn]: TokenQuantity[]): Promise<TokenQuantity[]> {
Expand Down Expand Up @@ -270,8 +274,11 @@ class STETHToWSTETH extends BaseWSTETHAction {
get actionName() {
return 'wrap'
}
planAction(input: gen.Value) {
return this.lido.weiroll.wstethInstance.wrap(input)
planAction(planner: gen.Planner, input: gen.Value) {
return planner.add(this.lido.weiroll.wstethInstance.wrap(input))!
}
get dependsOnRpc() {
return true
}
async quoteAction(amountsIn: TokenQuantity): Promise<TokenQuantity> {
return await this.lido.quoteWrap(amountsIn)
Expand All @@ -285,8 +292,12 @@ class WSTETHToSTETH extends BaseWSTETHAction {
get actionName() {
return 'unwrap'
}
planAction(input: gen.Value) {
return this.lido.weiroll.wstethInstance.unwrap(input)

get dependsOnRpc() {
return true
}
planAction(planner: gen.Planner, input: gen.Value) {
return planner.add(this.lido.weiroll.wstethInstance.unwrap(input))!
}
async quoteAction(amountsIn: TokenQuantity): Promise<TokenQuantity> {
return await this.lido.quoteUnwrap(amountsIn)
Expand Down
54 changes: 46 additions & 8 deletions src.ts/action/RTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
} from '../contracts'
import { Contract, Planner, Value } from '../tx-gen/Planner'
import { MultiInputUnit } from './MultiInputAction'
import { BlockCache } from '../base/BlockBasedCache'
import { DefaultMap } from '../base/DefaultMap'

export class RTokenDeployment {
public readonly burn: BurnRTokenAction
Expand Down Expand Up @@ -205,6 +207,15 @@ export class RTokenDeployment {
}

const ONE = 10n ** 18n

const quoteRedeem = async ([input]: TokenQuantity[]) => {
const unit = await unitBasket()
const out = unit.map((qty) =>
qty.token.from((qty.amount * input.amount) / ONE)
)
return out
}

const quoteMint = async (input: TokenQuantity[]) => {
if (input.some((i) => i.amount === 0n)) {
return [this.rToken.zero, ...input]
Expand All @@ -227,13 +238,27 @@ export class RTokenDeployment {
return [this.rToken.from(baskets), ...dust]
}

const quoteRedeem = async ([input]: TokenQuantity[]) => {
const unit = await unitBasket()
const out = unit.map((qty) =>
qty.token.from((qty.amount * input.amount) / ONE)
)
return out
}
// const quoteMint = async (input: TokenQuantity[]) => {
// const out = await this.contracts.facade.callStatic.maxIssuableByAmounts(
// this.rToken.address.address,
// input.map((i) => i.amount)
// )

// const unitsMinted = [this.rToken.from(out)]
// const inputConsumed = await this.burn.quote(unitsMinted)
// const outputs = [
// this.rToken.from(out),
// ...input.map((i, index) => {
// const remaining = i.sub(inputConsumed[index])
// if (remaining.amount <= 0n) {
// return this.universe.usd.zero
// }
// return remaining
// }),
// ]
// console.log(`${input.join(', ')} -> ${outputs.join(', ')}`)
// return outputs
// }

this.supply = getSupply

Expand Down Expand Up @@ -417,8 +442,21 @@ export class MintRTokenAction extends ReserveRTokenBase {
gasEstimate() {
return this.rTokenDeployment.mintEstimate
}
private lastBlock = 0
private cache = new Map<string, Promise<TokenQuantity[]>>()
async quote(amountsIn: TokenQuantity[]): Promise<TokenQuantity[]> {
return [(await this.rTokenDeployment.quoteMint(amountsIn))[0]]
if (this.lastBlock !== this.universe.currentBlock) {
this.lastBlock = this.universe.currentBlock
this.cache.clear()
}
const key = amountsIn.map((i) => i.amount).join(',')
let out = this.cache.get(key)
if (out == null) {
out = this.rTokenDeployment.quoteMint(amountsIn)
this.cache.set(key, out)
}

return await out
}
async quoteWithDust(amountsIn: TokenQuantity[]) {
const [rTokenQty, ...dust] = await this.rTokenDeployment.quoteMint(
Expand Down
Loading

0 comments on commit 5ef6888

Please sign in to comment.