Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable router on redemptions if not already done #2573

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 34 additions & 14 deletions centrifuge-js/src/modules/liquidityPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BN from 'bn.js'
import type { TransactionRequest, TransactionResponse } from 'ethers'
import { Contract, Interface, Provider, ethers, isAddress as isEvmAddress } from 'ethers'
import set from 'lodash/set'
import { combineLatestWith, firstValueFrom, from, map, startWith, switchMap } from 'rxjs'
import { combineLatestWith, firstValueFrom, from, map, of, startWith, switchMap } from 'rxjs'
import { Centrifuge } from '../Centrifuge'
import { TransactionOptions } from '../types'
import { addressToHex } from '../utils'
Expand Down Expand Up @@ -315,6 +315,22 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
)
}

function enableRouter(args: [centrifugeRouter: string, estimate: BN], options: TransactionRequest = {}) {
const [centrifugeRouter, estimate] = args

const getIsRouterEnabled = from(contract(centrifugeRouter, new Interface(ABI.CentrifugeRouter)).isEnabled())
return getIsRouterEnabled.pipe(
map((isRouterEnabled) => {
if (isRouterEnabled) {
return of(null)
}
return pending(
contract(centrifugeRouter, new Interface(ABI.CentrifugeRouter)).enable(centrifugeRouter, estimate, options)
)
})
)
}

function increaseRedeemOrder(
args: [lpAddress: string, order: BN, chainId: number],
options: TransactionRequest = {}
Expand All @@ -323,19 +339,23 @@ export function getLiquidityPoolsModule(inst: Centrifuge) {
const user = inst.getSignerAddress('evm')
return centrifugeRouter(chainId).pipe(
switchMap(({ estimate, centrifugeRouter }) => {
return pending(
contract(centrifugeRouter, new Interface(ABI.CentrifugeRouter)).requestRedeem(
lpAddress,
order.toString(),
user,
user,
estimate,
{
...options,
gasLimit: 300000,
value: estimate,
}
)
return enableRouter([centrifugeRouter, estimate], options).pipe(
switchMap(() => {
return pending(
contract(centrifugeRouter, new Interface(ABI.CentrifugeRouter)).requestRedeem(
lpAddress,
order.toString(),
user,
user,
estimate,
{
...options,
gasLimit: 300000,
value: estimate,
}
)
)
})
)
})
)
Expand Down
Loading