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

feat(sound): add getExternalUrl function #480

Merged
merged 11 commits into from
Jul 16, 2024
5 changes: 5 additions & 0 deletions .changeset/eleven-maps-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rabbitholegg/questdk-plugin-soundxyz": minor
---

add getExternalUrl function to sound plugin
34 changes: 33 additions & 1 deletion packages/soundxyz/src/Soundxyz.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getDynamicNameParams,
getExternalUrl,
getProjectFees,
mint,
simulateMint,
Expand All @@ -24,7 +25,7 @@ import {
type MintIntentParams,
getExitAddresses,
} from '@rabbitholegg/questdk-plugin-utils'
import { type Address, parseEther } from 'viem'
import { type Address, parseEther, getAddress } from 'viem'
import { describe, expect, test, vi } from 'vitest'

describe('Given the soundxyz plugin', () => {
Expand Down Expand Up @@ -208,3 +209,34 @@ describe('simulateMint function', () => {
expect(request.value).toBe(value)
})
})

describe('getExternalUrl', () => {
test('should return the correct link with referral', async () => {
const contractAddress: Address =
'0xE39Df1AD806e84de47D0F6ddf56a0007C678597c'
const mintParams = {
contractAddress,
chainId: Chains.BASE,
referral: getAddress('0x1234567890123456789012345678901234567890'),
}

const link = await getExternalUrl(mintParams)
expect(link).equals(
'https://www.sound.xyz/33below/midnight-diner-ii?referral=0x1234567890123456789012345678901234567890',
)
})

test('should return the correct link with our own referral if no referral is provided', async () => {
const contractAddress: Address =
'0xE39Df1AD806e84de47D0F6ddf56a0007C678597c'
const mintParams = {
contractAddress,
chainId: Chains.BASE,
}

const link = await getExternalUrl(mintParams)
expect(link).equals(
`https://www.sound.xyz/33below/midnight-diner-ii?referral=${ZORA_DEPLOYER_ADDRESS}`,
)
})
})
37 changes: 37 additions & 0 deletions packages/soundxyz/src/Soundxyz.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import axios from 'axios'
import {
CONTRACT_URI_ABI,
MINT_INFO_LIST_ABI,
NEXT_SCHEDULE_NUM_ABI,
SUPERMINTER,
Expand Down Expand Up @@ -287,3 +289,38 @@ export const getDynamicNameParams = async (
}
return values
}

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

try {
const client = createPublicClient({
chain: chainIdToViemChain(chainId),
transport: http(),
}) as PublicClient

const contractUri = (await client.readContract({
address: contractAddress,
abi: CONTRACT_URI_ABI,
functionName: 'contractURI',
})) as string

const cid = contractUri.split('/').slice(2).join('/')

const { data } = await axios.get(`https://arweave.net/${cid}`)
const { external_link } = data

return `${external_link}?referral=${referral ?? ZORA_DEPLOYER_ADDRESS}`
} catch (error) {
console.error('an error occurred fetching the contract uri')
if (error instanceof Error) {
console.error(error.message)
} else {
console.error(error)
}
// fallback to default sound.xyz url
return 'https://sound.xyz'
}
}
10 changes: 10 additions & 0 deletions packages/soundxyz/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,16 @@ export const MINT_INFO_LIST_ABI = [
},
]

export const CONTRACT_URI_ABI = [
{
inputs: [],
name: 'contractURI',
outputs: [{ internalType: 'string', name: '', type: 'string' }],
stateMutability: 'view',
type: 'function',
},
]

// for referrals
export const ZORA_DEPLOYER_ADDRESS =
'0xe3bBA2A4F8E0F5C32EF5097F988a4d88075C8B48'
5 changes: 4 additions & 1 deletion packages/soundxyz/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {

import {
getDynamicNameParams,
getExternalUrl,
getFees,
getMintIntent,
getProjectFees,
getSupportedChainIds,
getSupportedTokenAddresses,
mint,
simulateMint,
} from './Soundxyz.js'
} from './Soundxyz'

export const Soundxyz: IActionPlugin = {
pluginId: 'soundxyz',
Expand All @@ -24,6 +25,8 @@ export const Soundxyz: IActionPlugin = {
bridge: async () => new PluginActionNotImplementedError(),
swap: async () => new PluginActionNotImplementedError(),
getDynamicNameParams,
getExternalUrl: async (params: ActionParams) =>
getExternalUrl(params as unknown as MintActionParams),
getMintIntent,
getProjectFees: async (params: ActionParams) =>
getProjectFees(params as unknown as MintActionParams),
Expand Down
Loading