-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontract.js
53 lines (46 loc) · 1.54 KB
/
contract.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
import { ethers } from "ethers";
const provider = new ethers.JsonRpcProvider("https://base.llamarpc.com");
const message = "Sign to prove you own the address";
const contractAddress = "0x83102E2Dc04CF0d2879C4F5dbD17246Fec2C963a";
// 签名消息函数
export const signMessage = async (privateKey) => {
const wallet = new ethers.Wallet(privateKey, provider);
try {
const signedMessage = await wallet.signMessage(message);
return signedMessage;
} catch (error) {
console.error("Error signing message:", error);
}
};
const abi = [
{
"inputs": [
{
"internalType": "bytes",
"name": "signature",
"type": "bytes"
}
],
"name": "mint",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
// 铸造NFT函数
export const mintNft = async (privateKey, signature) => {
const wallet = new ethers.Wallet(privateKey, provider);
const contract = new ethers.Contract(contractAddress, abi, wallet);
try {
const transaction = await contract.mint(signature, {
gasLimit: 300000,
});
console.log("Mint Nft transaction sent. Waiting for confirmation...");
await transaction.wait();
console.log(`Nft have been succesfully minted, hash: https://basescan.org/tx/${transaction.hash}`);
return transaction.hash;
} catch (error) {
console.error("Error interacting with the contract:", error);
return null;
}
};