Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions .github/workflows/nextjs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
# Runs on pushes targeting the default branch
push:
branches: ["main"]
paths: ["frontend/**"]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -34,15 +35,18 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "20"
- name: Install and test modules
node-version: "24"
- name: Install and build
run: |
cd ./frontend
yarn
yarn build
pnpm install
pnpm run build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
Expand Down
21 changes: 19 additions & 2 deletions .github/workflows/tests-rs.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
name: Tests Contract RS
on: push
on:
push:
paths: ["contract-rs/**"]
pull_request:
paths: ["contract-rs/**"]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
workflows:
strategy:
Expand All @@ -8,9 +15,19 @@ jobs:
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v4

- name: Install system dependencies
if: matrix.platform == 'ubuntu-latest'
run: sudo apt-get update && sudo apt-get install -y libudev-dev

- uses: Swatinem/rust-cache@v2
with:
workspaces: contract-rs

- name: Install cargo-near
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/latest/download/cargo-near-installer.sh | sh
- name: Install and test modules

- name: Test contract
run: |
cd ./contract-rs
cargo test
22 changes: 14 additions & 8 deletions .github/workflows/tests-ts.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
name: Tests Contract TS
on:
pull_request:
push:
branches:
- main
paths: ["contract-ts/**"]
pull_request:
paths: ["contract-ts/**"]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
workflows:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 10
- uses: actions/setup-node@v4
with:
node-version: "20"
node-version: "24"
- name: Install and test modules
run: |
cd ./contract-ts
yarn
yarn test
pnpm install
pnpm test
13 changes: 4 additions & 9 deletions contract-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,16 @@ crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "5.24.0"
near-sdk = "5.29.0"

[dev-dependencies]
near-sdk = { version = "5.24.0", features = ["unit-testing"] }
near-sdk = { version = "5.29.0", features = ["unit-testing"] }
near-sandbox = "0.3"
near-api = "0.8"
cargo-near-build = "0.10"
tokio = { version = "1.12.0", features = ["full"] }
cargo-near-build = "0.11"
tokio = { version = "1.48.0", features = ["full"] }
serde_json = "1"
testresult = "0.4.1"
# This is temporary fix for the build error since those crates with a higher version require a higher version of Rust compiler (1.88.0)
cargo-platform = "=0.3.1"
darling = "=0.20.11"
bon = "=3.8.1"
home = "=0.5.9"

[profile.release]
codegen-units = 1
Expand Down
2 changes: 1 addition & 1 deletion contract-rs/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.86.0"
channel = "1.97.1"
components = ["rustfmt", "clippy", "rust-analyzer"]
targets = ["wasm32-unknown-unknown"]
20 changes: 13 additions & 7 deletions contract-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@
"type": "module",
"scripts": {
"build": "near-sdk-js build src/contract.ts build/counter.wasm",
"test": "$npm_execpath run build && ava -- ./build/counter.wasm"
"test": "$npm_execpath run build && ava --serial -- ./build/counter.wasm"
},
"dependencies": {
"near-sdk-js": "2.0.0"
},
"devDependencies": {
"ava": "^6.1.3",
"near-workspaces": "^3.5.0",
"typescript": "^5.4.5",
"near-cli": "^4.0.13"
"ava": "^8.0.1",
"near-api-js": "^7.2.0",
"near-sandbox": "^0.3.1",
"typescript": "^5.9.3"
},
"ava": {
"timeout": "20000",
"timeout": "60000",
"files": [
"sandbox-test/*.ava.js"
]
},
"pnpm": {
"onlyBuiltDependencies": [
"near-sdk-js",
"near-sandbox"
]
}
}
}
139 changes: 73 additions & 66 deletions contract-ts/sandbox-test/main.ava.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,103 @@
import anyTest from 'ava';
import { Worker, NEAR } from 'near-workspaces';
import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17
import { readFileSync } from 'fs';
import { Sandbox, DEFAULT_ACCOUNT_ID, DEFAULT_PRIVATE_KEY } from 'near-sandbox';
import { Account, JsonRpcProvider, KeyPair, KeyPairSigner, nearToYocto } from 'near-api-js';

/**
* @typedef {import('near-workspaces').NearAccount} NearAccount
* @type {import('ava').TestFn<{worker: Worker, accounts: Record<string, NearAccount>}>}
* @type {import('ava').TestFn<{sandbox: import('near-sandbox').Sandbox, provider: JsonRpcProvider, alice: Account, contract: Account}>}
*/
const test = anyTest;

