Skip to content

Commit

Permalink
fix: missing name in assets (#201)
Browse files Browse the repository at this point in the history
Fixes #155
  • Loading branch information
filo87 authored Jun 12, 2024
1 parent f4ccd62 commit 39f5dc3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
15 changes: 2 additions & 13 deletions src/mappings/handlers/loansHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ async function _handleLoanCreated(event: SubstrateEvent<LoanCreatedEvent>) {
}

await asset.updateAssetSpecs(assetSpecs)
await asset.updateIpfsAssetName().catch((err) => {
logger.error(`IPFS Request failed ${err}`)
return Promise.resolve()
})
await asset.updateItemMetadata()
await asset.updateIpfsAssetName()
await asset.save()

const epoch = await EpochService.getById(pool.id, pool.currentEpoch)
Expand Down Expand Up @@ -169,9 +167,6 @@ async function _handleLoanBorrowed(event: SubstrateEvent<LoanBorrowedEvent>): Pr
await epoch.increaseBorrowings(amount)
await epoch.save()
}

await asset.updateItemMetadata()
await asset.updateIpfsAssetName().catch((err) => logger.error(`IPFS Request failed ${err}`))
await asset.save()
}

Expand Down Expand Up @@ -248,7 +243,6 @@ async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
await epoch.save()
}

await asset.updateItemMetadata()
await asset.save()
}

Expand All @@ -259,7 +253,6 @@ async function _handleLoanWrittenOff(event: SubstrateEvent<LoanWrittenOffEvent>)
const { percentage, penalty } = status
const loan = await AssetService.getById(poolId.toString(), loanId.toString())
await loan.writeOff(percentage.toBigInt(), penalty.toBigInt())
await loan.updateItemMetadata()
await loan.save()

const pool = await PoolService.getById(poolId.toString())
Expand All @@ -281,7 +274,6 @@ async function _handleLoanClosed(event: SubstrateEvent<LoanClosedEvent>) {

const loan = await AssetService.getById(poolId.toString(), loanId.toString())
await loan.close()
await loan.updateItemMetadata()
await loan.save()

const epoch = await EpochService.getById(pool.id, pool.currentEpoch)
Expand Down Expand Up @@ -356,7 +348,6 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
)
await pool.increaseRealizedProfitFifo(realizedProfitFifo)
}
await fromAsset.updateIpfsAssetName()
await fromAsset.save()

await pool.increaseRepayments(repaidPrincipalAmount, repaidInterestAmount, repaidUnscheduledAmount)
Expand Down Expand Up @@ -403,7 +394,6 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
settlementPrice.toBigInt()
)
}
await toAsset.updateIpfsAssetName()
await toAsset.save()

await pool.increaseBorrowings(borrowPrincipalAmount)
Expand Down Expand Up @@ -494,7 +484,6 @@ async function _handleLoanDebtTransferred1024(event: SubstrateEvent<LoanDebtTran
await toAsset.activate()
await toAsset.updateExternalAssetPricingFromState()
await toAsset.borrow(amount)
await toAsset.updateIpfsAssetName()
await toAsset.save()

await pool.increaseBorrowings(amount)
Expand Down
32 changes: 18 additions & 14 deletions src/mappings/services/assetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ export class AssetService extends Asset {
nftClassId: bigint | undefined,
nftItemId: bigint | undefined,
timestamp: Date,
blockchain = '0',
name?: string
blockchain = '0'
) {
logger.info(`Initialising asset ${assetId} for pool ${poolId}`)
const isActive = false
Expand All @@ -34,8 +33,6 @@ export class AssetService extends Asset {
AssetStatus.CREATED
)

asset.name = name

asset.collateralNftClassId = nftClassId
asset.collateralNftItemId = nftItemId

Expand Down Expand Up @@ -67,17 +64,18 @@ export class AssetService extends Asset {
}

static initOnchainCash(poolId: string, timestamp: Date) {
return this.init(
const asset = this.init(
poolId,
ONCHAIN_CASH_ASSET_ID,
AssetType.OnchainCash,
AssetValuationMethod.Cash,
undefined,
undefined,
timestamp,
'0',
'Onchain reserve'
'0'
)
asset.name = 'Onchain reserve'
return asset
}

static async getById(poolId: string, assetId: string) {
Expand Down Expand Up @@ -174,7 +172,6 @@ export class AssetService extends Asset {

const payload = itemMetadata.unwrap()
this.metadata = payload.data.toUtf8()

return this
}

Expand All @@ -193,14 +190,21 @@ export class AssetService extends Asset {
return principal
}

public async updateIpfsAssetName(): Promise<string | null> {
public async updateIpfsAssetName(): Promise<void> {
logger.info(`Fetching IPFS asset name for asset ${this.id} `)
if (!this.metadata) return logger.warn('No IPFS metadata')
const metadata = await readIpfs<AssetIpfsMetadata>(this.metadata.match(cid)[0])
if (metadata?.name) {
this.name = metadata.name
if (!this.metadata){
logger.warn(`No metadata field set for asset ${this.id}`)
return
}
const metadata: AssetIpfsMetadata = await readIpfs<AssetIpfsMetadata>(this.metadata.match(cid)[0]).catch((err) => {
logger.error(`Request for metadata failed: ${err}`)
return undefined
})
if(!metadata) {
logger.error(`No http response from IPFS for asset id ${this.id} CID: ${this.metadata}`)
return
}
return metadata?.name ?? null
this.name = metadata.name
}

public isOffchainCash() {
Expand Down

0 comments on commit 39f5dc3

Please sign in to comment.