1- import { input } from '@inquirer/prompts' ;
21import signale , { error , info , success } from 'signale' ;
32import { TokenRegistryMintCommand } from '../../types' ;
43import {
@@ -7,14 +6,18 @@ import {
76 getErrorMessage ,
87 getEtherscanAddress ,
98 NetworkCmdName ,
10- promptRemarkAndEncryptionKey ,
11- promptNetworkSelection ,
129 promptWalletSelection ,
1310 getWalletOrSigner ,
1411 canEstimateGasPrice ,
1512 getGasFees ,
13+ extractDocumentInfo ,
14+ promptAndReadDocument ,
15+ promptRemark ,
16+ promptAddress ,
17+ performDryRunWithConfirmation ,
1618} from '../../utils' ;
17- import { BigNumberish , TransactionReceipt } from 'ethers' ;
19+ import { connectToTokenRegistry , validateAndEncryptRemark } from '../helpers' ;
20+ import { TransactionReceipt } from 'ethers' ;
1821import { mint } from '@trustvc/trustvc' ;
1922
2023export const command = 'mint' ;
@@ -34,76 +37,32 @@ export const handler = async (): Promise<void> => {
3437
3538// Prompt user for all required inputs
3639export const promptForInputs = async ( ) : Promise < TokenRegistryMintCommand > => {
37- // Network selection
38- const network = await promptNetworkSelection ( ) ;
39-
40- // Token Registry Address
41- const address = await input ( {
42- message : 'Enter the token registry contract address:' ,
43- required : true ,
44- validate : ( value : string ) => {
45- if ( ! value || value . trim ( ) === '' ) {
46- return 'Token registry address is required' ;
47- }
48- if ( ! / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( value ) ) {
49- return 'Invalid Ethereum address format' ;
50- }
51- return true ;
52- } ,
53- } ) ;
40+ // Extract document information using utility function
41+ const document = await promptAndReadDocument ( ) ;
5442
55- // Token ID (Document Hash)
56- const tokenId = await input ( {
57- message : 'Enter the document hash (tokenId) to mint:' ,
58- required : true ,
59- validate : ( value : string ) => {
60- if ( ! value || value . trim ( ) === '' ) {
61- return 'Token ID is required' ;
62- }
63- return true ;
64- } ,
65- } ) ;
43+ // Extract document information using utility function
44+ const { tokenRegistry, tokenId, network, documentId, registryVersion } =
45+ await extractDocumentInfo ( document ) ;
6646
6747 // Beneficiary Address
68- const beneficiary = await input ( {
69- message : 'Enter the beneficiary address (initial recipient):' ,
70- required : true ,
71- validate : ( value : string ) => {
72- if ( ! value || value . trim ( ) === '' ) {
73- return 'Beneficiary address is required' ;
74- }
75- if ( ! / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( value ) ) {
76- return 'Invalid Ethereum address format' ;
77- }
78- return true ;
79- } ,
80- } ) ;
48+ const beneficiary = await promptAddress ( 'beneficiary' , 'initial recipient' ) ;
8149
8250 // Holder Address
83- const holder = await input ( {
84- message : 'Enter the holder address (initial holder):' ,
85- required : true ,
86- validate : ( value : string ) => {
87- if ( ! value || value . trim ( ) === '' ) {
88- return 'Holder address is required' ;
89- }
90- if ( ! / ^ 0 x [ a - f A - F 0 - 9 ] { 40 } $ / . test ( value ) ) {
91- return 'Invalid Ethereum address format' ;
92- }
93- return true ;
94- } ,
95- } ) ;
51+ const holder = await promptAddress ( 'holder' , 'initial holder' ) ;
9652
9753 // Wallet selection
9854 const { encryptedWalletPath, key, keyFile } = await promptWalletSelection ( ) ;
9955
100- // Optional: Remark and Encryption Key
101- const { remark, encryptionKey } = await promptRemarkAndEncryptionKey ( ) ;
56+ // Optional: Remark (only for V5)
57+ const remark = await promptRemark ( registryVersion ) ;
58+
59+ // Use document ID as encryption key
60+ const encryptionKey = documentId ;
10261
10362 // Build the result object with proper typing
10463 const baseResult = {
10564 network,
106- address,
65+ address : tokenRegistry ,
10766 tokenId,
10867 beneficiary,
10968 holder,
@@ -177,30 +136,76 @@ const mintToTokenRegistry = async ({
177136 ...rest
178137} : TokenRegistryMintCommand ) : Promise < TransactionReceipt > => {
179138 const wallet = await getWalletOrSigner ( { network, ...rest } ) ;
180- let transactionOptions : { maxFeePerGas ?: BigNumberish ; maxPriorityFeePerGas ?: BigNumberish } = { } ;
181139
182140 if ( dryRun ) {
183141 console . log ( '🔧 Dry run mode is currently undergoing upgrades and will be available soon.' ) ;
184142 process . exit ( 0 ) ;
185143 }
186144
145+ // Automatic dry run for Ethereum and Polygon networks
146+ const shouldProceed = await performDryRunWithConfirmation ( {
147+ network,
148+ getTransactionCallback : async ( ) => {
149+ const tokenRegistry = await connectToTokenRegistry ( { address, wallet } ) ;
150+
151+ // Validate and encrypt the remark with document ID as encryption key
152+ const encryptedRemark = validateAndEncryptRemark ( remark , encryptionKey ) ;
153+
154+ // Populate the transaction for gas estimation
155+ const tx = await tokenRegistry . mint . populateTransaction (
156+ beneficiary ,
157+ holder ,
158+ tokenId ,
159+ encryptedRemark
160+ ) ;
161+
162+ // Ensure the transaction has a 'from' address for proper gas estimation
163+ return {
164+ ...tx ,
165+ from : await wallet . getAddress ( ) ,
166+ } ;
167+ } ,
168+ } ) ;
169+
170+ if ( ! shouldProceed ) {
171+ process . exit ( 0 ) ;
172+ }
173+
174+ let transaction ;
175+
176+ // Execute transaction with appropriate gas settings based on network capabilities
187177 if ( canEstimateGasPrice ( network ) ) {
178+ // Ensure provider is available for gas estimation
188179 if ( ! wallet . provider ) {
189180 throw new Error ( 'Provider is required for gas estimation' ) ;
190181 }
182+
183+ // Get current gas fees from the network
191184 const gasFees = await getGasFees ( { provider : wallet . provider , ...rest } ) ;
192- transactionOptions = {
193- maxFeePerGas : gasFees . maxFeePerGas as BigNumberish ,
194- maxPriorityFeePerGas : gasFees . maxPriorityFeePerGas as BigNumberish ,
195- } ;
185+
186+ // Execute mint with EIP-1559 gas parameters
187+ transaction = await mint (
188+ { tokenRegistryAddress : address } ,
189+ wallet ,
190+ { beneficiaryAddress : beneficiary , holderAddress : holder , tokenId, remarks : remark } ,
191+ {
192+ id : encryptionKey ,
193+ maxFeePerGas : gasFees . maxFeePerGas ?. toString ( ) ,
194+ maxPriorityFeePerGas : gasFees . maxPriorityFeePerGas ?. toString ( ) ,
195+ } ,
196+ ) ;
197+ } else {
198+ // Execute mint without gas estimation (for networks that don't support it)
199+ transaction = await mint (
200+ { tokenRegistryAddress : address } ,
201+ wallet ,
202+ { beneficiaryAddress : beneficiary , holderAddress : holder , tokenId, remarks : remark } ,
203+ {
204+ id : encryptionKey ,
205+ } ,
206+ ) ;
196207 }
197208
198- const transaction = await mint (
199- { tokenRegistryAddress : address } ,
200- wallet ,
201- { beneficiaryAddress : beneficiary , holderAddress : holder , tokenId, remarks : remark } ,
202- { id : encryptionKey , ...transactionOptions } ,
203- ) ;
204209 signale . await ( `Waiting for transaction ${ transaction . hash } to be mined` ) ;
205210 const receipt = ( await transaction . wait ( ) ) as unknown as TransactionReceipt ;
206211 if ( ! receipt ) {
0 commit comments