/
utils.js
50 lines (37 loc) · 1.86 KB
/
utils.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
import { connect, Contract, keyStores, WalletConnection } from 'near-api-js'
import getConfig from './config'
// Initialize contract & set global variables
export async function initContract() {
// Initialize connection to the NEAR testnet
const nearConfig = getConfig(process.env.NODE_ENV || 'development')
const near = await connect(Object.assign({ deps: { keyStore: new keyStores.BrowserLocalStorageKeyStore() } }, nearConfig));
window.nearConfig = nearConfig
window.near = near
// Initializing Wallet based Account. It can work with NEAR testnet wallet that
// is hosted at https://wallet.testnet.near.org
window.walletConnection = new WalletConnection(near)
// Getting the Account ID. If still unauthorized, it's just empty string
window.accountId = window.walletConnection.getAccountId()
// Initializing our contract APIs by contract name and configuration
window.contract = await new Contract(window.walletConnection.account(), nearConfig.contractName, {
// View methods are read only. They don't modify the state, but usually return some value.
viewMethods: ['get_article_by_id', 'get_greeting'],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ['add_new_article_default', 'send_payout'],
})
}
export function logout() {
window.walletConnection.signOut()
// reload page
window.location.replace(window.location.origin + window.location.pathname)
}
export function login() {
// Allow the current app to make calls to the specified contract on the
// user's behalf.
// This works by creating a new access key for the user's account and storing
// the private key in localStorage.
window.walletConnection.requestSignIn(nearConfig.contractName)
}
// export function create_article(article_number) {
// window.contract.add_new_article_default(article_number)
// }