diff --git a/src/hooks/usePricing/usePricing.ts b/src/hooks/usePricing/usePricing.ts index 3afadd6..b6181fc 100644 --- a/src/hooks/usePricing/usePricing.ts +++ b/src/hooks/usePricing/usePricing.ts @@ -22,7 +22,7 @@ interface UsePricing { ) => Promise buyDT: (dtAmount: number | string) => Promise sellDT: (dtAmount: number | string) => Promise - mint: (tokensToMint: string) => Promise + mint: (tokensToMint: string) => Promise pricingStep?: number pricingStepText?: string pricingError?: string @@ -83,10 +83,23 @@ function usePricing(ddo: DDO): UsePricing { setPricingStepText(messages[index]) } - async function mint(tokensToMint: string): Promise { + async function mint( + tokensToMint: string + ): Promise { Logger.log('mint function', dataToken, accountId) - const tx = await ocean.datatokens.mint(dataToken, accountId, tokensToMint) - return tx + const balance = new Decimal( + await ocean.datatokens.balance(dataToken, accountId) + ) + const tokens = new Decimal(tokensToMint) + if (tokens.greaterThan(balance)) { + const mintAmount = tokens.minus(balance) + const tx = await ocean.datatokens.mint( + dataToken, + accountId, + mintAmount.toString() + ) + return tx + } } async function buyDT(