Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gas meter api #229

Closed
wants to merge 6 commits into from
Closed
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ For an example, see the [fast-forward test](./__tests__/08.fast-forward.ava.ts)

Note: `fastForward` does not speed up an in-flight transactions.

Metering Gas Burnt
==================

You can meter the amount of gas burnt by transactions using `GasMeter`. This can be useful for testing gas consumption of your contracts. For example, you can test that a contract call consumes less gas than a certain amount.

It can be used like so:

```ts
const meter = new GasMeter();
const worker = await Worker.init({tx_callbacks: [meter.tx_callback()]});

const root = worker.rootAccount;
const alice = await root.createSubAccount('alice'); // this transaction will be metered


// checking the gas burnt for SubAccount creation
const elapsed = meter.elapsed();
```

Pro Tips
========

Expand Down
76 changes: 76 additions & 0 deletions __tests__/09.gas-meter-api.ava.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* This test file is a copy of '01.basic-transactions.ava.ts' with the addition
* of a gas meter. The gas meter is used to track the amount of gas consumed.
*
* 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, NearAccount, GasMeter, Gas} from 'near-workspaces';
import anyTest, {TestFn} from 'ava';

const test = anyTest as TestFn<{
worker: Worker;
meter: GasMeter;
accounts: Record<string, NearAccount>;
}>;

test.beforeEach(async t => {
// Init the worker and start a Sandbox server
const meter = new GasMeter();
const worker = await Worker.init({tx_callbacks: [meter.tx_callback()]});

// Prepare sandbox for tests, create accounts, deploy contracts, etx.
const root = worker.rootAccount;
const contract = await root.devDeploy(
'__tests__/build/debug/status_message.wasm',
{initialBalance: NEAR.parse('3 N').toJSON()},
);
const ali = await root.createSubAccount('ali', {initialBalance: NEAR.parse('3 N').toJSON()});

// Assert gas was burnt on account creation
t.assert(meter.elapsed.gt(Gas.from(10_000_000_000_000)), `meter.elapsed: ${meter.elapsed.toString()}`);

// Reset the meter
await meter.reset();

// Save state for test runs, it is unique for each test
t.context.worker = worker;
t.context.meter = meter;
t.context.accounts = {root, contract, ali};
});

test.afterEach.always(async t => {
// Reset the meter
await t.context.meter.reset();

// Stop Sandbox server
await t.context.worker.tearDown().catch(error => {
console.log('Failed to tear down the worker:', error);
});
});

test('Root gets null status', async t => {
const {root, contract} = t.context.accounts;
const result: null = await contract.view('get_status', {account_id: root.accountId});
t.is(result, null);
t.deepEqual(t.context.meter.elapsed, Gas.from(0), 'no gas consumed for this view call');
});

test('Ali sets then gets status', async t => {
const {ali, contract} = t.context.accounts;
await ali.call(contract, 'set_status', {message: 'hello'});
const result: string = await contract.view('get_status', {account_id: ali});
t.is(result, 'hello');
t.notDeepEqual(t.context.meter.elapsed, Gas.from(0)); // Amount of gas consumed > 0
});

test('Root and Ali have different statuses', async t => {
const {root, contract, ali} = t.context.accounts;
await root.call(contract, 'set_status', {message: 'world'});
const rootStatus: string = await contract.view('get_status', {account_id: root});
t.is(rootStatus, 'world');
const aliStatus: null = await contract.view('get_status', {account_id: ali});
t.is(aliStatus, null);
t.notDeepEqual(t.context.meter.elapsed, Gas.from(0)); // Amount of gas consumed > 0
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@
"workspaces": [
"packages/js"
]
}
}
3 changes: 2 additions & 1 deletion packages/js/dist/account/account-manager.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/js/dist/account/account-manager.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/js/dist/account/account-manager.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading