@@ -12,12 +12,12 @@ const destinations = Object.keys(fundSenderDestinations);
12
12
13
13
const processSPLCertificates = async ( client : ConfiguredClient ) => {
14
14
const allInputTokenAccountsResponse =
15
- await client . provider . connection . getParsedTokenAccountsByOwner (
16
- client . getInputAccount ( ) ,
17
- {
18
- programId : TOKEN_PROGRAM_ID ,
19
- }
20
- ) ;
15
+ await client . provider . connection . getParsedTokenAccountsByOwner (
16
+ client . getInputAccount ( ) ,
17
+ {
18
+ programId : TOKEN_PROGRAM_ID ,
19
+ }
20
+ ) ;
21
21
22
22
const allInputTokenAccounts = allInputTokenAccountsResponse . value ;
23
23
@@ -26,16 +26,37 @@ const processSPLCertificates = async (client: ConfiguredClient) => {
26
26
return ;
27
27
}
28
28
29
- console . log ( `Storing ${ allInputTokenAccounts . length } certificates...` ) ;
30
- for ( const inputTokenAccount of allInputTokenAccounts ) {
29
+ // check for zero balance on any token accounts (these accounts can be closed)
30
+ const nonZeroBalanceAccounts = allInputTokenAccounts . filter (
31
+ ( account ) => account . account . data . parsed . info . tokenAmount . uiAmount > 0
32
+ ) ;
33
+ console . log ( `Skipping ${ allInputTokenAccounts . length - nonZeroBalanceAccounts . length } accounts with zero balance` ) ;
34
+
35
+ console . log ( `Storing ${ nonZeroBalanceAccounts . length } certificates...` ) ;
36
+ for ( const inputTokenAccount of nonZeroBalanceAccounts ) {
31
37
const mint = new PublicKey ( inputTokenAccount . account . data . parsed . info . mint ) ;
32
- await client . storeCertificates ( inputTokenAccount . pubkey , mint ) ;
38
+ await client . storeCertificates ( inputTokenAccount . pubkey , mint ) . catch ( ( e ) => {
39
+ console . error ( "Error storing certificate: " , inputTokenAccount . pubkey . toBase58 ( ) , e ) ;
40
+ throw e ;
41
+ } ) ;
42
+ }
43
+
44
+ // if there are any zero balance accounts, ask if we want to close them...
45
+ if ( allInputTokenAccounts . length !== nonZeroBalanceAccounts . length ) {
46
+ const confirm = readlineSync . question ( chalk . yellow ( "Do you want to close the zero balance accounts? (y/n): " ) ) ;
47
+ if ( confirm === "y" ) {
48
+ console . log ( "Closing zero balance accounts..." ) ;
49
+ for ( const inputTokenAccount of allInputTokenAccounts ) {
50
+ if ( nonZeroBalanceAccounts . includes ( inputTokenAccount ) ) continue ;
51
+ await client . closeCertificateAccount ( inputTokenAccount . pubkey ) ;
52
+ }
53
+ }
33
54
}
34
55
} ;
35
56
36
57
const processCNFTCertificates = async (
37
- client : ConfiguredClient ,
38
- destination : { lookupTable ?: PublicKey }
58
+ client : ConfiguredClient ,
59
+ destination : { lookupTable ?: PublicKey }
39
60
) => {
40
61
const assets = await client . getCNFTCertificates ( ) ;
41
62
console . log ( "number of CNFT certificates" , assets . length ) ;
@@ -50,8 +71,8 @@ const processCNFTCertificates = async (
50
71
console . log ( "No CNFT address lookup table provided - creating..." ) ;
51
72
cnftAddressLookupTable = await client . createALTForCNFTTransfer ( ) ;
52
73
console . log (
53
- "Created CNFT address lookup table" ,
54
- cnftAddressLookupTable . toBase58 ( )
74
+ "Created CNFT address lookup table" ,
75
+ cnftAddressLookupTable . toBase58 ( )
55
76
) ;
56
77
}
57
78
@@ -78,15 +99,15 @@ export const submenuStoreCertificates = async () => {
78
99
79
100
const destinationName = destinations [ parseInt ( choice ) - 1 ] ;
80
101
const client = fundSenderClients . find (
81
- ( c ) => c . config . destinationName === destinationName
102
+ ( c ) => c . config . destinationName === destinationName
82
103
) ;
83
104
84
105
if ( ! client ) throw new Error ( "Client not found - trigger a refresh" ) ;
85
106
86
107
if ( fundSenderDestinations [ destinationName ] . type === "cnft" ) {
87
108
await processCNFTCertificates (
88
- client ,
89
- fundSenderDestinations [ destinationName ] as { lookupTable : PublicKey }
109
+ client ,
110
+ fundSenderDestinations [ destinationName ] as { lookupTable : PublicKey }
90
111
) ;
91
112
} else {
92
113
await processSPLCertificates ( client ) ;
0 commit comments