Skip to content

Commit ba8ab20

Browse files
sameh-faroukclaude
andcommitted
fix: resolve logic issues in contract and farm mapping handlers
L1: Contract state GracePeriod fell through to OutOfFunds default in updateNodeContract/updateNameContract. Extract parseContractState() helper handling all 4 __kind variants (Created, Deleted, GracePeriod, OutOfFunds) across all spec versions. L2: nodeContractCanceled used get() instead of find() for PublicIp, only freeing the first IP on multi-IP contracts. Remaining IPs were orphaned with stale contractId references. Now uses find() to reset all matching IPs. L3: contractUpdated ignored RentContract entirely and never updated solutionProviderID for any contract type. Add updateRentContract() handler and solutionProviderID updates for v105+ events. L4: farmUpdated used unsafe cast (item.event.args as Farm) to read dedicatedFarm, which fails for v9 events where the field doesn't exist. Replace with version-aware access via the already-parsed event and remove the redundant second save call. Refs: #210 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d5deadd commit ba8ab20

2 files changed

Lines changed: 48 additions & 28 deletions

File tree

src/mappings/contracts.ts

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ export async function contractUpdated(
199199
if (SavedNameContract) {
200200
await updateNameContract(ctx, contractEvent, SavedNameContract, ctx.store)
201201
}
202+
203+
const SavedRentContract = await ctx.store.get(RentContract, { where: { contractID: contractEvent.contractId } })
204+
if (SavedRentContract) {
205+
await updateRentContract(ctx, contractEvent, SavedRentContract, ctx.store)
206+
}
207+
}
208+
209+
function parseContractState(kind: string): ContractState {
210+
switch (kind) {
211+
case 'Created': return ContractState.Created
212+
case 'Deleted': return ContractState.Deleted
213+
case 'GracePeriod': return ContractState.GracePeriod
214+
case 'OutOfFunds': return ContractState.OutOfFunds
215+
default: return ContractState.OutOfFunds
216+
}
202217
}
203218

204219
async function updateNodeContract(ctx: Ctx, ctr: any, contract: NodeContract, store: Store) {
@@ -215,16 +230,10 @@ async function updateNodeContract(ctx: Ctx, ctr: any, contract: NodeContract, st
215230
contract.deploymentData = validateString(ctx, parsedNodeContract.deploymentData.toString())
216231
contract.deploymentHash = validateString(ctx, parsedNodeContract.deploymentHash.toString())
217232

218-
let state = ContractState.OutOfFunds
219-
switch (ctr.state.__kind) {
220-
case 'Created':
221-
state = ContractState.Created
222-
break
223-
case 'Deleted':
224-
state = ContractState.Deleted
225-
break
233+
contract.state = parseContractState(ctr.state.__kind)
234+
if (ctr.solutionProviderId !== undefined) {
235+
contract.solutionProviderID = Number(ctr.solutionProviderId) || 0
226236
}
227-
contract.state = state
228237
await store.save<NodeContract>(contract)
229238
}
230239

@@ -238,19 +247,30 @@ async function updateNameContract(ctx: Ctx, ctr: any, contract: NameContract, st
238247
contract.twinID = ctr.twinId
239248
contract.name = validateString(ctx, parsedNameContract.name.toString())
240249

241-
let state = ContractState.OutOfFunds
242-
switch (ctr.state.__kind) {
243-
case 'Created':
244-
state = ContractState.Created
245-
break
246-
case 'Deleted':
247-
state = ContractState.Deleted
248-
break
250+
contract.state = parseContractState(ctr.state.__kind)
251+
if (ctr.solutionProviderId !== undefined) {
252+
contract.solutionProviderID = Number(ctr.solutionProviderId) || 0
249253
}
250-
contract.state = state
251254
await store.save<NameContract>(contract)
252255
}
253256

257+
async function updateRentContract(ctx: Ctx, ctr: any, contract: RentContract, store: Store) {
258+
if (ctr.contractType.__kind !== "RentContract") return
259+
260+
const parsedRentContract = ctr.contractType.value
261+
262+
contract.contractID = ctr.contractId
263+
contract.gridVersion = ctr.version
264+
contract.twinID = ctr.twinId
265+
contract.nodeID = parsedRentContract.nodeId
266+
267+
contract.state = parseContractState(ctr.state.__kind)
268+
if (ctr.solutionProviderId !== undefined) {
269+
contract.solutionProviderID = Number(ctr.solutionProviderId) || 0
270+
}
271+
await store.save<RentContract>(contract)
272+
}
273+
254274
export async function nodeContractCanceled(
255275
ctx: Ctx,
256276
item: EventItem<'SmartContractModule.NodeContractCanceled', { event: { args: true } }>,
@@ -272,10 +292,12 @@ export async function nodeContractCanceled(
272292
savedContract.state = ContractState.Deleted
273293
await ctx.store.save<NodeContract>(savedContract)
274294

275-
const savedPublicIP = await ctx.store.get(PublicIp, { where: { contractId: contractID }, relations: { farm: true } })
276-
if (savedPublicIP) {
277-
savedPublicIP.contractId = BigInt(0)
278-
await ctx.store.save<PublicIp>(savedPublicIP)
295+
const savedPublicIPs = await ctx.store.find(PublicIp, { where: { contractId: contractID }, relations: { farm: true } })
296+
for (const ip of savedPublicIPs) {
297+
ip.contractId = BigInt(0)
298+
}
299+
if (savedPublicIPs.length > 0) {
300+
await ctx.store.save(savedPublicIPs)
279301
}
280302
}
281303

src/mappings/farms.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ export async function farmUpdated(
200200
}
201201
}
202202

203+
if ('dedicatedFarm' in farmUpdatedEventParsed) {
204+
savedFarm.dedicatedFarm = (farmUpdatedEventParsed as any).dedicatedFarm
205+
}
206+
203207
await ctx.store.save<Farm>(savedFarm)
204208

205209
const publicIPsOfFarm = await ctx.store.find<PublicIp>(PublicIp, { where: { farm: { id: savedFarm.id } }, relations: { farm: true } })
@@ -211,12 +215,6 @@ export async function farmUpdated(
211215
}
212216
}
213217

214-
let farm = item.event.args as Farm
215-
if (farm.dedicatedFarm) {
216-
savedFarm.dedicatedFarm = farm.dedicatedFarm
217-
}
218-
await ctx.store.save<Farm>(savedFarm)
219-
220218
}
221219

222220
export async function farmDeleted(

0 commit comments

Comments
 (0)