Skip to content

Commit

Permalink
feat(e2e): add increment contract FE-893 (#425)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelitow authored Nov 14, 2024
1 parent 5aac2bc commit b0f3be1
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 8 deletions.
27 changes: 26 additions & 1 deletion .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/setup-rust/action.yml
Original file line number Diff line number Diff line change
@@ -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 }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,6 @@ yarn-error.log*
**/*.tgz
e2e-tests/runner/playwright-report/
e2e-tests/runner/blob-report/
e2e-tests/runner/test-results/
e2e-tests/runner/test-results/
e2e-tests/runner/src/contracts/
e2e-tests/runner/src/contracts/contract-ids.json
8 changes: 6 additions & 2 deletions e2e-tests/react-next/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 10 additions & 2 deletions e2e-tests/runner/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: <explanation>
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();
Expand Down
2 changes: 2 additions & 0 deletions e2e-tests/runner/contracts/custom_asset/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out
target
35 changes: 35 additions & 0 deletions e2e-tests/runner/contracts/custom_asset/Forc.lock
Original file line number Diff line number Diff line change
@@ -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",
]
9 changes: 9 additions & 0 deletions e2e-tests/runner/contracts/custom_asset/Forc.toml
Original file line number Diff line number Diff line change
@@ -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" }
27 changes: 27 additions & 0 deletions e2e-tests/runner/contracts/custom_asset/src/main.sw
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -67,5 +71,7 @@ test.describe('FuelWalletConnector', () => {
await sessionTests(page, { connect, approveTransfer });

await transferTests(page, { connect, approveTransfer });

await incrementTests(page, { connect, approveTransfer });
});
});
25 changes: 25 additions & 0 deletions e2e-tests/runner/fuels.config.ts
Original file line number Diff line number Diff line change
@@ -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));
},
});
4 changes: 3 additions & 1 deletion e2e-tests/runner/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:*",
Expand Down
44 changes: 44 additions & 0 deletions e2e-tests/runner/scripts/deployCI.sh
Original file line number Diff line number Diff line change
@@ -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"
20 changes: 20 additions & 0 deletions e2e-tests/runner/scripts/deployLocal.sh
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions e2e-tests/runner/src/contract-ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"contract": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
3 changes: 3 additions & 0 deletions e2e-tests/runner/src/contracts/contract-ids.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"customAsset": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
1 change: 1 addition & 0 deletions e2e-tests/runner/src/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './contracts';

0 comments on commit b0f3be1

Please sign in to comment.