Skip to content

Commit

Permalink
Fix typescript errors in tinlake
Browse files Browse the repository at this point in the history
  • Loading branch information
sophialittlejohn committed Sep 16, 2024
1 parent 42ebed4 commit 3126dc5
Showing 1 changed file with 28 additions and 56 deletions.
84 changes: 28 additions & 56 deletions centrifuge-js/src/modules/tinlake.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import BN from 'bn.js'
import { Contract, TransactionRequest, TransactionResponse } from 'ethers'
import { Contract, Interface, TransactionRequest, TransactionResponse } from 'ethers'
import { from, map, startWith, switchMap } from 'rxjs'
import { Centrifuge } from '../Centrifuge'
import { TransactionOptions } from '../types'
Expand Down Expand Up @@ -68,10 +68,10 @@ export function getTinlakeModule(inst: Centrifuge) {
if (!inst.config.evmSigner) throw new Error('Needs signer')
if (!abi) throw new Error('ABI not found')
if (!contracts[contractAddress]) {
contracts[contractAddress] = new Contract(contractAddress, abi)
contracts[contractAddress] = new Contract(contractAddress, new Interface(abi))
}

return contracts[contractAddress].connect(inst.config.evmSigner)
return contracts[contractAddress].connect(inst.config.evmSigner) as Contract
}

