forked from thisisjoshford/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
60 lines (49 loc) · 1.77 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Load environment variables
require("dotenv").config();
// Load Near SDK components
const near = require("near-api-js");
// Directory where Near credentials are going to be stored
const credentialsPath = "./credentials";
// Configure the keyStore to be used with the SDK
const UnencryptedFileSystemKeyStore = near.keyStores.UnencryptedFileSystemKeyStore;
const keyStore = new UnencryptedFileSystemKeyStore(credentialsPath)
// Setup default client options
const options = {
networkId: process.env.NEAR_NETWORK,
nodeUrl: process.env.NEAR_NODE_URL,
walletUrl: `https://wallet.${process.env.NEAR_NETWORK}.near.org`,
helperUrl: `https://helper.${process.env.NEAR_NETWORK}.near.org`,
explorerUrl: `https://explorer.${process.env.NEAR_NETWORK}.near.org`,
accountId: process.env.NEAR_ACCOUNT,
deps: {
keyStore: keyStore
}
}
async function main() {
// Configure the client with options and our local key store
const client = await near.connect(options);
const account = await client.account(options.accountId);
const contractName = options.accountId;
// Construct a new contract object, we'll be using it to perform calls
const contract = new near.Contract(account, contractName, {
viewMethods: ["getValue"],
changeMethods: ["setValue"],
sender: options.accountId,
});
// Assign a new value
const value = (new Date()).toString();
console.log(`Calling contract call 'setValue' with '${value}'`);
await contract.setValue({ value: value });
// Get the value we assigned
console.log("Getting current value");
result = await contract.getValue();
console.log("Result:", result);
// Alternative way of calling a function
result = await account.functionCall(
contractName,
"getValue",
{}
);
console.log(result);
};
main();