-
Notifications
You must be signed in to change notification settings - Fork 1
/
near.js
65 lines (56 loc) · 1.96 KB
/
near.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
// // Access the near-api-js library directly
const nearAPI = window.nearApi;
var brownie = false
brownNum = 0,
loggedIn = false,
userName=""
async function initializeNear(){
try {
// Initialize NEAR
const near = await nearAPI.connect({
networkId: 'mainnet',
keyStore: new nearAPI.keyStores.BrowserLocalStorageKeyStore(window.localStorage, 'Brown Game'),
nodeUrl: 'https://rpc.mainnet.near.org',
walletUrl: 'https://wallet.mainnet.near.org', // NEAR Wallet URL
deps: { keyStore: new nearAPI.keyStores.BrowserLocalStorageKeyStore() },
});
window.walletConnection = new nearAPI.WalletConnection(near, "silk-road-near-arcade")
if(window.walletConnection.isSignedIn()){
loggedIn=true
buttons[17].label="logout"
checkNFT()
}
}catch(e){
console.log(e)
}
}
async function checkNFT() {
try {
userName = window.walletConnection.account().accountId
const contractId = 'mrbrownproject.near'
const args = { "account_id": window.walletConnection.getAccountId() }; // Use the authenticated user's account
var account = window.walletConnection.account()
const contract = new nearAPI.Contract(account, contractId, {
viewMethods: ["nft_tokens_for_owner"],
})
const result = await contract.nft_tokens_for_owner(args);
//console.log('View method result:', result);
if (result.length > 0) {
brownie=true
brownNum= result.length
//console.log("You have " + result.length + " Mr. Brown NFTs!");
}
} catch (error) {
console.error('Error:', error);
}
}
function logOut(){
window.walletConnection.signOut()
}
async function login(){
try{
await window.walletConnection.requestSignIn("brown.neararcade.near", 'Check Your NFTs on Mr. Brown');
}catch(e){
console.log(e)
}
}