@@ -23,12 +23,13 @@ import {
23
23
TokensToWrap ,
24
24
WrappedTokens ,
25
25
} from "../types/multiwrap" ;
26
- import { normalizePriceValue } from "../common/currency" ;
26
+ import { hasERC20Allowance , normalizePriceValue } from "../common/currency" ;
27
27
import { ITokenBundle , TokensWrappedEvent } from "contracts/Multiwrap" ;
28
28
import { MultiwrapContractSchema } from "../schema/contracts/multiwrap" ;
29
29
import { BigNumberish , ethers } from "ethers" ;
30
30
import TokenStruct = ITokenBundle . TokenStruct ;
31
31
import { QueryAllParams } from "../types" ;
32
+ import { isTokenApprovedForTransfer } from "../common/marketplace" ;
32
33
33
34
/**
34
35
* Multiwrap lets you wrap any number of ERC20, ERC721 and ERC1155 tokens you own into a single wrapped token bundle.
@@ -293,16 +294,32 @@ export class Multiwrap extends Erc721<MultiwrapContract> {
293
294
const tokens : TokenStruct [ ] = [ ] ;
294
295
295
296
const provider = this . contractWrapper . getProvider ( ) ;
297
+ const owner = await this . contractWrapper . getSignerAddress ( ) ;
296
298
297
299
if ( contents . erc20Tokens ) {
298
300
for ( const erc20 of contents . erc20Tokens ) {
301
+ const normalizedQuantity = await normalizePriceValue (
302
+ provider ,
303
+ erc20 . quantity ,
304
+ erc20 . contractAddress ,
305
+ ) ;
306
+ const hasAllowance = await hasERC20Allowance (
307
+ this . contractWrapper ,
308
+ erc20 . contractAddress ,
309
+ normalizedQuantity ,
310
+ ) ;
311
+ if ( ! hasAllowance ) {
312
+ throw new Error (
313
+ `ERC20 token with contract address "${
314
+ erc20 . contractAddress
315
+ } " 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("${
316
+ erc20 . contractAddress
317
+ } ").setAllowance("${ this . getAddress ( ) } ", ${ erc20 . quantity } );\n\n`,
318
+ ) ;
319
+ }
299
320
tokens . push ( {
300
321
assetContract : erc20 . contractAddress ,
301
- totalAmount : await normalizePriceValue (
302
- provider ,
303
- erc20 . quantity ,
304
- erc20 . contractAddress ,
305
- ) ,
322
+ totalAmount : normalizedQuantity ,
306
323
tokenId : 0 ,
307
324
tokenType : 0 ,
308
325
} ) ;
@@ -311,6 +328,26 @@ export class Multiwrap extends Erc721<MultiwrapContract> {
311
328
312
329
if ( contents . erc721Tokens ) {
313
330
for ( const erc721 of contents . erc721Tokens ) {
331
+ const isApproved = await isTokenApprovedForTransfer (
332
+ this . contractWrapper . getProvider ( ) ,
333
+ this . getAddress ( ) ,
334
+ erc721 . contractAddress ,
335
+ erc721 . tokenId ,
336
+ owner ,
337
+ ) ;
338
+
339
+ if ( ! isApproved ) {
340
+ throw new Error (
341
+ `ERC721 token "${ erc721 . tokenId } " with contract address "${
342
+ erc721 . contractAddress
343
+ } " is not approved for transfer.\n\nYou can give approval the multiwrap contract to transfer this token by running:\n\nawait sdk.getNFTCollection("${
344
+ erc721 . contractAddress
345
+ } ").setApprovalForToken("${ this . getAddress ( ) } ", ${
346
+ erc721 . tokenId
347
+ } );\n\n`,
348
+ ) ;
349
+ }
350
+
314
351
tokens . push ( {
315
352
assetContract : erc721 . contractAddress ,
316
353
totalAmount : 0 ,
@@ -322,6 +359,23 @@ export class Multiwrap extends Erc721<MultiwrapContract> {
322
359
323
360
if ( contents . erc1155Tokens ) {
324
361
for ( const erc1155 of contents . erc1155Tokens ) {
362
+ const isApproved = await isTokenApprovedForTransfer (
363
+ this . contractWrapper . getProvider ( ) ,
364
+ this . getAddress ( ) ,
365
+ erc1155 . contractAddress ,
366
+ erc1155 . tokenId ,
367
+ owner ,
368
+ ) ;
369
+
370
+ if ( ! isApproved ) {
371
+ throw new Error (
372
+ `ERC1155 token "${ erc1155 . tokenId } " with contract address "${
373
+ erc1155 . contractAddress
374
+ } " is not approved for transfer.\n\nYou can give approval the multiwrap contract to transfer this token by running:\n\nawait sdk.getEdition("${
375
+ erc1155 . contractAddress
376
+ } ").setApprovalForAll("${ this . getAddress ( ) } ", true);\n\n`,
377
+ ) ;
378
+ }
325
379
tokens . push ( {
326
380
assetContract : erc1155 . contractAddress ,
327
381
totalAmount : erc1155 . quantity ,
0 commit comments