/
utils.ts
154 lines (148 loc) · 6.17 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { AccountListType, CollectionListType, CommissionFeeType, ListingFeeType, OffchainDataType } from "./types"
import { formatPermill } from "../helpers/utils"
import { numberToBalance } from "../blockchain"
import { Errors } from "../constants"
import { MarketplaceConfigAction, MarketplaceConfigFeeType } from "./enum"
/**
* @name convertMarketplaceFee
* @summary Checks the type fee and format it accordingly. Numbers are formatted into BN. Percentages are formatted in Permill.
* @param fee The fee to format : It can only be an CommissionFeeType or ListingFeeType.
* @returns The formatted fee.
*/
export const convertMarketplaceFee = async (fee: CommissionFeeType | ListingFeeType) => {
if (typeof fee === "object") {
if (typeof fee.set.flat === "number") {
const flatFee = numberToBalance(fee.set.flat)
fee.set.flat = flatFee
}
if (fee.set.percentage) {
const percentageFee = formatPermill(fee.set.percentage)
fee.set.percentage = percentageFee
}
}
return fee
}
/**
* @name formatMarketplaceFee
* @summary Returns an object representing either the marketplace commission or listing fee in either in Flat or Percentage format.
*
* @param action - The type of Action. Can be either "Noop" (No Operation: to keep it as it is), "Remove" or "set".
* @param feeType - The type of fee. Can be either "percentage" or "flat",
* @param value - The value of the fee. If type is 'Percentage' value refers to a decimal number in range [0, 100]. If type is 'Flat' value refers to a balance amount in a number. Default is 0.
*
* @returns An object representing either the marketplace commission or listing fee.
*/
export const formatMarketplaceFee = (
action: "Noop" | "Remove" | "set",
feeType?: "percentage" | "flat",
value?: number,
): CommissionFeeType => {
if (action !== "Noop" && action !== "Remove" && action !== "set")
throw new Error("INCORRECT_ACTION: action has to be either 'Noop', 'Remove', 'set'.")
if (feeType && feeType !== "percentage" && feeType !== "flat")
throw new Error("INCORRECT_FEE_TYPE: feeType has to be either 'percentage' or 'flat'.")
switch (action) {
case "Noop":
return MarketplaceConfigAction.Noop
case "Remove":
return MarketplaceConfigAction.Remove
case "set": {
if (value === undefined || feeType === undefined) throw new Error(`${Errors.VALUE_MUST_BE_DEFINED}`)
if (feeType && feeType === "percentage") {
return {
[MarketplaceConfigAction.Set]: { [MarketplaceConfigFeeType.Percentage]: formatPermill(value) },
}
} else {
return {
[MarketplaceConfigAction.Set]: { [MarketplaceConfigFeeType.Flat]: numberToBalance(value) },
}
}
}
default:
return MarketplaceConfigAction.Noop
}
}
/**
* @name formatMarketplaceAccountList
* @summary Returns an object representing a list of accounts : if the marketplace kind is private, it allows these accounts to sell NFT. If the marketplace kind is public, it bans these accounts from selling NFT.
*
* @param action - The type of Action. Can be either "Noop" (No Operation: to keep it as it is), "Remove" or "set".
* @param value - An array of addresses (string) to add to the list.
*
* @returns An object representing either the whitelisted or banned accounts.
*/
export const formatMarketplaceAccountList = (action: "Noop" | "Remove" | "set", value?: string[]): AccountListType => {
if (action !== "Noop" && action !== "Remove" && action !== "set")
throw new Error("INCORRECT_ACTION: action has to be either 'Noop', 'Remove', 'set'.")
switch (action) {
case "Noop":
return MarketplaceConfigAction.Noop
case "Remove":
return MarketplaceConfigAction.Remove
case "set": {
if (value === undefined) throw new Error(`${Errors.VALUE_MUST_BE_DEFINED}`)
return {
[MarketplaceConfigAction.Set]: value,
}
}
default:
return MarketplaceConfigAction.Noop
}
}
/**
* @name formatMarketplaceOffchainData
* @summary Returns the off-chain related marketplace metadata. Can be an IPFS Hash, an URL or plain text.
*
* @param action - The type of Action. Can be either "Noop" (No Operation: to keep it as it is), "Remove" or "set".
* @param value - The marketplkace offchain metadata : a string
*
* @returns An object representing either the marketplace offchain metadata.
*/
export const formatMarketplaceOffchainData = (action: "Noop" | "Remove" | "set", value?: string): OffchainDataType => {
if (action !== "Noop" && action !== "Remove" && action !== "set")
throw new Error("INCORRECT_ACTION: action has to be either 'Noop', 'Remove', 'set'.")
switch (action) {
case "Noop":
return MarketplaceConfigAction.Noop
case "Remove":
return MarketplaceConfigAction.Remove
case "set": {
if (value === undefined) throw new Error(`${Errors.VALUE_MUST_BE_DEFINED}`)
return {
[MarketplaceConfigAction.Set]: value,
}
}
default:
return MarketplaceConfigAction.Noop
}
}
/**
* @name formatMarketplaceCollectionList
* @summary Returns an object representing a list of collection of NFT : if the marketplace kind is private, it allows these collection to be listed. If the marketplace kind is public, it bans these collection of NFT from listing.
*
* @param action - The type of Action. Can be either "Noop" (No Operation: to keep it as it is), "Remove" or "set".
* @param value - An array of Collection id (number) to add to the list.
*
* @returns An object representing either the whitelisted or banned collection Id.
*/
export const formatMarketplaceCollectionList = (
action: "Noop" | "Remove" | "set",
value?: number[],
): CollectionListType => {
if (action !== "Noop" && action !== "Remove" && action !== "set")
throw new Error("INCORRECT_ACTION: action has to be either 'Noop', 'Remove', 'set'.")
switch (action) {
case "Noop":
return MarketplaceConfigAction.Noop
case "Remove":
return MarketplaceConfigAction.Remove
case "set": {
if (value === undefined) throw new Error(`${Errors.VALUE_MUST_BE_DEFINED}`)
return {
[MarketplaceConfigAction.Set]: value,
}
}
default:
return MarketplaceConfigAction.Noop
}
}