-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseWeb3.ts
220 lines (207 loc) · 5.67 KB
/
useWeb3.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import { ethers, providers } from "ethers";
import axios from "axios";
import Web3Modal from "web3modal";
import Market from "../../artifacts/contracts/Market.sol/NFTMarket.json";
import NFT from "../../artifacts/contracts/NFT.sol/NFT.json";
import { nftaddress, nftmarketaddress } from "../../config";
export interface NFTInfo {
price: string;
tokenId: string;
seller: string;
owner: string;
image: string;
description: string;
name: string;
}
// 可能回合NFTInfo 信息有部分不一样
export interface NFTMetaInfo {
price: string;
itemId: string;
seller: string;
owner: string;
image: string;
name: string;
description: string;
}
/**
*
* @returns 获取provider
*/
export async function getProvider(): Promise<ethers.providers.Web3Provider> {
const web3modal = new Web3Modal();
const conn = await web3modal.connect();
return new ethers.providers.Web3Provider(conn);
}
export async function getMarketContract() {
const provider = await getProvider();
const signer = provider.getSigner();
return new ethers.Contract(nftmarketaddress, Market.abi, signer);
}
export async function getNFTContract() {
const provider = await getProvider();
const signer = provider.getSigner();
return new ethers.Contract(nftaddress, NFT.abi, signer);
}
/**
* 创建一个NFT并发布到NFT市场上去
* @param url NFT信息的地址
* @param inputPrice 输入的价格
* @param provider
*/
export async function createNFT(
url: string,
inputPrice: string
): Promise<boolean> {
const nftContract = await getNFTContract();
let transaction = await nftContract.createToken(url);
const tx = await transaction.wait();
const event = tx.events[0];
const value = event.args[2];
const tokenId = value.toNumber();
const price = ethers.utils.parseUnits(inputPrice, "ether");
// 之后把该NFT商品商家岛market
const martketContract = await getMarketContract();
const publishPirice = await martketContract.getListingPrice();
transaction = await martketContract.createMarketItem(
nftaddress,
tokenId,
price,
{
value: publishPirice,
}
);
await transaction.wait();
return true;
}
/**
* 获取我拥有的NFT资产
* @param provider
*/
export async function fetchMyNFTs() {
const marketContract = await getMarketContract();
const nftContract = await getNFTContract();
let data = await marketContract.fetchMyNFTs();
// 节省请求 取最近的10个
data = data.reduce((p: any, v: any, index: number) => {
if (index < 10) {
p.push(v);
}
return p;
}, []);
data.reverse();
const items = await Promise.all(
data.map(async (i: any) => {
const tokenUri = await nftContract.tokenURI(i.tokenId);
const meta = await axios.get(tokenUri);
const price = ethers.utils.formatUnits(i.price.toString(), "ether");
const nft: NFTInfo = {
name: meta.data.name,
price,
tokenId: i.tokenId.toNumber(),
seller: i.seller,
owner: i.owner,
image: meta.data.image,
description: meta.data.description,
};
return nft;
})
);
return items;
}
/**
* 获取我发布的NFT资产
* @param provider
*/
export async function fetchMyCreatedNFTs() {
const marketContract = await getMarketContract();
const tokenContract = await getNFTContract();
let data = await marketContract.fetchItemsCreated();
// 节省请求 取最近的10个
data = data.reduce((p: any, v: any, index: number) => {
if (index < 10) {
p.push(v);
}
return p;
}, []);
data.reverse();
const items = await Promise.all(
data.map(async (i: any) => {
const tokenUri = await tokenContract.tokenURI(i.tokenId);
const meta = await axios.get(tokenUri);
const price = ethers.utils.formatUnits(i.price.toString(), "ether");
const nft: NFTInfo = {
name: meta.data.name,
price,
tokenId: i.tokenId.toNumber(),
seller: i.seller,
owner: i.owner,
image: meta.data.image,
description: meta.data.description,
};
return nft;
})
);
return items;
}
/**
* 获取所有的NFT
* @param provider
*/
export async function fetchAllNFTs(provider?: ethers.providers.Web3Provider) {
const marketContract = await getMarketContract();
const tokenContract = await getNFTContract();
let data = await marketContract.fetchMarketItems(); // read-only, can't use reverse等
// 节省请求 取最近的10个
data = data.reduce((p: any, v: any, index: number) => {
if (index < 10) {
p.push(v);
}
return p;
}, []);
data.reverse();
const items = await Promise.all(
data.map(async (i: any) => {
try {
const tokenUri = await tokenContract.tokenURI(i.tokenId);
const meta = await axios.get(tokenUri);
// console.log(meta.data);
const price = ethers.utils.formatUnits(i.price.toString(), "ether");
const metaInfo: NFTMetaInfo = {
price,
itemId: i.itemId.toNumber(),
seller: i.seller,
owner: i.owner,
image: meta.data.image,
name: meta.data.name,
description: meta.data.description,
};
return metaInfo;
} catch (e) {
console.log(e);
}
})
);
return items;
}
/**
*
* @param nft 购买NFT
*/
export async function buyNFT(nft: any): Promise<boolean> {
try {
const contract = await getMarketContract();
const price = ethers.utils.parseUnits(nft.price.toString(), "ether");
const transaction = await contract.createMarketSale(
nftaddress,
nft.itemId,
{
value: price,
}
);
await transaction.wait();
return true;
} catch (error) {
console.error(error);
return false;
}
}