-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnear-wallet.js
129 lines (111 loc) · 4.04 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* A helper file that simplifies using the wallet selector */
// near api js
// wallet selector UI
import '@near-wallet-selector/modal-ui/styles.css';
import MyNearIconUrl from '@near-wallet-selector/my-near-wallet/assets/my-near-wallet-icon.png';
// wallet selector options
import { setupWalletSelector } from '@near-wallet-selector/core';
import { setupKeypom } from '@keypom/selector';
import { JsonRpcProvider } from '@near-js/providers';
import {
getTransactionLastResult,
} from '@near-js/utils';
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
import { setupModal } from '@near-wallet-selector/modal-ui';
import { KEYPOM_OPTIONS } from './keypom-data';
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 = network
}
// To be called when the website loads
async startUp() {
this.walletSelector = await setupWalletSelector({
network: this.network,
modules: [
setupMyNearWallet({ iconUrl: MyNearIconUrl }),
setupKeypom({
networkId: this.network,
signInContractId: this.createAccessKeyFor,
trialAccountSpecs: {
url: "http://localhost:1234/trial-url#ACCOUNT_ID/SECRET_KEY",
modalOptions: KEYPOM_OPTIONS
},
instantSignInSpecs: {
url: "http://localhost:1234/instant-url#ACCOUNT_ID/SECRET_KEY/MODULE_ID",
}
})
],
});
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 #.';
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 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
const outcome = await this.wallet.signAndSendTransaction({
signerId: this.accountId,
receiverId: contractId,
actions: [
{
type: 'FunctionCall',
params: {
methodName: method,
args,
gas,
deposit,
},
},
],
});
return getTransactionLastResult(outcome)
}
// Get transaction result from the network
async getTransactionResult(txhash) {
const { network } = this.walletSelector.options;
const provider = new JsonRpcProvider({ url: network.nodeUrl });
// Retrieve transaction result from the network
const transaction = await provider.txStatus(txhash, 'unnused');
return getTransactionLastResult(transaction);
}
}