-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example e2e test written with near-api-js run on near-sandbox
- Loading branch information
Showing
4 changed files
with
344 additions
and
1 deletion.
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
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") | ||
); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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(); |