function pending(txPromise: Promise<TransactionResponse>) {
Expand All @@ -93,7 +93,6 @@ export function getTinlakeModule(inst: Centrifuge) {
) {
const [tranche] = args
return pending(
// @ts-ignore
contract(contractAddresses, contractVersions, 'TINLAKE_CURRENCY').approve(
contractAddresses[tranche === 'junior' ? 'JUNIOR_TRANCHE' : 'SENIOR_TRANCHE'],
maxUint256,
Expand All @@ -110,7 +109,6 @@ export function getTinlakeModule(inst: Centrifuge) {
) {
const [tranche] = args
return pending(
// @ts-ignore
contract(contractAddresses, contractVersions, tranche === 'junior' ? 'JUNIOR_TOKEN' : 'SENIOR_TOKEN').approve(
contractAddresses[tranche === 'junior' ? 'JUNIOR_TRANCHE' : 'SENIOR_TRANCHE'],
maxUint256,
Expand All @@ -127,12 +125,10 @@ export function getTinlakeModule(inst: Centrifuge) {
) {
const [tranche, order] = args
return pending(
// @ts-ignore
contract(
contractAddresses,
contractVersions,
tranche === 'junior' ? 'JUNIOR_OPERATOR' : 'SENIOR_OPERATOR'
// @ts-ignore
).supplyOrder(order.toString(), options)
)
}
Expand All @@ -145,12 +141,10 @@ export function getTinlakeModule(inst: Centrifuge) {
) {
const [tranche, order] = args
return pending(
// @ts-ignore
contract(
contractAddresses,
contractVersions,
tranche === 'junior' ? 'JUNIOR_OPERATOR' : 'SENIOR_OPERATOR'
// @ts-ignore
).redeemOrder(order.toString(), options)
)
}
Expand All @@ -163,7 +157,6 @@ export function getTinlakeModule(inst: Centrifuge) {
) {
const [tranche] = args
return pending(
// @ts-ignore
contract(contractAddresses, contractVersions, tranche === 'junior' ? 'JUNIOR_OPERATOR' : 'SENIOR_OPERATOR')[
'disburse()'
](options)
Expand All @@ -187,37 +180,27 @@ export function getTinlakeModule(inst: Centrifuge) {
const reserve = toBN(
await (beforeClosing
? isMakerIntegrated
? // @ts-ignore
contract(contractAddresses, contractVersions, 'ASSESSOR').totalBalance()
: // @ts-ignore
contract(contractAddresses, contractVersions, 'RESERVE').totalBalance()
: // @ts-ignore
coordinator.epochReserve())
? contract(contractAddresses, contractVersions, 'ASSESSOR').totalBalance()
: contract(contractAddresses, contractVersions, 'RESERVE').totalBalance()
: coordinator.epochReserve())
)
const netAssetValue = toBN(
await (beforeClosing
? contractVersions?.FEED === 2
? // @ts-ignore
feed.latestNAV()
: // @ts-ignore
feed.approximatedNAV()
: // @ts-ignore
coordinator.epochNAV())
? feed.latestNAV()
: feed.approximatedNAV()
: coordinator.epochNAV())
)
const seniorAsset = beforeClosing
? isMakerIntegrated
? // @ts-ignore
toBN(await assessor.seniorDebt()).add(toBN(await assessor.seniorBalance()))
: // @ts-ignore
toBN(await assessor.seniorDebt_()).add(toBN(await assessor.seniorBalance_()))
: // @ts-ignore
toBN(await coordinator.epochSeniorAsset())

// @ts-ignore
? toBN(await assessor.seniorDebt()).add(toBN(await assessor.seniorBalance()))
: toBN(await assessor.seniorDebt_()).add(toBN(await assessor.seniorBalance_()))
: toBN(await coordinator.epochSeniorAsset())

const minDropRatio = toBN(await assessor.minSeniorRatio())
// @ts-ignore

const maxDropRatio = toBN(await assessor.maxSeniorRatio())
// @ts-ignore

const maxReserve = toBN(await assessor.maxReserve())

return { reserve, netAssetValue, seniorAsset, minDropRatio, maxDropRatio, maxReserve }
Expand All @@ -238,36 +221,32 @@ export function getTinlakeModule(inst: Centrifuge) {
const assessor = contract(contractAddresses, contractVersions, 'ASSESSOR')
const feed = contract(contractAddresses, contractVersions, 'FEED')

// @ts-ignore
const epochNAV = toBN(await feed.currentNAV())
// @ts-ignore

const epochReserve = toBN(await contract(contractAddresses, contractVersions, 'RESERVE').totalBalance())
const epochSeniorTokenPrice = toBN(
// @ts-ignore
await assessor['calcSeniorTokenPrice(uint256,uint256)'](epochNAV.toString(), epochReserve.toString())
)
const epochJuniorTokenPrice = toBN(
// @ts-ignore
await assessor['calcJuniorTokenPrice(uint256,uint256)'](epochNAV.toString(), epochReserve.toString())
)

return {
// @ts-ignore
dropInvest: toBN(await seniorTranche.totalSupply()),
// @ts-ignore

dropRedeem: toBN(await seniorTranche.totalRedeem())
.mul(epochSeniorTokenPrice)
.div(e27),
// @ts-ignore

tinInvest: toBN(await juniorTranche.totalSupply()),
// @ts-ignore

tinRedeem: toBN(await juniorTranche.totalRedeem())
.mul(epochJuniorTokenPrice)
.div(e27),
}
}
const coordinator = contract(contractAddresses, contractVersions, 'COORDINATOR')
// @ts-ignore

const orderState = await coordinator.order()

return {
Expand All @@ -285,15 +264,14 @@ export function getTinlakeModule(inst: Centrifuge) {
const coordinator = contract(contractAddresses, contractVersions, 'COORDINATOR')

return {
// @ts-ignore
dropInvest: toBN(await coordinator.weightSeniorSupply()),
// @ts-ignore

dropRedeem: toBN(await coordinator.weightSeniorRedeem()),
// @ts-ignore

tinInvest: toBN(await coordinator.weightJuniorSupply()),
// @ts-ignore

tinRedeem: toBN(await coordinator.weightJuniorRedeem()),
// @ts-ignore

seniorAsset: toBN(await coordinator.weightSeniorAsset()),
}
}
Expand All @@ -305,7 +283,7 @@ export function getTinlakeModule(inst: Centrifuge) {
options: TransactionRequest = {}
) {
const coordinator = contract(contractAddresses, contractVersions, 'COORDINATOR')
// @ts-ignore

return pending(coordinator.closeEpoch({ ...options, gasLimit: 5000000 }))
}

Expand All @@ -317,7 +295,7 @@ export function getTinlakeModule(inst: Centrifuge) {
) {
const submissionTx = (async () => {
const coordinator = contract(contractAddresses, contractVersions, 'COORDINATOR')
// @ts-ignore

if ((await coordinator.submissionPeriod()) !== true) throw new Error('Not in submission period')
const state = await getEpochState(contractAddresses, contractVersions, [])
const orders = await getOrders(contractAddresses, contractVersions, [])
Expand All @@ -327,7 +305,6 @@ export function getTinlakeModule(inst: Centrifuge) {
throw new Error('Failed to find a solution')
}

// @ts-ignore
return coordinator.submitSolution(
solution.dropRedeem.toString(),
solution.tinRedeem.toString(),
Expand Down Expand Up @@ -368,7 +345,6 @@ export function getTinlakeModule(inst: Centrifuge) {
throw new Error('Current epoch is still in the challenge period')
}

// @ts-ignore
return coordinator.executeEpoch({ ...options, gasLimit: 2000000 })
})()
return pending(tx)
Expand All @@ -386,23 +362,21 @@ export function getTinlakeModule(inst: Centrifuge) {
contractVersions: TinlakeContractVersions | undefined
) {
const coordinator = contract(contractAddresses, contractVersions, 'COORDINATOR')
// @ts-ignore

const minChallengePeriodEnd = toBN(await coordinator.minChallengePeriodEnd()).toNumber()
const latestBlockTimestamp = await getLatestBlockTimestamp()
if (minChallengePeriodEnd !== 0) {
if (minChallengePeriodEnd < latestBlockTimestamp) return 'challenge-period-ended'
return 'in-challenge-period'
}

// @ts-ignore
const submissionPeriod = await coordinator.submissionPeriod()
if (submissionPeriod === true) {
return 'in-submission-period'
}

// @ts-ignore
const lastEpochClosed = toBN(await coordinator.lastEpochClosed()).toNumber()
// @ts-ignore

const minimumEpochTime = toBN(await coordinator.minimumEpochTime()).toNumber()
if (submissionPeriod === false) {
if (lastEpochClosed + minimumEpochTime < latestBlockTimestamp) return 'can-be-closed'
Expand All @@ -425,7 +399,6 @@ export function getTinlakeModule(inst: Centrifuge) {
const [address] = args
const coordinator = contract(contractAddresses, contractVersions, 'CLAIM_CFG')

// @ts-ignore
const tx = coordinator.accounts(address, options)
return pending(tx)
}
Expand All @@ -439,7 +412,6 @@ export function getTinlakeModule(inst: Centrifuge) {
const [centAddress] = args
const coordinator = contract(contractAddresses, contractVersions, 'CLAIM_CFG')

// @ts-ignore
const tx = coordinator.update(centAddress, options)
return pending(tx)
}
Expand Down

0 comments on commit 3126dc5

Please sign in to comment.