• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Mintbase / mintbase-js / 8878006473

29 Apr 2024 11:48AM UTC coverage: 77.402% (+0.08%) from 77.325%
8878006473

push

github

web-flow
Fix decimals for FT minting (#506)

909 of 1364 branches covered (66.64%)

Branch coverage included in aggregate %.

9 of 11 new or added lines in 3 files covered. (81.82%)

1218 of 1384 relevant lines covered (88.01%)

13.79 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.71
/packages/sdk/src/createMetadata/createMetadata.ts
1
import BN from 'bn.js';
6✔
2
import { mbjs } from '../config/config';
6✔
3
import { GAS, STORAGE_BYTES, STORAGE_PRICE_PER_BYTE_EXPONENT } from '../constants';
6✔
4
import { ERROR_MESSAGES } from '../errorMessages';
6✔
5
import { CreateMetadataArgs, CreateMetadataArgsResponse, NearContractCall, TokenMetadata, TOKEN_METHOD_NAMES } from '../types';
6✔
6
import { isIntString, isStoreV2, processRoyalties } from '../utils';
6✔
7

8
/**
9
 * Mint a token given via reference json on a given contract with a specified owner, amount of copies as well and royalties can be specified via options
10
 * @param mintArguments {@link MintArgs}
11
 * @returns contract call to be passed to @mintbase-js/sdk execute method
12
 */
13
export const createMetadata = (
6✔
14
  args: CreateMetadataArgs,
15
): NearContractCall<CreateMetadataArgsResponse> => {
16
  const {
17
    contractAddress = mbjs.keys.contractAddress,
×
18
    metadata,
19
    metadataId = null,
22✔
20
    royalties = null,
14✔
21
    price,
22
    mintersAllowlist = null,
22✔
23
    maxSupply = null,
22✔
24
    startsAt = null,
22✔
25
    expiresAt = null,
22✔
26
    isDynamic = null,
22✔
27
    noMedia = false,
24✔
28
    noReference = false,
24✔
29
    ftAddress = null,
22✔
30
    ftDecimals = null,
22✔
31
  } = args;
36✔
32
  
33
  if (!contractAddress) {
36!
34
    throw new Error(ERROR_MESSAGES.CONTRACT_ADDRESS);
×
35
  }
36

37
  if (!isStoreV2(contractAddress)) {
36!
38
    throw new Error(ERROR_MESSAGES.ONLY_V2);
×
39
  }
40

41
  // Reference and media need to be present or explicitly opted out of
42
  if (!noReference && !metadata.reference) {
36✔
43
    throw new Error(ERROR_MESSAGES.NO_REFERENCE);
3✔
44
  }
45
  if (!noMedia && !metadata.media) {
33✔
46
    throw new Error(ERROR_MESSAGES.NO_MEDIA);
3✔
47
  }
48

49
  if (metadataId && !isIntString(metadataId)) {
30!
50
    throw new Error(ERROR_MESSAGES.METADATA_ID_NOT_INT);
×
51
  }
52

53
  if ((ftAddress && !ftDecimals) || (ftDecimals && !ftAddress)) {
30!
NEW
54
    throw new Error(ERROR_MESSAGES.FT_ADDRESS_DECIMALS);
×
55
  }
56

57
  const { royaltyTotal, roundedRoyalties } = processRoyalties(royalties);
30✔
58

59
  return {
24✔
60
    contractAddress: contractAddress || mbjs.keys.contractAddress,
16!
61
    args: {
62
      metadata: metadata,
63
      metadata_id: metadataId,
64
      // 10_000 = 100% (see above note)
65
      royalty_args: !royaltyTotal ? null : { split_between: roundedRoyalties, percentage: Math.round(royaltyTotal * 10000) },
16✔
66
      minters_allowlist: mintersAllowlist,
67
      max_supply: maxSupply,
68
      starts_at: startsAt ? (+startsAt * 1e6).toString() : null,
16✔
69
      expires_at: expiresAt ? (+expiresAt * 1e6).toString() : null,
16✔
70
      is_dynamic: isDynamic,
71
      price: formatPrice(price, ftDecimals),
72
      ft_contract_id: ftAddress,
73
    },
74
    methodName: TOKEN_METHOD_NAMES.CREATE_METADATA,
75
    gas: GAS,
76
    deposit: createMetadataDeposit({
77
      nRoyalties: !royalties ? 0 : Object.keys(royalties)?.length,
22!
78
      nMinters: !mintersAllowlist? 0 : mintersAllowlist.length,
16✔
79
      metadata,
80
    }),
81
  };
82
};
83

84
export function createMetadataDeposit({
6✔
85
  nRoyalties,
86
  nMinters,
87
  metadata,
88
}: {
89
  nRoyalties: number;
90
  nMinters: number;
91
  metadata: TokenMetadata;
92
}): string {
93
  const metadataBytesEstimate = JSON.stringify(metadata).length;
24✔
94
  // storage + nRoyalties * common + nMinters * common + 2 * common
95
  const totalBytes = 2 * STORAGE_BYTES.COMMON +
24✔
96
    STORAGE_BYTES.MINTING_FEE +
97
    metadataBytesEstimate +
98
    STORAGE_BYTES.COMMON * nRoyalties +
99
    STORAGE_BYTES.COMMON * nMinters;
100

101
  return `${Math.ceil(totalBytes)}${'0'.repeat(STORAGE_PRICE_PER_BYTE_EXPONENT)}`;
24✔
102
}
103

104
export function formatPrice(price: number, ftDecimals: number): string {
6✔
105
  const base = new BN(price * 1e6);
36✔
106
  const multiplier = new BN(`1${'0'.repeat((ftDecimals ?? 24) - 6)}`);
36✔
107
  return base.mul(multiplier).toString();
36✔
108
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc