forked from austinabell/near-crossword
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
39 lines (32 loc) · 1.63 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
import { connect, Contract, keyStores, WalletConnection } from 'near-api-js'
import getConfig from './config'
const nearConfig = getConfig(process.env.NODE_ENV || 'development')
// Initialize contract & set global variables
export async function initContract() {
// Initialize connection to the NEAR testnet
const near = await connect(Object.assign({ deps: { keyStore: new keyStores.BrowserLocalStorageKeyStore() } }, nearConfig))
// 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_greeting'],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ['set_greeting'],
})
}
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)
}