Skip to content

Commit

Permalink
example e2e test written with near-api-js run on near-sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
ailisp committed Jun 28, 2021
1 parent bf29993 commit d92dacb
Show file tree
Hide file tree
Showing 4 changed files with 344 additions and 1 deletion.
46 changes: 46 additions & 0 deletions borsh.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const borsh = require("borsh");

class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
this[key] = properties[key];
});
}
}

class StatusMessage extends Assignable {}

class Record extends Assignable {}

const schema = new Map([
[StatusMessage, { kind: "struct", fields: [["records", [Record]]] }],
[
Record,
{
kind: "struct",
fields: [
["k", "string"],
["v", "string"],
],
},
],
]);

const stateKey = "U1RBVEU=";
console.log(Buffer.from(stateKey, "base64"));
console.log(Buffer.from(stateKey, "base64").toString());
const stateValue =
"AgAAAA8AAABhbGljZS50ZXN0Lm5lYXIFAAAAaGVsbG8NAAAAYm9iLnRlc3QubmVhcgUAAAB3b3JsZA==";
const stateValueBuffer = Buffer.from(stateValue, "base64");
let statusMessage = borsh.deserialize(schema, StatusMessage, stateValueBuffer);
console.log(statusMessage);

console.log(
Buffer.from(borsh.serialize(schema, statusMessage)).toString("base64")
);
statusMessage.records.push(new Record({ k: "alice.near", v: "hello world" }));
console.log(statusMessage);

console.log(
Buffer.from(borsh.serialize(schema, statusMessage)).toString("base64")
);
169 changes: 168 additions & 1 deletion package-lock.json

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@
"url": "https://github.com/near-examples/rust-status-message/issues"
},
"homepage": "https://github.com/near-examples/rust-status-message#readme",
"dependencies": {
"borsh": "^0.4.0",
"near-api-js": "near/near-api-js#bace1ee"
},
"devDependencies": {}
}
126 changes: 126 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const nearAPI = require("near-api-js");
const BN = require("bn.js");
const fs = require("fs").promises;
const assert = require("assert").strict;

function getConfig(env) {
switch (env) {
case "sandbox":
case "local":
return {
networkId: "sandbox",
nodeUrl: "http://localhost:3030",
masterAccount: "test.near",
contractAccount: "status-message.test.near",
keyPath: "/tmp/near-sandbox/validator_key.json",
};
}
}

const contractMethods = {
viewMethods: ["get_status"],
changeMethods: ["set_status"],
};
let config;
let masterAccount;
let masterKey;
let pubKey;
let keyStore;
let near;

async function initNear() {
config = getConfig(process.env.NEAR_ENV || "sandbox");
const keyFile = require(config.keyPath);
masterKey = nearAPI.utils.KeyPair.fromString(
keyFile.secret_key || keyFile.private_key
);
pubKey = masterKey.getPublicKey();
keyStore = new nearAPI.keyStores.InMemoryKeyStore();
keyStore.setKey(config.networkId, config.masterAccount, masterKey);
near = await nearAPI.connect({
deps: {
keyStore,
},
networkId: config.networkId,
nodeUrl: config.nodeUrl,
});
masterAccount = new nearAPI.Account(near.connection, config.masterAccount);
console.log("Finish init NEAR");
}

async function createContractUser(
accountPrefix,
contractAccountId,
contractMethods
) {
let accountId = accountPrefix + "." + config.masterAccount;
await masterAccount.createAccount(
accountId,
pubKey,
new BN(10).pow(new BN(25))
);
keyStore.setKey(config.networkId, accountId, masterKey);
const account = new nearAPI.Account(near.connection, accountId);
const accountUseContract = new nearAPI.Contract(
account,
contractAccountId,
contractMethods
);
return accountUseContract;
}

async function initTest() {
const contract = await fs.readFile("./res/status_message.wasm");
const _contractAccount = await masterAccount.createAndDeployContract(
config.contractAccount,
pubKey,
contract,
new BN(10).pow(new BN(25))
);

const aliceUseContract = await createContractUser(
"alice",
config.contractAccount,
contractMethods
);

const bobUseContract = await createContractUser(
"bob",
config.contractAccount,
contractMethods
);
console.log("Finish deploy contracts and create test accounts");
return { aliceUseContract, bobUseContract };
}

async function test() {
// 1. Creates testing accounts and deploys a contract
await initNear();
const { aliceUseContract, bobUseContract } = await initTest();

// 2. Performs a `set_status` transaction signed by Alice and then calls `get_status` to confirm `set_status` worked
await aliceUseContract.set_status({ args: { message: "hello" } });
let alice_message = await aliceUseContract.get_status({
account_id: "alice.test.near",
});
assert.equal(alice_message, "hello");

// 3. Gets Bob's status and which should be `null` as Bob has not yet set status
let bob_message = await bobUseContract.get_status({
account_id: "bob.test.near",
});
assert.equal(bob_message, null);

// 4. Performs a `set_status` transaction signed by Bob and then calls `get_status` to show Bob's changed status and should not affect Alice's status
await bobUseContract.set_status({ args: { message: "world" } });
bob_message = await bobUseContract.get_status({
account_id: "bob.test.near",
});
assert.equal(bob_message, "world");
alice_message = await aliceUseContract.get_status({
account_id: "alice.test.near",
});
assert.equal(alice_message, "hello");
}

test();

0 comments on commit d92dacb

Please sign in to comment.