Skip to content

Commit 5716b10

Browse files
committed
WIP to close certificate token accounts
1 parent 8cf72c1 commit 5716b10

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

packages/fund-sender/client/index.ts

+11
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,17 @@ export class FundSenderClient {
480480
return this;
481481
}
482482

483+
public async closeCertificateAccount(
484+
inputTokenAccount: PublicKey,
485+
): Promise<FundSenderClient> {
486+
if (!this.config) {
487+
throw new Error("Client not initialized");
488+
}
489+
490+
console.log("TODO: Implement closeCertificateAccount");
491+
return this;
492+
}
493+
483494
public async getCNFTCertificates() {
484495
if (!this.config) throw new Error("Client not initialized");
485496

scripts/submenu/storeCertificates.ts

+37-16
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ const destinations = Object.keys(fundSenderDestinations);
1212

1313
const processSPLCertificates = async (client: ConfiguredClient) => {
1414
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+
);
2121

2222
const allInputTokenAccounts = allInputTokenAccountsResponse.value;
2323

@@ -26,16 +26,37 @@ const processSPLCertificates = async (client: ConfiguredClient) => {
2626
return;
2727
}
2828

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) {
3137
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+
}
3354
}
3455
};
3556

3657
const processCNFTCertificates = async (
37-
client: ConfiguredClient,
38-
destination: { lookupTable?: PublicKey }
58+
client: ConfiguredClient,
59+
destination: { lookupTable?: PublicKey }
3960
) => {
4061
const assets = await client.getCNFTCertificates();
4162
console.log("number of CNFT certificates", assets.length);
@@ -50,8 +71,8 @@ const processCNFTCertificates = async (
5071
console.log("No CNFT address lookup table provided - creating...");
5172
cnftAddressLookupTable = await client.createALTForCNFTTransfer();
5273
console.log(
53-
"Created CNFT address lookup table",
54-
cnftAddressLookupTable.toBase58()
74+
"Created CNFT address lookup table",
75+
cnftAddressLookupTable.toBase58()
5576
);
5677
}
5778

@@ -78,15 +99,15 @@ export const submenuStoreCertificates = async () => {
7899

79100
const destinationName = destinations[parseInt(choice) - 1];
80101
const client = fundSenderClients.find(
81-
(c) => c.config.destinationName === destinationName
102+
(c) => c.config.destinationName === destinationName
82103
);
83104

84105
if (!client) throw new Error("Client not found - trigger a refresh");
85106

86107
if (fundSenderDestinations[destinationName].type === "cnft") {
87108
await processCNFTCertificates(
88-
client,
89-
fundSenderDestinations[destinationName] as { lookupTable: PublicKey }
109+
client,
110+
fundSenderDestinations[destinationName] as { lookupTable: PublicKey }
90111
);
91112
} else {
92113
await processSPLCertificates(client);

0 commit comments

Comments
 (0)