Skip to content

Commit

Permalink
Add dependency audit contract
Browse files Browse the repository at this point in the history
  • Loading branch information
janezpodhostnik committed May 15, 2024
1 parent 272df6c commit 43a6787
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions contracts/DependencyAudit.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import "MigrationContractStaging"

access(all) contract DependencyAudit {
// The system addresses have contracts that will not be stages via the migration contract so we exclude them from the dependency chekcs
access(self) excludedAddresses: {Address: Bool}

access(all) event UnstagedDependencies(dependenciesAddresses: [Address], dependenciesNames: [String])

// checkDependencies is called from the FlowServiceAccount contract
access(all) fun checkDependencies(_ dependenciesAddresses: [Address], _ dependenciesNames: [String], _ authorizers: [Address]) {
var numDependencies = dependenciesAddresses.length
var i = 0

var unstagedDependenciesAddresses: [Address] = []
var unstagedDependenciesNames: [String] = []

while i < numDependencies {
i = i + 1

let isExcluded = DependencyAudit.excludedAddresses[dependenciesAddresses[i]] ?? false
if isExcluded {
continue
}

let staged = MigrationContractStaging.isStaged(address: dependenciesAddresses[i], name: dependenciesNames[i])
if !staged {
unstagedDependenciesAddresses.append(dependenciesAddresses[i])
unstagedDependenciesNames.append(dependenciesNames[i])
}
}

if unstagedDependenciesAddresses.length > 0 {
emit UnstagedDependencies(dependenciesAddresses: unstagedDependenciesAddresses, dependenciesNames: unstagedDependenciesNames)
// or alternatively panic("Found unstaged dependencies: ... ")
}
}

access(all) resource Administrator {
access(all) fun addExcludedAddresses(addresses: [Address]) {
for address in addresses {
DependencyAudit.excludedAddresses[address] = true
}
}

access(all) fun removeExcludedAddresses(addresses: [Address]) {
for address in addresses {
DependencyAudit.excludedAddresses.remove(key: address)
}
}
}

init(excludedAddresses: [Address]) {
self.excludedAddresses = {}

for address in excludedAddresses {
self.excludedAddresses[address] = true
}

let admin <- create Administrator()
self.account.save(<-admin, to: /storage/flowDependencyAuditAdmin)
}
}

0 comments on commit 43a6787

Please sign in to comment.