Skip to content

Commit

Permalink
fix: use real mint fees
Browse files Browse the repository at this point in the history
  • Loading branch information
hassnian committed Nov 9, 2023
1 parent 2e34ac9 commit 7ffa2e1
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 66 deletions.
19 changes: 12 additions & 7 deletions components/create/Confirm/MintConfirmModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@
<div class="py-5">
<div class="is-flex is-justify-content-space-between px-6">
<AutoTeleportActionButton
:amount="totalFee + networkFee"
:amount="autoTeleportParams.amount"
:actions="autoTeleportActions"
:label="btnLabel"
:disabled="disabled"
:fees="{
actions: networkFee,
actions: autoTeleportParams.fees,
actionAutoFees: false,
}"
auto-close-modal
Expand All @@ -70,12 +70,12 @@ import { CreateComponent } from '@/composables/useCreate'
import { useFiatStore } from '@/stores/fiat'
import { usePreferencesStore } from '@/stores/preferences'
import { availablePrefixes } from '@/utils/chain'
import { getTransitionFee } from '@/utils/transactionExecutor'
import { calculateBalanceUsdValue } from '@/utils/format/balance'
import { BASE_FEE } from '@/utils/support'
import ConfirmMintItem from './ConfirmMintItem.vue'
import PriceItem from './PriceItem.vue'
import type { AutoTeleportAction } from '@/composables/autoTeleport/types'
import useAutoTeleportActions from '@/composables/autoTeleport/useAutoTeleportActions'
export type NftInformation = {
file: Blob | null
Expand Down Expand Up @@ -115,6 +115,7 @@ const { urlPrefix } = usePrefix()
const { $i18n } = useNuxtApp()
const fiatStore = useFiatStore()
const preferencesStore = usePreferencesStore()
const { getActionFees } = useAutoTeleportActions()
const { isBasilisk } = useIsChain(urlPrefix)
const { metadataDeposit, collectionDeposit, existentialDeposit, itemDeposit } =
Expand Down Expand Up @@ -187,6 +188,13 @@ const extendedInformation = computed(() => ({
blockchain: blockchain.value,
}))
const autoTeleportParams = computed(() => {
return {
amount: totalFee.value,
fees: networkFee.value,
}
})
const onClose = () => {
emit('update:modelValue', false)
}
Expand All @@ -199,10 +207,7 @@ watchEffect(async () => {
networkFee.value = 0
if (!isBasilisk.value) {
const fee = await getTransitionFee(accountId.value, [''], decimals.value)
networkFee.value = props.nftInformation.listForSale
? Number(fee) * 2
: Number(fee)
networkFee.value = await getActionFees(props.autoTeleportActions)
}
})
</script>
Expand Down
37 changes: 37 additions & 0 deletions composables/autoTeleport/useAutoTeleportActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getActionTransactionFee } from '@/utils/transactionExecutor'
import { AutoTeleportAction } from './types'
import { type Chain } from '@/utils/teleport'
import { sum } from 'lodash'

export default function () {
const { apiInstance, apiInstanceByPrefix } = useApi()
const { chain: currentChain, getAddressByChain } = useTeleport()

const getActionFees = async (
actions: AutoTeleportAction[],
): Promise<number> => {
try {
const feesPromisses = actions.map(async ({ action, prefix }) => {
let api = await apiInstance.value
if (prefix) {
api = await apiInstanceByPrefix(prefix)
}
const address = getAddressByChain(currentChain.value as Chain)
return getActionTransactionFee({
api,
action: action,
address,
})
})
const fees = await Promise.all(feesPromisses)
return sum(fees.map(Number))
} catch (error) {
console.error(`[AUTOTELEPORT]: Failed getting action fee ${error}`)
return 0
}
}

return {
getActionFees,
}
}
31 changes: 5 additions & 26 deletions composables/autoTeleport/useAutoTeleportTransitionDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
} from '@/utils/teleport'
import { chainPropListOf } from '@/utils/config/chain.config'
import { getMaxKeyByValue } from '@/utils/math'
import { getActionTransactionFee } from '@/utils/transactionExecutor'
import { sum } from 'lodash'
import type { AutoTeleportAction, AutoTeleportFeeParams } from './types'
import useAutoTeleportActions from './useAutoTeleportActions'

