Skip to content

Commit

Permalink
update Burner for optional resources and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Mar 26, 2024
1 parent a31032a commit a684e5e
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 83 deletions.
8 changes: 7 additions & 1 deletion contracts/utility/Burner.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ access(all) contract Burner {
/// 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.
access(all) fun burn(_ r: @AnyResource) {
access(all) fun burn(_ toBurn: @AnyResource?) {
if toBurn == nil {
destroy toBurn
return
}
let r <- toBurn!

if let s <- r as? @{Burnable} {
s.burnCallback()
destroy s
Expand Down
148 changes: 72 additions & 76 deletions coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
"coverage": {
"./contracts/ExampleToken.cdc": {
"line_hits": {
"101": 1,
"102": 1,
"104": 1,
"101": 12,
"102": 12,
"104": 12,
"108": 1,
"112": 5,
"117": 0,
"118": 0,
"119": 0,
"123": 0,
"128": 0,
"142": 7,
"143": 7,
"142": 17,
"143": 17,
"156": 6,
"157": 6,
"158": 6,
Expand All @@ -34,20 +34,20 @@
"220": 4,
"222": 4,
"223": 4,
"26": 47,
"26": 50,
"28": 2,
"33": 4,
"39": 4,
"40": 4,
"51": 40,
"51": 43,
"58": 3,
"62": 0,
"66": 1,
"92": 21,
"93": 21,
"94": 21,
"95": 21,
"96": 21
"92": 31,
"93": 31,
"94": 31,
"95": 31,
"96": 31
},
"missed_lines": [
62,
Expand All @@ -63,24 +63,24 @@
},
"./contracts/FungibleToken.cdc": {
"line_hits": {
"100": 7,
"148": 1,
"151": 1,
"100": 17,
"148": 12,
"151": 12,
"153": 0,
"163": 0,
"164": 0,
"168": 0,
"174": 0,
"182": 8,
"186": 7,
"190": 15,
"182": 18,
"186": 17,
"190": 35,
"201": 6,
"203": 6,
"206": 18,
"215": 0,
"224": 9,
"225": 9,
"98": 7
"98": 17
},
"missed_lines": [
153,
Expand All @@ -99,14 +99,14 @@
"103": 2,
"104": 2,
"107": 0,
"145": 40,
"146": 40,
"148": 40,
"149": 40,
"150": 40,
"151": 40,
"152": 40,
"153": 40,
"145": 43,
"146": 43,
"148": 43,
"149": 43,
"150": 43,
"151": 43,
"152": 43,
"153": 43,
"163": 0,
"164": 0,
"165": 0,
Expand Down Expand Up @@ -247,38 +247,33 @@
},
"./contracts/utility/Burner.cdc": {
"line_hits": {
"24": 1,
"25": 1,
"26": 1,
"27": 0,
"28": 0,
"29": 0,
"30": 0,
"32": 0,
"33": 0,
"34": 0,
"35": 0,
"36": 0,
"37": 0,
"39": 0,
"41": 0
"24": 14,
"25": 0,
"26": 0,
"28": 14,
"30": 14,
"31": 12,
"32": 12,
"33": 2,
"34": 1,
"35": 5,
"36": 5,
"38": 1,
"39": 1,
"40": 1,
"41": 1,
"42": 5,
"43": 5,
"45": 1,
"47": 0
},
"missed_lines": [
27,
28,
29,
30,
32,
33,
34,
35,
36,
37,
39,
41
25,
26,
47
],
"statements": 15,
"percentage": "20.0%"
"statements": 19,
"percentage": "84.2%"
},
"./contracts/utility/MetadataViews.cdc": {
"line_hits": {
Expand Down Expand Up @@ -590,30 +585,31 @@
}
},
"excluded_locations": [
"A.0000000000000001.FlowDKG",
"A.0000000000000001.FlowIDTableStaking",
"A.0000000000000002.FungibleTokenSwitchboard",
"A.0000000000000003.FlowToken",
"A.0000000000000001.FlowEpoch",
"A.0000000000000001.NodeVersionBeacon",
"A.0000000000000001.StakingProxy",
"A.0000000000000002.FungibleToken",
"A.0000000000000001.LockedTokens",
"I.Test",
"A.0000000000000001.FlowClusterQC",
"A.0000000000000001.NonFungibleToken",
"A.0000000000000002.FungibleTokenMetadataViews",
"A.0000000000000001.FlowStakingCollection",
"I.Crypto",
"s.7465737400000000000000000000000000000000000000000000000000000000",
"A.0000000000000001.ExampleNFT",
"I.BlockchainHelpers",
"A.0000000000000001.MetadataViews",
"A.0000000000000001.FlowServiceAccount",
"A.0000000000000001.ViewResolver",
"A.0000000000000002.FungibleTokenMetadataViews",
"A.0000000000000001.RandomBeaconHistory",
"A.0000000000000001.FlowEpoch",
"A.0000000000000001.EVM",
"A.0000000000000004.FlowFees",
"A.0000000000000001.ExampleNFT",
"A.0000000000000001.FlowStorageFees"
"I.Crypto",
"A.0000000000000001.FlowServiceAccount",
"A.0000000000000003.FlowToken",
"A.0000000000000001.FlowStorageFees",
"A.0000000000000001.LockedTokens",
"A.0000000000000001.NonFungibleToken",
"A.0000000000000001.FlowDKG",
"A.0000000000000001.FlowIDTableStaking",
"A.0000000000000001.Burner",
"A.0000000000000001.ViewResolver",
"I.Test",
"A.0000000000000002.FungibleToken",
"A.0000000000000001.StakingProxy",
"A.0000000000000002.FungibleTokenSwitchboard",
"A.0000000000000001.FlowClusterQC",
"A.0000000000000001.NodeVersionBeacon",
"A.0000000000000001.MetadataViews",
"s.7465737400000000000000000000000000000000000000000000000000000000"
]
}
6 changes: 3 additions & 3 deletions lib/go/contracts/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 51 additions & 3 deletions tests/example_token_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fun testTransferTokenAmountGreaterThanBalance() {

access(all)
fun testBurnTokens() {
let txResult = executeTransaction(
var txResult = executeTransaction(
"../transactions/burn_tokens.cdc",
[50.0],
admin
Expand All @@ -172,15 +172,63 @@ fun testBurnTokens() {
Test.assertEqual(50.0, tokensBurnedEvent.amount)
Test.assertEqual("A.0000000000000007.ExampleToken.Vault", tokensBurnedEvent.type)

let scriptResult = executeScript(
var scriptResult = executeScript(
"../transactions/scripts/get_balance.cdc",
[admin.address]
)
Test.expect(scriptResult, Test.beSucceeded())

// The admin should now have the initial supply of 1000.0 tokens
let balance = scriptResult.returnValue! as! UFix64
var balance = scriptResult.returnValue! as! UFix64
Test.assertEqual(1000.0, balance)

txResult = executeTransaction(
"transactions/burn_array.cdc",
[10.0, 5],
admin
)
Test.expect(txResult, Test.beSucceeded())

scriptResult = executeScript(
"../transactions/scripts/get_supply.cdc",
[]
)
Test.expect(scriptResult, Test.beSucceeded())

var totalSupply = scriptResult.returnValue! as! UFix64
Test.assertEqual(1150.0, totalSupply)

txResult = executeTransaction(
"transactions/burn_dict.cdc",
[10.0, 5],
admin
)
Test.expect(txResult, Test.beSucceeded())

scriptResult = executeScript(
"../transactions/scripts/get_supply.cdc",
[]
)
Test.expect(scriptResult, Test.beSucceeded())

totalSupply = scriptResult.returnValue! as! UFix64
Test.assertEqual(1100.0, totalSupply)

txResult = executeTransaction(
"transactions/burn_optional.cdc",
[],
admin
)
Test.expect(txResult, Test.beSucceeded())

scriptResult = executeScript(
"../transactions/scripts/get_supply.cdc",
[]
)
Test.expect(scriptResult, Test.beSucceeded())

totalSupply = scriptResult.returnValue! as! UFix64
Test.assertEqual(200.0, totalSupply)
}

access(all)
Expand Down
37 changes: 37 additions & 0 deletions tests/transactions/burn_array.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import "FungibleToken"
import "ExampleToken"
import "FungibleTokenMetadataViews"
import "Burner"

/// This transaction is for testing burning an array of Vaults
///
transaction(amountPerIndex: UFix64, numIndicies: Int) {

/// Vault resource that holds the tokens that are being burned
let burnVaults: @[ExampleToken.Vault]

prepare(signer: auth(BorrowValue) &Account) {

let vaultData = ExampleToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("Could not get vault data view for the contract")

// Withdraw tokens from the signer's vault in storage
let sourceVault = signer.storage.borrow<auth(FungibleToken.Withdraw) &ExampleToken.Vault>(
from: vaultData.storagePath
) ?? panic("Could not borrow a reference to the signer's ExampleToken vault")

self.burnVaults <- []
var i = 0
while i < numIndicies {
let vault <- sourceVault.withdraw(amount: amountPerIndex) as! @ExampleToken.Vault
self.burnVaults.append(<-vault)
i = i + 1
}
}

execute {

Burner.burn(<-self.burnVaults)

}
}
Loading

0 comments on commit a684e5e

Please sign in to comment.