Smart Contract
Burner
A.f233dcee88fe0abe.Burner
1/// Burner is a contract that can facilitate the destruction of any resource on flow.
2///
3/// Contributors
4/// - Austin Kline - https://twitter.com/austin_flowty
5/// - Deniz Edincik - https://twitter.com/bluesign
6/// - Bastian Müller - https://twitter.com/turbolent
7access(all) contract Burner {
8 /// When Crescendo (Cadence 1.0) is released, custom destructors will be removed from cadece.
9 /// Burnable is an interface meant to replace this lost feature, allowing anyone to add a callback
10 /// method to ensure they do not destroy something which is not meant to be,
11 /// or to add logic based on destruction such as tracking the supply of a FT Collection
12 ///
13 /// NOTE: The only way to see benefit from this interface
14 /// is to always use the burn method in this contract. Anyone who owns a resource can always elect **not**
15 /// to destroy a resource this way
16 access(all) resource interface Burnable {
17 access(contract) fun burnCallback()
18 }
19
20 /// burn is a global method which will destroy any resource it is given.
21 /// If the provided resource implements the Burnable interface,
22 /// it will call the burnCallback method and then destroy afterwards.
23 access(all) fun burn(_ toBurn: @AnyResource?) {
24 if toBurn == nil {
25 destroy toBurn
26 return
27 }
28 let r <- toBurn!
29
30 if let s <- r as? @{Burnable} {
31 s.burnCallback()
32 destroy s
33 } else if let arr <- r as? @[AnyResource] {
34 while arr.length > 0 {
35 let item <- arr.removeFirst()
36 self.burn(<-item)
37 }
38 destroy arr
39 } else if let dict <- r as? @{HashableStruct: AnyResource} {
40 let keys = dict.keys
41 while keys.length > 0 {
42 let item <- dict.remove(key: keys.removeFirst())!
43 self.burn(<-item)
44 }
45 destroy dict
46 } else {
47 destroy r
48 }
49 }
50}