PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 25, 2022

[FIXED] How do you make a resource self-destruct on flow / cadence?

 September 25, 2022     blockchain, cadence, onflow-cadence     No comments   

Issue

I would like for a resource to have a function where at the end of the function it self destructs by calling destroy self

pub resource SelfDestructingConsumable {
  pub fun consume() {
    // do some stuff
    destroy self
  }
}

The type checker complains that this is illegal

Cannot destroy self

I'm wondering if anyone has any creative ways around this? I have a feeling that maybe it should be wrapped in some sort of ConsumableCollection or ConsumableManager like so

pub resource ConsumableCollection {
  pub let consumables: @{UInt64: SelfDestructingConsumable}
  pub fun consume(id: UInt64) {
    let consumable <- consumables.remove(id)
    // do some stuff
    destroy consumable
  }
}

But i feel that it makes more sense to be able to just use a consumable and have it independently self destruct.

I'd like to get some thoughts or ideas on a better way to do this.


Solution

You do need to create a wrapper around it in order to destroy like you have suggested. Some use cases also do this in a transaction itself. It depend upon your usecase, would also strongly suggest emitting an Burned event or something when you do so as it is a good practise to always emit events on state changes.



Answered By - bjartek
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing