From b0f3be14c91d52ed18c515086f10fec152d5dc2a Mon Sep 17 00:00:00 2001 From: Nelito Junior Date: Thu, 14 Nov 2024 18:39:24 -0300 Subject: [PATCH] feat(e2e): add increment contract FE-893 (#425) --- .github/workflows/pr-tests.yml | 27 +++++++++++- .github/workflows/setup-rust/action.yml | 14 ++++++ .gitignore | 4 +- e2e-tests/react-next/src/app/page.tsx | 8 +++- e2e-tests/runner/common/common.ts | 12 ++++- .../runner/contracts/custom_asset/.gitignore | 2 + .../runner/contracts/custom_asset/Forc.lock | 35 +++++++++++++++ .../runner/contracts/custom_asset/Forc.toml | 9 ++++ .../runner/contracts/custom_asset/src/main.sw | 27 ++++++++++++ .../FuelWalletConnector.test.ts | 8 +++- e2e-tests/runner/fuels.config.ts | 25 +++++++++++ e2e-tests/runner/package.json | 4 +- e2e-tests/runner/scripts/deployCI.sh | 44 +++++++++++++++++++ e2e-tests/runner/scripts/deployLocal.sh | 20 +++++++++ e2e-tests/runner/src/contract-ids.json | 3 ++ .../runner/src/contracts/contract-ids.json | 3 ++ e2e-tests/runner/src/contracts/index.ts | 1 + 17 files changed, 238 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/setup-rust/action.yml create mode 100644 e2e-tests/runner/contracts/custom_asset/.gitignore create mode 100644 e2e-tests/runner/contracts/custom_asset/Forc.lock create mode 100644 e2e-tests/runner/contracts/custom_asset/Forc.toml create mode 100644 e2e-tests/runner/contracts/custom_asset/src/main.sw create mode 100644 e2e-tests/runner/fuels.config.ts create mode 100644 e2e-tests/runner/scripts/deployCI.sh create mode 100644 e2e-tests/runner/scripts/deployLocal.sh create mode 100644 e2e-tests/runner/src/contract-ids.json create mode 100644 e2e-tests/runner/src/contracts/contract-ids.json create mode 100644 e2e-tests/runner/src/contracts/index.ts diff --git a/.github/workflows/pr-tests.yml b/.github/workflows/pr-tests.yml index 678a751d..1b7aadb5 100644 --- a/.github/workflows/pr-tests.yml +++ b/.github/workflows/pr-tests.yml @@ -105,6 +105,31 @@ jobs: - name: Run build:connectors run: pnpm build:connectors + - uses: ./.github/workflows/setup-rust + + - name: Verify Forc and Fuel-Core + run: | + forc --version + fuel-core --version + + # Check the directory structure before deployment + - name: Debug Paths Before Deployment + run: | + echo "Checking paths before deployment" + ls -la e2e-tests/runner/src + ls -la examples/react-app/src/types/ + + - name: Build & Deploy Contracts + run: pnpm deploy:contracts:ci + working-directory: e2e-tests/runner/scripts + + # Check the directory structure after deployment + - name: Debug Paths After Deployment + run: | + echo "Checking paths after deployment" + ls -la e2e-tests/runner/src/contracts/ + ls -la examples/react-app/src/types/ + # E2E tests running with Playwright - name: Install Playwright Browsers run: pnpm exec playwright install --with-deps chromium @@ -117,7 +142,7 @@ jobs: VITE_FUEL_PROVIDER_URL: "http://localhost:4000/v1/graphql" NEXT_PUBLIC_PROVIDER_URL: "http://localhost:4000/v1/graphql" NEXT_PUBLIC_WC_PROJECT_ID: e01471314fc69cc4efba6dce12dfd710 - NEXT_PUBLIC_CHAIN_ID_NAME: testnet + NEXT_PUBLIC_CHAIN_ID_NAME: local REACT_APP_PORT: 5173 REACT_NEXT_PORT: 3002 VITE_WALLET_SECRET: "0xa449b1ffee0e2205fa924c6740cc48b3b473aa28587df6dab12abc245d1f5298" diff --git a/.github/workflows/setup-rust/action.yml b/.github/workflows/setup-rust/action.yml new file mode 100644 index 00000000..1f3aac25 --- /dev/null +++ b/.github/workflows/setup-rust/action.yml @@ -0,0 +1,14 @@ +name: 'Forc Setup' + +inputs: + forc-components: + default: 'forc@0.65.2, fuel-core@0.39.0' + +runs: + using: 'composite' + steps: + - name: Install Fuel toolchain + uses: FuelLabs/action-fuel-toolchain@v0.6.0 + with: + name: fuel-bridge + components: ${{ inputs.forc-components }} diff --git a/.gitignore b/.gitignore index 78032dfe..f31cfdc1 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,6 @@ yarn-error.log* **/*.tgz e2e-tests/runner/playwright-report/ e2e-tests/runner/blob-report/ -e2e-tests/runner/test-results/ \ No newline at end of file +e2e-tests/runner/test-results/ +e2e-tests/runner/src/contracts/ +e2e-tests/runner/src/contracts/contract-ids.json diff --git a/e2e-tests/react-next/src/app/page.tsx b/e2e-tests/react-next/src/app/page.tsx index 264899a9..ae64a2ea 100644 --- a/e2e-tests/react-next/src/app/page.tsx +++ b/e2e-tests/react-next/src/app/page.tsx @@ -13,8 +13,12 @@ import COUNTER_CONTRACT_ID_TESTNET from 'react-app/src/types/contract-ids-testne const CHAIN_ID_NAME = process.env .NEXT_PUBLIC_CHAIN_ID_NAME as keyof typeof CHAIN_IDS.fuel; -const CHAIN_ID = CHAIN_IDS.fuel[CHAIN_ID_NAME]; -const PROVIDER_URL = process.env.NEXT_PUBLIC_PROVIDER_URL || ''; +const CHAIN_ID = CHAIN_IDS.fuel[CHAIN_ID_NAME] || 0; +const PROVIDER_URL = process.env.NEXT_PUBLIC_PROVIDER_URL; + +if (!PROVIDER_URL) { + throw new Error(`PROVIDER_URL is not set: ${PROVIDER_URL}`); +} function getContractId() { switch (CHAIN_ID_NAME) { diff --git a/e2e-tests/runner/common/common.ts b/e2e-tests/runner/common/common.ts index 3d18378a..90b42978 100644 --- a/e2e-tests/runner/common/common.ts +++ b/e2e-tests/runner/common/common.ts @@ -57,15 +57,23 @@ export const transferTests = async ( expect( await page.waitForSelector('text=Transferred successfully!'), ).toBeTruthy(); + + await page.click('text=Disconnect'); }; // biome-ignore lint/suspicious/noExportsInTest: export const incrementTests = async ( page: Page, - { approveTransfer }: ConnectorFunctions, + { approveTransfer, connect }: ConnectorFunctions, ) => { await test.step('should connect and increment', async () => { - await page.click('text=Increment'); + await connect(page); + + const incrementButton = await page.getByRole('button', { + name: 'Increment', + }); + await incrementButton.click(); + await approveTransfer(page); expect(await page.waitForSelector('text=Success')).toBeTruthy(); diff --git a/e2e-tests/runner/contracts/custom_asset/.gitignore b/e2e-tests/runner/contracts/custom_asset/.gitignore new file mode 100644 index 00000000..77d3844f --- /dev/null +++ b/e2e-tests/runner/contracts/custom_asset/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/e2e-tests/runner/contracts/custom_asset/Forc.lock b/e2e-tests/runner/contracts/custom_asset/Forc.lock new file mode 100644 index 00000000..b39ef2d8 --- /dev/null +++ b/e2e-tests/runner/contracts/custom_asset/Forc.lock @@ -0,0 +1,35 @@ +[[package]] +name = "core" +source = "path+from-root-4BA3A18A2D620E67" + +[[package]] +name = "custom_asset" +source = "member" +dependencies = [ + "standards git+https://github.com/FuelLabs/sway-standards?tag=v0.4.1#0a6f3ba0bce036a0ce61f15ed4480c71af32d3aa", + "std", + "sway_libs", +] + +[[package]] +name = "standards" +source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.4.1#0a6f3ba0bce036a0ce61f15ed4480c71af32d3aa" +dependencies = ["std"] + +[[package]] +name = "standards" +source = "git+https://github.com/FuelLabs/sway-standards?tag=v0.4.3#6f63eb7dff2458a7d976184e565b5cbf26f61da2" +dependencies = ["std"] + +[[package]] +name = "std" +source = "git+https://github.com/fuellabs/sway?tag=v0.64.0#2156bfbbee01ffb85bfca2aae8f185f8e7c715a4" +dependencies = ["core"] + +[[package]] +name = "sway_libs" +source = "git+https://github.com/FuelLabs/sway-libs?tag=v0.21.0#6a227ed34c86fe1ebd334dbdfeccf66c43e3915b" +dependencies = [ + "standards git+https://github.com/FuelLabs/sway-standards?tag=v0.4.3#6f63eb7dff2458a7d976184e565b5cbf26f61da2", + "std", +] diff --git a/e2e-tests/runner/contracts/custom_asset/Forc.toml b/e2e-tests/runner/contracts/custom_asset/Forc.toml new file mode 100644 index 00000000..c1d90684 --- /dev/null +++ b/e2e-tests/runner/contracts/custom_asset/Forc.toml @@ -0,0 +1,9 @@ +[project] +authors = ["Matt Auer"] +entry = "main.sw" +license = "Apache-2.0" +name = "custom_asset" + +[dependencies] +standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.4.1" } +sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.21.0" } diff --git a/e2e-tests/runner/contracts/custom_asset/src/main.sw b/e2e-tests/runner/contracts/custom_asset/src/main.sw new file mode 100644 index 00000000..efab39e9 --- /dev/null +++ b/e2e-tests/runner/contracts/custom_asset/src/main.sw @@ -0,0 +1,27 @@ +contract; + +abi CounterContract { + #[storage(read)] + fn get_count() -> u64; + + #[storage(read, write)] + fn increment_counter() -> u64; +} + +storage { + counter: u64 = 0, +} + +impl CounterContract for Contract { + #[storage(read)] + fn get_count() -> u64 { + storage.counter.try_read().unwrap_or(0) + } + + #[storage(read, write)] + fn increment_counter() -> u64 { + let incremented = storage.counter.try_read().unwrap_or(0) + 1; + storage.counter.write(incremented); + incremented + } +} diff --git a/e2e-tests/runner/examples/connectors/FuelWalletConnector/FuelWalletConnector.test.ts b/e2e-tests/runner/examples/connectors/FuelWalletConnector/FuelWalletConnector.test.ts index 37512aad..a68abe7c 100644 --- a/e2e-tests/runner/examples/connectors/FuelWalletConnector/FuelWalletConnector.test.ts +++ b/e2e-tests/runner/examples/connectors/FuelWalletConnector/FuelWalletConnector.test.ts @@ -7,7 +7,11 @@ import type { FuelWalletTestHelper } from '@fuels/playwright-utils'; import { test } from '@fuels/playwright-utils'; import dotenv from 'dotenv'; import { type WalletUnlocked, bn } from 'fuels'; -import { sessionTests, transferTests } from '../../../common/common'; +import { + incrementTests, + sessionTests, + transferTests, +} from '../../../common/common'; import type { ConnectorFunctions } from '../../../common/types'; import { testSetup, transferMaxBalance } from '../setup'; dotenv.config(); @@ -67,5 +71,7 @@ test.describe('FuelWalletConnector', () => { await sessionTests(page, { connect, approveTransfer }); await transferTests(page, { connect, approveTransfer }); + + await incrementTests(page, { connect, approveTransfer }); }); }); diff --git a/e2e-tests/runner/fuels.config.ts b/e2e-tests/runner/fuels.config.ts new file mode 100644 index 00000000..d5b5f1c7 --- /dev/null +++ b/e2e-tests/runner/fuels.config.ts @@ -0,0 +1,25 @@ +import { existsSync, readFileSync, writeFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { createConfig } from 'fuels'; + +export default createConfig({ + output: './src/contracts', + contracts: ['./contracts/custom_asset'], + forcBuildFlags: ['--release'], + privateKey: + '0xa449b1ffee0e2205fa924c6740cc48b3b473aa28587df6dab12abc245d1f5298', + providerUrl: 'http://localhost:4000/v1/graphql', + onDeploy: (_, contracts) => { + const contractIdsPath = join(__dirname, './src/contract-ids.json'); + let contractIds = {}; + if (existsSync(contractIdsPath)) { + contractIds = JSON.parse( + readFileSync(contractIdsPath, 'utf8').toString(), + ); + } + + contractIds[process.env.CONTRACT_NAME || 'contract'] = + contracts[0].contractId; + writeFileSync(contractIdsPath, JSON.stringify(contractIds, null, 2)); + }, +}); diff --git a/e2e-tests/runner/package.json b/e2e-tests/runner/package.json index 0bc3ccd0..5e5fbef2 100644 --- a/e2e-tests/runner/package.json +++ b/e2e-tests/runner/package.json @@ -14,7 +14,9 @@ "test:react-next": "pnpm playwright test --project=react-next", "start:react-app": "pnpm --filter react-app dev", "start:react-next": "pnpm --filter @e2e-tests/react-next dev", - "start:servers": "pnpm run start:react-app & pnpm run start:react-next" + "start:servers": "pnpm run start:react-app & pnpm run start:react-next", + "deploy:contracts": "sh ./scripts/deployLocal.sh", + "deploy:contracts:ci": "sh ./scripts/deployCI.sh" }, "devDependencies": { "@fuels/connectors": "workspace:*", diff --git a/e2e-tests/runner/scripts/deployCI.sh b/e2e-tests/runner/scripts/deployCI.sh new file mode 100644 index 00000000..16260cdb --- /dev/null +++ b/e2e-tests/runner/scripts/deployCI.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Get the directory where the script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/scripts" + +echo "Build contracts" +pnpm fuels build + +echo "Deploy contract" +pnpm fuels deploy + +# Define paths for the input and output files +CONTRACT_IDS_PATH="$SCRIPT_DIR/../src/contracts/contract-ids.json" +OUTPUT_PATH_APP="$SCRIPT_DIR/../../../examples/react-app/src/types/contract-ids-local.json" + +# Ensure the output directory exists +mkdir -p "$(dirname "$OUTPUT_PATH_APP")" + +# Debug: Show paths and check if CONTRACT_IDS_PATH exists +echo "SCRIPT_DIR is: $SCRIPT_DIR" +echo "CONTRACT_IDS_PATH is: $CONTRACT_IDS_PATH" +echo "OUTPUT_PATH_APP is: $OUTPUT_PATH_APP" + +# Check if the contract-ids.json file exists and display its contents +if [ -f "$CONTRACT_IDS_PATH" ]; then + echo "Found contract-ids.json at $CONTRACT_IDS_PATH" + cat "$CONTRACT_IDS_PATH" +else + echo "Error: $CONTRACT_IDS_PATH not found." + exit 1 +fi + +# Extract the customAsset contract ID from contract-ids.json +CONTRACT_ID=$(jq -r '.customAsset // empty' "$CONTRACT_IDS_PATH") + +# Check if CONTRACT_ID was successfully extracted +if [ -z "$CONTRACT_ID" ]; then + echo "Error: Contract ID for 'customAsset' not found in $CONTRACT_IDS_PATH" + exit 1 +fi + +# Save the contract ID to contract-ids-local.json +jq -n --arg counter "$CONTRACT_ID" '{ "counter": $counter }' > "$OUTPUT_PATH_APP" +echo "Saved contract ID as 'counter' in contract-ids-local.json" diff --git a/e2e-tests/runner/scripts/deployLocal.sh b/e2e-tests/runner/scripts/deployLocal.sh new file mode 100644 index 00000000..73e2eb79 --- /dev/null +++ b/e2e-tests/runner/scripts/deployLocal.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Get the directory where the script is located +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +echo "Build contracts" +pnpm fuels build + +echo "Deploy contract" +pnpm fuels deploy + +CONTRACT_IDS_PATH="$SCRIPT_DIR/../src/contracts/contract-ids.json" +OUTPUT_PATH_APP="$SCRIPT_DIR/../../../examples/react-app/src/types/contract-ids-local.json" + +mkdir -p "$(dirname "$OUTPUT_PATH_APP")" + +CONTRACT_ID=$(jq -r '.customAsset // empty' "$CONTRACT_IDS_PATH") + +jq -n --arg counter "$CONTRACT_ID" '{ "counter": $counter }' > "$OUTPUT_PATH_APP" +echo "Saved contract ID as 'counter' in contract-ids-local.json" \ No newline at end of file diff --git a/e2e-tests/runner/src/contract-ids.json b/e2e-tests/runner/src/contract-ids.json new file mode 100644 index 00000000..09ef89f5 --- /dev/null +++ b/e2e-tests/runner/src/contract-ids.json @@ -0,0 +1,3 @@ +{ + "contract": "0x0000000000000000000000000000000000000000000000000000000000000000" +} diff --git a/e2e-tests/runner/src/contracts/contract-ids.json b/e2e-tests/runner/src/contracts/contract-ids.json new file mode 100644 index 00000000..12239342 --- /dev/null +++ b/e2e-tests/runner/src/contracts/contract-ids.json @@ -0,0 +1,3 @@ +{ + "customAsset": "0x0000000000000000000000000000000000000000000000000000000000000000" +} diff --git a/e2e-tests/runner/src/contracts/index.ts b/e2e-tests/runner/src/contracts/index.ts new file mode 100644 index 00000000..c7041c4c --- /dev/null +++ b/e2e-tests/runner/src/contracts/index.ts @@ -0,0 +1 @@ +export * from './contracts';