/
multiwrap.ts
433 lines (408 loc) · 13.7 KB
/
multiwrap.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import type { Multiwrap as MultiwrapContract } from "@thirdweb-dev/contracts-js";
import {
ITokenBundle,
TokensWrappedEvent,
} from "@thirdweb-dev/contracts-js/dist/declarations/src/Multiwrap";
import { ThirdwebStorage } from "@thirdweb-dev/storage";
import { utils, type BigNumberish, type CallOverrides } from "ethers";
import { NFT, NFTMetadataOrUri } from "../../../core/schema/nft";
import { fetchCurrencyMetadata } from "../../common/currency/fetchCurrencyMetadata";
import { hasERC20Allowance } from "../../common/currency/hasERC20Allowance";
import { normalizePriceValue } from "../../common/currency/normalizePriceValue";
import { resolveAddress } from "../../common/ens/resolveAddress";
import { isTokenApprovedForTransfer } from "../../common/marketplace";
import { uploadOrExtractURI } from "../../common/nft";
import { buildTransactionFunction } from "../../common/transactions";
import { ContractAppURI } from "../../core/classes/contract-appuri";
import { ContractEncoder } from "../../core/classes/contract-encoder";
import { ContractEvents } from "../../core/classes/contract-events";
import { ContractMetadata } from "../../core/classes/contract-metadata";
import { ContractOwner } from "../../core/classes/contract-owner";
import { ContractRoles } from "../../core/classes/contract-roles";
import { ContractRoyalty } from "../../core/classes/contract-royalty";
import { ContractWrapper } from "../../core/classes/internal/contract-wrapper";
import { StandardErc721 } from "../../core/classes/internal/erc721/erc-721-standard";
import { GasCostEstimator } from "../../core/classes/gas-cost-estimator";
import { Transaction } from "../../core/classes/transactions";
import { NetworkInput, TransactionResultWithId } from "../../core/types";
import { Abi, AbiInput, AbiSchema } from "../../schema/contracts/custom";
import { MultiwrapContractSchema } from "../../schema/contracts/multiwrap";
import { SDKOptions } from "../../schema/sdk-options";
import { AddressOrEns } from "../../schema/shared/AddressOrEnsSchema";
import {
ERC1155Wrappable,
ERC20Wrappable,
ERC721Wrappable,
TokensToWrap,
WrappedTokens,
} from "../../types/multiwrap";
import { MULTIWRAP_CONTRACT_ROLES } from "../contractRoles";
/**
* Multiwrap lets you wrap any number of ERC20, ERC721 and ERC1155 tokens you own into a single wrapped token bundle.
*
* @example
*
* ```javascript
* import { ThirdwebSDK } from "@thirdweb-dev/sdk";
*
* const sdk = new ThirdwebSDK("{{chainName}}");
* const contract = await sdk.getContract("{{contract_address}}", "multiwrap");
* ```
*
* @beta
*/
// TODO create extension wrappers for this
export class Multiwrap extends StandardErc721<MultiwrapContract> {
static contractRoles = MULTIWRAP_CONTRACT_ROLES;
public abi: Abi;
public encoder: ContractEncoder<MultiwrapContract>;
public estimator: GasCostEstimator<MultiwrapContract>;
public metadata: ContractMetadata<
MultiwrapContract,
typeof MultiwrapContractSchema
>;
public app: ContractAppURI<MultiwrapContract>;
public events: ContractEvents<MultiwrapContract>;
public roles: ContractRoles<
MultiwrapContract,
(typeof Multiwrap.contractRoles)[number]
>;
/**
* Configure royalties
* @remarks Set your own royalties for the entire contract or per token
* @example
* ```javascript
* // royalties on the whole contract
* contract.royalties.setDefaultRoyaltyInfo({
* seller_fee_basis_points: 100, // 1%
* fee_recipient: "0x..."
* });
* // override royalty for a particular token
* contract.royalties.setTokenRoyaltyInfo(tokenId, {
* seller_fee_basis_points: 500, // 5%
* fee_recipient: "0x..."
* });
* ```
*/
public royalties: ContractRoyalty<
MultiwrapContract,
typeof MultiwrapContractSchema
>;
public owner: ContractOwner;
constructor(
network: NetworkInput,
address: string,
storage: ThirdwebStorage,
options: SDKOptions = {},
abi: AbiInput,
chainId: number,
contractWrapper = new ContractWrapper<MultiwrapContract>(
network,
address,
abi,
options,
storage,
),
) {
super(contractWrapper, storage, chainId);
this.abi = AbiSchema.parse(abi || []);
this.metadata = new ContractMetadata(
this.contractWrapper,
MultiwrapContractSchema,
this.storage,
);
this.app = new ContractAppURI(
this.contractWrapper,
this.metadata,
this.storage,
);
this.roles = new ContractRoles(
this.contractWrapper,
Multiwrap.contractRoles,
);
this.encoder = new ContractEncoder(this.contractWrapper);
this.estimator = new GasCostEstimator(this.contractWrapper);
this.events = new ContractEvents(this.contractWrapper);
this.royalties = new ContractRoyalty(this.contractWrapper, this.metadata);
this.owner = new ContractOwner(this.contractWrapper);
}
/** ******************************
* READ FUNCTIONS
*******************************/
/**
* Get the contents of a wrapped token bundle
* @example
* ```javascript
* const contents = await contract.getWrappedContents(wrappedTokenId);
* console.log(contents.erc20Tokens);
* console.log(contents.erc721Tokens);
* console.log(contents.erc1155Tokens);
* ```
* @param wrappedTokenId - the id of the wrapped token bundle
*/
public async getWrappedContents(
wrappedTokenId: BigNumberish,
): Promise<WrappedTokens> {
const wrappedTokens = await this.contractWrapper.read(
"getWrappedContents",
[wrappedTokenId],
);
const erc20Tokens: ERC20Wrappable[] = [];
const erc721Tokens: ERC721Wrappable[] = [];
const erc1155Tokens: ERC1155Wrappable[] = [];
for (const token of wrappedTokens) {
switch (token.tokenType) {
case 0: {
const tokenMetadata = await fetchCurrencyMetadata(
this.contractWrapper.getProvider(),
token.assetContract,
);
erc20Tokens.push({
contractAddress: token.assetContract,
quantity: utils.formatUnits(
token.totalAmount,
tokenMetadata.decimals,
),
});
break;
}
case 1: {
erc721Tokens.push({
contractAddress: token.assetContract,
tokenId: token.tokenId,
});
break;
}
case 2: {
erc1155Tokens.push({
contractAddress: token.assetContract,
tokenId: token.tokenId,
quantity: token.totalAmount.toString(),
});
break;
}
}
}
return {
erc20Tokens,
erc721Tokens,
erc1155Tokens,
};
}
/** ******************************
* WRITE FUNCTIONS
*******************************/
/**
* Wrap any number of ERC20/ERC721/ERC1155 tokens into a single wrapped token
* @example
* ```javascript
* const tx = await contract.wrap({
* erc20Tokens: [{
* contractAddress: "0x...",
* quantity: "0.8"
* }],
* erc721Tokens: [{
* contractAddress: "0x...",
* tokenId: "0"
* }],
* erc1155Tokens: [{
* contractAddress: "0x...",
* tokenId: "1",
* quantity: "2"
* }]
* }, {
* name: "Wrapped bundle",
* description: "This is a wrapped bundle of tokens and NFTs",
* image: "ipfs://...",
* });
* const receipt = tx.receipt(); // the transaction receipt
* const wrappedTokenId = tx.id; // the id of the wrapped token bundle
* ```
* @param contents - the contents to wrap
* @param wrappedTokenMetadata - metadata to represent the wrapped token bundle
* @param recipientAddress - Optional. The address to send the wrapped token bundle to
*/
wrap = /* @__PURE__ */ buildTransactionFunction(
async (
contents: TokensToWrap,
wrappedTokenMetadata: NFTMetadataOrUri,
recipientAddress?: AddressOrEns,
): Promise<Transaction<TransactionResultWithId<NFT>>> => {
const [uri, tokens, recipient] = await Promise.all([
uploadOrExtractURI(wrappedTokenMetadata, this.storage),
this.toTokenStructList(contents),
resolveAddress(
recipientAddress
? recipientAddress
: await this.contractWrapper.getSignerAddress(),
),
]);
return Transaction.fromContractWrapper({
contractWrapper: this.contractWrapper,
method: "wrap",
args: [tokens, uri, recipient],
parse: (receipt) => {
const event = this.contractWrapper.parseLogs<TokensWrappedEvent>(
"TokensWrapped",
receipt?.logs,
);
if (event.length === 0) {
throw new Error("TokensWrapped event not found");
}
const tokenId = event[0].args.tokenIdOfWrappedToken;
return {
id: tokenId,
receipt,
data: () => this.get(tokenId),
};
},
});
},
);
/**
* Unwrap a wrapped token bundle, and retrieve its contents
* @example
* ```javascript
* await contract.unwrap(wrappedTokenId);
* ```
* @param wrappedTokenId - the id of the wrapped token bundle
* @param recipientAddress - Optional. The address to send the unwrapped tokens to
*/
unwrap = /* @__PURE__ */ buildTransactionFunction(
async (wrappedTokenId: BigNumberish, recipientAddress?: AddressOrEns) => {
const recipient = await resolveAddress(
recipientAddress
? recipientAddress
: await this.contractWrapper.getSignerAddress(),
);
return Transaction.fromContractWrapper({
contractWrapper: this.contractWrapper,
method: "unwrap",
args: [wrappedTokenId, recipient],
});
},
);
/** ******************************
* PRIVATE FUNCTIONS
*******************************/
private async toTokenStructList(contents: TokensToWrap) {
const tokens: ITokenBundle.TokenStruct[] = [];
const provider = this.contractWrapper.getProvider();
const owner = await this.contractWrapper.getSignerAddress();
if (contents.erc20Tokens) {
for (const erc20 of contents.erc20Tokens) {
const normalizedQuantity = await normalizePriceValue(
provider,
erc20.quantity,
erc20.contractAddress,
);
const hasAllowance = await hasERC20Allowance(
this.contractWrapper,
erc20.contractAddress,
normalizedQuantity,
);
if (!hasAllowance) {
throw new Error(
`ERC20 token with contract address "${
erc20.contractAddress
}" does not have enough allowance to transfer.\n\nYou can set allowance to the multiwrap contract to transfer these tokens by running:\n\nawait sdk.getToken("${
erc20.contractAddress
}").setAllowance("${this.getAddress()}", ${erc20.quantity});\n\n`,
);
}
tokens.push({
assetContract: erc20.contractAddress,
totalAmount: normalizedQuantity,
tokenId: 0,
tokenType: 0,
});
}
}
if (contents.erc721Tokens) {
for (const erc721 of contents.erc721Tokens) {
const isApproved = await isTokenApprovedForTransfer(
this.contractWrapper.getProvider(),
this.getAddress(),
erc721.contractAddress,
erc721.tokenId,
owner,
);
if (!isApproved) {
throw new Error(
`ERC721 token "${erc721.tokenId}" with contract address "${
erc721.contractAddress
}" is not approved for transfer.\n\nYou can give approval the multiwrap contract to transfer this token by running:\n\nawait sdk.getNFTCollection("${
erc721.contractAddress
}").setApprovalForToken("${this.getAddress()}", ${
erc721.tokenId
});\n\n`,
);
}
tokens.push({
assetContract: erc721.contractAddress,
totalAmount: 0,
tokenId: erc721.tokenId,
tokenType: 1,
});
}
}
if (contents.erc1155Tokens) {
for (const erc1155 of contents.erc1155Tokens) {
const isApproved = await isTokenApprovedForTransfer(
this.contractWrapper.getProvider(),
this.getAddress(),
erc1155.contractAddress,
erc1155.tokenId,
owner,
);
if (!isApproved) {
throw new Error(
`ERC1155 token "${erc1155.tokenId}" with contract address "${
erc1155.contractAddress
}" is not approved for transfer.\n\nYou can give approval the multiwrap contract to transfer this token by running:\n\nawait sdk.getEdition("${
erc1155.contractAddress
}").setApprovalForAll("${this.getAddress()}", true);\n\n`,
);
}
tokens.push({
assetContract: erc1155.contractAddress,
totalAmount: erc1155.quantity,
tokenId: erc1155.tokenId,
tokenType: 2,
});
}
}
return tokens;
}
/**
* @internal
*/
public async prepare<
TMethod extends
keyof MultiwrapContract["functions"] = keyof MultiwrapContract["functions"],
>(
method: string & TMethod,
args: any[] & Parameters<MultiwrapContract["functions"][TMethod]>,
overrides?: CallOverrides,
) {
return Transaction.fromContractWrapper({
contractWrapper: this.contractWrapper,
method,
args,
overrides,
});
}
/**
* @internal
*/
public async call<
TMethod extends
keyof MultiwrapContract["functions"] = keyof MultiwrapContract["functions"],
>(
functionName: string & TMethod,
args?: Parameters<MultiwrapContract["functions"][TMethod]>,
overrides?: CallOverrides,
): Promise<ReturnType<MultiwrapContract["functions"][TMethod]>> {
return this.contractWrapper.call(functionName, args, overrides);
}
}