-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserveAuctionETH.ts
197 lines (175 loc) · 5.9 KB
/
reserveAuctionETH.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
//reserveAuctionETH.ts
// Class for Zora V3 Reserve Auction Modules
import {BigNumberish} from '@ethersproject/bignumber';
import {ContractTransaction} from '@ethersproject/contracts';
import {Provider} from '@ethersproject/providers';
import {Signer, utils} from 'ethers';
import {
ReserveAuctionCoreEth as ReserveAuctionCoreEthType,
ReserveAuctionCoreEth__factory,
} from '@catalogworks/zorav3-with-types/dist/';
import {zoraAddresses} from './addresses';
import {chainIdToNetwork, validateAndParseAddress} from './utils';
import type {AuctionStructOutputETH} from './types';
export class ReserveAuctionETH {
public chainId: number;
public contractAddress: string;
public signerOrProvider: Signer | Provider;
public contract: ReserveAuctionCoreEthType;
public readOnly: boolean;
// Constructor
constructor(
signerOrProvider: Signer | Provider,
chainid: number,
contractAddress?: string
) {
Signer.isSigner(signerOrProvider)
? (this.readOnly = false)
: (this.readOnly = true);
this.signerOrProvider = signerOrProvider;
this.chainId = chainid;
if (contractAddress) {
const parsedContractAddress = validateAndParseAddress(contractAddress);
this.contractAddress = parsedContractAddress;
} else {
// Get contract address from chainId
const network = chainIdToNetwork(this.chainId);
this.contractAddress = zoraAddresses[network].reserveAuctionCoreEth;
}
this.contract = ReserveAuctionCoreEth__factory.connect(
this.contractAddress,
this.signerOrProvider
);
}
// View Methods [Getters]
// auctionForNFT
// @param {string} inputAddress - Address of the token contract
// @param {BigNumberish} inputTokenId - Token ID
public async fetchAuctionForNFT(
inputAddress: string,
inputTokenId: BigNumberish
): Promise<AuctionStructOutputETH> {
return this.contract.auctionForNFT(inputAddress, inputTokenId);
}
// erc721TransferHelper
// @returns {string} - Contract address of deployer erc721 transfer helper
// @note This is here as a helper
public async fetchERC721TransferHelper(): Promise<string> {
return this.contract.erc721TransferHelper();
}
// Write Methods [Transactions]
// createAuction
// @param {string} tokenContractAddress - Address of the token contract
// @param {BigNumberish} tokenId - Token ID
// @param {BigNumberish} duration - Auction duration (unspecified units in natspec)
// @param {BigNumberish} reservePrice - Auction reserve price
// @param {string} sellerFundsRecipient - Address of the funds recipient
// @param {BigNumberish} startTime - Auction start time
// @returns {Promise<ContractTransaction>}
public async createAuction(
tokenContractAddress: string,
tokenId: BigNumberish,
duration: BigNumberish,
reservePrice: BigNumberish,
sellerFundsRecipient: string,
startTime: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
validateAndParseAddress(sellerFundsRecipient);
} catch (err) {
return Promise.reject(err);
}
return this.contract.createAuction(
tokenContractAddress,
tokenId,
duration,
reservePrice,
sellerFundsRecipient,
startTime
);
}
// cancelAuction
// @param {string} tokenContractAddress - Address of the token contract
// @param {BigNumberish} tokenId - Token ID
// @returns {Promise<ContractTransaction>}
public async cancelAuction(
tokenContractAddress: string,
tokenId: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
} catch (err) {
return Promise.reject(err);
}
return this.contract.cancelAuction(tokenContractAddress, tokenId);
}
// createBid
// @param {string} amount - Amount of ETH to send (payable function)
// @param {string} tokenContractAddress - Address of the token contract
// @param {BigNumberish} tokenId - Token ID
// @returns {Promise<ContractTransaction>}
public async createBid(
amount: string,
tokenContractAddress: string,
tokenId: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
} catch (err) {
return Promise.reject(err);
}
return this.contract.createBid(tokenContractAddress, tokenId, {
value: utils.parseEther(amount),
});
}
// setAuctionReservePrice
// @param {string} tokenContractAddress - Address of the token contract
// @param {BigNumberish} tokenId - Token ID
// @param {BigNumberish} reservePrice - Auction reserve price
// @returns {Promise<ContractTransaction>}
public async setAuctionReservePrice(
tokenContractAddress: string,
tokenId: BigNumberish,
reservePrice: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
} catch (err) {
return Promise.reject(err);
}
return this.contract.setAuctionReservePrice(
tokenContractAddress,
tokenId,
reservePrice
);
}
// settleAuction
// @param {string} tokenContractAddress - Address of the token contract
// @param {BigNumberish} tokenId - Token ID
// @returns {Promise<ContractTransaction>}
public async settleAuction(
tokenContractAddress: string,
tokenId: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
} catch (err) {
return Promise.reject(err);
}
return this.contract.settleAuction(tokenContractAddress, tokenId);
}
// Private methods
// Throws an error if called on read-only instance
// @returns {void}
private ensureNotReadOnly() {
if (this.readOnly) {
throw new Error('ensureReadOnly: Cannot modify read-only instance');
}
}
}