-
Notifications
You must be signed in to change notification settings - Fork 50
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
Add Burner contract to allow resource callbacks before destruction #407
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Burner is a contract that can facilitate the destruction of any resource on flow. | ||
// | ||
// Contributors | ||
// - Austin Kline - https://twitter.com/austin_flowty | ||
// - Deniz Edincik - https://twitter.com/bluesign | ||
// - Bastian Müller - https://twitter.com/turbolent | ||
pub contract Burner { | ||
// When Crescendo (Cadence 1.0) is released, custom destructors will be removed from cadece. | ||
// Burnable is an interface meant to replace this lost feature, allowing anyone to add a callback | ||
// method to ensure they do not destroy something which is not meant to be, or to add logic based on destruction | ||
// such as tracking the supply of an NFT Collection | ||
// | ||
// NOTE: The only way to see benefit from this interface is to call the burnCallback method yourself, | ||
// or to always use the burn method in this contract. Anyone who owns a resource can always elect **not** | ||
// to destroy a resource this way | ||
pub resource interface Burnable { | ||
pub fun burnCallback() | ||
} | ||
|
||
// burn is a global method which will destroy any resource it is given. | ||
// If the provided resource implements the Burnable interface, it will call the burnCallback | ||
// method and then destroy afterwards. | ||
pub fun burn(_ r: @AnyResource) { | ||
if let s <- r as? @{Burnable} { | ||
s.burnCallback() | ||
destroy s | ||
} else if let arr <- r as? @[AnyResource] { | ||
while arr.length > 0 { | ||
let item <- arr.removeFirst() | ||
self.burn(<-item) | ||
} | ||
destroy arr | ||
} else if let stringDict <- r as? @{String: AnyResource} { | ||
let keys = stringDict.keys | ||
while keys.length > 0 { | ||
let item <- stringDict.remove(key: keys.removeFirst())! | ||
self.burn(<-item) | ||
} | ||
destroy stringDict | ||
} else if let numDict <- r as? @{Number: AnyResource} { | ||
let keys = numDict.keys | ||
while keys.length > 0 { | ||
let item <- numDict.remove(key: keys.removeFirst())! | ||
self.burn(<-item) | ||
} | ||
destroy numDict | ||
} else if let typeDict <- r as? @{Type: AnyResource} { | ||
let keys = typeDict.keys | ||
while keys.length > 0 { | ||
let item <- typeDict.remove(key: keys.removeFirst())! | ||
self.burn(<-item) | ||
} | ||
destroy typeDict | ||
} else if let addressDict <- r as? @{Address: AnyResource} { | ||
let keys = addressDict.keys | ||
while keys.length > 0 { | ||
let item <- addressDict.remove(key: keys.removeFirst())! | ||
self.burn(<-item) | ||
} | ||
destroy addressDict | ||
} else if let pathDict <- r as? @{Path: AnyResource} { | ||
let keys = pathDict.keys | ||
while keys.length > 0 { | ||
let item <- pathDict.remove(key: keys.removeFirst())! | ||
self.burn(<-item) | ||
} | ||
destroy pathDict | ||
} else if let charDict <- r as? @{Character: AnyResource} { | ||
let keys = charDict.keys | ||
while keys.length > 0 { | ||
let item <- charDict.remove(key: keys.removeFirst())! | ||
self.burn(<-item) | ||
} | ||
destroy charDict | ||
} else { | ||
destroy r | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import "Burner" | ||
|
||
pub contract BurnableTest { | ||
pub var totalBurned: UInt64 | ||
|
||
pub resource WithCallback: Burner.Burnable { | ||
pub let allowDestroy: Bool | ||
|
||
pub fun burnCallback() { | ||
assert(self.allowDestroy, message: "allowDestroy must be set to true") | ||
BurnableTest.totalBurned = BurnableTest.totalBurned + 1 | ||
} | ||
|
||
init(_ allowDestroy: Bool) { | ||
self.allowDestroy = allowDestroy | ||
} | ||
} | ||
|
||
pub resource WithoutCallback {} | ||
|
||
pub fun createSafe(allowDestroy: Bool): @WithCallback { | ||
return <- create WithCallback(allowDestroy) | ||
} | ||
|
||
pub fun createUnsafe(): @WithoutCallback { | ||
return <- create WithoutCallback() | ||
} | ||
|
||
init() { | ||
self.totalBurned = 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import Test | ||
import BlockchainHelpers | ||
|
||
pub fun setup() { | ||
Test.deployContract(name: "Burner", path: "../contracts/Burner.cdc", arguments: []) | ||
Test.deployContract(name: "BurnableTest", path: "../contracts/testContracts/BurnableTest.cdc", arguments: []) | ||
} | ||
|
||
pub fun testWithCallbackDestory_Allowed() { | ||
let acct = Test.createAccount() | ||
txExecutor( | ||
"burner/create_and_destroy_with_callback.cdc", | ||
[acct], | ||
[true] | ||
) | ||
} | ||
|
||
pub fun testWithCallback_NotAllowed() { | ||
let acct = Test.createAccount() | ||
|
||
Test.expectFailure(fun() { | ||
txExecutor( | ||
"burner/create_and_destroy_with_callback.cdc", | ||
[acct], | ||
[false] | ||
) | ||
}, errorMessageSubstring: "allowDestroy must be set to true") | ||
} | ||
|
||
pub fun testWithoutCallbackDestroy_Allowed() { | ||
let acct = Test.createAccount() | ||
txExecutor( | ||
"burner/create_and_destroy_without_callback.cdc", | ||
[acct], | ||
[] | ||
) | ||
} | ||
|
||
pub fun testDestroy_Dict() { | ||
let acct = Test.createAccount() | ||
|
||
let types = [Type<Address>(), Type<String>(), Type<CapabilityPath>(), Type<Number>(), Type<Type>(), Type<Character>()] | ||
for type in types { | ||
txExecutor( | ||
"burner/create_and_destroy_dict.cdc", | ||
[acct], | ||
[true, type] | ||
) | ||
} | ||
} | ||
|
||
pub fun testDestroy_Dict_NotAllowed() { | ||
let acct = Test.createAccount() | ||
|
||
let types = [Type<Address>(), Type<String>(), Type<CapabilityPath>(), Type<Number>(), Type<Type>(), Type<Character>()] | ||
for type in types { | ||
Test.expectFailure(fun() { | ||
txExecutor( | ||
"burner/create_and_destroy_dict.cdc", | ||
[acct], | ||
[false, type] | ||
) | ||
}, errorMessageSubstring: "allowDestroy must be set to true") | ||
} | ||
} | ||
|
||
pub fun testDestroy_Array() { | ||
let acct = Test.createAccount() | ||
txExecutor( | ||
"burner/create_and_destroy_array.cdc", | ||
[acct], | ||
[true] | ||
) | ||
} | ||
|
||
pub fun loadCode(_ fileName: String, _ baseDirectory: String): String { | ||
return Test.readFile("./".concat(baseDirectory).concat("/").concat(fileName)) | ||
} | ||
|
||
pub fun txExecutor(_ txName: String, _ signers: [Test.Account], _ arguments: [AnyStruct]): Test.TransactionResult { | ||
let txCode = loadCode(txName, "transactions") | ||
|
||
let authorizers: [Address] = [] | ||
for signer in signers { | ||
authorizers.append(signer.address) | ||
} | ||
let tx = Test.Transaction( | ||
code: txCode, | ||
authorizers: authorizers, | ||
signers: signers, | ||
arguments: arguments, | ||
) | ||
let txResult = Test.executeTransaction(tx) | ||
if let err = txResult.error { | ||
panic(err.message) | ||
} | ||
return txResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import "BurnableTest" | ||
import "Burner" | ||
|
||
transaction(allowDestroy: Bool) { | ||
prepare(acct: AuthAccount) { | ||
let before = BurnableTest.totalBurned | ||
|
||
let r <- BurnableTest.createSafe(allowDestroy: allowDestroy) | ||
Burner.burn(<- [<- r]) | ||
|
||
if allowDestroy { | ||
assert(before + 1 == BurnableTest.totalBurned, message: "totalBurned was lower than expected") | ||
} else { | ||
assert(before == BurnableTest.totalBurned, message: "totalBurned value changed unexpectedly") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import "BurnableTest" | ||
import "Burner" | ||
|
||
transaction(allowDestroy: Bool, dictType: Type) { | ||
prepare(acct: AuthAccount) { | ||
let before = BurnableTest.totalBurned | ||
|
||
let r <- BurnableTest.createSafe(allowDestroy: allowDestroy) | ||
if dictType as? Number != nil { | ||
let d: @{Number: AnyResource} <- {1: <-r} | ||
Burner.burn(<-d) | ||
} else if dictType as? String != nil { | ||
let d: @{String: AnyResource} <- {"a": <-r} | ||
Burner.burn(<-d) | ||
} else if dictType as? Path != nil { | ||
let d: @{Path: AnyResource} <- {/public/foo: <-r} | ||
Burner.burn(<-d) | ||
} else if dictType as? Address != nil { | ||
let d: @{Address: AnyResource} <- {Address(0x1): <-r} | ||
Burner.burn(<-d) | ||
} else if dictType as? Character != nil { | ||
let d: @{Character: AnyResource} <- {"c": <-r} | ||
Burner.burn(<-d) | ||
} else { | ||
let d: @{Type: AnyResource} <- {Type<Burner>(): <-r} | ||
Burner.burn(<-d) | ||
} | ||
|
||
if allowDestroy { | ||
assert(before + 1 == BurnableTest.totalBurned, message: "totalBurned was lower than expected") | ||
} else { | ||
assert(before == BurnableTest.totalBurned, message: "totalBurned value changed unexpectedly") | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/transactions/burner/create_and_destroy_with_callback.cdc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import "BurnableTest" | ||
import "Burner" | ||
|
||
transaction(allowDestroy: Bool) { | ||
prepare(acct: AuthAccount) { | ||
let before = BurnableTest.totalBurned | ||
|
||
let r <- BurnableTest.createSafe(allowDestroy: allowDestroy) | ||
Burner.burn(<- r) | ||
|
||
if allowDestroy { | ||
assert(before + 1 == BurnableTest.totalBurned, message: "totalBurned was lower than expected") | ||
} else { | ||
assert(before == BurnableTest.totalBurned, message: "totalBurned value changed unexpectedly") | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this method is public and we use it to emit the
FungibleToken.Burned
event, wouldn't that be able to be spoofed or spammed since anyone can call it from their resource? I propose making thisaccess(contract)
so that only theburn
function in this contract can call it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wait, is that even possible? If I make an
access(contract)
method in an interface, does that mean that only the contract the defines the resource interface implementation can access it, or does it mean theBurner
contract can access it?