-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4322be7
commit 9440d16
Showing
4 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
examples/contracts/contract-interaction-with-viem/batch-mint-erc721-by-quantity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { getContract, http, createWalletClient, defineChain } from 'viem'; | ||
import { privateKeyToAccount } from 'viem/accounts'; | ||
import { ImmutableERC721Abi } from '@imtbl/contracts'; | ||
|
||
const PRIVATE_KEY = '0xYOUR_PRIVATE_KEY'; // should be read from environment variable, should be of type `0x${string}` | ||
const CONTRACT_ADDRESS = '0xYOUR_CONTRACT_ADDRESS'; // should be of type `0x${string}` | ||
const ACCOUNT_ADDRESS_1: `0x${string}` = '0xACCOUNT_ADDRESS_1'; // should be of type `0x${string}` | ||
const ACCOUNT_ADDRESS_2: `0x${string}` = '0xACCOUNT_ADDRESS_2'; // should be of type `0x${string}` | ||
const MINTS = [ | ||
{ | ||
to: ACCOUNT_ADDRESS_1, | ||
quantity: BigInt(3), | ||
}, | ||
{ | ||
to: ACCOUNT_ADDRESS_2, | ||
quantity: BigInt(3), | ||
}, | ||
]; | ||
|
||
export const batchMintERC721ByQuantity = async ( | ||
privateKey: `0x${string}`, | ||
contractAddress: `0x${string}`, | ||
mints: { | ||
to: `0x${string}`; | ||
quantity: bigint; | ||
}[], | ||
): Promise<string> => { | ||
const immutableTestnet = defineChain({ | ||
id: 13473, | ||
name: 'imtbl-zkevm-testnet', | ||
nativeCurrency: { name: 'IMX', symbol: 'IMX', decimals: 18 }, | ||
rpcUrls: { | ||
default: { | ||
http: ['https://rpc.testnet.immutable.com'], | ||
}, | ||
}, | ||
}); | ||
|
||
const walletClient = createWalletClient({ | ||
chain: immutableTestnet, | ||
transport: http(), | ||
account: privateKeyToAccount(privateKey), | ||
}); | ||
|
||
// Bound contract instance | ||
const contract = getContract({ | ||
address: contractAddress, | ||
abi: ImmutableERC721Abi, | ||
client: walletClient, | ||
}); | ||
|
||
// We can use the read function hasRole to check if the intended signer | ||
// has sufficient permissions to mint before we send the transaction | ||
const minterRole = await contract.read.MINTER_ROLE(); | ||
|
||
const hasMinterRole = await contract.read.hasRole([ | ||
minterRole, | ||
walletClient.account.address, | ||
]); | ||
|
||
if (!hasMinterRole) { | ||
// Handle scenario without permissions... | ||
console.log('Account doesnt have permissions to mint.'); | ||
return Promise.reject( | ||
new Error('Account doesnt have permissions to mint.'), | ||
); | ||
} | ||
|
||
const txHash = await contract.write.mintBatchByQuantity([mints]); | ||
|
||
console.log(`txHash: ${txHash}`); | ||
return txHash; | ||
}; | ||
|
||
batchMintERC721ByQuantity(PRIVATE_KEY, CONTRACT_ADDRESS, MINTS); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { batchMintERC721ByID } from './batch-mint-erc721-by-id'; | ||
import { batchMintERC721ByQuantity } from './batch-mint-erc721-by-quantity'; | ||
|
||
export { batchMintERC721ByID }; | ||
export { batchMintERC721ByID, batchMintERC721ByQuantity }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters