-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnear.js
86 lines (75 loc) · 2.68 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// connect to NEAR
const { providers } = nearApi;
const { utils } = nearApi;
const near = new nearApi.Near({
keyStore: new nearApi.keyStores.BrowserLocalStorageKeyStore(),
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
walletUrl: 'https://wallet.testnet.near.org'
});
// connect to the NEAR Wallet
const wallet = new nearApi.WalletConnection(near, 'my-app');
// connect to a NEAR smart contract
const contract = new nearApi.Contract(wallet.account(), 'nft.ippishio.testnet', {
viewMethods: [],
changeMethods: ['nft_mint']
});
const button = document.getElementById('open');
const logoutbut = document.getElementById('log-out');
if (!wallet.isSignedIn()) {
button.textContent = 'SignIn with NEAR'
logoutbut.style.visibility = "hidden";
} else {
logoutbut.style.visibility = "visible";
}
logoutbut.addEventListener('click', () => {
wallet.signOut();
location.href = 'https://'+window.location.hostname;
window.location.reload();
console.log("logged out");
})
// Either # or call the addMessage change method on button click
var randID;
var isOpen = false;
var isSignedIn = wallet.isSignedIn();
//network config (replace testnet with mainnet or betanet)
const provider = new providers.JsonRpcProvider(
"https://archival-rpc.testnet.near.org"
);
const TX_HASH = new URLSearchParams(window.location.search).get('transactionHashes');
if(wallet.isSignedIn()) {
if(TX_HASH) {
if(!(new URLSearchParams(window.location.search).get('errorCode'))) {
console.log("no error");
const result = await provider.txStatus(TX_HASH, wallet.getAccountId().toString());
randID = parseInt(window.atob(result['status']['SuccessValue']));
if(randID){
console.log(randID);
isOpen = true;
}
} else {
console.log("error");
}
console.log(TX_HASH);
}
}
export {randID, isOpen, isSignedIn}
button.addEventListener('click', () => {
if (wallet.isSignedIn()) {
const randomid = Math.floor(Math.random() * 49)+1;
contract.nft_mint({
token_id: (Math.floor(Math.random() * 900000000)+1).toString(),
metadata: {"title":"UGLY CAT NFT #"+randomid.toString(),"description":"A little funny looking cat","media":"https://bafybeidf7fvevqcn4ulumwwefedlg343qy2rsgg27ev4fpaz57iwywwnby.ipfs.nftstorage.link/images/"+randomid.toString()+".png"},
receiver_id: wallet.getAccountId().toString(),
image_number: randomid
},"300000000000000","100000000000000000000000"
)
isSignedIn = true;
} else {
wallet.requestSignIn({
contractId: 'nft.ippishio.testnet',
methodNames: ['nft_mint']
});
isSignedIn = false;
}
});