@bridgesplit/rwa-token-sdk
Version:
RWA Token SDK for the development of permissioned tokens on SVM blockchains.
501 lines • 23.4 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRevokeTokensIx = exports.getThawTokenIx = exports.getFreezeTokenIx = exports.getCloseMintIx = exports.getDisableMemoTransferIx = exports.getEnableMemoTransferIx = exports.getUpdateInterestBearingMintRateIx = exports.getSetupUserIxs = exports.getSetupAssetControllerIxs = exports.getTransferTokensIxs = exports.getVoidTokensIx = exports.getIssueTokensIx = exports.getUpdateAssetMetadataIx = exports.getCreateAssetControllerIx = void 0;
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
const web3_js_1 = require("@solana/web3.js");
const policy_engine_1 = require("../policy-engine");
const data_registry_1 = require("../data-registry");
const identity_registry_1 = require("../identity-registry");
const spl_token_1 = require("@solana/spl-token");
const utils_1 = require("./utils");
const anchor_1 = require("@coral-xyz/anchor");
/**
* Builds the transaction instruction to create an Asset Controller.
* @param args - {@link CreateAssetControllerIx}
* @returns Create asset controller transaction instruction
*/
function getCreateAssetControllerIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.createAssetController({
decimals: args.decimals,
name: args.name,
uri: args.uri,
symbol: args.symbol,
delegate: args.delegate ? new web3_js_1.PublicKey(args.delegate) : null,
interestRate: args.interestRate ? new anchor_1.BN(args.interestRate) : null,
})
.accountsStrict({
payer: args.payer,
assetMint: args.assetMint,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
extraMetasAccount: (0, policy_engine_1.getExtraMetasListPda)(args.assetMint),
systemProgram: web3_js_1.SystemProgram.programId,
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
authority: args.authority,
policyEngineAccount: (0, policy_engine_1.getPolicyEnginePda)(args.assetMint),
dataRegistryAccount: (0, data_registry_1.getDataRegistryPda)(args.assetMint),
identityRegistryAccount: (0, identity_registry_1.getIdentityRegistryPda)(args.assetMint),
policyEngine: policy_engine_1.policyEngineProgramId,
identityRegistry: identity_registry_1.identityRegistryProgramId,
dataRegistry: data_registry_1.dataRegistryProgramId,
eventAuthority: (0, utils_1.getAssetControllerEventAuthority)(),
program: utils_1.assetControllerProgramId,
})
.instruction();
return ix;
});
}
exports.getCreateAssetControllerIx = getCreateAssetControllerIx;
/**
* Builds the transaction instruction to create an Asset Controller.
* @param args - {@link UpdateAssetMetadataArgs}
* @returns Create asset controller transaction instruction
*/
function getUpdateAssetMetadataIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.updateMetadata({
name: args.name || null,
uri: args.uri || null,
symbol: args.symbol || null,
})
.accountsStrict({
payer: args.payer,
assetMint: args.assetMint,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
authority: args.authority,
eventAuthority: (0, utils_1.getAssetControllerEventAuthority)(),
systemProgram: web3_js_1.SystemProgram.programId,
program: utils_1.assetControllerProgramId,
})
.instruction();
return ix;
});
}
exports.getUpdateAssetMetadataIx = getUpdateAssetMetadataIx;
/**
* Creates transaction instruction to issue tokens for a specific amount for a specific asset.
* @param args {@link IssueTokenArgs}
* @returns A transaction instruction distributing the specified amount for the specific asset.
*/
function getIssueTokensIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.issueTokens(new anchor_1.BN(args.amount))
.accountsStrict({
authority: new web3_js_1.PublicKey(args.authority),
assetMint: new web3_js_1.PublicKey(args.assetMint),
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
tokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.owner), false, spl_token_1.TOKEN_2022_PROGRAM_ID),
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: web3_js_1.SystemProgram.programId,
to: new web3_js_1.PublicKey(args.owner),
})
.instruction();
return [ix];
});
}
exports.getIssueTokensIx = getIssueTokensIx;
function getVoidTokensIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const ix = yield assetProgram.methods
.burnTokens(new anchor_1.BN(args.amount))
.accountsStrict({
assetMint: new web3_js_1.PublicKey(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
tokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.owner), false, spl_token_1.TOKEN_2022_PROGRAM_ID),
owner: args.owner,
})
.instruction();
return ix;
});
}
exports.getVoidTokensIx = getVoidTokensIx;
/**
* Creates a transaction instruction to transfer a token between addresses with transfer controls.
* @param args {@link TransferTokensArgs}
* @returns Transaction instruction to transfer RWA token.
*/
function getTransferTokensIxs(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const remainingAccounts = [
{
pubkey: (0, policy_engine_1.getPolicyEnginePda)(args.assetMint),
isWritable: false,
isSigner: false,
},
{
pubkey: identity_registry_1.identityRegistryProgramId,
isWritable: false,
isSigner: false,
},
{
pubkey: (0, identity_registry_1.getIdentityRegistryPda)(args.assetMint),
isWritable: false,
isSigner: false,
},
{
pubkey: (0, identity_registry_1.getIdentityAccountPda)(args.assetMint, args.to),
isWritable: false,
isSigner: false,
},
{
pubkey: (0, policy_engine_1.getTrackerAccountPda)(args.assetMint, args.to),
isWritable: true,
isSigner: false,
},
{
pubkey: (0, policy_engine_1.getPolicyAccountPda)(args.assetMint),
isWritable: true,
isSigner: false,
},
{
pubkey: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
isWritable: false,
isSigner: false,
},
{
pubkey: (0, policy_engine_1.getExtraMetasListPda)(args.assetMint),
isWritable: false,
isSigner: false,
},
{
pubkey: policy_engine_1.policyEngineProgramId,
isWritable: false,
isSigner: false,
},
{
pubkey: (0, identity_registry_1.getIdentityAccountPda)(args.assetMint, args.from),
isWritable: false,
isSigner: false,
}
];
const ixs = [];
try {
const ta = yield (0, spl_token_1.getAccount)(provider.connection, (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.to), true, spl_token_1.TOKEN_2022_PROGRAM_ID), undefined, spl_token_1.TOKEN_2022_PROGRAM_ID);
const isMemoTransfer = (0, spl_token_1.getMemoTransfer)(ta);
if (isMemoTransfer) {
if (!args.message) {
throw new Error("Memo is required for memo transfer");
}
ixs.push(new web3_js_1.TransactionInstruction({
keys: [{ pubkey: new web3_js_1.PublicKey(args.from), isSigner: true, isWritable: true }],
data: Buffer.from(args.message, "utf-8"),
programId: new web3_js_1.PublicKey("MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr"),
}));
}
}
catch (error) {
if (args.createTa) {
ixs.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(new web3_js_1.PublicKey(args.from), (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.to), true, spl_token_1.TOKEN_2022_PROGRAM_ID), new web3_js_1.PublicKey(args.to), new web3_js_1.PublicKey(args.assetMint), spl_token_1.TOKEN_2022_PROGRAM_ID));
}
}
const ix = (0, spl_token_1.createTransferCheckedInstruction)((0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.from), true, spl_token_1.TOKEN_2022_PROGRAM_ID), new web3_js_1.PublicKey(args.assetMint), (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.to), true, spl_token_1.TOKEN_2022_PROGRAM_ID), new web3_js_1.PublicKey(args.from), args.amount, args.decimals, [], spl_token_1.TOKEN_2022_PROGRAM_ID);
ix.keys = ix.keys.concat(remainingAccounts);
ixs.push(ix);
return ixs;
});
}
exports.getTransferTokensIxs = getTransferTokensIxs;
/**
* Generates a new asset controller.
* This includes generation of a new key pair, a new asset registry, policy registry, data registry, identity registry.
* @param args - {@link SetupAssetControllerArgs}
* @returns - {@link IxReturn}, an object of the initialize transaction instructions and a new keypair.
*/
function getSetupAssetControllerIxs(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const mintKp = new web3_js_1.Keypair();
const mint = mintKp.publicKey;
const updatedArgs = Object.assign(Object.assign({}, args), { assetMint: mint.toString(), signer: args.authority });
// Get asset registry create ix
const assetControllerCreateIx = yield getCreateAssetControllerIx(updatedArgs, provider);
return {
ixs: [
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({ units: 450000 }),
assetControllerCreateIx,
],
signers: [mintKp],
};
});
}
exports.getSetupAssetControllerIxs = getSetupAssetControllerIxs;
/**
* Generate instructions to set up a user for permissioned based assets.
* This function constructs the instructions necessary for setting up a user, which includes
* creating an identity account, indicating permissions, and a token account for the user.
* @param args {@link SetupUserArgs}
* @returns - {@link IxReturn}, a promise that resolves to a list of generated transaction instructions.
*/
function getSetupUserIxs(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const ixs = [];
const identityAccountIx = yield (0, identity_registry_1.getCreateIdentityAccountIx)({
payer: args.payer,
signer: args.signer,
assetMint: args.assetMint,
owner: args.owner,
level: args.levels[0],
}, provider);
ixs.push(identityAccountIx);
if (args.levels.length > 1) {
for (let i = 1; i < args.levels.length; i++) {
const addLevelIx = yield (0, identity_registry_1.getAddLevelToIdentityAccount)({
authority: args.signer,
owner: args.owner,
assetMint: args.assetMint,
level: args.levels[i],
signer: args.signer,
payer: args.payer,
}, provider);
ixs.push(addLevelIx);
}
}
const trackerAccountIx = yield (0, policy_engine_1.getCreateTrackerAccountIx)({
payer: args.payer,
owner: args.owner,
assetMint: args.assetMint,
}, provider);
ixs.push(trackerAccountIx);
return {
ixs,
signers: [],
};
});
}
exports.getSetupUserIxs = getSetupUserIxs;
/**
* Generate Instructions to update interest rate
* @param args - {@link InterestRateArgs}
* @returns - {@link TransactionInstruction}
* */
function getUpdateInterestBearingMintRateIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.updateInterestBearingMintRate(new anchor_1.BN(args.rate))
.accountsStrict({
authority: new web3_js_1.PublicKey(args.authority),
assetMint: new web3_js_1.PublicKey(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
program: utils_1.assetControllerProgramId,
eventAuthority: (0, utils_1.getAssetControllerEventAuthority)(),
})
.instruction();
return ix;
});
}
exports.getUpdateInterestBearingMintRateIx = getUpdateInterestBearingMintRateIx;
/**
* Generate Instructions to disable memo transfer
* @param args - {@link MemoTranferArgs}
* @returns - {@link TransactionInstruction}
* */
function getEnableMemoTransferIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.enableMemoTransfer()
.accountsStrict({
owner: new web3_js_1.PublicKey(args.owner),
tokenAccount: new web3_js_1.PublicKey(args.tokenAccount),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
assetMint: new web3_js_1.PublicKey(args.assetMint),
program: utils_1.assetControllerProgramId,
eventAuthority: (0, utils_1.getAssetControllerEventAuthority)(),
payer: new web3_js_1.PublicKey(args.owner),
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: web3_js_1.SystemProgram.programId,
})
.instruction();
return ix;
});
}
exports.getEnableMemoTransferIx = getEnableMemoTransferIx;
/**
* Generate Instructions to disable memo transfer
* @param args - {@link MemoTranferArgs}
* @returns - {@link TransactionInstruction}
* */
function getDisableMemoTransferIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.disableMemoTransfer()
.accountsStrict({
owner: new web3_js_1.PublicKey(args.owner),
tokenAccount: new web3_js_1.PublicKey(args.tokenAccount),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
program: utils_1.assetControllerProgramId,
eventAuthority: (0, utils_1.getAssetControllerEventAuthority)(),
})
.instruction();
return ix;
});
}
exports.getDisableMemoTransferIx = getDisableMemoTransferIx;
/**
* Generate Instructions to close a mint
* @param args - {@link CloseMintArgs}
* @returns - {@link TransactionInstruction}
*/
function getCloseMintIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.closeMintAccount()
.accountsStrict({
authority: new web3_js_1.PublicKey(args.authority),
assetMint: new web3_js_1.PublicKey(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
})
.instruction();
return ix;
});
}
exports.getCloseMintIx = getCloseMintIx;
/**
* Generate Instructions to freeze token account
* @param args - {@link FreezeTokenArgs}
* @returns - {@link TransactionInstruction}
*/
function getFreezeTokenIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.freezeTokenAccount()
.accountsStrict({
authority: new web3_js_1.PublicKey(args.authority),
assetMint: new web3_js_1.PublicKey(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
tokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.owner), false, spl_token_1.TOKEN_2022_PROGRAM_ID),
})
.instruction();
return ix;
});
}
exports.getFreezeTokenIx = getFreezeTokenIx;
/**
* Generate Instructions to thaw token account
* @param args - {@link FreezeTokenArgs}
* @returns - {@link TransactionInstruction}
* */
function getThawTokenIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const ix = yield assetProgram.methods
.thawTokenAccount()
.accountsStrict({
authority: new web3_js_1.PublicKey(args.authority),
assetMint: new web3_js_1.PublicKey(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
tokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.owner), false, spl_token_1.TOKEN_2022_PROGRAM_ID),
})
.instruction();
return ix;
});
}
exports.getThawTokenIx = getThawTokenIx;
/**
* Revoke tokens from a user
* @param args - {@link RevokeTokensArgs}
* @returns - {@link TransactionInstruction}
* */
function getRevokeTokensIx(args, provider) {
return __awaiter(this, void 0, void 0, function* () {
const assetProgram = (0, utils_1.getAssetControllerProgram)(provider);
const remainingAccounts = [
{
pubkey: (0, policy_engine_1.getPolicyEnginePda)(args.assetMint),
isWritable: false,
isSigner: false,
},
{
pubkey: identity_registry_1.identityRegistryProgramId,
isWritable: false,
isSigner: false,
},
{
pubkey: (0, identity_registry_1.getIdentityRegistryPda)(args.assetMint),
isWritable: false,
isSigner: false,
},
{
pubkey: (0, identity_registry_1.getIdentityAccountPda)(args.assetMint, (0, utils_1.getAssetControllerPda)(args.assetMint).toString()),
isWritable: false,
isSigner: false,
},
{
pubkey: (0, policy_engine_1.getTrackerAccountPda)(args.assetMint, (0, utils_1.getAssetControllerPda)(args.assetMint).toString()),
isWritable: true,
isSigner: false,
},
{
pubkey: (0, policy_engine_1.getPolicyAccountPda)(args.assetMint),
isWritable: true,
isSigner: false,
},
{
pubkey: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
isWritable: false,
isSigner: false,
},
{
pubkey: policy_engine_1.policyEngineProgramId,
isWritable: false,
isSigner: false,
},
{
pubkey: (0, policy_engine_1.getExtraMetasListPda)(args.assetMint),
isWritable: false,
isSigner: false,
},
{
pubkey: (0, identity_registry_1.getIdentityAccountPda)(args.assetMint, args.owner),
isWritable: false,
isSigner: false,
}
];
const ixs = [];
const ix = yield assetProgram.methods
.revokeTokens(new anchor_1.BN(args.amount))
.accountsStrict({
authority: new web3_js_1.PublicKey(args.authority),
assetMint: new web3_js_1.PublicKey(args.assetMint),
tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
assetController: (0, utils_1.getAssetControllerPda)(args.assetMint),
revokeTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), new web3_js_1.PublicKey(args.owner), false, spl_token_1.TOKEN_2022_PROGRAM_ID),
authorityTokenAccount: (0, spl_token_1.getAssociatedTokenAddressSync)(new web3_js_1.PublicKey(args.assetMint), (0, utils_1.getAssetControllerPda)(args.assetMint), true, spl_token_1.TOKEN_2022_PROGRAM_ID),
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: web3_js_1.SystemProgram.programId,
})
.remainingAccounts(remainingAccounts)
.instruction();
ixs.push(ix);
return ixs;
});
}
exports.getRevokeTokensIx = getRevokeTokensIx;
//# sourceMappingURL=instructions.js.map