Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into daniyar/abi-gen
Browse files Browse the repository at this point in the history
  • Loading branch information
itegulov committed Dec 2, 2022
2 parents ee2a8f6 + eadac70 commit 29e6fb9
Show file tree
Hide file tree
Showing 38 changed files with 723 additions and 246 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ jobs:
run: cd examples && yarn build:parking-lot && yarn test:parking-lot
- name: standard nft
run: cd examples && yarn build-nft && yarn test:nft
- name: programmatic-update
run: cd examples && yarn build:programmatic-update && yarn test:programmatic-update
- name: cross-contract-call-loop
run: cd examples && yarn build:cross-contract-call-loop && yarn test:cross-contract-call-loop
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ There are a couple of contract examples in the project:
- [Non fungible token receiver contract](https://github.com/near/near-sdk-js/tree/develop/examples/src/non-fungible-token-receiver.js)
- [Status message board](https://github.com/near/near-sdk-js/tree/develop/examples/src/status-message.js)
- [Status message board with unique messages](https://github.com/near/near-sdk-js/tree/develop/examples/src/status-message-collections.js)
- [Programmatic Update After Locking The Contract](https://github.com/near/near-sdk-js/tree/develop/examples/src/programmatic-update.js)

To build all examples, run `yarn build` in `examples/`. To test all examples, run `yarn test`. You can also build and test one specific example with `yarn build:<example-name>` and `yarn test:<example-name>`, see `examples/package.json`.

Expand Down
16 changes: 0 additions & 16 deletions examples/__tests__/standard-nft/test_approval.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ test("Simple approve", async (t) => {
{
token_id: "0",
account_id: ali.accountId,
msg: null,
},
{ attachedDeposit: "510000000000000000000" }
);
Expand All @@ -94,7 +93,6 @@ test("Simple approve", async (t) => {
let alice_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: ali.accountId,
approval_id: null,
});
t.assert(alice_approved);

Expand All @@ -118,7 +116,6 @@ test("Simple approve", async (t) => {
{
token_id: "0",
account_id: ali.accountId,
msg: null,
},
{
attachedDeposit: "1",
Expand All @@ -138,7 +135,6 @@ test("Simple approve", async (t) => {
{
token_id: "0",
account_id: bob.accountId,
msg: null,
},
{
attachedDeposit: "550000000000000000000",
Expand Down Expand Up @@ -191,7 +187,6 @@ test("Approved account transfers token", async (t) => {
{
token_id: "0",
account_id: ali.accountId,
msg: null,
},
{ attachedDeposit: "510000000000000000000" }
);
Expand All @@ -206,7 +201,6 @@ test("Approved account transfers token", async (t) => {
{
receiver_id: ali.accountId,
token_id: "0",
approval_id: null,
memo: "gotcha! bahahaha",
},
{ attachedDeposit: "1" }
Expand All @@ -226,7 +220,6 @@ test("revoke", async (t) => {
{
token_id: "0",
account_id: ali.accountId,
msg: null,
},
{ attachedDeposit: "510000000000000000000" }
);
Expand All @@ -238,7 +231,6 @@ test("revoke", async (t) => {
{
token_id: "0",
account_id: bob.accountId,
msg: null,
},
{
attachedDeposit: "510000000000000000000",
Expand All @@ -262,14 +254,12 @@ test("revoke", async (t) => {
let alice_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: ali.accountId,
approval_id: null,
});
t.assert(!alice_approved);

let bob_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: bob.accountId,
approval_id: null,
});
t.assert(bob_approved);

Expand All @@ -289,14 +279,12 @@ test("revoke", async (t) => {
alice_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: ali.accountId,
approval_id: null,
});
t.assert(!alice_approved);

bob_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: bob.accountId,
approval_id: null,
});
t.assert(!bob_approved);
});
Expand All @@ -310,7 +298,6 @@ test("revoke all", async (t) => {
{
token_id: "0",
accountId: ali.accountId,
msg: null,
},
{ attachedDeposit: "510000000000000000000" }
);
Expand All @@ -322,7 +309,6 @@ test("revoke all", async (t) => {
{
token_id: "0",
accountId: bob.accountId,
msg: null,
},
{
attachedDeposit: "510000000000000000000",
Expand All @@ -343,14 +329,12 @@ test("revoke all", async (t) => {
let alice_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: ali.accountId,
approval_id: null,
});
t.assert(!alice_approved);

let bob_approved = await nft.view("nft_is_approved", {
token_id: "0",
approved_account_id: bob.accountId,
approval_id: null,
});
t.assert(!bob_approved);
});
8 changes: 0 additions & 8 deletions examples/__tests__/standard-nft/test_core.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ test("Simple transfer", async (t) => {
{
receiver_id: ali.accountId,
token_id: "0",
approval_id: null,
memo: "simple transfer",
},
{ attachedDeposit: "1" }
Expand All @@ -105,7 +104,6 @@ test("Transfer call fast return to sender", async (t) => {
{
receiver_id: nftReceiver.accountId,
token_id: "0",
approval_id: null,
memo: "transfer & call",
msg: "return-it-now",
},
Expand All @@ -129,7 +127,6 @@ test("Transfer call slow return to sender", async (t) => {
{
receiver_id: nftReceiver.accountId,
token_id: "0",
approval_id: null,
memo: "transfer & call",
msg: "return-it-later",
},
Expand All @@ -153,7 +150,6 @@ test("Transfer call fast keep with sender", async (t) => {
{
receiver_id: nftReceiver.accountId,
token_id: "0",
approval_id: null,
memo: "transfer & call",
msg: "keep-it-now",
},
Expand All @@ -177,7 +173,6 @@ test("Transfer call slow keep with sender", async (t) => {
{
receiver_id: nftReceiver.accountId,
token_id: "0",
approval_id: null,
memo: "transfer & call",
msg: "keep-it-later",
},
Expand All @@ -201,7 +196,6 @@ test("Transfer call receiver panics", async (t) => {
{
receiver_id: nftReceiver.accountId,
token_id: "0",
approval_id: null,
memo: "transfer & call",
msg: "incorrect message",
},
Expand All @@ -227,7 +221,6 @@ test("Transfer call receiver panics and nft_resolve_transfer produces no log if
{
receiver_id: nftReceiver.accountId,
token_id: "0",
approval_id: null,
memo: "transfer & call",
msg: "incorrect message",
},
Expand All @@ -254,7 +247,6 @@ test("Simple transfer no logs on failure", async (t) => {
{
receiver_id: nftOwner.accountId,
token_id: "0",
approval_id: null,
memo: "simple transfer",
},
{ attachedDeposit: "1" }
Expand Down
13 changes: 2 additions & 11 deletions examples/__tests__/standard-nft/test_enumeration.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ test("Enumerate NFT tokens total supply", async (t) => {
test("Enumerate NFT tokens", async (t) => {
const { nft } = t.context.accounts;

let nftTokens = await nft.view("nft_tokens", { from_index: 1, limit: null });
let nftTokens = await nft.view("nft_tokens", { from_index: 1 });
t.is(nftTokens.length, 3);
t.is(nftTokens[0].token_id, "1");
t.is(nftTokens[1].token_id, "2");
t.is(nftTokens[2].token_id, "3");

nftTokens = await nft.view("nft_tokens", { from_index: null, limit: 2 });
nftTokens = await nft.view("nft_tokens", { limit: 2 });
t.is(nftTokens.length, 2);
t.is(nftTokens[0].token_id, "0");
t.is(nftTokens[1].token_id, "1");
Expand All @@ -123,8 +123,6 @@ test("Enumerate NFT tokens for owner", async (t) => {

let nftTokens = await nft.view("nft_tokens_for_owner", {
account_id: nftOwner.accountId,
from_index: null,
limit: null,
});
t.is(nftTokens.length, 4);
t.is(nftTokens[0].token_id, "0");
Expand All @@ -135,7 +133,6 @@ test("Enumerate NFT tokens for owner", async (t) => {
nftTokens = await nft.view("nft_tokens_for_owner", {
account_id: nftOwner.accountId,
from_index: 1,
limit: null,
});
t.is(nftTokens.length, 3);
t.is(nftTokens[0].token_id, "1");
Expand All @@ -144,7 +141,6 @@ test("Enumerate NFT tokens for owner", async (t) => {

nftTokens = await nft.view("nft_tokens_for_owner", {
account_id: nftOwner.accountId,
from_index: null,
limit: 2,
});
t.is(nftTokens.length, 2);
Expand All @@ -157,7 +153,6 @@ test("Enumerate NFT tokens for owner", async (t) => {
{
receiver_id: ali.accountId,
token_id: "0",
approval_id: null,
memo: "simple transfer",
},
{ attachedDeposit: "1" }
Expand All @@ -166,15 +161,11 @@ test("Enumerate NFT tokens for owner", async (t) => {

nftTokens = await nft.view("nft_tokens_for_owner", {
account_id: ali.accountId,
from_index: null,
limit: null,
});
t.is(nftTokens.length, 1);

nftTokens = await nft.view("nft_tokens_for_owner", {
account_id: nftOwner.accountId,
from_index: null,
limit: null,
});
t.is(nftTokens.length, 3);
t.is(nftTokens[0].token_id, "3");
Expand Down
48 changes: 48 additions & 0 deletions examples/__tests__/test-cross-contract-call-loop.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Worker, NEAR } from "near-workspaces";
import test from "ava";

test.beforeEach(async (t) => {
const worker = await Worker.init();
const root = worker.rootAccount;

const xccLoop = await root.createSubAccount("xcc-loop");
await xccLoop.deploy("./build/cross-contract-call-loop.wasm");

const firstContract = await root.createSubAccount("first-contract");
const secondContract = await root.createSubAccount("second-contract");
const thirdContract = await root.createSubAccount("third-contract");
await firstContract.deploy("./build/counter.wasm");
await secondContract.deploy("./build/counter.wasm");
await thirdContract.deploy("./build/counter.wasm");

await root.call(firstContract, "increase", {});
await root.call(secondContract, "increase", {});
await root.call(thirdContract, "increase", {});

const alice = await root.createSubAccount("alice", {
initialBalance: NEAR.parse("100 N").toJSON(),
});

t.context.worker = worker;
t.context.accounts = { root, alice, xccLoop };
});

test.afterEach.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("should have a count of 3 after calling incrementCount", async (t) => {
const { xccLoop, alice } = t.context.accounts;
const expected = 3;
const callbackResult = await alice.call(
xccLoop,
"incrementCount",
{},
{ gas: "300" + "0".repeat(12) }
);
t.is(callbackResult, 3);
const result = await xccLoop.view("getCount");
t.deepEqual(result, expected);
});
49 changes: 49 additions & 0 deletions examples/__tests__/test-programmatic-update.ava.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Worker } from "near-workspaces";
import test from "ava";
import * as fs from "fs";
import * as path from "path";

test.beforeEach(async (t) => {
const worker = await Worker.init();
const root = worker.rootAccount;

const ali = await root.createSubAccount("ali");

const contract = await root.devDeploy(
"build/programmatic-update-before.wasm"
);

await contract.call(contract, "init", { manager: ali.accountId });

t.context.worker = worker;
t.context.accounts = { root, contract, ali };
});

test.afterEach.always(async (t) => {
await t.context.worker.tearDown().catch((error) => {
console.log("Failed to tear down the worker:", error);
});
});

test("the contract can be programmatically updated", async (t) => {
const { ali, contract } = t.context.accounts;

// ASSERT BEFORE CODE UPDATE
const codeBefore = await contract.viewCodeRaw();
const beforeDefaultGreeting = await contract.view("get_greeting", {});
t.is(beforeDefaultGreeting, "Hello");

// ACT (UPDATE CODE)
const code = fs.readFileSync(
path.resolve("./build/programmatic-update-after.wasm")
);
await ali.call(contract, "updateContract", code, {
gas: "300" + "0".repeat(12), // 300 Tgas
});

// ASSERT AFTER CODE UPDATE
const codeAfter = await contract.viewCodeRaw();
const afterDefaultGreeting = await contract.view("view_greeting", {});
t.not(codeBefore, codeAfter, "code should be different after update");
t.is(afterDefaultGreeting, "Hi");
});
4 changes: 4 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
"build:counter-lowlevel": "near-sdk-js build src/counter-lowlevel.js build/counter-lowlevel.wasm",
"build:counter-ts": "near-sdk-js build src/counter.ts build/counter-ts.wasm",
"build:cross-contract-call": "near-sdk-js build src/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-call.js build/cross-contract-call.wasm",
"build:cross-contract-call-loop": "near-sdk-js build src/counter.js build/counter.wasm && near-sdk-js build src/cross-contract-call-loop.js build/cross-contract-call-loop.wasm",
"build:fungible-token-lockable": "near-sdk-js build src/fungible-token-lockable.js build/fungible-token-lockable.wasm",
"build:fungible-token": "near-sdk-js build src/fungible-token.ts build/fungible-token.wasm && near-sdk-js build src/fungible-token-helper.ts build/fungible-token-helper.wasm",
"build:non-fungible-token": "near-sdk-js build src/non-fungible-token-receiver.js build/non-fungible-token-receiver.wasm && near-sdk-js build src/non-fungible-token.js build/non-fungible-token.wasm",
"build:status-message-collections": "near-sdk-js build src/status-message-collections.js build/status-message-collections.wasm",
"build:parking-lot": "near-sdk-js build src/parking-lot.ts build/parking-lot.wasm",
"build:programmatic-update": "near-sdk-js build src/programmatic-update-before.ts build/programmatic-update-before.wasm && near-sdk-js build src/programmatic-update-after.ts build/programmatic-update-after.wasm",
"build:nested-collections": "near-sdk-js build src/nested-collections.ts build/nested-collections.wasm",
"build-nft": "run-s build:nft-*",
"build:nft-contract": "near-sdk-js build src/standard-nft/my-nft.ts build/my-nft.wasm",
Expand All @@ -31,11 +33,13 @@
"test:counter-lowlevel": "COUNTER_LOWLEVEL=1 ava __tests__/test-counter.ava.js",
"test:counter-ts": "COUNTER_TS=1 ava __tests__/test-counter.ava.js",
"test:cross-contract-call": "ava __tests__/test-cross-contract-call.ava.js",
"test:cross-contract-call-loop": "ava __tests__/test-cross-contract-call-loop.ava.js",
"test:fungible-token-lockable": "ava __tests__/test-fungible-token-lockable.ava.js",
"test:fungible-token": "ava __tests__/test-fungible-token.ava.js",
"test:non-fungible-token": "ava __tests__/test-non-fungible-token.ava.js",
"test:status-message-collections": "ava __tests__/test-status-message-collections.ava.js",
"test:parking-lot": "ava __tests__/test-parking-lot.ava.js",
"test:programmatic-update": "ava __tests__/test-programmatic-update.ava.js",
"test:nested-collections": "ava __tests__/test-nested-collections.ava.js"
},
"author": "Near Inc <hello@nearprotocol.com>",
Expand Down
Loading

0 comments on commit 29e6fb9

Please sign in to comment.