-
Notifications
You must be signed in to change notification settings - Fork 65
/
blockchain.ts
193 lines (168 loc) · 4.78 KB
/
blockchain.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
import { Web3Provider, Contract } from "zksync-web3";
// import { WrapperBuilder } from "@redstone-finance/evm-connector";
import { utils } from "ethers";
import localAddresses from "../config/zkSyncTestnet-addresses.json";
import nftAbi from "../config/nft-abi.json";
import marketplaceAbi from "../config/marketplace-abi.json";
import { zkSyncLocalTestnetChainId } from "../constants";
declare global {
interface Window {
ethereum: any;
}
}
interface OrdersFromContract {
tokenId: { toNumber: () => number };
price: number;
creator: string;
status: number;
}
const ABIs = {
nft: nftAbi,
marketplace: marketplaceAbi,
};
///////// NFT AND MARKETPLACE FUNCTIONS /////////
async function getOwnedNfts(address: string) {
try {
const nft = await getContractInstance("nft");
const nftCount = await nft.balanceOf(address);
const tokenIds = [];
for (let i = 0; i < nftCount; i++) {
const tokenId = await nft.tokenOfOwnerByIndex(address, i);
tokenIds.push(tokenId);
}
return tokenIds;
} catch {
const errText = "Error happened while fetching owned NFTs";
alert(errText);
}
}
async function mintNft() {
try {
const nft = await getContractInstance("nft");
const tx = await nft.mint();
await tx.wait();
return tx;
} catch {
const errText = "Error happened while minting the NFT";
alert(errText);
}
}
async function getAllOrders() {
try {
const marketplace = await getContractInstance("marketplace");
const orders = await marketplace.getAllOrders();
return (orders ?? [])
.map((order: OrdersFromContract, index: number) => ({
orderId: index,
tokenId: order.tokenId.toNumber(),
usdPrice: utils.formatEther(order.price),
creator: order.creator,
status: order.status,
}))
.filter((order: OrdersFromContract) => order.status === 0);
} catch {
const errText = "Error happened while fetching all orders";
alert(errText);
}
}
async function postOrder({
tokenId,
usdPrice,
}: {
tokenId: string;
usdPrice: number;
}) {
try {
const marketplace = await getContractInstance("marketplace");
const nftContract = await getContractInstance("nft");
// Sending approve tx
const approveTx = await nftContract.approve(marketplace.address, tokenId);
await approveTx.wait();
// Posting order tx
const postOrderTx = await marketplace.postSellOrder(
nftContract.address,
tokenId,
utils.parseEther(String(usdPrice))
);
await postOrderTx.wait();
} catch {
const errText = "Error happened while posting the order";
alert(errText);
}
}
async function cancelOrder(orderId: string) {
try {
const marketplace = await getContractInstance("marketplace");
const cancelTx = await marketplace.cancelOrder(orderId);
await cancelTx.wait();
return cancelTx;
} catch {
const errText = "Error happened while canceling the order";
alert(errText);
}
}
/*
Marketplace contract instance should be wrapped.
It enables fetching data from redstone data pool
for each contract function call
*/
async function buy(orderId: string) {
const marketplace = await getContractInstance("marketplace");
// TO IMPLEMENT
}
///////// STANDARD BLOCKCHAIN UTILS FUNCTIONS /////////
function shortenAddress(address: string) {
return address.slice(0, 7) + ".." + address.slice(address.length - 7);
}
function onAddressChange(callback: () => void) {
window.ethereum.on("accountsChanged", callback);
}
async function getUserAddress() {
try {
const signer = await getSigner();
return await signer.getAddress();
} catch (error: any) {
alert(error?.message);
}
}
async function connectWallet() {
getChainId();
await window.ethereum.request({ method: "eth_requestAccounts" });
}
async function getSigner() {
await connectWallet();
const signer = new Web3Provider(window.ethereum).getSigner();
return signer;
}
async function getContractInstance(contractName: "nft" | "marketplace") {
const abi = ABIs[contractName];
const address = await getContractAddress(contractName);
const signer = await getSigner();
return new Contract(address, abi, signer);
}
async function getContractAddress(contractName: "nft" | "marketplace") {
return localAddresses[contractName];
}
async function getChainId() {
const provider = new Web3Provider(window.ethereum);
const network = await provider.getNetwork();
const chainId = network.chainId;
// Check if network is supported
if (chainId !== zkSyncLocalTestnetChainId) {
const errText = "Please connect to zkSync local network and reload the page";
alert(errText);
}
return chainId;
}
export default {
getOwnedNfts,
mintNft,
getAllOrders,
postOrder,
cancelOrder,
buy,
connectWallet,
getUserAddress,
shortenAddress,
onAddressChange,
};