@@ -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
204219async 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+
254274export 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
0 commit comments