-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNearProvider.js
More file actions
167 lines (152 loc) · 4.76 KB
/
NearProvider.js
File metadata and controls
167 lines (152 loc) · 4.76 KB
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
// Packages //
import { useState, useEffect, useContext, createContext } from "react";
import { keyStores, connect, WalletConnection, Contract } from "near-api-js";
const NearContext = createContext();
export function useNear() {
return useContext(NearContext);
}
export function decode(data) {
let res = "";
for (let i = 0; i < data.length; i++) res += String.fromCharCode(data[i]);
return JSON.parse(res);
}
const EMPTY_QUERY = Buffer.from("{}").toString("base64");
const DEFAULT_SEARCH = Buffer.from(
JSON.stringify({
from_index: "0",
limit: 64,
})
).toString("base64");
export function NearProvider({ children }) {
const [near, setNear] = useState(null);
const [wallet, setWallet] = useState(null);
const [contract, setContract] = useState(null);
const [redirect, setRedirect] = useState(true);
const [signedIn, setSignedIn] = useState(false);
const [roles, setRoles] = useState(["listener"]);
const [artist, setArtist] = useState("No Artist");
const [connected, setConnected] = useState(false);
const [terms, setTerms] = useState([]);
const [tokens, setTokens] = useState([]);
const [costPerByte, setCostPerByte] = useState(null);
const setup_near = async () => {
const config = {
networkId: "testnet",
keyStore: new keyStores.BrowserLocalStorageKeyStore(),
nodeUrl: "https://rpc.testnet.near.org",
walletUrl: "https://wallet.testnet.near.org",
helperUrl: "https://helper.testnet.near.org",
explorerUrl: "https://explorer.testnet.near.org",
};
const near_connection = await connect(config);
setNear(near_connection);
const wallet_connection = new WalletConnection(near_connection);
setWallet(wallet_connection);
const _tokens = near_connection.connection.provider.query({
request_type: "call_function",
finality: "final",
account_id: "nearsound.testnet",
method_name: "nft_tokens",
args_base64: DEFAULT_SEARCH,
});
const _search = near_connection.connection.provider.query({
request_type: "call_function",
finality: "final",
account_id: "nearsound.testnet",
method_name: "get_search_terms",
args_base64: EMPTY_QUERY,
});
const _request =
near_connection.connection.provider.experimental_genesisConfig();
const [default_tokens, search_terms, response] = await Promise.all([
_tokens,
_search,
_request,
]);
setCostPerByte(response.runtime_config.storage_amount_per_byte);
setTokens(decode(default_tokens.result));
setTerms(decode(search_terms.result));
const contract_connection = new Contract(
wallet_connection.account(), // the account object that is connecting
"nearsound.testnet",
{
viewMethods: [
"nft_tokens",
"get_artist",
"get_search_terms",
"nft_tokens_for_search",
"nft_token",
], // view methods do not change state but usually return a value
changeMethods: ["nft_mint", "create_artist"], // change methods modify state
sender: wallet_connection.account(), // account object to initialize and sign transactions.
}
);
setContract(contract_connection);
setRoles([...roles, "artist"]);
setArtist("Halfmoon");
setConnected(true);
};
const connect_to_near = () => {
wallet.requestSignIn(
"nearsound.testnet", // contract requesting access
"Nearsound" // optional
);
};
const getTransaction = async (hash, account_id) => {
const result = await near.connection.provider.txStatus(hash, account_id);
return result;
};
const search = async (search_term) => {
const _tokens = await contract.nft_tokens_for_search({
search_term,
from_index: "0",
limit: 64,
});
if (_tokens.length == 0) reset_search();
else setTokens(_tokens);
};
const reset_search = async (from_index = "0", limit = 64) => {
const _args = Buffer.from(
JSON.stringify({
from_index,
limit,
})
).toString("base64");
const response = await near.connection.provider.query({
request_type: "call_function",
finality: "final",
account_id: "nearsound.testnet",
method_name: "nft_tokens",
args_base64: _args,
});
setTokens(decode(response.result));
};
useEffect(() => {
if (wallet != undefined && wallet.isSignedIn()) setSignedIn(true);
else setSignedIn(false);
}, [wallet]);
useEffect(() => {
setup_near();
}, []);
const context = {
near,
wallet,
contract,
connected,
roles,
artist,
terms,
tokens,
connect_to_near,
getTransaction,
signedIn,
redirect,
setRedirect,
search,
reset_search,
costPerByte,
};
return (
<NearContext.Provider value={context}>{children}</NearContext.Provider>
);
}