-
Notifications
You must be signed in to change notification settings - Fork 170
/
Copy pathWalletSelectorProvider.tsx
298 lines (263 loc) · 8.56 KB
/
WalletSelectorProvider.tsx
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import { createContext, useState, useEffect, useCallback, useRef } from "react";
import type {
FinalExecutionOutcome,
SignedMessage,
Transaction,
Wallet,
WalletSelector,
WalletSelectorParams,
} from "@near-wallet-selector/core";
import { setupWalletSelector } from "@near-wallet-selector/core";
import { setupModal } from "@near-wallet-selector/modal-ui";
import { providers } from "near-api-js";
import type { QueryResponseKind } from "near-api-js/lib/providers/provider";
class WalletError extends Error {
constructor(message: string) {
super(message);
this.name = "WalletError";
}
}
export interface QueryResponseKindWithAmount extends QueryResponseKind {
amount: string;
}
export interface ViewMethodParams {
contractId: string;
method: string;
args?: Record<string, unknown>;
}
export interface CallMethodParams extends ViewMethodParams {
gas?: string | number | bigint;
deposit?: string | bigint;
}
export interface SignMessageParams {
message: string;
recipient: string;
nonce: Buffer;
}
export type SetupParams = WalletSelectorParams & {
createAccessKeyFor?: string;
};
export interface WalletSelectorProviderValue {
walletSelector: Promise<WalletSelector> | null;
signedAccountId: string | null;
wallet: Wallet | null;
signIn: () => void;
signOut: () => Promise<void>;
viewFunction: (params: ViewMethodParams) => Promise<unknown>;
callFunction: (params: CallMethodParams) => Promise<unknown>;
getBalance: (accountId: string) => Promise<bigint>;
getAccessKeys: (accountId: string) => Promise<Array<unknown>>;
signAndSendTransactions: (params: {
transactions: Array<Transaction>;
}) => Promise<void | Array<FinalExecutionOutcome>>;
signMessage: (params: SignMessageParams) => Promise<void | SignedMessage>;
}
const DEFAULT_GAS = "30000000000000";
const DEFAULT_DEPOSIT = "0";
export const WalletSelectorContext = createContext<
WalletSelectorProviderValue | undefined
>(undefined);
export function WalletSelectorProvider({
children,
config,
}: {
children: React.ReactNode;
config: SetupParams;
}) {
const walletSelectorRef = useRef<Promise<WalletSelector> | null>(null);
const [signedAccountId, setSignedAccountId] = useState<string | null>(null);
const [wallet, setWallet] = useState<Wallet | null>(null);
const rpcProviderUrls =
config.fallbackRpcUrls && config.fallbackRpcUrls.length > 0
? config.fallbackRpcUrls
: [`https://rpc.${config.network}.near.org`];
const provider = new providers.FailoverRpcProvider(
rpcProviderUrls.map((url) => new providers.JsonRpcProvider({ url }))
);
useEffect(() => {
const walletSelector = setupWalletSelector(config);
walletSelectorRef.current = walletSelector;
walletSelector.then(async (selector) => {
selector.store.observable.subscribe(async (state) => {
const signedAccount = state?.accounts.find(
(account) => account.active
)?.accountId;
setSignedAccountId(signedAccount || null);
if (signedAccount) {
const walletInstance = await selector.wallet();
setWallet(walletInstance);
} else {
setWallet(null);
}
});
});
}, [config]);
/**
* Displays a modal for the user to sign in
* @returns {Promise<void>} - a promise that resolves when the modal is opened
*/
const signIn = async () => {
const ws = await walletSelectorRef.current;
const modalInstance = setupModal(ws!, {
contractId: config.createAccessKeyFor || "",
});
modalInstance.show();
};
/**
* Logs out the wallet
*/
const signOut = useCallback(async () => {
if (!wallet) {
throw new WalletError("No wallet connected");
}
await wallet.signOut();
}, [wallet]);
/**
* Makes a read-only call to a contract
* @param {Object} options - the options for the call
* @param {string} options.contractId - the contract's account id
* @param {string} options.method - the method to call
* @param {Object} options.args - the arguments to pass to the method
* @returns {Promise<JSON.value>} - the result of the method call
*/
const viewFunction = async ({
contractId,
method,
args = {},
}: ViewMethodParams) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const res = await provider.query<any>({
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());
};
/**
* Makes a call to a contract
* @param {Object} options - the options for the call
* @param {string} options.contractId - the contract's account id
* @param {string} options.method - the method to call
* @param {Object} options.args - the arguments to pass to the method
* @param {string} options.gas - the amount of gas to use
* @param {string} options.deposit - the amount of yoctoNEAR to deposit
* @returns {Promise<Transaction>} - the resulting transaction
*/
const callFunction = useCallback(
async ({
contractId,
method,
args = {},
gas = DEFAULT_GAS,
deposit = DEFAULT_DEPOSIT,
}: CallMethodParams) => {
if (!wallet) {
throw new WalletError("No wallet connected");
}
const outcome = await wallet.signAndSendTransaction({
receiverId: contractId,
actions: [
{
type: "FunctionCall",
params: {
methodName: method,
args,
gas: gas.toString(),
deposit: deposit.toString(),
},
},
],
});
return providers.getTransactionLastResult(
outcome as FinalExecutionOutcome
);
},
[wallet]
);
/**
* Gets the balance of an account in yoctoNEAR
* @param {string} accountId - the account id to get the balance of
* @returns {Promise<number>} - the balance of the account
*/
const getBalance = async (accountId: string): Promise<bigint> => {
const account = (await provider.query({
request_type: "view_account",
account_id: accountId,
finality: "final",
})) as QueryResponseKindWithAmount;
return account.amount ? BigInt(account.amount) : BigInt(0);
};
/**
* Gets the access keys for an account
* @param {string} accountId - the account id to get the access keys for
* @returns {Promise<Object[]>} - the access keys for the
*/
const getAccessKeys = async (accountId: string): Promise<Array<unknown>> => {
// Retrieve account state from the network
const keys = await provider.query({
request_type: "view_access_key_list",
account_id: accountId,
finality: "final",
});
return (keys as unknown as { keys: Array<unknown> }).keys;
};
/**
* Signs transactions and broadcasts them to the network
* @param {Object[]} transactions - the transactions to sign and send
* @returns {Promise<Transaction[]>} - the resulting transactions
*/
const signAndSendTransactions = useCallback(
async ({ transactions }: { transactions: Array<Transaction> }) => {
if (!wallet) {
throw new WalletError("No wallet connected");
}
const sentTxs = (await wallet.signAndSendTransactions({
transactions,
})) as Array<FinalExecutionOutcome>;
return sentTxs.map((tx: FinalExecutionOutcome) =>
providers.getTransactionLastResult(tx)
);
},
[wallet]
);
/**
* Signs a message off-chain
* @param {Object} options - the options for the message
* @param {string} options.message - the message to sign
* @param {string} options.recipient - the recipient of the message
* @param {Buffer} options.nonce - the nonce of the message
* @returns {Promise<SignedMessage>} - the signed message
*/
const signMessage = useCallback(
({ message, recipient, nonce }: SignMessageParams) => {
if (!wallet) {
throw new WalletError("No wallet connected");
}
if (!wallet.signMessage) {
throw new WalletError("Wallet does not support message signing");
}
return wallet.signMessage({ message, recipient, nonce });
},
[wallet]
);
const contextValue: WalletSelectorProviderValue = {
walletSelector: walletSelectorRef.current,
signedAccountId,
wallet,
signIn,
signOut,
viewFunction,
callFunction,
getBalance,
getAccessKeys,
signAndSendTransactions,
signMessage,
};
return (
<WalletSelectorContext.Provider value={contextValue}>
{children}
</WalletSelectorContext.Provider>
);
}