-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/api-key
- Loading branch information
Showing
18 changed files
with
181 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import {NearAccount, getNetworkFromEnv} from 'near-workspaces'; | ||
import anyTest, {TestFn} from 'ava'; | ||
import {Worker} from 'near-workspaces/dist/worker'; | ||
|
||
// The contract provided contains only one view call, returning the | ||
// block_timestamp and epoch_height of the current block as a tuple. | ||
// Source is here <https://github.com/near/near-workspaces-rs/blob/main/examples/simple-contract/src/lib.rs> | ||
const contract_wasm = '__tests__/build/debug/simple_contract.wasm'; | ||
|
||
// Represents the timestamp and epoch_height result from the view call. | ||
type EnvData = [number, number]; | ||
|
||
if (getNetworkFromEnv() === 'sandbox') { | ||
const test = anyTest as TestFn<{ | ||
worker: Worker; | ||
contract: NearAccount; | ||
}>; | ||
|
||
test.beforeEach(async t => { | ||
const worker = await Worker.init(); | ||
const root = worker.rootAccount; | ||
const contract = await root.devDeploy(contract_wasm); | ||
|
||
t.context.worker = worker; | ||
t.context.contract = contract; | ||
}); | ||
|
||
test.afterEach.always(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('Fast Forward', async t => { | ||
const before = await t.context.contract.view('current_env_data'); | ||
const env_before = before as EnvData; | ||
console.log(`Before: timestamp = ${env_before[0]}, epoch_height = ${env_before[1]}`); | ||
|
||
const forward_height = 10_000; | ||
|
||
// Call into fastForward. This will take a bit of time to invoke, but is | ||
// faster than manually waiting for the same amounts of blocks to be produced | ||
await t.context.worker.provider.fastForward(forward_height); | ||
|
||
const after = await t.context.contract.view('current_env_data'); | ||
const env_after = after as EnvData; | ||
console.log(`After: timestamp = ${env_after[0]}, epoch_height = ${env_after[1]}`); | ||
|
||
const block = await t.context.worker.provider.block({finality: 'final'}); | ||
|
||
// Rounding off to nearest hundred, providing wiggle room incase not perfectly `forward_height` | ||
t.true(Math.ceil(block.header.height / 100) * 100 === forward_height); | ||
}); | ||
} |
Binary file not shown.
41 changes: 41 additions & 0 deletions
41
examples/simple-project/__tests__/test-simple-project.ava.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* This test demonstrates basic behavior of near-workspaces, making simple | ||
* function calls and view calls to the contract from | ||
* https://github.com/near-examples/rust-status-message | ||
* | ||
* Note that the same tests will be run on both a local sandbox environment and | ||
* on testnet by using the `test:sandbox` and `test:testnet` scripts in | ||
* package.json. | ||
*/ | ||
import {Worker, NEAR} from 'near-workspaces'; | ||
import test from 'ava'; | ||
|
||
test.beforeEach(async t => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
const contract = await root.devDeploy( | ||
// source code: https://github.com/NEARFoundation/near-smart-contract-rust-template/tree/main | ||
'contracts/near_smart_contract_rust_template.wasm', | ||
{initialBalance: NEAR.parse('30 N').toJSON()}, | ||
); | ||
const ali = await root.createSubAccount('ali', {initialBalance: NEAR.parse('3 N').toJSON()}); | ||
|
||
// Save state for test runs, it is unique for each test | ||
t.context.worker = worker; | ||
t.context.accounts = {root, contract, ali}; | ||
}); | ||
|
||
test.afterEach.always(async t => { | ||
// Stop Sandbox server | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('Root call new', async t => { | ||
const {contract, ali} = t.context.accounts; | ||
await ali.call(contract, 'new', {}); | ||
}); |
Binary file added
BIN
+358 KB
examples/simple-project/contracts/near_smart_contract_rust_template.wasm
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.