From b1024b3ff27294d705bddff32d826019146122d3 Mon Sep 17 00:00:00 2001 From: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com> Date: Mon, 29 Jan 2024 10:49:43 -0600 Subject: [PATCH] remove MigrationContractStaging Host path derivation with constant --- README.md | 11 +++++----- contracts/MigrationContractStaging.cdc | 22 +++++-------------- .../stage_contract.cdc | 7 +++--- .../unstage_contract.cdc | 3 +-- 4 files changed, 15 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index dd2c393..9f65650 100644 --- a/README.md +++ b/README.md @@ -65,12 +65,11 @@ transaction(contractName: String, contractCode: String) { prepare(signer: AuthAccount) { // Configure Host resource if needed - let hostStoragePath: StoragePath = MigrationContractStaging.deriveHostStoragePath(hostAddress: signer.address) - if signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath) == nil { - signer.save(<-MigrationContractStaging.createHost(), to: hostStoragePath) + if signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath) == nil { + signer.save(<-MigrationContractStaging.createHost(), to: MigrationContractStaging.HostStoragePath) } // Assign Host reference - self.host = signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath)! + self.host = signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath)! } execute { @@ -126,7 +125,7 @@ The basic interface to stage a contract is the same as deploying a contract - na `stageContract()` again for the same contract will overwrite any existing staged code for that contract. ```cadence -/// 1 - Create a host and save it in your contract-hosting account at the path derived from deriveHostStoragePath(). +/// 1 - Create a host and save it in your contract-hosting account at MigrationContractStaging.HostStoragePath access(all) fun createHost(): @Host /// 2 - Call stageContract() with the host reference and contract name and contract code you wish to stage. access(all) fun stageContract(host: &Host, name: String, code: String) @@ -181,7 +180,7 @@ access(all) resource Capsule { } ``` -To support ongoing staging progress across the network, the single `StagingStatusUpdated` event is emitted any time a +To support monitoring staging progress across the network, the single `StagingStatusUpdated` event is emitted any time a contract is staged (`status == true`), staged code is replaced (`status == false`), or a contract is unstaged (`status == nil`). diff --git a/contracts/MigrationContractStaging.cdc b/contracts/MigrationContractStaging.cdc index dc99b8b..4cc3de0 100644 --- a/contracts/MigrationContractStaging.cdc +++ b/contracts/MigrationContractStaging.cdc @@ -12,11 +12,11 @@ /// access(all) contract MigrationContractStaging { - // Path derivation constants + // Path constants // access(self) let delimiter: String - access(self) let hostPathPrefix: String access(self) let capsulePathPrefix: String + access(all) let HostStoragePath: StoragePath /// Maps contract addresses to an array of staged contract names access(self) let stagedContracts: {Address: [String]} @@ -37,7 +37,7 @@ access(all) contract MigrationContractStaging { /* --- Staging Methods --- */ - /// 1 - Create a host and save it in your contract-hosting account at the path derived from deriveHostStoragePath() + /// 1 - Create a host and save it in your contract-hosting account at MigrationContractStaging.HostStoragePath /// /// Creates a Host serving as identification for the contract account. Reference to this resource identifies the /// calling address so it must be saved in account storage before being used to stage a contract update. @@ -147,16 +147,6 @@ access(all) contract MigrationContractStaging { return stagedCode } - /// Returns a StoragePath to store the Host of the form: - /// /storage/self.hostPathPrefix_ADDRESS - access(all) fun deriveHostStoragePath(hostAddress: Address): StoragePath { - return StoragePath( - identifier: self.hostPathPrefix - .concat(self.delimiter) - .concat(hostAddress.toString()) - ) ?? panic("Could not derive Host StoragePath for given address") - } - /// Returns a StoragePath to store the Capsule of the form: /// /storage/self.capsulePathPrefix_ADDRESS_NAME access(all) fun deriveCapsuleStoragePath(contractAddress: Address, contractName: String): StoragePath { @@ -325,9 +315,9 @@ access(all) contract MigrationContractStaging { init() { self.delimiter = "_" - self.hostPathPrefix = "MigrationContractStagingHost".concat(self.delimiter) - .concat(self.delimiter) - .concat(self.account.address.toString()) + self.HostStoragePath = StoragePath( + identifier: "MigrationContractStagingHost".concat(self.delimiter).concat(self.account.address.toString()) + ) ?? panic("Could not derive Host StoragePath") self.capsulePathPrefix = "MigrationContractStagingCapsule" .concat(self.delimiter) .concat(self.account.address.toString()) diff --git a/transactions/migration-contract-staging/stage_contract.cdc b/transactions/migration-contract-staging/stage_contract.cdc index 222e4e8..69bf685 100644 --- a/transactions/migration-contract-staging/stage_contract.cdc +++ b/transactions/migration-contract-staging/stage_contract.cdc @@ -15,12 +15,11 @@ transaction(contractName: String, contractCode: String) { prepare(signer: AuthAccount) { // Configure Host resource if needed - let hostStoragePath: StoragePath = MigrationContractStaging.deriveHostStoragePath(hostAddress: signer.address) - if signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath) == nil { - signer.save(<-MigrationContractStaging.createHost(), to: hostStoragePath) + if signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath) == nil { + signer.save(<-MigrationContractStaging.createHost(), to: MigrationContractStaging.HostStoragePath) } // Assign Host reference - self.host = signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath)! + self.host = signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath)! } execute { diff --git a/transactions/migration-contract-staging/unstage_contract.cdc b/transactions/migration-contract-staging/unstage_contract.cdc index d7d2fda..7e8e065 100644 --- a/transactions/migration-contract-staging/unstage_contract.cdc +++ b/transactions/migration-contract-staging/unstage_contract.cdc @@ -10,8 +10,7 @@ transaction(contractName: String) { prepare(signer: AuthAccount) { // Assign Host reference - let hostStoragePath: StoragePath = MigrationContractStaging.deriveHostStoragePath(hostAddress: signer.address) - self.host = signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath) + self.host = signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath) ?? panic("Host was not found in storage") }