-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
STREAM-1060: check all token accounts ATA #118
Changes from 4 commits
8b01f0a
73ca0ae
60a9154
2e3bee1
ec780ae
b9a4ee9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ import { | |
ICreateStreamSolanaExt, | ||
IInteractStreamSolanaExt, | ||
ITopUpStreamSolanaExt, | ||
CheckAssociatedTokenAccountData, | ||
CheckAssociatedTokenAccountsData, | ||
} from "./types"; | ||
import { | ||
ata, | ||
|
@@ -479,7 +479,7 @@ export default class SolanaStreamClient extends BaseStreamClient { | |
|
||
const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY); | ||
const partnerTokens = await ata(data.mint, data.partner); | ||
await this.checkAssociatedTokenAccount(data, { invoker }, ixs); | ||
await this.checkAssociatedTokenAccounts(data, { invoker }, ixs); | ||
|
||
ixs.push( | ||
withdrawStreamInstruction(amount, this.programId, { | ||
|
@@ -534,7 +534,7 @@ export default class SolanaStreamClient extends BaseStreamClient { | |
const partnerTokens = await ata(data.mint, data.partner); | ||
|
||
const ixs: TransactionInstruction[] = []; | ||
await this.checkAssociatedTokenAccount(data, { invoker }, ixs); | ||
await this.checkAssociatedTokenAccounts(data, { invoker }, ixs); | ||
|
||
ixs.push( | ||
cancelStreamInstruction(this.programId, { | ||
|
@@ -947,25 +947,45 @@ export default class SolanaStreamClient extends BaseStreamClient { | |
} | ||
|
||
/** | ||
* Utility function that checks whether associated token account for the recipient exists and adds an instruction to add if not | ||
* Utility function that checks whether associated token accounts still exist and adds instructions to add them if not | ||
*/ | ||
private async checkAssociatedTokenAccount( | ||
data: CheckAssociatedTokenAccountData, | ||
{ invoker }: IInteractStreamSolanaExt, | ||
private async checkAssociatedTokenAccounts( | ||
data: CheckAssociatedTokenAccountsData, | ||
{ invoker, checkTokenAccounts }: IInteractStreamSolanaExt, | ||
ixs: TransactionInstruction[] | ||
) { | ||
const accountExists = await this.connection.getAccountInfo(data.recipientTokens); | ||
if (!accountExists?.data) { | ||
ixs.push( | ||
createAssociatedTokenAccountInstruction( | ||
invoker.publicKey!, | ||
data.recipientTokens, | ||
data.recipient, | ||
data.mint, | ||
TOKEN_PROGRAM_ID, | ||
ASSOCIATED_TOKEN_PROGRAM_ID | ||
) | ||
); | ||
if (!checkTokenAccounts) { | ||
return; | ||
} | ||
const checkedKeys: Set<PublicKey> = new Set(); | ||
const accountArrays = [ | ||
[data.sender, data.senderTokens], | ||
[data.recipient, data.recipientTokens], | ||
[data.partner, data.partnerTokens], | ||
[data.streamflowTreasury, data.streamflowTreasuryTokens], | ||
].filter((value) => { | ||
if (checkedKeys.has(value[1])) { | ||
return false; | ||
} | ||
checkedKeys.add(value[1]); | ||
return true; | ||
}); | ||
const response = await this.connection.getMultipleAccountsInfo( | ||
accountArrays.map((item) => item[1]) | ||
); | ||
for (let i = 0; i < response.length; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we iterate here over accountArrays in forof loop? for accountArray of accountArrays ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should we? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More clean then traditional for loop with indexes :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need it here because every index in response corresponds to index in accountArrays |
||
if (!response[1]) { | ||
Yolley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ixs.push( | ||
createAssociatedTokenAccountInstruction( | ||
invoker.publicKey!, | ||
accountArrays[i][0], | ||
accountArrays[i][1], | ||
data.mint, | ||
TOKEN_PROGRAM_ID, | ||
ASSOCIATED_TOKEN_PROGRAM_ID | ||
) | ||
); | ||
} | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create a proper object out of this?
We could just have a type and cast data to it.
Much easier then to work with indexes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean here? Seems complex.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's easier to read then associating [1] to senderTokens for instance.