-
Notifications
You must be signed in to change notification settings - Fork 0
/
asksv11.ts
208 lines (190 loc) · 6.01 KB
/
asksv11.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
//aksv11.ts
// Class for Zora Asks V1.1 Module
import {BigNumberish} from '@ethersproject/bignumber';
import {ContractTransaction} from '@ethersproject/contracts';
import {Provider} from '@ethersproject/providers';
import {Signer, utils} from 'ethers';
import {
AsksV1_1 as AsksV11Type,
AsksV1_1__factory,
} from '@catalogworks/zorav3-with-types/dist/';
import {zoraAddresses} from './addresses';
import {chainIdToNetwork, validateAndParseAddress} from './utils';
import type {AskStructOutput} from './types';
export class AsksV11 {
public chainId: number;
public contractAddress: string;
public signerOrProvider: Signer | Provider;
public contract: AsksV11Type;
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].asks;
}
this.contract = AsksV1_1__factory.connect(
this.contractAddress,
this.signerOrProvider
);
}
// View Methods [Getters]
// askForNFT
// @param {string} inputAddress - Address of the token contract
// @param {BigNumberish} inputTokenId - Token ID
// @returns {Promise<AskStruct>} - Ask data for the NFT
public async fetchAskForNFT(
inputAddress: string,
tokenId: BigNumberish
): Promise<AskStructOutput> {
return this.contract.askForNFT(inputAddress, tokenId);
}
// 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]
// Create Ask
// @param {string} inputAddress - Address of the token contract
// @param {BigNumberish} inputTokenId - Token ID
// @param {BigNumberish} askPrice - Ask price
// @param {string} askCurrency - Ask currency contract address
// @param {string} sellerFundsRecipient - Address of the funds recipient
// @param {BigNumberish} findersFeeBPS - Finders fee BPS
// @returns {Promise<ContractTransaction>} - Transaction object
public async createAsk(
tokenContractAddress: string,
tokenId: BigNumberish,
askPrice: BigNumberish,
askCurrency: string,
sellerFundsRecipient: string,
findersFeeBPS: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
validateAndParseAddress(sellerFundsRecipient);
validateAndParseAddress(askCurrency);
} catch (err) {
return Promise.reject(err);
}
return this.contract.createAsk(
tokenContractAddress,
tokenId,
askPrice,
askCurrency,
sellerFundsRecipient,
findersFeeBPS
);
}
// setAskPrice
// @param {string} inputAddress - Address of the token contract
// @param {BigNumberish} inputTokenId - Token ID
// @param {BigNumberish} askPrice - Ask price
// @param {string} askCurrency - Ask currency contract address
// @returns {Promise<ContractTransaction>} - Transaction object
public async setAskPrice(
tokenContractAddress: string,
tokenId: BigNumberish,
askPrice: BigNumberish,
askCurrency: string
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
validateAndParseAddress(askCurrency);
} catch (err) {
return Promise.reject(err);
}
return this.contract.setAskPrice(
tokenContractAddress,
tokenId,
askPrice,
askCurrency
);
}
// cancelAsk
// @param {string} inputAddress - Address of the token contract
// @param {BigNumberish} inputTokenId - Token ID
// @returns {Promise<ContractTransaction>} - Transaction object
public async cancelAsk(
tokenContractAddress: string,
tokenId: BigNumberish
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
} catch (err) {
return Promise.reject(err);
}
return this.contract.cancelAsk(tokenContractAddress, tokenId);
}
// fillAsk
// @param {string} inputAddress - Address of the token contract
// @param {BigNumberish} inputTokenId - Token ID
// @param {string} fillCurrency - Fill currency contract address
// @param {BigNumberish} fillAmount - Fill amount
// @param {string} finder - Address of the finder
// @param {string} amount - optional amount in ETH (payable)
// @returns {Promise<ContractTransaction>} - Transaction object
public async fillAsk(
tokenContractAddress: string,
tokenId: BigNumberish,
fillCurrency: string,
fillAmount: BigNumberish,
finder: string,
amount?: string
): Promise<ContractTransaction> {
try {
this.ensureNotReadOnly();
validateAndParseAddress(tokenContractAddress);
validateAndParseAddress(fillCurrency);
validateAndParseAddress(finder);
} catch (err) {
return Promise.reject(err);
}
if (amount) {
return this.contract.fillAsk(
tokenContractAddress,
tokenId,
fillCurrency,
fillAmount,
finder,
{
value: utils.parseEther(amount),
}
);
} else {
return this.contract.fillAsk(
tokenContractAddress,
tokenId,
fillCurrency,
fillAmount,
finder
);
}
}
// 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');
}
}
}