1
- // near api js
2
- import { providers } from 'near-api-js' ;
3
-
4
1
// wallet selector
5
- import { distinctUntilChanged , map } from 'rxjs' ;
6
2
import '@near-wallet-selector/modal-ui/styles.css' ;
7
- import { setupModal } from '@near-wallet-selector/modal-ui' ;
3
+
4
+ import { setupBitteWallet } from '@near-wallet-selector/bitte-wallet' ;
8
5
import { setupWalletSelector } from '@near-wallet-selector/core' ;
9
- import { setupHereWallet } from '@near-wallet-selector/here-wallet' ;
6
+ import { setupEthereumWallets } from '@near-wallet-selector/ethereum-wallets' ;
7
+ import { setupLedger } from '@near-wallet-selector/ledger' ;
8
+ import { setupMeteorWallet } from '@near-wallet-selector/meteor-wallet' ;
9
+ import { setupModal } from '@near-wallet-selector/modal-ui' ;
10
10
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet' ;
11
+ import { setupSender } from '@near-wallet-selector/sender' ;
12
+ import { setupHereWallet } from '@near-wallet-selector/here-wallet' ;
13
+ import { setupNearMobileWallet } from '@near-wallet-selector/near-mobile-wallet' ;
14
+ import { setupWelldoneWallet } from '@near-wallet-selector/welldone-wallet' ;
15
+
16
+
17
+ // near api js
18
+ import { providers , utils } from 'near-api-js' ;
19
+ import { createContext } from 'react' ;
20
+
21
+ // ethereum wallets
22
+ import { wagmiConfig , web3Modal } from '@/wallets/web3modal' ;
11
23
12
24
const THIRTY_TGAS = '30000000000000' ;
13
25
const NO_DEPOSIT = '0' ;
@@ -30,27 +42,32 @@ export class Wallet {
30
42
/**
31
43
* To be called when the website loads
32
44
* @param {Function } accountChangeHook - a function that is called when the user signs in or out#
33
- * @returns {Promise<string> } - the accountId of the signed-in user
45
+ * @returns {Promise<string> } - the accountId of the signed-in user
34
46
*/
35
47
startUp = async ( accountChangeHook ) => {
36
48
this . selector = setupWalletSelector ( {
37
49
network : this . networkId ,
38
- modules : [ setupMyNearWallet ( ) , setupHereWallet ( ) ]
50
+ modules : [
51
+ setupMeteorWallet ( ) ,
52
+ setupEthereumWallets ( { wagmiConfig, web3Modal, alwaysOnboardDuringSignIn : true } ) ,
53
+ setupLedger ( ) ,
54
+ setupBitteWallet ( ) ,
55
+ setupHereWallet ( ) ,
56
+ setupSender ( ) ,
57
+ setupNearMobileWallet ( ) ,
58
+ setupWelldoneWallet ( ) ,
59
+ setupMyNearWallet ( ) ,
60
+ ] ,
39
61
} ) ;
40
62
41
63
const walletSelector = await this . selector ;
42
64
const isSignedIn = walletSelector . isSignedIn ( ) ;
43
65
const accountId = isSignedIn ? walletSelector . store . getState ( ) . accounts [ 0 ] . accountId : '' ;
44
66
45
- walletSelector . store . observable
46
- . pipe (
47
- map ( state => state . accounts ) ,
48
- distinctUntilChanged ( )
49
- )
50
- . subscribe ( accounts => {
51
- const signedAccount = accounts . find ( ( account ) => account . active ) ?. accountId ;
52
- accountChangeHook ( signedAccount ) ;
53
- } ) ;
67
+ walletSelector . store . observable . subscribe ( async ( state ) => {
68
+ const signedAccount = state ?. accounts . find ( ( account ) => account . active ) ?. accountId ;
69
+ accountChangeHook ( signedAccount || '' ) ;
70
+ } ) ;
54
71
55
72
return accountId ;
56
73
} ;
@@ -83,7 +100,7 @@ export class Wallet {
83
100
const url = `https://rpc.${ this . networkId } .near.org` ;
84
101
const provider = new providers . JsonRpcProvider ( { url } ) ;
85
102
86
- let res = await provider . query ( {
103
+ const res = await provider . query ( {
87
104
request_type : 'call_function' ,
88
105
account_id : contractId ,
89
106
method_name : method ,
@@ -93,7 +110,6 @@ export class Wallet {
93
110
return JSON . parse ( Buffer . from ( res . result ) . toString ( ) ) ;
94
111
} ;
95
112
96
-
97
113
/**
98
114
* Makes a call to a contract
99
115
* @param {Object } options - the options for the call
@@ -126,7 +142,7 @@ export class Wallet {
126
142
} ;
127
143
128
144
/**
129
- * Retrieves transaction result from the network
145
+ * Makes a call to a contract
130
146
* @param {string } txhash - the transaction hash
131
147
* @returns {Promise<JSON.value> } - the result of the transaction
132
148
*/
@@ -135,7 +151,77 @@ export class Wallet {
135
151
const { network } = walletSelector . options ;
136
152
const provider = new providers . JsonRpcProvider ( { url : network . nodeUrl } ) ;
137
153
138
- const transaction = await provider . txStatus ( txhash , 'unnused' ) ;
154
+ // Retrieve transaction result from the network
155
+ const transaction = await provider . txStatus ( txhash , 'unused' ) ;
139
156
return providers . getTransactionLastResult ( transaction ) ;
140
157
} ;
158
+
159
+ /**
160
+ * Gets the balance of an account
161
+ * @param {string } accountId - the account id to get the balance of
162
+ * @param {boolean } format - whether to format the balance
163
+ * @returns {Promise<number> } - the balance of the account
164
+ *
165
+ */
166
+ getBalance = async ( accountId , format = false ) => {
167
+ const walletSelector = await this . selector ;
168
+ const { network } = walletSelector . options ;
169
+ const provider = new providers . JsonRpcProvider ( { url : network . nodeUrl } ) ;
170
+
171
+ // Retrieve account state from the network
172
+ const account = await provider . query ( {
173
+ request_type : 'view_account' ,
174
+ account_id : accountId ,
175
+ finality : 'final' ,
176
+ } ) ;
177
+
178
+ // Format the amount if needed
179
+ if ( format ) {
180
+ return account . amount ? utils . format . formatNearAmount ( account . amount ) : '0' ;
181
+ } else {
182
+ return account . amount || '0' ;
183
+ }
184
+ } ;
185
+
186
+ /**
187
+ * Signs and sends transactions
188
+ * @param {Object[] } transactions - the transactions to sign and send
189
+ * @returns {Promise<Transaction[]> } - the resulting transactions
190
+ *
191
+ */
192
+ signAndSendTransactions = async ( { transactions } ) => {
193
+ const selectedWallet = await ( await this . selector ) . wallet ( ) ;
194
+ return selectedWallet . signAndSendTransactions ( { transactions } ) ;
195
+ } ;
196
+
197
+ /**
198
+ *
199
+ * @param {string } accountId
200
+ * @returns {Promise<Object[]> } - the access keys for the
201
+ */
202
+ getAccessKeys = async ( accountId ) => {
203
+ const walletSelector = await this . selector ;
204
+ const { network } = walletSelector . options ;
205
+ const provider = new providers . JsonRpcProvider ( { url : network . nodeUrl } ) ;
206
+
207
+ // Retrieve account state from the network
208
+ const keys = await provider . query ( {
209
+ request_type : 'view_access_key_list' ,
210
+ account_id : accountId ,
211
+ finality : 'final' ,
212
+ } ) ;
213
+ return keys . keys ;
214
+ } ;
141
215
}
216
+
217
+ /**
218
+ * @typedef NearContext
219
+ * @property {import('./wallets/near').Wallet } wallet Current wallet
220
+ * @property {string } signedAccountId The AccountId of the signed user
221
+ */
222
+
223
+ /** @type {import ('react').Context<NearContext> } */
224
+ export const NearContext = createContext ( {
225
+ wallet : undefined ,
226
+ signedAccountId : '' ,
227
+ } ) ;
0 commit comments