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

Mintbase / mintbase-js / 8877082828

29 Apr 2024 10:28AM UTC coverage: 77.402%. First build
8877082828

Pull #506

github

tifrel
Fix lint & test
Pull Request #506: Fix decimals for FT minting

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

79.01
/packages/sdk/src/mintOnMetadata/mintOnMetadata.ts
1
import BN from 'bn.js';
3✔
2
import { mbjs } from '../config/config';
3✔
3
import { GAS, MAX_GAS, ONE_YOCTO, STORAGE_BYTES, STORAGE_PRICE_PER_BYTE_EXPONENT } from '../constants';
3✔
4
import { ERROR_MESSAGES } from '../errorMessages';
3✔
5
import { EmptyObject, FT_METHOD_NAMES, FtTransferCallArgsResponse, MintOnMetadataArgs, MintOnMetadataArgsResponse, NearContractCall, TOKEN_METHOD_NAMES } from '../types';
3✔
6
import { isIntString, isStoreV2 } from '../utils';
3✔
7
import { formatPrice } from '../createMetadata/createMetadata';
3✔
8

9
type DepositAndMintCalls = [NearContractCall<EmptyObject>, NearContractCall<MintOnMetadataArgsResponse | FtTransferCallArgsResponse>]
10

11
/**
12
 * 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
13
 * @param mintArguments {@link MintArgs}
14
 * @returns contract call to be passed to @mintbase-js/sdk execute method
15
 */
16
export const mintOnMetadata = (
3✔
17
  args: MintOnMetadataArgs,
18
): DepositAndMintCalls  => {
19
  const {
20
    contractAddress = mbjs.keys.contractAddress,
×
21
    metadataId,
22
    ownerId,
23
    amount = null,
8✔
24
    tokenIds = null,
6✔
25
    price,
26
    ftAddress,
27
    ftDecimals,
28
  } = args;
18✔
29

30
  if (!contractAddress) {
18!
31
    throw new Error(ERROR_MESSAGES.CONTRACT_ADDRESS);
×
32
  }
33

34
  if (!isStoreV2(contractAddress)) {
18!
35
    throw new Error(ERROR_MESSAGES.ONLY_V2);
×
36
  }
37

38
  if (!isIntString(metadataId)) {
18!
39
    throw new Error(ERROR_MESSAGES.METADATA_ID_NOT_INT);
×
40
  }
41

42
  if ((ftAddress && !ftDecimals) || (ftDecimals && !ftAddress)) {
18!
NEW
43
    throw new Error(ERROR_MESSAGES.FT_ADDRESS_DECIMALS);
×
44
  }
45

46
  if (tokenIds && tokenIds.length === 0) {
18✔
47
    throw new Error(ERROR_MESSAGES.EMPTY_TOKEN_IDS);
3✔
48
  }
49

50
  if (amount && tokenIds && tokenIds.length !== amount) {
15✔
51
    throw new Error(ERROR_MESSAGES.MUTUAL_EXCLUSIVE_AMOUNT);
3✔
52
  }
53
  const amountCalc = amount || (tokenIds && tokenIds.length) || 1;
12✔
54
  if (amountCalc> 99 || amountCalc < 1) {
12!
55
    throw new Error(ERROR_MESSAGES.INVALID_AMOUNT);
×
56
  }
57

58
  if (tokenIds && !tokenIds.every(isIntString)) {
12!
59
    throw new Error(ERROR_MESSAGES.TOKEN_ID_NOT_INT);
×
60
  }
61

62
  const mintArgs = {
12✔
63
    metadata_id: metadataId,
64
    owner_id: ownerId,
65
    num_to_mint: tokenIds ? null : amountCalc,
8✔
66
    token_ids: tokenIds,
67
  };
68
  const storageDeposit = depositMintingStorage(
12✔
69
    contractAddress, mintOnMetadataDeposit(amountCalc),
70
  );
71
  const formattedPrice = formatPrice(price * amountCalc, ftDecimals);
12✔
72

73
  if (ftAddress) {
12✔
74
    return [
3✔
75
      storageDeposit,
76
      {
77
        contractAddress: ftAddress,
78
        args: {
79
          receiver_id: contractAddress,
80
          amount: formattedPrice,
81
          msg: JSON.stringify(mintArgs),
82
          memo: null,
83
        },
84
        methodName: FT_METHOD_NAMES.FT_TRANSFER_CALL,
85
        deposit: ONE_YOCTO,
86
        gas: MAX_GAS,
87
      },
88
    ];
89
  }
90

91
  return [
9✔
92
    storageDeposit,
93
    {
94
      contractAddress: contractAddress || mbjs.keys.contractAddress,
6!
95
      args: mintArgs,
96
      methodName: TOKEN_METHOD_NAMES.MINT_ON_METADATA,
97
      gas: GAS,
98
      deposit: formattedPrice,
99
    },
100
  ];
101
};
102

103
const depositMintingStorage = (
3✔
104
  contractAddress: string, amount: string,
105
): NearContractCall<EmptyObject> => ({
12✔
106
  contractAddress,
107
  args: {},
108
  methodName: TOKEN_METHOD_NAMES.DEPOSIT_STORAGE,
109
  gas: GAS,
110
  deposit: amount,
111
});
112

113

114
export function mintOnMetadataDeposit(nTokens: number): string {
3✔
115
  const totalBytes = STORAGE_BYTES.MINTING_BASE +
12✔
116
    STORAGE_BYTES.MINTING_FEE +
117
    (STORAGE_BYTES.TOKEN_BASE + STORAGE_BYTES.COMMON) * nTokens;
118
  const storageCost = new BN(`${Math.ceil(totalBytes)}${'0'.repeat(STORAGE_PRICE_PER_BYTE_EXPONENT)}`);
12✔
119
  return storageCost.toString();
12✔
120
}
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

© 2025 Coveralls, Inc