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

fix: missing name in assets #201

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
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
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 @@ -165,9 +163,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 @@ -239,7 +234,6 @@ async function _handleLoanRepaid(event: SubstrateEvent<LoanRepaidEvent>) {
await epoch.save()
}

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

Expand All @@ -250,7 +244,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 @@ -272,7 +265,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 @@ -342,7 +334,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 @@ -385,7 +376,6 @@ async function _handleLoanDebtTransferred(event: SubstrateEvent<LoanDebtTransfer
settlementPrice.toBigInt()
)
}
await toAsset.updateIpfsAssetName()
await toAsset.save()

await pool.increaseBorrowings(borrowPrincipalAmount)
Expand Down Expand Up @@ -476,7 +466,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 @@ -16,8 +16,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 @@ -32,8 +31,6 @@ export class AssetService extends Asset {
AssetStatus.CREATED
)

asset.name = name

asset.collateralNftClassId = nftClassId
asset.collateralNftItemId = nftItemId

Expand Down Expand Up @@ -65,17 +62,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 @@ -162,7 +160,6 @@ export class AssetService extends Asset {

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

return this
}

Expand All @@ -181,14 +178,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
Loading