test.beforeEach(async (t) => {
// Init the worker and start a Sandbox server
const worker = t.context.worker = await Worker.init();
const root = worker.rootAccount;
// Start a fresh sandbox for each test (NEAR_SANDBOX_VERSION overrides the binary, e.g. on older glibc)
const sandbox = await Sandbox.start({ version: process.env.NEAR_SANDBOX_VERSION });
const provider = new JsonRpcProvider({ url: sandbox.rpcUrl });

// some test accounts
const alice = await root.createSubAccount("alice", {
initialBalance: NEAR.parse("30 N").toJSON(),
});
const contract = await root.createSubAccount("contract", {
initialBalance: NEAR.parse("30 N").toJSON(),
});
// All accounts share the sandbox genesis key for simplicity
const keyPair = KeyPair.fromString(DEFAULT_PRIVATE_KEY);
const signer = new KeyPairSigner(keyPair);

const root = new Account(DEFAULT_ACCOUNT_ID, provider, signer);

for (const prefix of ['alice', 'contract']) {
await root.createSubAccount({
accountOrPrefix: prefix,
publicKey: keyPair.getPublicKey(),
nearToTransfer: nearToYocto('30'),
});
}

const alice = new Account(`alice.${DEFAULT_ACCOUNT_ID}`, provider, signer);
const contract = new Account(`contract.${DEFAULT_ACCOUNT_ID}`, provider, signer);

// Get wasm file path from package.json test script in folder above
await contract.deploy(process.argv[2]);
// Deploy the wasm file passed by the package.json test script
await contract.deployContract(readFileSync(process.argv[2]));

// Save state for test runs, it is unique for each test
t.context.accounts = { contract, alice };
t.context = { sandbox, provider, alice, contract };
});

test.afterEach.always(async (t) => {
// Stop Sandbox server
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to stop the Sandbox:", error);
// Stop the sandbox and clean up temporary files
await t.context.sandbox.tearDown().catch((error) => {
console.log('Failed to stop the Sandbox:', error);
});
});

test("can be incremented", async (t) => {
const { alice, contract } = t.context.accounts;
const startCounter = await contract.view("get_num", {});
await alice.call(contract, "increment", {});
const endCounter = await contract.view("get_num", {});
t.is(endCounter, startCounter + 1);
const getNum = (t) => t.context.provider.callFunction({
contractId: t.context.contract.accountId,
method: 'get_num',
args: {},
});

const call = (t, methodName, args = {}) => t.context.alice.callFunction({
contractId: t.context.contract.accountId,
methodName,
args,
});

test('can be incremented', async (t) => {
const startCounter = await getNum(t);
await call(t, 'increment');
t.is(await getNum(t), startCounter + 1);
});

test("can be incremented 10 points", async (t) => {
const { alice, contract } = t.context.accounts;
const startCounter = await contract.view("get_num", {});
await alice.call(contract, "increment", { number: 10 });
const endCounter = await contract.view("get_num", {});
t.is(endCounter, startCounter + 10);
test('can be incremented 10 points', async (t) => {
const startCounter = await getNum(t);
await call(t, 'increment', { number: 10 });
t.is(await getNum(t), startCounter + 10);
});

test("can be incremented -10 points", async (t) => {
const { alice, contract } = t.context.accounts;
const startCounter = await contract.view("get_num", {});
await alice.call(contract, "increment", { number: -10 });
const endCounter = await contract.view("get_num", {});
t.is(endCounter, startCounter - 10);
test('can be incremented -10 points', async (t) => {
const startCounter = await getNum(t);
await call(t, 'increment', { number: -10 });
t.is(await getNum(t), startCounter - 10);
});

test("can be decremented", async (t) => {
const { alice, contract } = t.context.accounts;
await alice.call(contract, "increment", {});
const startCounter = await contract.view("get_num", {});
await alice.call(contract, "decrement", {});
const endCounter = await contract.view("get_num", {});
t.is(endCounter, startCounter - 1);
test('can be decremented', async (t) => {
await call(t, 'increment');
const startCounter = await getNum(t);
await call(t, 'decrement');
t.is(await getNum(t), startCounter - 1);
});

test("can be decremented 10 points", async (t) => {
const { alice, contract } = t.context.accounts;
await alice.call(contract, "increment", {});
const startCounter = await contract.view("get_num", {});
await alice.call(contract, "decrement", { number: 10 });
const endCounter = await contract.view("get_num", {});
t.is(endCounter, startCounter - 10);
test('can be decremented 10 points', async (t) => {
await call(t, 'increment');
const startCounter = await getNum(t);
await call(t, 'decrement', { number: 10 });
t.is(await getNum(t), startCounter - 10);
});

test("can be decremented -10 points", async (t) => {
const { alice, contract } = t.context.accounts;
await alice.call(contract, "increment", {});
const startCounter = await contract.view("get_num", {});
await alice.call(contract, "decrement", { number: -10 });
const endCounter = await contract.view("get_num", {});
t.is(endCounter, startCounter + 10);
test('can be decremented -10 points', async (t) => {
await call(t, 'increment');
const startCounter = await getNum(t);
await call(t, 'decrement', { number: -10 });
t.is(await getNum(t), startCounter + 10);
});

test("can be reset", async (t) => {
const { alice, contract } = t.context.accounts;
await alice.call(contract, "increment", {});
await alice.call(contract, "increment", {});
await alice.call(contract, "reset", {});
const endCounter = await contract.view("get_num", {});
t.is(endCounter, 0);
test('can be reset', async (t) => {
await call(t, 'increment');
await call(t, 'increment');
await call(t, 'reset');
t.is(await getNum(t), 0);
});
10 changes: 6 additions & 4 deletions contract-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "ES5",
"target": "es2020",
"module": "esnext",
"moduleResolution": "bundler",
"noEmit": true,
"noImplicitAny": false,
"noImplicitAny": false
},
"files": [
"src/contract.ts"
],
"exclude": [
"node_modules"
],
}
]
}
Loading
Loading