forked from p-destri/clickdapp-frado
-
Notifications
You must be signed in to change notification settings - Fork 0
/
near-wallet.js
106 lines (90 loc) · 3.28 KB
/
near-wallet.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* A helper file that simplifies using the wallet selector */
// near api js
import { providers } from 'near-api-js';
// wallet selector
import '@near-wallet-selector/modal-ui/styles.css';
import { setupModal } from '@near-wallet-selector/modal-ui';
import { setupWalletSelector } from '@near-wallet-selector/core';
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
const THIRTY_TGAS = '30000000000000';
const NO_DEPOSIT = '0';
// Wallet that simplifies using the wallet selector
export class Wallet {
walletSelector;
wallet;
network;
createAccessKeyFor;
constructor({ createAccessKeyFor = undefined, network = 'testnet' }) {
// Login to a wallet passing a contractId will create a local
// key, so the user skips signing non-payable transactions.
// Omitting the accountId will result in the user being
// asked to sign all transactions.
this.createAccessKeyFor = createAccessKeyFor
this.network = 'testnet'
}
// To be called when the website loads
async startUp() {
this.walletSelector = await setupWalletSelector({
network: this.network,
modules: [setupMyNearWallet()],
});
const isSignedIn = this.walletSelector.isSignedIn();
if (isSignedIn) {
this.wallet = await this.walletSelector.wallet();
this.accountId = this.walletSelector.store.getState().accounts[0].accountId;
}
return isSignedIn;
}
// Sign-in method
signIn() {
const description = 'Please select a wallet to sign in.';
const modal = setupModal(this.walletSelector, { contractId: this.createAccessKeyFor, description });
modal.show();
}
// Sign-out method
signOut() {
this.wallet.signOut();
this.wallet = this.accountId = this.createAccessKeyFor = null;
window.location.replace(window.location.origin + window.location.pathname);
}
// Make a read-only call to retrieve information from the network
async viewMethod({ contractId, method, args = {} }) {
const { network } = this.walletSelector.options;
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
let res = await provider.query({
request_type: 'call_function',
account_id: contractId,
method_name: method,
args_base64: Buffer.from(JSON.stringify(args)).toString('base64'),
finality: 'optimistic',
});
return JSON.parse(Buffer.from(res.result).toString());
}
// Call a method that changes the contract's state
async callMethod({ contractId, method, args = {}, gas = THIRTY_TGAS, deposit = NO_DEPOSIT }) {
// Sign a transaction with the "FunctionCall" action
return await this.wallet.signAndSendTransaction({
signerId: this.accountId,
receiverId: contractId,
actions: [
{
type: 'FunctionCall',
params: {
methodName: method,
args,
gas,
deposit,
},
},
],
});
}
// Get transaction result from the network
async getTransactionResult(txhash) {
const { network } = this.walletSelector.options;
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
// Retrieve transaction result from the network
const transaction = await provider.txStatus(txhash, 'unnused');
return providers.getTransactionLastResult(transaction);
}
}