Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: execMintStatemine and execMintStatemine #6928

Merged
merged 4 commits into from
Aug 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 3 additions & 37 deletions composables/transaction/mintToken/transactionMintBasilisk.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import type {
ActionMintToken,
MintTokenParams,
MintedCollection,
TokenToMint,
} from '../types'
import type { ActionMintToken, MintedCollection, TokenToMint } from '../types'
import { isRoyaltyValid } from '@/utils/royalty'
import { constructMeta } from './constructMeta'
import { BaseMintedCollection } from '@/components/base/types'
import { transactionFactory } from './utils'

const prepareTokenMintArgs = async (
token: TokenToMint,
Expand Down Expand Up @@ -60,34 +56,4 @@ const getArgs = async (item: ActionMintToken, api) => {
return [arg]
}

export async function execMintBasilisk({
item,
api,
executeTransaction,
isLoading,
status,
}: MintTokenParams) {
const { $i18n } = useNuxtApp()
isLoading.value = true
status.value = 'loader.ipfs'
const args = await getArgs(item, api)

const nameInNotifications = Array.isArray(item.token)
? item.token.map((t) => t.name).join(', ')
: item.token.name

executeTransaction({
cb: api.tx.utility.batchAll,
arg: args,
successMessage:
item.successMessage ||
((blockNumber) =>
$i18n.t('mint.mintNFTSuccess', {
name: nameInNotifications,
block: blockNumber,
})),
errorMessage:
item.errorMessage ||
$i18n.t('mint.errorCreateNewNft', { name: nameInNotifications }),
})
}
export const execMintStatemine = transactionFactory(getArgs)
6 changes: 2 additions & 4 deletions composables/transaction/mintToken/transactionMintRmrk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '../types'
import { constructMeta } from './constructMeta'
import { isRoyaltyValid } from '@/utils/royalty'
import { calculateFees, copiesToMint } from './utils'
import { calculateFees, copiesToMint, getNameInNotifications } from './utils'

const getOnChainProperties = ({ tags, royalty, hasRoyalty }: TokenToMint) => {
let onChainProperties = convertAttributesToProperties(tags)
Expand Down Expand Up @@ -151,9 +151,7 @@ export async function execMintRmrk({
status.value = 'loader.ipfs'
const { args, createdNFTs } = await getArgs(item, api)

const nameInNotifications = Array.isArray(item.token)
? item.token.map((t) => t.name).join(', ')
: item.token.name
const nameInNotifications = getNameInNotifications(item)

const isSingle = args.length === 1
const cb = isSingle ? api.tx.system.remark : api.tx.utility.batchAll
Expand Down
41 changes: 3 additions & 38 deletions composables/transaction/mintToken/transactionMintStatemine.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import { BaseMintedCollection } from '@/components/base/types'
import type {
ActionMintToken,
MintTokenParams,
MintedCollection,
} from '../types'
import type { ActionMintToken, MintedCollection } from '../types'
import { TokenToMint } from '../types'
import { constructMeta } from './constructMeta'
import { calculateFees, copiesToMint } from './utils'
import { calculateFees, copiesToMint, transactionFactory } from './utils'
import { canSupport } from '@/utils/support'

type id = { id: number }
Expand Down Expand Up @@ -117,35 +113,4 @@ const getArgs = async (item: ActionMintToken, api) => {
return [[...arg.flat(), ...supportInteraction]]
}

export async function execMintStatemine({
item,
api,
executeTransaction,
isLoading,
status,
}: MintTokenParams) {
const { $i18n } = useNuxtApp()

isLoading.value = true
status.value = 'loader.ipfs'
const args = await getArgs(item, api)

const nameInNotifications = Array.isArray(item.token)
? item.token.map((t) => t.name).join(', ')
: item.token.name

executeTransaction({
cb: api.tx.utility.batchAll,
arg: args,
successMessage:
item.successMessage ||
((blockNumber) =>
$i18n.t('mint.mintNFTSuccess', {
name: nameInNotifications,
block: blockNumber,
})),
errorMessage:
item.errorMessage ||
$i18n.t('mint.errorCreateNewNft', { name: nameInNotifications }),
})
}
export const execMintStatemine = transactionFactory(getArgs)
42 changes: 41 additions & 1 deletion composables/transaction/mintToken/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { usePreferencesStore } from '@/stores/preferences'
import { Max, MintedCollection, TokenToMint } from '../types'
import {
ActionMintToken,
Max,
MintTokenParams,
MintedCollection,
TokenToMint,
} from '../types'

export const copiesToMint = <T extends TokenToMint>(token: T): number => {
const { copies, selectedCollection } = token
Expand All @@ -22,3 +28,37 @@ export const calculateFees = () => {

return { enabledFees, feeMultiplier }
}

export const getNameInNotifications = (item: ActionMintToken) => {
return Array.isArray(item.token)
? item.token.map((t) => t.name).join(', ')
: item.token.name
}

export const transactionFactory = (getArgs) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function transactionFactory has 29 lines of code (exceeds 25 allowed). Consider refactoring.

return async (mintTokenParams: MintTokenParams) => {
const { item, api, executeTransaction, isLoading, status } = mintTokenParams
const { $i18n } = useNuxtApp()

isLoading.value = true
status.value = 'loader.ipfs'
const args = await getArgs(item, api)

const nameInNotifications = getNameInNotifications(item)

executeTransaction({
cb: api.tx.utility.batchAll,
arg: args,
successMessage:
item.successMessage ||
((blockNumber) =>
$i18n.t('mint.mintNFTSuccess', {
name: nameInNotifications,
block: blockNumber,
})),
errorMessage:
item.errorMessage ||
$i18n.t('mint.errorCreateNewNft', { name: nameInNotifications }),
})
}
}