@zoralabs/protocol-sdk 0.5.6 → 0.5.7-MINT.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -502,6 +502,13 @@ var networkConfigByChain = {
502
502
  subgraphUrl: getSubgraph("zora-create-zora-testnet", "stable")
503
503
  }
504
504
  };
505
+ var getSubgraphUrl = (chainId) => {
506
+ const networkConfig = networkConfigByChain[chainId];
507
+ if (!networkConfig) {
508
+ throw new Error(`Network not configured for chain id ${chainId}`);
509
+ }
510
+ return networkConfig.subgraphUrl;
511
+ };
505
512
 
506
513
  // src/mint/mint-api-client.ts
507
514
  var getApiNetworkConfigForChain = (chainId) => {
@@ -1574,7 +1581,7 @@ function create1155CreatorClient({
1574
1581
  throw new Error("Invariant: contract cannot be missing and an address");
1575
1582
  }
1576
1583
  if (!contractExists && typeof contract !== "string") {
1577
- const request = {
1584
+ const request2 = {
1578
1585
  abi: zoraCreator1155FactoryImplABI,
1579
1586
  functionName: "createContractDeterministic",
1580
1587
  account,
@@ -1593,13 +1600,13 @@ function create1155CreatorClient({
1593
1600
  ]
1594
1601
  };
1595
1602
  return {
1596
- request,
1603
+ request: request2,
1597
1604
  tokenSetupActions,
1598
1605
  contractAddress,
1599
1606
  contractExists
1600
1607
  };
1601
1608
  } else if (contractExists) {
1602
- const request = {
1609
+ const request2 = {
1603
1610
  abi: zoraCreator1155ImplABI3,
1604
1611
  functionName: "multicall",
1605
1612
  account,
@@ -1607,7 +1614,7 @@ function create1155CreatorClient({
1607
1614
  args: [tokenSetupActions]
1608
1615
  };
1609
1616
  return {
1610
- request,
1617
+ request: request2,
1611
1618
  tokenSetupActions,
1612
1619
  contractAddress,
1613
1620
  contractExists
@@ -1617,6 +1624,93 @@ function create1155CreatorClient({
1617
1624
  }
1618
1625
  return { createNew1155Token };
1619
1626
  }
1627
+
1628
+ // src/mints/mints-queries.ts
1629
+ import { request, gql } from "graphql-request";
1630
+ var getMintsAccountBalanceWithPriceQuery = (account) => {
1631
+ const query = gql`
1632
+ query GetMintAccountBalances($account: String!) {
1633
+ mintAccountBalances(where: { account: $account }) {
1634
+ balance
1635
+ mintToken {
1636
+ id
1637
+ pricePerToken
1638
+ }
1639
+ }
1640
+ }
1641
+ `;
1642
+ return {
1643
+ query,
1644
+ variables: { account }
1645
+ };
1646
+ };
1647
+ var selectMintsToCollectWithFromQueryResult = (mintAccountBalances, quantityToCollect) => {
1648
+ const parsed = mintAccountBalances.map((r) => {
1649
+ return {
1650
+ tokenId: BigInt(r.mintToken.id),
1651
+ quantity: BigInt(r.balance),
1652
+ pricePerToken: BigInt(r.mintToken.pricePerToken)
1653
+ };
1654
+ });
1655
+ const sorted = parsed.sort((a, b) => {
1656
+ if (a.pricePerToken < b.pricePerToken) {
1657
+ return -1;
1658
+ }
1659
+ return 1;
1660
+ });
1661
+ let remainingQuantity = quantityToCollect;
1662
+ const tokenIds = [];
1663
+ const quantities = [];
1664
+ while (remainingQuantity > 0) {
1665
+ const next = sorted.shift();
1666
+ if (!next) {
1667
+ throw new Error("Not enough MINTs to collect with");
1668
+ }
1669
+ const quantityToUse = remainingQuantity > next.quantity ? next.quantity : remainingQuantity;
1670
+ tokenIds.push(next.tokenId);
1671
+ quantities.push(quantityToUse);
1672
+ remainingQuantity -= quantityToUse;
1673
+ }
1674
+ return {
1675
+ tokenIds,
1676
+ quantities
1677
+ };
1678
+ };
1679
+ var sumBalances = (mintAccountBalances) => {
1680
+ return mintAccountBalances.reduce((acc, curr) => {
1681
+ return acc + BigInt(curr.balance);
1682
+ }, BigInt(0));
1683
+ };
1684
+ var getMINTsToCollectWith = async ({
1685
+ account,
1686
+ chainId,
1687
+ quantityToCollect
1688
+ }) => {
1689
+ const subgraphUrl = getSubgraphUrl(chainId);
1690
+ const { query, variables } = getMintsAccountBalanceWithPriceQuery(account);
1691
+ const result = await request(
1692
+ subgraphUrl,
1693
+ query,
1694
+ variables
1695
+ );
1696
+ return selectMintsToCollectWithFromQueryResult(
1697
+ result.mintTokenBalances,
1698
+ quantityToCollect
1699
+ );
1700
+ };
1701
+ var getMINTsBalance = async ({
1702
+ chainId,
1703
+ account
1704
+ }) => {
1705
+ const subgraphUrl = getSubgraphUrl(chainId);
1706
+ const { query, variables } = getMintsAccountBalanceWithPriceQuery(account);
1707
+ const result = await request(
1708
+ subgraphUrl,
1709
+ query,
1710
+ variables
1711
+ );
1712
+ return sumBalances(result.mintTokenBalances);
1713
+ };
1620
1714
  export {
1621
1715
  DEFAULT_SALE_SETTINGS,
1622
1716
  Errors,
@@ -1639,7 +1733,10 @@ export {
1639
1733
  encodePremintForAPI,
1640
1734
  getApiNetworkConfigForChain,
1641
1735
  getDefaultFixedPriceMinterAddress,
1736
+ getMINTsBalance,
1737
+ getMINTsToCollectWith,
1642
1738
  getMintCosts,
1739
+ getMintsAccountBalanceWithPriceQuery,
1643
1740
  getPremintCollectionAddress,
1644
1741
  getPremintExecutorAddress,
1645
1742
  getPremintMintCosts,
@@ -1655,6 +1752,8 @@ export {
1655
1752
  premintTypedDataDefinition,
1656
1753
  recoverCreatorFromCreatorAttribution,
1657
1754
  recoverPremintSigner,
1755
+ selectMintsToCollectWithFromQueryResult,
1756
+ sumBalances,
1658
1757
  supportedPremintVersions,
1659
1758
  supportsPremintVersion,
1660
1759
  tryRecoverPremintSigner,