-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/new tokens node #49
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { IExecuteFunctions, NodeApiError } from 'n8n-workflow'; | ||
| import { CrossmintApi } from '../../transport/CrossmintApi'; | ||
| import { API_VERSIONS } from '../../utils/constants'; | ||
| import { validateRequiredField } from '../../utils/validation'; | ||
|
|
||
| export async function getMintStatus( | ||
| context: IExecuteFunctions, | ||
| api: CrossmintApi, | ||
| itemIndex: number, | ||
| ): Promise<any> { | ||
| const collectionId = context.getNodeParameter('statusCollectionId', itemIndex) as string; | ||
| const tokenId = context.getNodeParameter('tokenId', itemIndex) as string; | ||
|
|
||
| validateRequiredField(collectionId, 'Collection ID', context, itemIndex); | ||
| validateRequiredField(tokenId, 'Token ID', context, itemIndex); | ||
|
|
||
| const endpoint = `collections/${encodeURIComponent(collectionId)}/nfts/${encodeURIComponent(tokenId)}`; | ||
|
|
||
| try { | ||
| return await api.get(endpoint, API_VERSIONS.COLLECTIONS); | ||
| } catch (error: any) { | ||
| // Pass through the original Crossmint API error exactly as received | ||
| throw new NodeApiError(context.getNode(), error); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { IExecuteFunctions, NodeApiError } from 'n8n-workflow'; | ||
| import { CrossmintApi } from '../../transport/CrossmintApi'; | ||
| import { API_VERSIONS } from '../../utils/constants'; | ||
| import { buildTokenWalletIdentifier } from '../../utils/locators'; | ||
|
|
||
| export async function getTokensFromWallet( | ||
| context: IExecuteFunctions, | ||
| api: CrossmintApi, | ||
| itemIndex: number, | ||
| ): Promise<any> { | ||
| const walletIdentifierData = context.getNodeParameter('walletIdentifier', itemIndex) as any; | ||
| const chain = context.getNodeParameter('walletChain', itemIndex) as string; | ||
|
|
||
| // Build the wallet identifier using the utility function | ||
| const identifier = buildTokenWalletIdentifier(walletIdentifierData, chain, context, itemIndex); | ||
|
|
||
| // Get options | ||
| const options = context.getNodeParameter('walletTokensOptions', itemIndex, {}) as any; | ||
|
|
||
| // Build query parameters | ||
| const queryParams: string[] = []; | ||
|
|
||
| // Add pagination | ||
| if (options.page) { | ||
| queryParams.push(`page=${options.page}`); | ||
| } | ||
| if (options.perPage) { | ||
| queryParams.push(`perPage=${options.perPage}`); | ||
| } | ||
|
|
||
| // Add filters | ||
| if (options.contractAddress) { | ||
| const addresses = options.contractAddress.split(',').map((addr: string) => addr.trim()); | ||
| addresses.forEach((addr: string) => { | ||
| queryParams.push(`contractAddress=${encodeURIComponent(addr)}`); | ||
| }); | ||
| } | ||
| if (options.tokenId) { | ||
| queryParams.push(`tokenId=${encodeURIComponent(options.tokenId)}`); | ||
| } | ||
|
|
||
| const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; | ||
| const endpoint = `wallets/${encodeURIComponent(identifier)}/nfts${queryString}`; | ||
|
|
||
| try { | ||
| return await api.get(endpoint, API_VERSIONS.COLLECTIONS); | ||
| } catch (error: any) { | ||
| // Pass through the original Crossmint API error exactly as received | ||
| throw new NodeApiError(context.getNode(), error); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { IExecuteFunctions, NodeOperationError, NodeApiError, IDataObject } from 'n8n-workflow'; | ||
| import { CrossmintApi } from '../../transport/CrossmintApi'; | ||
| import { API_VERSIONS } from '../../utils/constants'; | ||
| import { validateRequiredField } from '../../utils/validation'; | ||
| import { buildTokenRecipient } from '../../utils/locators'; | ||
| import { TokenMintRequest } from '../../transport/types'; | ||
|
|
||
| export async function mintToken( | ||
| context: IExecuteFunctions, | ||
| api: CrossmintApi, | ||
| itemIndex: number, | ||
| ): Promise<any> { | ||
| const collectionId = context.getNodeParameter('collectionId', itemIndex) as string; | ||
| const recipientData = context.getNodeParameter('tokenRecipient', itemIndex) as any; | ||
| const metadataType = context.getNodeParameter('metadataType', itemIndex) as string; | ||
|
|
||
| validateRequiredField(collectionId, 'Collection ID', context, itemIndex); | ||
|
|
||
| const chain = context.getNodeParameter('tokenChain', itemIndex) as string; | ||
| const recipient = buildTokenRecipient(recipientData, chain, context, itemIndex); | ||
|
|
||
| const requestBody: TokenMintRequest = { | ||
| recipient: recipient, | ||
| sendNotification: context.getNodeParameter('sendNotification', itemIndex) as boolean, | ||
| locale: context.getNodeParameter('tokenLocale', itemIndex) as string, | ||
| reuploadLinkedFiles: context.getNodeParameter('reuploadLinkedFiles', itemIndex) as boolean, | ||
| compressed: context.getNodeParameter('compressed', itemIndex) as boolean, | ||
| }; | ||
|
|
||
| if (metadataType === 'template') { | ||
| const templateId = context.getNodeParameter('templateId', itemIndex) as string; | ||
| validateRequiredField(templateId, 'Template ID', context, itemIndex); | ||
| requestBody.templateId = templateId; | ||
| } else if (metadataType === 'url') { | ||
| const metadataUrl = context.getNodeParameter('metadataUrl', itemIndex) as string; | ||
| validateRequiredField(metadataUrl, 'Metadata URL', context, itemIndex); | ||
| requestBody.metadata = metadataUrl; | ||
| } else { | ||
| const name = context.getNodeParameter('tokenName', itemIndex) as string; | ||
| const image = context.getNodeParameter('tokenImage', itemIndex) as string; | ||
| const description = context.getNodeParameter('tokenDescription', itemIndex) as string; | ||
|
|
||
| if (!name || !image || !description) { | ||
| throw new NodeOperationError(context.getNode(), 'Name, Image, and Description are required for metadata object mode', { | ||
| itemIndex, | ||
| }); | ||
| } | ||
|
|
||
| const metadata: any = { | ||
| name: name, | ||
| image: image, | ||
| description: description, | ||
| }; | ||
|
|
||
| const animationUrl = context.getNodeParameter('tokenAnimationUrl', itemIndex) as string; | ||
| const symbol = context.getNodeParameter('tokenSymbol', itemIndex) as string; | ||
| const attributesJson = context.getNodeParameter('tokenAttributes', itemIndex) as string; | ||
|
|
||
| if (animationUrl) { | ||
| metadata.animation_url = animationUrl; | ||
| } | ||
|
|
||
| if (symbol) { | ||
| metadata.symbol = symbol; | ||
| } | ||
|
|
||
| if (attributesJson && attributesJson.trim() !== '') { | ||
| try { | ||
| const attributes = JSON.parse(attributesJson); | ||
| if (Array.isArray(attributes)) { | ||
| metadata.attributes = attributes; | ||
| } | ||
|
Comment on lines
+67
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Silent failure when attributes is not an array - consider adding a warning or error message to inform users |
||
| } catch { | ||
| throw new NodeOperationError(context.getNode(), 'Invalid JSON format for attributes', { | ||
| description: 'Please provide a valid JSON array for attributes', | ||
| itemIndex, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| requestBody.metadata = metadata; | ||
| } | ||
|
|
||
| const endpoint = `collections/${encodeURIComponent(collectionId)}/nfts`; | ||
|
|
||
| try { | ||
| return await api.post(endpoint, requestBody as unknown as IDataObject, API_VERSIONS.COLLECTIONS); | ||
| } catch (error: any) { | ||
| // Pass through the original Crossmint API error exactly as received | ||
| throw new NodeApiError(context.getNode(), error); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { IExecuteFunctions, NodeOperationError, NodeApiError, IDataObject } from 'n8n-workflow'; | ||
| import { CrossmintApi } from '../../transport/CrossmintApi'; | ||
| import { API_VERSIONS, PAGINATION } from '../../utils/constants'; | ||
| import { validatePrivateKey, validateRequiredField } from '../../utils/validation'; | ||
| import { signMessage } from '../../utils/blockchain'; | ||
| import { ApprovalRequest, ApiResponse } from '../../transport/types'; | ||
|
|
||
| async function getTransactionStatus( | ||
| api: CrossmintApi, | ||
| walletAddress: string, | ||
| transactionId: string, | ||
| ): Promise<unknown> { | ||
| const endpoint = `wallets/${encodeURIComponent(walletAddress)}/transactions/${encodeURIComponent(transactionId)}`; | ||
| return await api.get(endpoint, API_VERSIONS.WALLETS); | ||
| } | ||
|
|
||
| async function handleWaitForCompletion( | ||
| api: CrossmintApi, | ||
| walletAddress: string, | ||
| transactionId: string, | ||
| initialResponse: unknown, | ||
| simplifiedOutput: Record<string, unknown>, | ||
| context: IExecuteFunctions, | ||
| itemIndex: number, | ||
| ): Promise<IDataObject> { | ||
| let currentStatus = (initialResponse as Record<string, unknown>).status; | ||
| let attempts = 0; | ||
| let finalResponse: IDataObject = { | ||
| 'simplified-output': simplifiedOutput, | ||
| raw: initialResponse | ||
| } as IDataObject; | ||
|
|
||
| while (currentStatus === 'pending' && attempts < PAGINATION.MAX_ATTEMPTS) { | ||
| attempts++; | ||
|
|
||
| try { | ||
| const statusResponse = await getTransactionStatus(api, walletAddress, transactionId) as IDataObject; | ||
| currentStatus = (statusResponse as Record<string, unknown>).status; | ||
|
|
||
| const updatedSimplifiedOutput = { | ||
| ...simplifiedOutput, | ||
| status: currentStatus, | ||
| }; | ||
|
|
||
| if (statusResponse.completedAt) { | ||
| (updatedSimplifiedOutput as Record<string, unknown>).completedAt = statusResponse.completedAt; | ||
| } | ||
| if (statusResponse.error) { | ||
| (updatedSimplifiedOutput as Record<string, unknown>).error = statusResponse.error; | ||
| } | ||
|
|
||
| finalResponse = { | ||
| 'simplified-output': updatedSimplifiedOutput, | ||
| raw: statusResponse | ||
| } as IDataObject; | ||
|
|
||
| } catch (error) { | ||
| // Log the error using n8n's proper error handling | ||
| throw new NodeOperationError(context.getNode(), `Failed to get transaction status during polling: ${error}`, { | ||
| itemIndex, | ||
| description: 'Transaction status polling failed. This may be due to temporary API issues.', | ||
| }); | ||
| } | ||
|
|
||
| if (currentStatus === 'success' || currentStatus === 'failed') { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return finalResponse; | ||
| } | ||
|
|
||
| export async function signTransaction( | ||
| context: IExecuteFunctions, | ||
| api: CrossmintApi, | ||
| itemIndex: number, | ||
| ): Promise<IDataObject> { | ||
| const chain = context.getNodeParameter('signSubmitChain', itemIndex) as string; | ||
| const privateKey = context.getNodeParameter('signSubmitPrivateKey', itemIndex) as string; | ||
| const transactionData = context.getNodeParameter('signSubmitTransactionData', itemIndex) as string; | ||
| const walletAddress = context.getNodeParameter('signSubmitWalletAddress', itemIndex) as string; | ||
| const transactionId = context.getNodeParameter('signSubmitTransactionId', itemIndex) as string; | ||
| const signerAddress = context.getNodeParameter('signSubmitSignerAddress', itemIndex) as string; | ||
| const waitForCompletion = context.getNodeParameter('waitForCompletion', itemIndex) as boolean; | ||
|
|
||
| validatePrivateKey(privateKey, context, itemIndex); | ||
|
|
||
| const signature = await signMessage(transactionData, privateKey, context, itemIndex); | ||
|
|
||
| if (!signature) { | ||
| throw new NodeOperationError(context.getNode(), 'Failed to generate signature', { | ||
| itemIndex, | ||
| }); | ||
| } | ||
|
|
||
| validateRequiredField(walletAddress, 'Wallet Address', context, itemIndex); | ||
| validateRequiredField(transactionId, 'Transaction ID', context, itemIndex); | ||
| validateRequiredField(signerAddress, 'Signer Address', context, itemIndex); | ||
|
|
||
| const requestBody: ApprovalRequest = { | ||
| approvals: [ | ||
| { | ||
| signer: `external-wallet:${signerAddress}`, | ||
| signature: signature, | ||
| } | ||
| ] | ||
| }; | ||
|
|
||
| const endpoint = `wallets/${encodeURIComponent(walletAddress)}/transactions/${encodeURIComponent(transactionId)}/approvals`; | ||
|
|
||
| let rawResponse: ApiResponse; | ||
| try { | ||
| rawResponse = await api.post(endpoint, requestBody as unknown as IDataObject, API_VERSIONS.WALLETS); | ||
| } catch (error: unknown) { | ||
| // Pass through the original Crossmint API error exactly as received | ||
| console.log('API Request Failed:'); | ||
| console.log(JSON.stringify({ | ||
| endpoint, | ||
| requestBody, | ||
| apiVersion: API_VERSIONS.WALLETS, | ||
| }, null, 2)); | ||
|
Comment on lines
+116
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: Debug console.log statements in production code - consider using proper logging or removing |
||
| throw new NodeApiError(context.getNode(), error as object & { message?: string }); | ||
| } | ||
|
|
||
| const rawResponseData = rawResponse as IDataObject; | ||
| const simplifiedOutput = { | ||
| chainType: rawResponseData.chainType, | ||
| walletType: rawResponseData.walletType, | ||
| id: rawResponseData.id, | ||
| status: rawResponseData.status, | ||
| createdAt: rawResponseData.createdAt, | ||
| approvals: rawResponseData.approvals || {}, | ||
| signingDetails: { | ||
| signature: signature, | ||
| signedTransaction: signature, | ||
| chainType: 'solana', | ||
| chain: chain, | ||
| transactionData: transactionData, | ||
| }, | ||
| submittedApproval: { | ||
| walletAddress, | ||
| transactionId, | ||
| signerAddress, | ||
| signature, | ||
| }, | ||
| }; | ||
|
|
||
| if (waitForCompletion) { | ||
| return await handleWaitForCompletion(api, walletAddress, transactionId, rawResponse, simplifiedOutput, context, itemIndex); | ||
| } | ||
|
|
||
| return { | ||
| 'simplified-output': simplifiedOutput, | ||
| raw: rawResponse | ||
| } as IDataObject; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider using the
validateRequiredFieldutility function consistently instead of manual validation to match the pattern used elsewhere in the function