-
Notifications
You must be signed in to change notification settings - Fork 8
/
IPFS.html
111 lines (96 loc) · 2.99 KB
/
IPFS.html
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
<html>
<head>
<!-- Moralis SDK code -->
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
<script src="https://unpkg.com/moralis/dist/moralis.js"></script>
</head>
<body>
<h1>IPFS Demo</h1>
<button id="btn-login" onclick="login()">Moralis Login</button>
<br />
<br />
<input
type="text"
name="metadataName"
id="metadataName"
placeholder="Metadata Name"
/>
<br />
<br />
<input
type="text"
name="metadataDescription"
id="metadataDescription"
placeholder="Metadata Description"
/>
<br />
<br />
<input type="file" name="fileInput" id="file" placeholder="File" />
<button onclick="toTheMoon()">To the moon</button>
<script>
// connect to Moralis server
Moralis.initialize("");
Moralis.serverURL = "";
const nftContractAddress = "0x351bbee7C6E9268A1BF741B098448477E08A0a53"; // Make this variable
// Ethereum Rinkeby 0x0Fb6EF3505b9c52Ed39595433a21aF9B5FCc4431
// Polygon Mumbai 0x351bbee7C6E9268A1BF741B098448477E08A0a53
// BSC Testnet 0x88624DD1c725C6A95E223170fa99ddB22E1C6DDD
const web3 = new Web3(window.ethereum);
login = async () => {
Moralis.Web3.authenticate().then(async (result) => {
console.log(result);
alert("Login successful");
});
};
toTheMoon = async () => {
// Storing the file
const fileInput = document.getElementById("file");
const data = fileInput.files[0];
const imageFile = new Moralis.File(data.name, data);
await imageFile.saveIPFS();
// Storing the metadata
const imageURI = imageFile.ipfs();
const metadata = {
name: document.getElementById("metadataName").value,
description: document.getElementById("metadataDescription").value,
image: imageURI,
};
const metadataFile = new Moralis.File("metadata.json", {
base64: btoa(JSON.stringify(metadata)),
});
await metadataFile.saveIPFS();
const metadataURI = metadataFile.ipfs();
console.log(metadataURI);
alert("Upload successful");
// minting
const txt = await mintToken(metadataURI);
console.log("Minted!!");
};
async function mintToken(_uri) {
const encodedFunction = web3.eth.abi.encodeFunctionCall(
{
name: "mintToken",
type: "function",
inputs: [
{
type: "string",
name: "tokenURI",
},
],
},
[_uri]
);
const transactionParameters = {
to: nftContractAddress,
from: ethereum.selectedAddress,
data: encodedFunction,
};
const txt = await ethereum.request({
method: "eth_sendTransaction",
params: [transactionParameters],
});
return txt;
}
</script>
</body>
</html>