Skip to content

Commit

Permalink
add ManagerSaved event and onSave function to emit it (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinkline authored Sep 19, 2024
1 parent decd835 commit 93d8816
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 37 deletions.
29 changes: 29 additions & 0 deletions contracts/ContractManager.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ access(all) contract ContractManager {
access(all) let StoragePath: StoragePath
access(all) let PublicPath: PublicPath

access(all) let OwnerStoragePath: StoragePath
access(all) let OwnerPublicPath: PublicPath

access(all) entitlement Manage

access(all) event ManagerSaved(uuid: UInt64, contractAddress: Address, ownerAddress: Address)

access(all) resource Manager {
access(self) let acct: Capability<auth(Storage, Contracts, Keys, Inbox, Capabilities) &Account>
access(self) let routerCap: Capability<auth(FungibleTokenRouter.Owner) &FungibleTokenRouter.Router>
Expand Down Expand Up @@ -36,6 +41,26 @@ access(all) contract ContractManager {
return getAccount(self.acct.address)
}

// Should be called after saving a ContractManager resource to signal that a new address stores (and therefore "owns")
// this manager resource's acct capability. Without this, it is not possible to track the original creator of a contract
// when using the ContractManager
access(Manage) fun onSave() {
let acct = self.acct.borrow()!

acct.storage.load<Address>(from: ContractManager.OwnerStoragePath)
acct.storage.save(self.owner!.address, to: ContractManager.OwnerStoragePath)

if !acct.capabilities.get<&Address>(ContractManager.OwnerPublicPath).check() {
acct.capabilities.unpublish(ContractManager.OwnerPublicPath)
acct.capabilities.publish(
acct.capabilities.storage.issue<&Address>(ContractManager.OwnerStoragePath),
at: ContractManager.OwnerPublicPath
)
}

emit ManagerSaved(uuid: self.uuid, contractAddress: self.acct.address, ownerAddress: self.owner!.address)
}

init(tokens: @FlowToken.Vault, defaultRouterAddress: Address) {
pre {
tokens.balance >= 0.001: "minimum balance of 0.001 required for initialization"
Expand Down Expand Up @@ -69,5 +94,9 @@ access(all) contract ContractManager {
let identifier = "ContractManager_".concat(self.account.address.toString())
self.StoragePath = StoragePath(identifier: identifier)!
self.PublicPath = PublicPath(identifier: identifier)!

let ownerIdentifier = "ContractManager_Owner_".concat(self.account.address.toString())
self.OwnerStoragePath = StoragePath(identifier: ownerIdentifier)!
self.OwnerPublicPath = PublicPath(identifier: ownerIdentifier)!
}
}
4 changes: 3 additions & 1 deletion transactions/contract-manager/setup.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ transaction(flowTokenAmount: UFix64) {
let v = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)!
let tokens <- v.withdraw(amount: flowTokenAmount) as! @FlowToken.Vault

acct.storage.save(<- ContractManager.createManager(tokens: <-tokens), to: ContractManager.StoragePath)
acct.storage.save(<- ContractManager.createManager(tokens: <-tokens, defaultRouterAddress: acct.address), to: ContractManager.StoragePath)

acct.storage.borrow<auth(ContractManager.Manage) &ContractManager.Manager>(from: ContractManager.StoragePath)!.onSave()
}
}
34 changes: 0 additions & 34 deletions transactions/factory/create_open_edition_contract.cdc

This file was deleted.

5 changes: 3 additions & 2 deletions transactions/factory/create_open_edition_drop.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import "FlowtyAddressVerifiers"

transaction(contractName: String, managerInitialTokenBalance: UFix64, start: UInt64?, end: UInt64?, price: UFix64, paymentTokenType: String, phaseArgs: {String: String}, metadataArgs: {String: String}, collectionInfoArgs: {String: String}, dropDetailArgs: {String: String}) {
prepare(acct: auth(Storage, Capabilities) &Account) {
if acct.storage.borrow<&AnyResource>(from: ContractManager.StoragePath) == nil {
if acct.storage.type(at: ContractManager.StoragePath) == nil {
let v = acct.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)!
let tokens <- v.withdraw(amount: managerInitialTokenBalance) as! @FlowToken.Vault

acct.storage.save(<- ContractManager.createManager(tokens: <-tokens), to: ContractManager.StoragePath)
acct.storage.save(<- ContractManager.createManager(tokens: <-tokens, defaultRouterAddress: acct.address), to: ContractManager.StoragePath)
acct.storage.borrow<auth(ContractManager.Manage) &ContractManager.Manager>(from: ContractManager.StoragePath)!.onSave()

acct.capabilities.publish(
acct.capabilities.storage.issue<&ContractManager.Manager>(ContractManager.StoragePath),
Expand Down

0 comments on commit 93d8816

Please sign in to comment.