Skip to content

Commit

Permalink
add freeze, thaw and revoke
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhargavamacha committed Aug 20, 2024
1 parent 2533eb8 commit cac5bbf
Show file tree
Hide file tree
Showing 18 changed files with 889 additions and 212 deletions.
2 changes: 1 addition & 1 deletion clients/rwa-token-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bridgesplit/rwa-token-sdk",
"version": "0.0.20",
"version": "0.0.24",
"description": "RWA Token SDK for the development of permissioned tokens on SVM blockchains.",
"homepage": "https://github.com/bridgesplit/rwa-token#readme",
"main": "dist/index",
Expand Down
127 changes: 124 additions & 3 deletions clients/rwa-token-sdk/src/asset-controller/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export type IssueTokenArgs = {
amount: number;
authority: string;
owner: string;
createTa?: boolean;
} & CommonArgs;

/**
Expand All @@ -142,8 +143,26 @@ export type IssueTokenArgs = {
export async function getIssueTokensIx(
args: IssueTokenArgs,
provider: AnchorProvider
): Promise<TransactionInstruction> {
): Promise<TransactionInstruction[]> {
const assetProgram = getAssetControllerProgram(provider);
const ixs: TransactionInstruction[] = [];
try {
await getAccount(provider.connection, getAssociatedTokenAddressSync(
new PublicKey(args.assetMint),
new PublicKey(args.owner),
true,
TOKEN_2022_PROGRAM_ID
), undefined, TOKEN_2022_PROGRAM_ID);
} catch (error) {
if (args.createTa) {
ixs.push(createAssociatedTokenAccountInstruction(new PublicKey(args.payer), getAssociatedTokenAddressSync(
new PublicKey(args.assetMint),
new PublicKey(args.owner),
true,
TOKEN_2022_PROGRAM_ID
), new PublicKey(args.owner), new PublicKey(args.assetMint), TOKEN_2022_PROGRAM_ID));
}
}
const ix = await assetProgram.methods
.issueTokens({
amount: new BN(args.amount),
Expand All @@ -161,7 +180,8 @@ export async function getIssueTokensIx(
),
})
.instruction();
return ix;
ixs.push(ix);
return ixs;
}

export type VoidTokensArgs = {
Expand All @@ -176,7 +196,7 @@ export async function getVoidTokensIx(
const assetProgram = getAssetControllerProgram(provider);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const ix = await assetProgram.methods
.voidTokens(new BN(args.amount))
.burnTokens(new BN(args.amount))
.accountsStrict({
assetMint: new PublicKey(args.assetMint),
tokenProgram: TOKEN_2022_PROGRAM_ID,
Expand Down Expand Up @@ -529,4 +549,105 @@ export async function getCloseMintIx(
})
.instruction();
return ix;
}

export type FreezeTokenArgs = {
authority: string;
owner: string;
} & CommonArgs;

/**
* Generate Instructions to freeze token account
* @param args - {@link FreezeTokenArgs}
* @returns - {@link TransactionInstruction}
*/
export async function getFreezeTokenIx(
args: FreezeTokenArgs,
provider: AnchorProvider
): Promise<TransactionInstruction> {
const assetProgram = getAssetControllerProgram(provider);
const ix = await assetProgram.methods
.freezeTokenAccount()
.accountsStrict({
authority: new PublicKey(args.authority),
assetMint: new PublicKey(args.assetMint),
tokenProgram: TOKEN_2022_PROGRAM_ID,
assetController: getAssetControllerPda(args.assetMint),
tokenAccount: getAssociatedTokenAddressSync(
new PublicKey(args.assetMint),
new PublicKey(args.owner),
false,
TOKEN_2022_PROGRAM_ID
),
})
.instruction();
return ix;
}

/**
* Generate Instructions to thaw token account
* @param args - {@link FreezeTokenArgs}
* @returns - {@link TransactionInstruction}
* */
export async function getThawTokenIx(
args: FreezeTokenArgs,
provider: AnchorProvider
): Promise<TransactionInstruction> {
const assetProgram = getAssetControllerProgram(provider);
const ix = await assetProgram.methods
.thawTokenAccount()
.accountsStrict({
authority: new PublicKey(args.authority),
assetMint: new PublicKey(args.assetMint),
tokenProgram: TOKEN_2022_PROGRAM_ID,
assetController: getAssetControllerPda(args.assetMint),
tokenAccount: getAssociatedTokenAddressSync(
new PublicKey(args.assetMint),
new PublicKey(args.owner),
false,
TOKEN_2022_PROGRAM_ID
),
})
.instruction();
return ix;
}

export type RevokeTokensArgs = {
amount: number;
owner: string;
authority: string;
} & CommonArgs;

/**
* Revoke tokens from a user
* @param args - {@link RevokeTokensArgs}
* @returns - {@link TransactionInstruction}
* */
export async function getRevokeTokensIx(
args: RevokeTokensArgs,
provider: AnchorProvider
): Promise<TransactionInstruction> {
const assetProgram = getAssetControllerProgram(provider);
const ix = await assetProgram.methods
.revokeTokens(new BN(args.amount))
.accountsStrict({
authority: new PublicKey(args.authority),
assetMint: new PublicKey(args.assetMint),
tokenProgram: TOKEN_2022_PROGRAM_ID,
assetController: getAssetControllerPda(args.assetMint),
revokeTokenAccount: getAssociatedTokenAddressSync(
new PublicKey(args.assetMint),
new PublicKey(args.owner),
false,
TOKEN_2022_PROGRAM_ID
),
authorityTokenAccount: getAssociatedTokenAddressSync(
new PublicKey(args.assetMint),
new PublicKey(args.authority),
false,
TOKEN_2022_PROGRAM_ID
),
})
.instruction();
return ix;
}
6 changes: 3 additions & 3 deletions clients/rwa-token-sdk/src/classes/AssetController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ export class AssetController {
*/
async issueTokenIxns(
IssueArgs: IssueTokenArgs
): Promise<TransactionInstruction> {
const issueTokensIx = await getIssueTokensIx(
): Promise<TransactionInstruction[]> {
const issueTokensIxs = await getIssueTokensIx(
IssueArgs,
this.rwaClient.provider
);
return issueTokensIx;
return issueTokensIxs;
}

/**
Expand Down
Loading

0 comments on commit cac5bbf

Please sign in to comment.