Created
September 26, 2022 15:45
-
-
Save gchatz22/6b80629481a7c14988d1ee3e98fd7aa6 to your computer and use it in GitHub Desktop.
Wrap Token (issue + claim token manager for mint in same transaction)
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
export const withWrapToken = async ( | |
transaction: Transaction, | |
connection: Connection, | |
wallet: Wallet, | |
mintId: PublicKey, | |
listingAuthorityName?: string, | |
payer = wallet.publicKey | |
): Promise<[Transaction, PublicKey]> => { | |
const [tokenManagerId] = await findTokenManagerAddress(mintId); | |
const checkTokenManager = await tryGetAccount(() => | |
getTokenManager(connection, tokenManagerId) | |
); | |
if (checkTokenManager?.parsed) { | |
throw "Token is already wrapped"; | |
} | |
const issuerTokenAccountId = await findAta(mintId, wallet.publicKey, true); | |
let kind = TokenManagerKind.Edition; | |
try { | |
await MasterEdition.getPDA(mintId); | |
} catch (e) { | |
kind = TokenManagerKind.Managed; | |
} | |
await withIssueToken( | |
transaction, | |
connection, | |
wallet, | |
{ | |
mint: mintId, | |
invalidationType: InvalidationType.Release, | |
issuerTokenAccountId: issuerTokenAccountId, | |
kind: kind, | |
listingAuthorityName: listingAuthorityName, | |
}, | |
payer | |
); | |
const tokenManagerTokenAccountId = await findAta( | |
mintId, | |
tokenManagerId, | |
true | |
); | |
const recipientTokenAccountId = await withFindOrInitAssociatedTokenAccount( | |
transaction, | |
connection, | |
mintId, | |
wallet.publicKey, | |
payer, | |
true | |
); | |
transaction.add( | |
await claim( | |
connection, | |
wallet, | |
tokenManagerId, | |
kind, | |
mintId, | |
tokenManagerTokenAccountId, | |
recipientTokenAccountId, | |
undefined | |
) | |
); | |
return [transaction, tokenManagerId]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment