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

fix(moshicam): fix amount zero issue #470

Merged
merged 2 commits into from
Aug 8, 2024
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
5 changes: 5 additions & 0 deletions .changeset/rude-days-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk-plugin-moshicam": patch
---

fix amount zero issue
29 changes: 11 additions & 18 deletions packages/moshicam/src/Moshicam.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
mint,
simulateMint,
} from './Moshicam'
import { IMOSHI_PIC1155_ABI } from './abi.ts'
import { DEFAULT_MINT_PRICE, MOSHIMINTER_ADMIN } from './constants.ts'
import { IMOSHI_PIC1155_ABI } from './abi'
import { DEFAULT_MINT_PRICE, MOSHIMINTER_ADMIN } from './constants'
import { failingTestCases, passingTestCases } from './test-transactions'
import { COLLECT_FROM_USER_MOSHICAM } from './test-transactions'
import { GreaterThanOrEqual, apply } from '@rabbitholegg/questdk'
import { apply } from '@rabbitholegg/questdk'
import { Chains, MintIntentParams } from '@rabbitholegg/questdk-plugin-utils'
import { encodeFunctionData, parseEther } from 'viem'
import { describe, expect, test } from 'vitest'
Expand Down Expand Up @@ -62,6 +62,7 @@ describe('Given the getMintIntent function', () => {
chainId: Chains.BASE,
contractAddress: COLLECT_FROM_USER_MOSHICAM.params.contractAddress,
recipient: COLLECT_FROM_USER_MOSHICAM.transaction.from,
amount: 1n,
tokenId: 1,
}
const data = encodeFunctionData({
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('Given the getFee function', () => {
})

test('it should return the expected fee if amount is not provided', async () => {
const mint: MintIntentParams = {
const mint = {
chainId: Chains.BASE,
contractAddress: COLLECT_FROM_USER_MOSHICAM.params.contractAddress,
recipient: COLLECT_FROM_USER_MOSHICAM.transaction.from,
Expand All @@ -127,7 +128,7 @@ describe('Given the getFee function', () => {
})

test('it should return the expected fee if failed to fetch', async () => {
const mint: MintIntentParams = {
const mint = {
chainId: Chains.BLAST, // unsupported chain
contractAddress: COLLECT_FROM_USER_MOSHICAM.params.contractAddress,
recipient: COLLECT_FROM_USER_MOSHICAM.transaction.from,
Expand All @@ -146,7 +147,7 @@ describe('Given the simulateMint function', () => {
const mint: MintIntentParams = {
chainId: Chains.BASE,
contractAddress: COLLECT_FROM_USER_MOSHICAM.params.contractAddress,
amount: 1,
amount: 1n,
recipient: COLLECT_FROM_USER_MOSHICAM.transaction.from,
tokenId: 0,
}
Expand All @@ -173,12 +174,8 @@ describe('Given the simulateMint function', () => {

expect(
async () =>
await simulateMint(
mint,
DEFAULT_MINT_PRICE,
MOSHIMINTER_ADMIN,
).toThrowError(),
)
await simulateMint(mint, DEFAULT_MINT_PRICE, MOSHIMINTER_ADMIN),
).rejects.toThrowError()
})

test('it should throw error if chain is not supported', async () => {
Expand All @@ -191,11 +188,7 @@ describe('Given the simulateMint function', () => {

expect(
async () =>
await simulateMint(
mint,
DEFAULT_MINT_PRICE,
MOSHIMINTER_ADMIN,
).toThrowError(),
)
await simulateMint(mint, DEFAULT_MINT_PRICE, MOSHIMINTER_ADMIN),
).rejects.toThrowError()
})
})
10 changes: 6 additions & 4 deletions packages/moshicam/src/Moshicam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
DEFAULT_ACCOUNT,
type MintIntentParams,
chainIdToViemChain,
formatAmount,
getMintAmount,
} from '@rabbitholegg/questdk-plugin-utils'
import {
type Address,
Expand All @@ -33,7 +35,7 @@ export const mint = async (
$abi: IMOSHI_PIC1155_ABI,
to: recipient,
id: tokenId,
quantity: amount,
quantity: formatAmount(amount),
},
})
}
Expand All @@ -45,7 +47,7 @@ export const getMintIntent = async (

const tokenIdToMint = tokenId ? tokenId : 0

const quantityToMint = typeof amount === 'number' ? BigInt(amount) : BigInt(1)
const quantityToMint = getMintAmount(amount)

const data = encodeFunctionData({
abi: IMOSHI_PIC1155_ABI,
Expand Down Expand Up @@ -75,7 +77,7 @@ export const getFees = async (
chain: chainIdToViemChain(chainId),
transport: http(),
})
const quantityToMint = typeof amount === 'number' ? BigInt(amount) : BigInt(1)
const quantityToMint = getMintAmount(amount)
try {
const data = (await client.readContract({
address: contractAddress,
Expand Down Expand Up @@ -107,7 +109,7 @@ export const simulateMint = async (
throw new Error(`${chainId} is not supported`)
}

const amountToMint = amount ? amount : 1n
const amountToMint = getMintAmount(amount)

const _client = (client ??
createPublicClient({
Expand Down
4 changes: 2 additions & 2 deletions packages/moshicam/src/test-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export const failingTestCases = [
createTestCase(COLLECT_FROM_USER_MOSHICAM, 'when chainId is not correct', {
chainId: 99,
}),
createTestCase(COLLECT_FROM_USER_MOSHICAM, 'when amount is less than one', {
amount: 0,
createTestCase(COLLECT_FROM_USER_MOSHICAM, 'when amount is not sufficient', {
amount: GreaterThanOrEqual(11),
}),
createTestCase(
COLLECT_FROM_USER_MOSHICAM,
Expand Down
Loading