create-near-app 8.1.0 → 8.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app.js +11 -1
- package/dist/make.js +23 -1
- package/dist/messages.js +48 -27
- package/dist/tracking.js +1 -1
- package/dist/utils/checkCargoNear.js +15 -0
- package/dist/utils/donwloadFile.js +25 -0
- package/dist/utils/index.js +11 -0
- package/package.json +2 -1
- package/templates/contracts/rs/.github/workflows/deploy-production.yml +2 -4
- package/templates/contracts/rs/.github/workflows/deploy-staging.yml +4 -8
- package/templates/contracts/rs/.github/workflows/test.yml +2 -0
- package/templates/contracts/rs/.github/workflows/undeploy-staging.yml +2 -4
- package/templates/contracts/rs/Cargo.toml +15 -9
- package/templates/contracts/rs/README.md +6 -0
- package/templates/contracts/rs/rust-toolchain.toml +2 -2
- package/templates/frontend/next-app/next.config.js +1 -0
- package/templates/frontend/next-app/package.json +23 -17
- package/templates/frontend/next-app/src/app/hello-near/page.js +7 -9
- package/templates/frontend/next-app/src/app/layout.js +34 -9
- package/templates/frontend/next-app/src/components/navigation.js +7 -9
- package/templates/frontend/next-app/src/config.js +1 -18
- package/templates/frontend/next-app/src/wallets/web3modal.js +24 -40
- package/templates/frontend/next-page/next.config.js +1 -0
- package/templates/frontend/next-page/package.json +23 -17
- package/templates/frontend/next-page/src/components/navigation.js +7 -9
- package/templates/frontend/next-page/src/config.js +1 -18
- package/templates/frontend/next-page/src/pages/_app.js +34 -11
- package/templates/frontend/next-page/src/pages/hello-near/index.js +7 -9
- package/templates/frontend/next-page/src/wallets/web3modal.js +24 -40
- package/templates/frontend/vite-react/package.json +26 -26
- package/templates/frontend/vite-react/src/App.jsx +35 -15
- package/templates/frontend/vite-react/src/components/navigation.jsx +8 -10
- package/templates/frontend/vite-react/src/config.js +1 -18
- package/templates/frontend/vite-react/src/pages/hello_near.jsx +7 -9
- package/templates/frontend/vite-react/src/wallets/web3modal.js +24 -38
- package/templates/frontend/vite-react/vite.config.js +2 -15
- package/templates/contracts/rs/Cargo.lock +0 -4348
- package/templates/frontend/next-app/src/wallets/near.js +0 -216
- package/templates/frontend/next-page/src/wallets/near.js +0 -216
- package/templates/frontend/vite-react/src/wallets/near.js +0 -228
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'react';
|
|
2
|
-
|
|
3
|
-
// near api js
|
|
4
|
-
import { providers, utils } from 'near-api-js';
|
|
5
|
-
|
|
6
|
-
// wallet selector
|
|
7
|
-
import '@near-wallet-selector/modal-ui/styles.css';
|
|
8
|
-
import { setupModal } from '@near-wallet-selector/modal-ui';
|
|
9
|
-
import { setupWalletSelector } from '@near-wallet-selector/core';
|
|
10
|
-
import { setupHereWallet } from '@near-wallet-selector/here-wallet';
|
|
11
|
-
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
|
|
12
|
-
import { setupLedger } from '@near-wallet-selector/ledger';
|
|
13
|
-
import { setupMeteorWallet } from '@near-wallet-selector/meteor-wallet';
|
|
14
|
-
import { setupSender } from '@near-wallet-selector/sender';
|
|
15
|
-
import { setupBitteWallet } from '@near-wallet-selector/bitte-wallet';
|
|
16
|
-
|
|
17
|
-
// ethereum wallets
|
|
18
|
-
import { wagmiConfig, web3Modal } from '@/wallets/web3modal';
|
|
19
|
-
import { setupEthereumWallets } from "@near-wallet-selector/ethereum-wallets";
|
|
20
|
-
|
|
21
|
-
const THIRTY_TGAS = '30000000000000';
|
|
22
|
-
const NO_DEPOSIT = '0';
|
|
23
|
-
|
|
24
|
-
export class Wallet {
|
|
25
|
-
/**
|
|
26
|
-
* @constructor
|
|
27
|
-
* @param {Object} options - the options for the wallet
|
|
28
|
-
* @param {string} options.networkId - the network id to connect to
|
|
29
|
-
* @param {string} options.createAccessKeyFor - the contract to create an access key for
|
|
30
|
-
* @example
|
|
31
|
-
* const wallet = new Wallet({ networkId: 'testnet', createAccessKeyFor: 'contractId' });
|
|
32
|
-
* wallet.startUp((signedAccountId) => console.log(signedAccountId));
|
|
33
|
-
*/
|
|
34
|
-
constructor({ networkId = 'testnet', createAccessKeyFor = undefined }) {
|
|
35
|
-
this.createAccessKeyFor = createAccessKeyFor;
|
|
36
|
-
this.networkId = networkId;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* To be called when the website loads
|
|
41
|
-
* @param {Function} accountChangeHook - a function that is called when the user signs in or out#
|
|
42
|
-
* @returns {Promise<string>} - the accountId of the signed-in user
|
|
43
|
-
*/
|
|
44
|
-
startUp = async (accountChangeHook) => {
|
|
45
|
-
this.selector = setupWalletSelector({
|
|
46
|
-
network: this.networkId,
|
|
47
|
-
modules: [
|
|
48
|
-
setupMyNearWallet(),
|
|
49
|
-
setupHereWallet(),
|
|
50
|
-
setupLedger(),
|
|
51
|
-
setupMeteorWallet(),
|
|
52
|
-
setupSender(),
|
|
53
|
-
setupBitteWallet(),
|
|
54
|
-
setupEthereumWallets({ wagmiConfig, web3Modal, alwaysOnboardDuringSignIn: true }),
|
|
55
|
-
],
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
const walletSelector = await this.selector;
|
|
59
|
-
const isSignedIn = walletSelector.isSignedIn();
|
|
60
|
-
const accountId = isSignedIn ? walletSelector.store.getState().accounts[0].accountId : '';
|
|
61
|
-
|
|
62
|
-
walletSelector.store.observable.subscribe(async (state) => {
|
|
63
|
-
const signedAccount = state?.accounts.find(account => account.active)?.accountId;
|
|
64
|
-
accountChangeHook(signedAccount || '');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return accountId;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Displays a modal to login the user
|
|
72
|
-
*/
|
|
73
|
-
signIn = async () => {
|
|
74
|
-
const modal = setupModal(await this.selector, { contractId: this.createAccessKeyFor });
|
|
75
|
-
modal.show();
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Logout the user
|
|
80
|
-
*/
|
|
81
|
-
signOut = async () => {
|
|
82
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
83
|
-
selectedWallet.signOut();
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Makes a read-only call to a contract
|
|
88
|
-
* @param {Object} options - the options for the call
|
|
89
|
-
* @param {string} options.contractId - the contract's account id
|
|
90
|
-
* @param {string} options.method - the method to call
|
|
91
|
-
* @param {Object} options.args - the arguments to pass to the method
|
|
92
|
-
* @returns {Promise<JSON.value>} - the result of the method call
|
|
93
|
-
*/
|
|
94
|
-
viewMethod = async ({ contractId, method, args = {} }) => {
|
|
95
|
-
const url = `https://rpc.${this.networkId}.near.org`;
|
|
96
|
-
const provider = new providers.JsonRpcProvider({ url });
|
|
97
|
-
|
|
98
|
-
const res = await provider.query({
|
|
99
|
-
request_type: 'call_function',
|
|
100
|
-
account_id: contractId,
|
|
101
|
-
method_name: method,
|
|
102
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString('base64'),
|
|
103
|
-
finality: 'optimistic',
|
|
104
|
-
});
|
|
105
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Makes a call to a contract
|
|
110
|
-
* @param {Object} options - the options for the call
|
|
111
|
-
* @param {string} options.contractId - the contract's account id
|
|
112
|
-
* @param {string} options.method - the method to call
|
|
113
|
-
* @param {Object} options.args - the arguments to pass to the method
|
|
114
|
-
* @param {string} options.gas - the amount of gas to use
|
|
115
|
-
* @param {string} options.deposit - the amount of yoctoNEAR to deposit
|
|
116
|
-
* @returns {Promise<Transaction>} - the resulting transaction
|
|
117
|
-
*/
|
|
118
|
-
callMethod = async ({ contractId, method, args = {}, gas = THIRTY_TGAS, deposit = NO_DEPOSIT }) => {
|
|
119
|
-
// Sign a transaction with the "FunctionCall" action
|
|
120
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
121
|
-
const outcome = await selectedWallet.signAndSendTransaction({
|
|
122
|
-
receiverId: contractId,
|
|
123
|
-
actions: [
|
|
124
|
-
{
|
|
125
|
-
type: 'FunctionCall',
|
|
126
|
-
params: {
|
|
127
|
-
methodName: method,
|
|
128
|
-
args,
|
|
129
|
-
gas,
|
|
130
|
-
deposit,
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
],
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
return providers.getTransactionLastResult(outcome);
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Makes a call to a contract
|
|
141
|
-
* @param {string} txhash - the transaction hash
|
|
142
|
-
* @returns {Promise<JSON.value>} - the result of the transaction
|
|
143
|
-
*/
|
|
144
|
-
getTransactionResult = async (txhash) => {
|
|
145
|
-
const walletSelector = await this.selector;
|
|
146
|
-
const { network } = walletSelector.options;
|
|
147
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
148
|
-
|
|
149
|
-
// Retrieve transaction result from the network
|
|
150
|
-
const transaction = await provider.txStatus(txhash, 'unnused');
|
|
151
|
-
return providers.getTransactionLastResult(transaction);
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Gets the balance of an account
|
|
156
|
-
* @param {string} accountId - the account id to get the balance of
|
|
157
|
-
* @returns {Promise<number>} - the balance of the account
|
|
158
|
-
*
|
|
159
|
-
*/
|
|
160
|
-
getBalance = async (accountId) => {
|
|
161
|
-
const walletSelector = await this.selector;
|
|
162
|
-
const { network } = walletSelector.options;
|
|
163
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
164
|
-
|
|
165
|
-
// Retrieve account state from the network
|
|
166
|
-
const account = await provider.query({
|
|
167
|
-
request_type: 'view_account',
|
|
168
|
-
account_id: accountId,
|
|
169
|
-
finality: 'final',
|
|
170
|
-
});
|
|
171
|
-
// return amount on NEAR
|
|
172
|
-
return account.amount ? Number(utils.format.formatNearAmount(account.amount)) : 0;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Signs and sends transactions
|
|
177
|
-
* @param {Object[]} transactions - the transactions to sign and send
|
|
178
|
-
* @returns {Promise<Transaction[]>} - the resulting transactions
|
|
179
|
-
*
|
|
180
|
-
*/
|
|
181
|
-
signAndSendTransactions = async ({ transactions }) => {
|
|
182
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
183
|
-
return selectedWallet.signAndSendTransactions({ transactions });
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
*
|
|
188
|
-
* @param {string} accountId
|
|
189
|
-
* @returns {Promise<Object[]>} - the access keys for the
|
|
190
|
-
*/
|
|
191
|
-
getAccessKeys = async (accountId) => {
|
|
192
|
-
const walletSelector = await this.selector;
|
|
193
|
-
const { network } = walletSelector.options;
|
|
194
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
195
|
-
|
|
196
|
-
// Retrieve account state from the network
|
|
197
|
-
const keys = await provider.query({
|
|
198
|
-
request_type: 'view_access_key_list',
|
|
199
|
-
account_id: accountId,
|
|
200
|
-
finality: 'final',
|
|
201
|
-
});
|
|
202
|
-
return keys.keys;
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @typedef NearContext
|
|
208
|
-
* @property {import('./wallets/near').Wallet} wallet Current wallet
|
|
209
|
-
* @property {string} signedAccountId The AccountId of the signed user
|
|
210
|
-
*/
|
|
211
|
-
|
|
212
|
-
/** @type {import ('react').Context<NearContext>} */
|
|
213
|
-
export const NearContext = createContext({
|
|
214
|
-
wallet: undefined,
|
|
215
|
-
signedAccountId: '',
|
|
216
|
-
});
|
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'react';
|
|
2
|
-
|
|
3
|
-
// near api js
|
|
4
|
-
import { providers, utils } from 'near-api-js';
|
|
5
|
-
|
|
6
|
-
// wallet selector
|
|
7
|
-
import '@near-wallet-selector/modal-ui/styles.css';
|
|
8
|
-
import { setupModal } from '@near-wallet-selector/modal-ui';
|
|
9
|
-
import { setupWalletSelector } from '@near-wallet-selector/core';
|
|
10
|
-
import { setupHereWallet } from '@near-wallet-selector/here-wallet';
|
|
11
|
-
import { setupMyNearWallet } from '@near-wallet-selector/my-near-wallet';
|
|
12
|
-
import { setupLedger } from '@near-wallet-selector/ledger';
|
|
13
|
-
import { setupMeteorWallet } from '@near-wallet-selector/meteor-wallet';
|
|
14
|
-
import { setupSender } from '@near-wallet-selector/sender';
|
|
15
|
-
import { setupBitteWallet } from '@near-wallet-selector/bitte-wallet';
|
|
16
|
-
|
|
17
|
-
// ethereum wallets
|
|
18
|
-
import { wagmiConfig, web3Modal } from '@/wallets/web3modal';
|
|
19
|
-
import { setupEthereumWallets } from "@near-wallet-selector/ethereum-wallets";
|
|
20
|
-
|
|
21
|
-
const THIRTY_TGAS = '30000000000000';
|
|
22
|
-
const NO_DEPOSIT = '0';
|
|
23
|
-
|
|
24
|
-
export class Wallet {
|
|
25
|
-
/**
|
|
26
|
-
* @constructor
|
|
27
|
-
* @param {Object} options - the options for the wallet
|
|
28
|
-
* @param {string} options.networkId - the network id to connect to
|
|
29
|
-
* @param {string} options.createAccessKeyFor - the contract to create an access key for
|
|
30
|
-
* @example
|
|
31
|
-
* const wallet = new Wallet({ networkId: 'testnet', createAccessKeyFor: 'contractId' });
|
|
32
|
-
* wallet.startUp((signedAccountId) => console.log(signedAccountId));
|
|
33
|
-
*/
|
|
34
|
-
constructor({ networkId = 'testnet', createAccessKeyFor = undefined }) {
|
|
35
|
-
this.createAccessKeyFor = createAccessKeyFor;
|
|
36
|
-
this.networkId = networkId;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* To be called when the website loads
|
|
41
|
-
* @param {Function} accountChangeHook - a function that is called when the user signs in or out#
|
|
42
|
-
* @returns {Promise<string>} - the accountId of the signed-in user
|
|
43
|
-
*/
|
|
44
|
-
startUp = async (accountChangeHook) => {
|
|
45
|
-
this.selector = setupWalletSelector({
|
|
46
|
-
network: this.networkId,
|
|
47
|
-
modules: [
|
|
48
|
-
setupMyNearWallet(),
|
|
49
|
-
setupHereWallet(),
|
|
50
|
-
setupLedger(),
|
|
51
|
-
setupMeteorWallet(),
|
|
52
|
-
setupSender(),
|
|
53
|
-
setupBitteWallet(),
|
|
54
|
-
setupEthereumWallets({ wagmiConfig, web3Modal, alwaysOnboardDuringSignIn: true }),
|
|
55
|
-
],
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
const walletSelector = await this.selector;
|
|
59
|
-
const isSignedIn = walletSelector.isSignedIn();
|
|
60
|
-
const accountId = isSignedIn ? walletSelector.store.getState().accounts[0].accountId : '';
|
|
61
|
-
|
|
62
|
-
walletSelector.store.observable.subscribe(async (state) => {
|
|
63
|
-
const signedAccount = state?.accounts.find(account => account.active)?.accountId;
|
|
64
|
-
accountChangeHook(signedAccount || '');
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
return accountId;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Displays a modal to login the user
|
|
72
|
-
*/
|
|
73
|
-
signIn = async () => {
|
|
74
|
-
const modal = setupModal(await this.selector, { contractId: this.createAccessKeyFor });
|
|
75
|
-
modal.show();
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Logout the user
|
|
80
|
-
*/
|
|
81
|
-
signOut = async () => {
|
|
82
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
83
|
-
selectedWallet.signOut();
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Makes a read-only call to a contract
|
|
88
|
-
* @param {Object} options - the options for the call
|
|
89
|
-
* @param {string} options.contractId - the contract's account id
|
|
90
|
-
* @param {string} options.method - the method to call
|
|
91
|
-
* @param {Object} options.args - the arguments to pass to the method
|
|
92
|
-
* @returns {Promise<JSON.value>} - the result of the method call
|
|
93
|
-
*/
|
|
94
|
-
viewMethod = async ({ contractId, method, args = {} }) => {
|
|
95
|
-
const url = `https://rpc.${this.networkId}.near.org`;
|
|
96
|
-
const provider = new providers.JsonRpcProvider({ url });
|
|
97
|
-
|
|
98
|
-
const res = await provider.query({
|
|
99
|
-
request_type: 'call_function',
|
|
100
|
-
account_id: contractId,
|
|
101
|
-
method_name: method,
|
|
102
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString('base64'),
|
|
103
|
-
finality: 'optimistic',
|
|
104
|
-
});
|
|
105
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Makes a call to a contract
|
|
110
|
-
* @param {Object} options - the options for the call
|
|
111
|
-
* @param {string} options.contractId - the contract's account id
|
|
112
|
-
* @param {string} options.method - the method to call
|
|
113
|
-
* @param {Object} options.args - the arguments to pass to the method
|
|
114
|
-
* @param {string} options.gas - the amount of gas to use
|
|
115
|
-
* @param {string} options.deposit - the amount of yoctoNEAR to deposit
|
|
116
|
-
* @returns {Promise<Transaction>} - the resulting transaction
|
|
117
|
-
*/
|
|
118
|
-
callMethod = async ({ contractId, method, args = {}, gas = THIRTY_TGAS, deposit = NO_DEPOSIT }) => {
|
|
119
|
-
// Sign a transaction with the "FunctionCall" action
|
|
120
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
121
|
-
const outcome = await selectedWallet.signAndSendTransaction({
|
|
122
|
-
receiverId: contractId,
|
|
123
|
-
actions: [
|
|
124
|
-
{
|
|
125
|
-
type: 'FunctionCall',
|
|
126
|
-
params: {
|
|
127
|
-
methodName: method,
|
|
128
|
-
args,
|
|
129
|
-
gas,
|
|
130
|
-
deposit,
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
],
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
return providers.getTransactionLastResult(outcome);
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Makes a call to a contract
|
|
141
|
-
* @param {string} txhash - the transaction hash
|
|
142
|
-
* @returns {Promise<JSON.value>} - the result of the transaction
|
|
143
|
-
*/
|
|
144
|
-
getTransactionResult = async (txhash) => {
|
|
145
|
-
const walletSelector = await this.selector;
|
|
146
|
-
const { network } = walletSelector.options;
|
|
147
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
148
|
-
|
|
149
|
-
// Retrieve transaction result from the network
|
|
150
|
-
const transaction = await provider.txStatus(txhash, 'unnused');
|
|
151
|
-
return providers.getTransactionLastResult(transaction);
|
|
152
|
-
};
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Gets the balance of an account
|
|
156
|
-
* @param {string} accountId - the account id to get the balance of
|
|
157
|
-
* @returns {Promise<number>} - the balance of the account
|
|
158
|
-
*
|
|
159
|
-
*/
|
|
160
|
-
getBalance = async (accountId) => {
|
|
161
|
-
const walletSelector = await this.selector;
|
|
162
|
-
const { network } = walletSelector.options;
|
|
163
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
164
|
-
|
|
165
|
-
// Retrieve account state from the network
|
|
166
|
-
const account = await provider.query({
|
|
167
|
-
request_type: 'view_account',
|
|
168
|
-
account_id: accountId,
|
|
169
|
-
finality: 'final',
|
|
170
|
-
});
|
|
171
|
-
// return amount on NEAR
|
|
172
|
-
return account.amount ? Number(utils.format.formatNearAmount(account.amount)) : 0;
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Signs and sends transactions
|
|
177
|
-
* @param {Object[]} transactions - the transactions to sign and send
|
|
178
|
-
* @returns {Promise<Transaction[]>} - the resulting transactions
|
|
179
|
-
*
|
|
180
|
-
*/
|
|
181
|
-
signAndSendTransactions = async ({ transactions }) => {
|
|
182
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
183
|
-
return selectedWallet.signAndSendTransactions({ transactions });
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
*
|
|
188
|
-
* @param {string} accountId
|
|
189
|
-
* @returns {Promise<Object[]>} - the access keys for the
|
|
190
|
-
*/
|
|
191
|
-
getAccessKeys = async (accountId) => {
|
|
192
|
-
const walletSelector = await this.selector;
|
|
193
|
-
const { network } = walletSelector.options;
|
|
194
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
195
|
-
|
|
196
|
-
// Retrieve account state from the network
|
|
197
|
-
const keys = await provider.query({
|
|
198
|
-
request_type: 'view_access_key_list',
|
|
199
|
-
account_id: accountId,
|
|
200
|
-
finality: 'final',
|
|
201
|
-
});
|
|
202
|
-
return keys.keys;
|
|
203
|
-
};
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* @typedef NearContext
|
|
208
|
-
* @property {import('./wallets/near').Wallet} wallet Current wallet
|
|
209
|
-
* @property {string} signedAccountId The AccountId of the signed user
|
|
210
|
-
*/
|
|
211
|
-
|
|
212
|
-
/** @type {import ('react').Context<NearContext>} */
|
|
213
|
-
export const NearContext = createContext({
|
|
214
|
-
wallet: undefined,
|
|
215
|
-
signedAccountId: '',
|
|
216
|
-
});
|
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
// wallet selector
|
|
2
|
-
import '@near-wallet-selector/modal-ui/styles.css';
|
|
3
|
-
|
|
4
|
-
import { setupBitteWallet } from '@near-wallet-selector/bitte-wallet';
|
|
5
|
-
import { setupWalletSelector } from '@near-wallet-selector/core';
|
|
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
|
-
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
|
-
import { Buffer } from 'buffer';
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
// near api js
|
|
19
|
-
import { providers, utils } from 'near-api-js';
|
|
20
|
-
import { createContext } from 'react';
|
|
21
|
-
|
|
22
|
-
// ethereum wallets
|
|
23
|
-
import { wagmiConfig, web3Modal } from '@/wallets/web3modal';
|
|
24
|
-
|
|
25
|
-
const THIRTY_TGAS = '30000000000000';
|
|
26
|
-
const NO_DEPOSIT = '0';
|
|
27
|
-
|
|
28
|
-
export class Wallet {
|
|
29
|
-
/**
|
|
30
|
-
* @constructor
|
|
31
|
-
* @param {Object} options - the options for the wallet
|
|
32
|
-
* @param {string} options.networkId - the network id to connect to
|
|
33
|
-
* @param {string} options.createAccessKeyFor - the contract to create an access key for
|
|
34
|
-
* @example
|
|
35
|
-
* const wallet = new Wallet({ networkId: 'testnet', createAccessKeyFor: 'contractId' });
|
|
36
|
-
* wallet.startUp((signedAccountId) => console.log(signedAccountId));
|
|
37
|
-
*/
|
|
38
|
-
constructor({ networkId = 'testnet', createAccessKeyFor = undefined }) {
|
|
39
|
-
this.createAccessKeyFor = createAccessKeyFor;
|
|
40
|
-
this.networkId = networkId;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* To be called when the website loads
|
|
45
|
-
* @param {Function} accountChangeHook - a function that is called when the user signs in or out#
|
|
46
|
-
* @returns {Promise<string>} - the accountId of the signed-in user
|
|
47
|
-
*/
|
|
48
|
-
startUp = async (accountChangeHook) => {
|
|
49
|
-
this.selector = setupWalletSelector({
|
|
50
|
-
network: this.networkId,
|
|
51
|
-
modules: [
|
|
52
|
-
setupMeteorWallet(),
|
|
53
|
-
setupEthereumWallets({ wagmiConfig, web3Modal, alwaysOnboardDuringSignIn: true }),
|
|
54
|
-
setupLedger(),
|
|
55
|
-
setupBitteWallet(),
|
|
56
|
-
setupHereWallet(),
|
|
57
|
-
setupSender(),
|
|
58
|
-
setupNearMobileWallet(),
|
|
59
|
-
setupWelldoneWallet(),
|
|
60
|
-
setupMyNearWallet(),
|
|
61
|
-
],
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
const walletSelector = await this.selector;
|
|
65
|
-
const isSignedIn = walletSelector.isSignedIn();
|
|
66
|
-
const accountId = isSignedIn ? walletSelector.store.getState().accounts[0].accountId : '';
|
|
67
|
-
|
|
68
|
-
walletSelector.store.observable.subscribe(async (state) => {
|
|
69
|
-
const signedAccount = state?.accounts.find((account) => account.active)?.accountId;
|
|
70
|
-
accountChangeHook(signedAccount || '');
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
return accountId;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Displays a modal to login the user
|
|
78
|
-
*/
|
|
79
|
-
signIn = async () => {
|
|
80
|
-
const modal = setupModal(await this.selector, { contractId: this.createAccessKeyFor });
|
|
81
|
-
modal.show();
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Logout the user
|
|
86
|
-
*/
|
|
87
|
-
signOut = async () => {
|
|
88
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
89
|
-
selectedWallet.signOut();
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Makes a read-only call to a contract
|
|
94
|
-
* @param {Object} options - the options for the call
|
|
95
|
-
* @param {string} options.contractId - the contract's account id
|
|
96
|
-
* @param {string} options.method - the method to call
|
|
97
|
-
* @param {Object} options.args - the arguments to pass to the method
|
|
98
|
-
* @returns {Promise<JSON.value>} - the result of the method call
|
|
99
|
-
*/
|
|
100
|
-
viewMethod = async ({ contractId, method, args = {} }) => {
|
|
101
|
-
const url = `https://rpc.${this.networkId}.near.org`;
|
|
102
|
-
const provider = new providers.JsonRpcProvider({ url });
|
|
103
|
-
|
|
104
|
-
const res = await provider.query({
|
|
105
|
-
request_type: 'call_function',
|
|
106
|
-
account_id: contractId,
|
|
107
|
-
method_name: method,
|
|
108
|
-
args_base64: Buffer.from(JSON.stringify(args)).toString('base64'),
|
|
109
|
-
finality: 'optimistic',
|
|
110
|
-
});
|
|
111
|
-
return JSON.parse(Buffer.from(res.result).toString());
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Makes a call to a contract
|
|
116
|
-
* @param {Object} options - the options for the call
|
|
117
|
-
* @param {string} options.contractId - the contract's account id
|
|
118
|
-
* @param {string} options.method - the method to call
|
|
119
|
-
* @param {Object} options.args - the arguments to pass to the method
|
|
120
|
-
* @param {string} options.gas - the amount of gas to use
|
|
121
|
-
* @param {string} options.deposit - the amount of yoctoNEAR to deposit
|
|
122
|
-
* @returns {Promise<Transaction>} - the resulting transaction
|
|
123
|
-
*/
|
|
124
|
-
callMethod = async ({ contractId, method, args = {}, gas = THIRTY_TGAS, deposit = NO_DEPOSIT }) => {
|
|
125
|
-
// Sign a transaction with the "FunctionCall" action
|
|
126
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
127
|
-
const outcome = await selectedWallet.signAndSendTransaction({
|
|
128
|
-
receiverId: contractId,
|
|
129
|
-
actions: [
|
|
130
|
-
{
|
|
131
|
-
type: 'FunctionCall',
|
|
132
|
-
params: {
|
|
133
|
-
methodName: method,
|
|
134
|
-
args,
|
|
135
|
-
gas,
|
|
136
|
-
deposit,
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
],
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
return providers.getTransactionLastResult(outcome);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Makes a call to a contract
|
|
147
|
-
* @param {string} txhash - the transaction hash
|
|
148
|
-
* @returns {Promise<JSON.value>} - the result of the transaction
|
|
149
|
-
*/
|
|
150
|
-
getTransactionResult = async (txhash) => {
|
|
151
|
-
const walletSelector = await this.selector;
|
|
152
|
-
const { network } = walletSelector.options;
|
|
153
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
154
|
-
|
|
155
|
-
// Retrieve transaction result from the network
|
|
156
|
-
const transaction = await provider.txStatus(txhash, 'unnused');
|
|
157
|
-
return providers.getTransactionLastResult(transaction);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Gets the balance of an account
|
|
162
|
-
* @param {string} accountId - the account id to get the balance of
|
|
163
|
-
* @param {boolean} format - whether to format the balance
|
|
164
|
-
* @returns {Promise<number>} - the balance of the account
|
|
165
|
-
*
|
|
166
|
-
*/
|
|
167
|
-
getBalance = async (accountId, format = false) => {
|
|
168
|
-
const walletSelector = await this.selector;
|
|
169
|
-
const { network } = walletSelector.options;
|
|
170
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
171
|
-
|
|
172
|
-
// Retrieve account state from the network
|
|
173
|
-
const account = await provider.query({
|
|
174
|
-
request_type: 'view_account',
|
|
175
|
-
account_id: accountId,
|
|
176
|
-
finality: 'final',
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
// Format the amount if needed
|
|
180
|
-
if (format) {
|
|
181
|
-
return account.amount ? utils.format.formatNearAmount(account.amount) : '0';
|
|
182
|
-
} else {
|
|
183
|
-
return account.amount || '0';
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Signs and sends transactions
|
|
189
|
-
* @param {Object[]} transactions - the transactions to sign and send
|
|
190
|
-
* @returns {Promise<Transaction[]>} - the resulting transactions
|
|
191
|
-
*
|
|
192
|
-
*/
|
|
193
|
-
signAndSendTransactions = async ({ transactions }) => {
|
|
194
|
-
const selectedWallet = await (await this.selector).wallet();
|
|
195
|
-
return selectedWallet.signAndSendTransactions({ transactions });
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
*
|
|
200
|
-
* @param {string} accountId
|
|
201
|
-
* @returns {Promise<Object[]>} - the access keys for the
|
|
202
|
-
*/
|
|
203
|
-
getAccessKeys = async (accountId) => {
|
|
204
|
-
const walletSelector = await this.selector;
|
|
205
|
-
const { network } = walletSelector.options;
|
|
206
|
-
const provider = new providers.JsonRpcProvider({ url: network.nodeUrl });
|
|
207
|
-
|
|
208
|
-
// Retrieve account state from the network
|
|
209
|
-
const keys = await provider.query({
|
|
210
|
-
request_type: 'view_access_key_list',
|
|
211
|
-
account_id: accountId,
|
|
212
|
-
finality: 'final',
|
|
213
|
-
});
|
|
214
|
-
return keys.keys;
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* @typedef NearContext
|
|
220
|
-
* @property {import('./wallets/near').Wallet} wallet Current wallet
|
|
221
|
-
* @property {string} signedAccountId The AccountId of the signed user
|
|
222
|
-
*/
|
|
223
|
-
|
|
224
|
-
/** @type {import ('react').Context<NearContext>} */
|
|
225
|
-
export const NearContext = createContext({
|
|
226
|
-
wallet: undefined,
|
|
227
|
-
signedAccountId: '',
|
|
228
|
-
});
|