const BUFFER_FEE_PERCENT = 0.2
const BUFFER_AMOUNT_PERCENT = 0.02
Expand All @@ -24,12 +23,12 @@ export default function (
fetchChainsBalances,
getAddressByChain,
} = useTeleport()
const { apiInstance, apiInstanceByPrefix } = useApi()
const { balance } = useBalance()
const { getActionFees } = useAutoTeleportActions()

const hasBalances = ref(false)
const teleportTxFee = ref(0)
const actionTxFees = ref<number[]>([])
const actionTxFees = ref<number>(0)

const chainSymbol = computed(
() => currentChain.value && getChainCurrency(currentChain.value),
Expand All @@ -40,8 +39,7 @@ export default function (
)

const totalFees = computed(
() =>
teleportTxFee.value + sum(actionTxFees.value) + Math.ceil(fees.actions),
() => teleportTxFee.value + actionTxFees.value + Math.ceil(fees.actions),
)

const neededAmountWithFees = computed(
Expand Down Expand Up @@ -156,26 +154,7 @@ export default function (
actions,
async () => {
if (fees.actionAutoFees) {
try {
const feesPromisses = actions.value.map(
async ({ action, prefix }) => {
let api = await apiInstance.value
if (prefix) {
api = await apiInstanceByPrefix(prefix)
}
const address = getAddressByChain(currentChain.value as Chain)
return getActionTransactionFee({
api,
action: action,
address,
})
},
)
const fees = await Promise.all(feesPromisses)
actionTxFees.value = fees.map(Number)
} catch (error) {
console.error(`[AUTOTELEPORT]: Failed getting action fee ${error}`)
}
actionTxFees.value = await getActionFees(actions.value)
}
},
{ immediate: true },
Expand Down
5 changes: 5 additions & 0 deletions composables/transaction/mintCollection/constructMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const createAttributes = (item: ActionMintCollection) => {
return []
}

export const constructSimulatableMeta = (
item: ActionMintCollection,
simulate?: boolean,
) => (simulate ? '' : constructMeta(item))

export async function constructMeta(item: ActionMintCollection) {
const { file, name, description } = item.collection

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { MintCollectionParams } from '../types'
import { constructMeta } from './constructMeta'
import { constructSimulatableMeta } from './constructMeta'
import { useNewCollectionId } from './useNewCollectionId'
import { createArgs } from './utils'

Expand All @@ -9,13 +9,14 @@ export async function execMintCollectionBasilisk({
executeTransaction,
isLoading,
status,
simulate,
}: MintCollectionParams) {
const { $i18n } = useNuxtApp()

isLoading.value = true
status.value = 'loader.ipfs'

const metadata = await constructMeta(item)
const metadata = await constructSimulatableMeta(item, simulate)

const cb = api.tx.nft.createCollection

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CollectionToMintKusama, MintCollectionParams } from '../types'
import { constructMeta } from './constructMeta'
import { constructSimulatableMeta } from './constructMeta'
import {
Interaction,
createCollection,
Expand All @@ -18,6 +18,7 @@ export async function execMintCollectionRmrk({
executeTransaction,
isLoading,
status,
simulate,
}: MintCollectionParams) {
const { isV2 } = useRmrkVersion()
const { accountId } = useAuth()
Expand All @@ -26,7 +27,7 @@ export async function execMintCollectionRmrk({
isLoading.value = true
status.value = 'loader.ipfs'

const metadata = await constructMeta(item)
const metadata = await constructSimulatableMeta(item, simulate)
const { symbol, name, nftCount } = item.collection as CollectionToMintKusama

const mint = createCollection(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CollectionToMintStatmine, MintCollectionParams } from '../types'
import { constructMeta } from './constructMeta'
import { constructSimulatableMeta } from './constructMeta'
import { useStatemineNewCollectionId } from './useNewCollectionId'
import { createArgsForNftPallet } from './utils'

Expand All @@ -9,13 +9,14 @@ export async function execMintCollectionStatemine({
executeTransaction,
isLoading,
status,
simulate,
}: MintCollectionParams) {
const { $i18n } = useNuxtApp()

isLoading.value = true
status.value = 'loader.ipfs'

const metadata = await constructMeta(item)
const metadata = await constructSimulatableMeta(item, simulate)
const { nftCount } = item.collection as CollectionToMintStatmine
const { accountId } = useAuth()

Expand Down
20 changes: 16 additions & 4 deletions composables/transaction/mintToken/constructMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,24 @@ import { uploadDirectWhenMultiple } from '@/utils/directUpload'
import { preheatFileFromIPFS } from '@/utils/ipfs'
import { TokenToMint } from '../types'

export async function constructMeta(
tokenToMint: TokenToMint,
type ConstructMetaParams = {
tokenToMint: TokenToMint
options?: {
enableCarbonOffset?: boolean
},
): Promise<string> {
}
}

export async function constructSimulatableMeta(
parms: ConstructMetaParams,
simulate?: boolean,
) {
return simulate ? '' : constructMeta(parms)
}

export async function constructMeta({
tokenToMint,
options,
}: ConstructMetaParams): Promise<string> {
const preferencesStore = usePreferencesStore()
const { $consola } = useNuxtApp()
const { file, name, description, secondFile, tags, nsfw } = tokenToMint
Expand Down
8 changes: 6 additions & 2 deletions composables/transaction/mintToken/transactionMintBasilisk.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { ActionMintToken, MintedCollection, TokenToMint } from '../types'
import { isRoyaltyValid } from '@/utils/royalty'
import { constructMeta } from './constructMeta'
import { constructSimulatableMeta } from './constructMeta'
import { BaseMintedCollection } from '@/components/base/types'
import { expandCopies, transactionFactory } from './utils'

const prepareTokenMintArgs = async (
token: TokenToMint,
api,
nextId: number,
simulate: boolean,
) => {
const { id: collectionId } = token.selectedCollection as BaseMintedCollection
const { price, royalty, hasRoyalty } = token
const metadata = await constructMeta(token)
const metadata = await constructSimulatableMeta(
{ tokenToMint: token },
simulate,
)

const create = api.tx.nft.mint(collectionId, nextId, metadata)

Expand Down
18 changes: 13 additions & 5 deletions composables/transaction/mintToken/transactionMintRmrk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
MintedCollectionKusama,
TokenToMint,
} from '../types'
import { constructMeta } from './constructMeta'
import { constructSimulatableMeta } from './constructMeta'
import { isRoyaltyValid } from '@/utils/royalty'
import { calculateFees, copiesToMint, getNameInNotifications } from './utils'

Expand Down Expand Up @@ -91,11 +91,18 @@ const createMintInteractionObject = (
const processSingleTokenToMint = async (
token: TokenToMint,
api,
simulate?: boolean,
): Promise<{
arg: Extrinsic[]
createdNFTs: CreatedNFT[] | NewCreatedNFT[]
}> => {
const metadata = await constructMeta(token, { enableCarbonOffset: true })
const metadata = await constructSimulatableMeta(
{
tokenToMint: token,
options: { enableCarbonOffset: true },
},
simulate,
)
const onChainProperties = getOnChainProperties(token)
const mint = createMintObject(token, metadata, getUpdateNameFn(token))
const mintInteraction = createMintInteractionObject(mint, onChainProperties)
Expand All @@ -111,14 +118,14 @@ const processSingleTokenToMint = async (
}
}

const getArgs = async (item: ActionMintToken, api) => {
const getArgs = async (item: ActionMintToken, api, simulate: boolean) => {
const { $consola } = useNuxtApp()
const tokens = Array.isArray(item.token) ? item.token : [item.token]

const argsAndNftsArray = (
await Promise.all(
tokens.map((token) => {
return processSingleTokenToMint(token, api).catch((e) => {
return processSingleTokenToMint(token, api, simulate).catch((e) => {
$consola.error('Error:', e)
})
}),
Expand All @@ -141,11 +148,12 @@ export async function execMintRmrk({
executeTransaction,
isLoading,
status,
simulate,
}: MintTokenParams) {
const { $i18n } = useNuxtApp()
isLoading.value = true
status.value = 'loader.ipfs'
const { args, createdNFTs } = await getArgs(item, api)
const { args, createdNFTs } = await getArgs(item, api, simulate)

const nameInNotifications = getNameInNotifications(item)

Expand Down
Loading

0 comments on commit 7ffa2e1

Please sign in to comment.