Skip to content

Commit

Permalink
mint task wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Day committed Jan 20, 2025
1 parent 995df58 commit 69c668c
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/demo-canvas/contracts/src/Canvas.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ contract Canvas is CanvasGenerated {
uint256 tokenId = _nextTokenId;
_nextTokenId++;
_safeMint(to, tokenId);
//TODO: set canvas name in metadata
_metadataStorage[tokenId] = new uint256[](1);
return tokenId;
}
Expand Down
11 changes: 11 additions & 0 deletions examples/demo-canvas/tasks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Task } from '@patchworkdev/pdk/utils';
import { mintCanvasTask } from './mint-first-canvas';

export const tasks: Task[] = [
{
name: 'Mint Canvas',
description: 'Checks if a canvas has been minted and offers to mint the first one',
enabled: true,
execute: mintCanvasTask,
},
];
148 changes: 148 additions & 0 deletions examples/demo-canvas/tasks/mint-first-canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { getChainForNetwork, TaskExecuteParams } from '@patchworkdev/pdk/utils';
import { stdin as input, stdout as output } from 'node:process';
import * as readline from 'node:readline/promises';
import { createPublicClient, createWalletClient, http } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';

// Enum for MintMode matching the contract
enum MintMode {
OWNER = 0,
OPEN = 1
}

// ABI for the Canvas contract functions we need
const canvasAbi = [
{
type: 'function',
name: 'ownerOf',
inputs: [{ type: 'uint256', name: 'tokenId' }],
outputs: [{ type: 'address' }],
stateMutability: 'view',
},
{
type: 'function',
name: 'setMintMode',
inputs: [{ type: 'uint8', name: 'mode' }],
outputs: [],
stateMutability: 'nonpayable',
},
{
type: 'function',
name: 'mint',
inputs: [
{ type: 'address', name: 'to' },
{ type: 'bytes', name: 'data' }
],
outputs: [{ type: 'uint256' }],
stateMutability: 'payable',
}
] as const;

async function askQuestion(question: string): Promise<string> {
const rl = readline.createInterface({ input, output });
try {
const answer = await rl.question(question);
return answer;
} finally {
rl.close();
}
}

export async function mintCanvasTask({ deployConfig, deployedContracts }: TaskExecuteParams): Promise<void> {
const publicClient = createPublicClient({
transport: http(deployConfig.rpcUrl),
});

const account = privateKeyToAccount(deployConfig.privateKey as `0x${string}`);

const walletClient = createWalletClient({
account,
chain: getChainForNetwork(deployConfig.network),
transport: http(deployConfig.rpcUrl),
});

const canvasContractAddress = deployedContracts.Canvas.deployedAddress as `0x${string}`;

console.log('Canvas Contract:', canvasContractAddress);

// Check if any canvas has been minted by checking ownership of token ID 0
try {
try {
const owner = await publicClient.readContract({
address: canvasContractAddress,
abi: canvasAbi,
functionName: 'ownerOf',
args: [0n],
});

console.log('Canvas has already been minted. Owner of first canvas:', owner);
return;
} catch (error) {
// If ownerOf reverts, it means token 0 doesn't exist, so no canvas has been minted
console.log('No canvas has been minted yet.');
}

// Ask if user wants to mint their first canvas
const shouldMint = await askQuestion('\nNo canvas has been minted yet. Would you like to mint the first canvas? (y/n): ');

if (shouldMint.toLowerCase() === 'y') {
try {
console.log('Minting canvas...');

const { request } = await publicClient.simulateContract({
address: canvasContractAddress,
abi: canvasAbi,
functionName: 'mint',
args: [account.address, '0x'] as const,
account,
});

const hash = await walletClient.writeContract(request);

console.log('Transaction sent:', hash);

const receipt = await publicClient.waitForTransactionReceipt({ hash });

console.log('Canvas minted! Block:', receipt.blockNumber);

// Ask if they want to enable open minting
const shouldOpenMinting = await askQuestion('\nWould you like to enable open minting for everyone? (y/n): ');

if (shouldOpenMinting.toLowerCase() === 'y') {
try {
console.log('Setting mint mode to OPEN...');

const { request } = await publicClient.simulateContract({
address: canvasContractAddress,
abi: canvasAbi,
functionName: 'setMintMode',
args: [MintMode.OPEN],
account,
});

const mintModeHash = await walletClient.writeContract(request);

console.log('Transaction sent:', mintModeHash);

const mintModeReceipt = await publicClient.waitForTransactionReceipt({ hash: mintModeHash });

console.log('Mint mode set to OPEN! Block:', mintModeReceipt.blockNumber);
} catch (error) {
console.error('Error setting mint mode:', error);
throw error;
}
} else {
console.log('Keeping mint mode as OWNER only.');
}
} catch (error) {
console.error('Error minting canvas:', error);
throw error;
}
} else {
console.log('Skipping canvas minting.');
}
} catch (error) {
console.error('Error checking canvas state:', error);
throw error;
}
}

0 comments on commit 69c668c

Please sign in to comment.