11import { multicall } from "../../../extensions/common/__generated__/IMulticall/write/multicall.js" ;
2- import type { BaseTransactionOptions } from "../../../transaction/types.js" ;
2+ import type {
3+ BaseTransactionOptions ,
4+ WithOverrides ,
5+ } from "../../../transaction/types.js" ;
36import { uri } from "../__generated__/IERC1155/read/uri.js" ;
47import { encodeMintTo } from "../__generated__/IMintableERC1155/write/mintTo.js" ;
58import type { MintAdditionalSupplyToParams } from "./mintAdditionalSupplyTo.js" ;
69
710/**
811 * @extension ERC1155
912 */
10- export type MintAdditionalSupplyToBatchParams = {
13+ export type MintAdditionalSupplyToBatchParams = WithOverrides < {
1114 nfts : MintAdditionalSupplyToParams [ ] ;
12- } ;
15+ } > ;
1316
1417/**
1518 * This extension batches multiple `mintAdditionalSupplyToBatch` extensions into one single multicall.
@@ -38,8 +41,9 @@ export function mintAdditionalSupplyToBatch(
3841 return multicall ( {
3942 contract : options . contract ,
4043 asyncParams : async ( ) => {
44+ const nfts = optimizeMintBatchContent ( options . nfts ) ;
4145 const data = await Promise . all (
42- options . nfts . map ( async ( nft ) => {
46+ nfts . map ( async ( nft ) => {
4347 const tokenUri = await uri ( {
4448 contract : options . contract ,
4549 tokenId : nft . tokenId ,
@@ -54,5 +58,48 @@ export function mintAdditionalSupplyToBatch(
5458 ) ;
5559 return { data } ;
5660 } ,
61+ overrides : options . overrides ,
5762 } ) ;
5863}
64+
65+ /**
66+ * Optimization
67+ *
68+ * We can batch the records that share the same "to" & "tokenId" into 1 transaction
69+ *
70+ * For example, this struct:
71+ * [
72+ * { tokenId: 0n, supply: 99n, to: account.address },
73+ * { tokenId: 1n, supply: 49n, to: account.address },
74+ * { tokenId: 1n, supply: 51n, to: account.address },
75+ * ]
76+ *
77+ * ...can be packed into:
78+ * [
79+ * { tokenId: 0n, supply: 99n, to: account.address },
80+ * { tokenId: 1n, supply: 100n, to: account.address },
81+ * ]
82+ * @internal
83+ */
84+ export function optimizeMintBatchContent (
85+ nfts : MintAdditionalSupplyToParams [ ] ,
86+ ) : MintAdditionalSupplyToParams [ ] {
87+ const results : MintAdditionalSupplyToParams [ ] = [ ] ;
88+ for ( const item of nfts ) {
89+ const matchingIndex = results . findIndex (
90+ ( o ) =>
91+ o . tokenId === item . tokenId &&
92+ o . to . toLowerCase ( ) === item . to . toLowerCase ( ) ,
93+ ) ;
94+ if ( matchingIndex !== - 1 ) {
95+ results [ matchingIndex ] = {
96+ to : item . to ,
97+ tokenId : item . tokenId ,
98+ supply : item . supply + ( results [ matchingIndex ] ?. supply || 0n ) ,
99+ } ;
100+ } else {
101+ results . push ( item ) ;
102+ }
103+ }
104+ return results ;
105+ }
0 commit comments