Skip to content

Commit

Permalink
Merge pull request #486 from rabbitholegg/matthew/boost-4269-getexter…
Browse files Browse the repository at this point in the history
…nalurl-manifold

feat(manifold): implement `getExternalUrl` function
  • Loading branch information
mmackz authored Jul 16, 2024
2 parents 8e8428a + bd70f64 commit 3f68afb
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-pans-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk-plugin-manifold": minor
---

implement getExternalUrl function
40 changes: 38 additions & 2 deletions packages/manifold/src/Manifold.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getFees, getMintIntent, mint, simulateMint } from './Manifold'
import {
getExternalUrl,
getFees,
getMintIntent,
mint,
simulateMint,
} from './Manifold'
import { ERC721_CONTRACT, ERC1155_CONTRACT } from './constants'
import { failingTestCases, passingTestCases } from './test-transactions'
import { apply } from '@rabbitholegg/questdk'
Expand All @@ -8,7 +14,7 @@ import {
type MintIntentParams,
} from '@rabbitholegg/questdk-plugin-utils'
import axios from 'axios'
import { type Address, parseEther } from 'viem'
import { type Address, parseEther, getAddress } from 'viem'
import { MockedFunction, beforeEach, describe, expect, test, vi } from 'vitest'

vi.mock('axios', () => {
Expand Down Expand Up @@ -218,3 +224,33 @@ describe('simulateMint function', () => {
expect(request.value).toBe(value)
})
})

describe('getExternalUrl function', () => {
beforeEach(() => {
vi.resetAllMocks()
})
test('should return the correct url for a 721 mint', async () => {
const mint = {
chainId: Chains.OPTIMISM,
contractAddress: getAddress('0x6935cd348193bab133f3081f53eb99ee6f0d685b'),
}
;(axios.get as MockedFunction<typeof axios.get>).mockResolvedValue({
status: 200,
data: {
slug: 'girls-man',
},
})

const result = await getExternalUrl(mint)
expect(result).toBe('https://app.manifold.xyz/c/girls-man')
})

test('should return the fallback url for an unknown contract', async () => {
const mint = {
chainId: Chains.OPTIMISM,
contractAddress: getAddress('0x7935cd348193bab133f3081f53eb99ee6f0d685b'),
}
const result = await getExternalUrl(mint)
expect(result).toBe('https://app.manifold.xyz/')
})
})
35 changes: 35 additions & 0 deletions packages/manifold/src/Manifold.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,41 @@ export const getFees = async (
}
}

export const getExternalUrl = async (
mint: MintActionParams,
): Promise<string> => {
const { chainId, contractAddress, tokenId } = mint

const baseUrl = 'https://app.manifold.xyz/'

try {
const instanceId = await getInstanceId(
chainId,
contractAddress,
tokenId ?? 1,
)

const { data } = await axios.get<{ slug?: string }>(
`https://apps.api.manifoldxyz.dev/public/instance/data?id=${instanceId}`,
)
const slug = data.slug

if (!slug) {
throw new Error('Slug not found in response')
}

return `${baseUrl}c/${slug}`
} catch (err) {
if (err instanceof Error) {
console.error(err.message)
} else {
console.error(err)
}
// fallback to default manifold url
return baseUrl
}
}

export const getSupportedTokenAddresses = async (
_chainId: number,
): Promise<Address[]> => {
Expand Down
3 changes: 3 additions & 0 deletions packages/manifold/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
} from '@rabbitholegg/questdk'

import {
getExternalUrl,
getFees,
getMintIntent,
getProjectFees,
Expand All @@ -23,6 +24,8 @@ export const Manifold: IActionPlugin = {
getProjectFees(params as unknown as MintActionParams),
getFees: async (params: ActionParams) =>
getFees(params as unknown as MintActionParams),
getExternalUrl: async (params: ActionParams) =>
getExternalUrl(params as unknown as MintActionParams),
getMintIntent,
simulateMint,
}

0 comments on commit 3f68afb

Please sign in to comment.