From 64de9533215027d99b5f230865aeb276df8678fe Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Thu, 5 Dec 2024 00:53:59 +0400 Subject: [PATCH] Restructure repo files and CI --- .github/workflows/cadence_lint.yml | 29 +++++++++-- .gitignore | 4 +- cadence/contract.cdc | 47 ------------------ cadence/contracts/Recipe.cdc | 48 +++++++++++++++++++ cadence/tests/Recipe_test.cdc | 6 +++ .../using_admin_resource.cdc} | 0 emulator-account.pkey | 1 + flow.json | 22 +++++++-- 8 files changed, 102 insertions(+), 55 deletions(-) delete mode 100644 cadence/contract.cdc create mode 100644 cadence/contracts/Recipe.cdc create mode 100644 cadence/tests/Recipe_test.cdc rename cadence/{transaction.cdc => transactions/using_admin_resource.cdc} (100%) create mode 100644 emulator-account.pkey diff --git a/.github/workflows/cadence_lint.yml b/.github/workflows/cadence_lint.yml index 58565d0..1100626 100644 --- a/.github/workflows/cadence_lint.yml +++ b/.github/workflows/cadence_lint.yml @@ -1,4 +1,4 @@ -name: Run Cadence Lint +name: Run Cadence Contract Compilation, Deployment, Transaction Execution, and Lint on: push jobs: @@ -9,7 +9,7 @@ jobs: uses: actions/checkout@v3 with: submodules: 'true' - + - name: Install Flow CLI run: | brew update @@ -23,8 +23,29 @@ jobs: else echo "Flow project already initialized." fi + flow dependencies install + + - name: Start Flow Emulator + run: | + echo "Starting Flow emulator in the background..." + nohup flow emulator start > emulator.log 2>&1 & + sleep 5 # Wait for the emulator to start + flow project deploy --network=emulator # Deploy the recipe contracts indicated in flow.json + + - name: Run All Transactions + run: | + echo "Running all transactions in the transactions folder..." + for file in ./cadence/transactions/*.cdc; do + echo "Running transaction: $file" + TRANSACTION_OUTPUT=$(flow transactions send "$file" --signer emulator-account) + echo "$TRANSACTION_OUTPUT" + if echo "$TRANSACTION_OUTPUT" | grep -q "Transaction Error"; then + echo "Transaction Error detected in $file, failing the action..." + exit 1 + fi + done - name: Run Cadence Lint run: | - echo "Running Cadence linter on all .cdc files in the current repository" - flow cadence lint **/*.cdc + echo "Running Cadence linter on .cdc files in the current repository" + flow cadence lint ./cadence/**/*.cdc diff --git a/.gitignore b/.gitignore index 496ee2c..b1d92af 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.DS_Store \ No newline at end of file +.DS_Store +/imports/ +/.idea/ \ No newline at end of file diff --git a/cadence/contract.cdc b/cadence/contract.cdc deleted file mode 100644 index 8844033..0000000 --- a/cadence/contract.cdc +++ /dev/null @@ -1,47 +0,0 @@ -// Above is more code from the SetAndSeries Contract... -access(all) resource Admin { - - access(all) fun addSeries(seriesId: UInt32, metadata: {String: String}) { - pre { - SetAndSeries.series[seriesId] == nil: - "Cannot add Series: The Series already exists" - } - - // Create the new Series - let newSeries <- create Series( - seriesId: seriesId, - metadata: metadata - ) - - // Add the new Series resource to the Series dictionary in the contract - SetAndSeries.series[seriesId] <-! newSeries - } - - access(all) fun borrowSeries(seriesId: UInt32): &Series { - pre { - SetAndSeries.series[seriesId] != nil: - "Cannot borrow Series: The Series does not exist" - } - - // Get a reference to the Series and return it - return &SetAndSeries.series[seriesId] as &Series - } - - access(all) fun createNewAdmin(): @Admin { - return <-create Admin() - } -} - -... - -// Code used in init() at the end of the contract - -// Save Admin resource in storage -self.account.storage.save(<-create Admin(), to: self.AdminStoragePath) - -// Publish a capability for the Admin resource -let adminCapability = self.account.capabilities.storage.issue<&SetAndSeries.Admin>( - from: self.AdminStoragePath -) -self.account.capabilities.publish(adminCapability, at: self.AdminPrivatePath) - ?? panic("Could not get a capability to the admin") diff --git a/cadence/contracts/Recipe.cdc b/cadence/contracts/Recipe.cdc new file mode 100644 index 0000000..4904728 --- /dev/null +++ b/cadence/contracts/Recipe.cdc @@ -0,0 +1,48 @@ +access(all) contract Recipe { + // Above is more code from the SetAndSeries Contract... + access(all) resource Admin { + + access(all) fun addSeries(seriesId: UInt32, metadata: {String: String}) { + pre { + SetAndSeries.series[seriesId] == nil: + "Cannot add Series: The Series already exists" + } + + // Create the new Series + let newSeries <- create Series( + seriesId: seriesId, + metadata: metadata + ) + + // Add the new Series resource to the Series dictionary in the contract + SetAndSeries.series[seriesId] <-! newSeries + } + + access(all) fun borrowSeries(seriesId: UInt32): &Series { + pre { + SetAndSeries.series[seriesId] != nil: + "Cannot borrow Series: The Series does not exist" + } + + // Get a reference to the Series and return it + return &SetAndSeries.series[seriesId] as &Series + } + + access(all) fun createNewAdmin(): @Admin { + return <-create Admin() + } + } + + // Add the init() function to properly initialize the contract + init() { + // Save Admin resource in storage + self.account.storage.save(<-create Admin(), to: self.AdminStoragePath) + + // Publish a capability for the Admin resource + let adminCapability = self.account.capabilities.storage.issue<&SetAndSeries.Admin>( + from: self.AdminStoragePath + ) + self.account.capabilities.publish(adminCapability, at: self.AdminPrivatePath) + ?? panic("Could not get a capability to the admin") + } +} diff --git a/cadence/tests/Recipe_test.cdc b/cadence/tests/Recipe_test.cdc new file mode 100644 index 0000000..986e8fe --- /dev/null +++ b/cadence/tests/Recipe_test.cdc @@ -0,0 +1,6 @@ +import Test + +access(all) fun testExample() { + let array = [1, 2, 3] + Test.expect(array.length, Test.equal(3)) +} diff --git a/cadence/transaction.cdc b/cadence/transactions/using_admin_resource.cdc similarity index 100% rename from cadence/transaction.cdc rename to cadence/transactions/using_admin_resource.cdc diff --git a/emulator-account.pkey b/emulator-account.pkey new file mode 100644 index 0000000..75611bd --- /dev/null +++ b/emulator-account.pkey @@ -0,0 +1 @@ +0xdc07d83a937644ff362b279501b7f7a3735ac91a0f3647147acf649dda804e28 \ No newline at end of file diff --git a/flow.json b/flow.json index e81ec35..7fd52d3 100644 --- a/flow.json +++ b/flow.json @@ -1,9 +1,9 @@ { "contracts": { - "Counter": { - "source": "cadence/contracts/Counter.cdc", + "Recipe": { + "source": "./cadence/contracts/Recipe.cdc", "aliases": { - "testing": "0000000000000007" + "emulator": "f8d6e0586b0a20c7" } } }, @@ -12,5 +12,21 @@ "mainnet": "access.mainnet.nodes.onflow.org:9000", "testing": "127.0.0.1:3569", "testnet": "access.devnet.nodes.onflow.org:9000" + }, + "accounts": { + "emulator-account": { + "address": "f8d6e0586b0a20c7", + "key": { + "type": "file", + "location": "emulator-account.pkey" + } + } + }, + "deployments": { + "emulator": { + "emulator-account": [ + "Recipe" + ] + } } } \ No newline at end of file