@goequitize/rwa-token-sdk
Version:
SDK for creating and managing RWA token transactions with compliance features
137 lines (136 loc) • 5.49 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenModule = void 0;
const ethers_1 = require("ethers");
// Import the JSON file and define its type
const RWATokenABIJson = __importStar(require("../utils/abi/RWATokenABI.json"));
// Use type assertion to access the ABI
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const RWATokenABI = RWATokenABIJson;
const tokenAbi = RWATokenABI.abi;
/**
* Module for handling token-related operations
*/
class TokenModule {
/**
* Create a new instance of the TokenModule
* @param provider The JSON-RPC provider instance
* @param chainId The chain ID of the connected network
*/
constructor(provider, chainId) {
this.provider = provider;
this.chainId = chainId;
}
/**
* Create an unsigned transaction for minting tokens
* @param params The parameters for the mint operation
* @returns An unsigned transaction object that can be sent to a wallet for signing
*/
async createMintTransaction(params) {
const tokenContract = new ethers_1.ethers.Contract(params.contractAddress, tokenAbi, this.provider);
// Encode the mint function call
// Note: Some tokens use mint(), others use _mint() - adjust based on your token contract
const data = tokenContract.interface.encodeFunctionData('mint', [
params.recipient,
params.amount
]);
return {
to: params.contractAddress,
data,
value: '0', // No ETH is sent with this transaction
};
}
/**
* Create an unsigned transaction for burning tokens
* @param params The parameters for the burn operation
* @returns An unsigned transaction object that can be sent to a wallet for signing
*/
async createBurnTransaction(params) {
const tokenContract = new ethers_1.ethers.Contract(params.contractAddress, tokenAbi, this.provider);
// Encode the burn function call
const data = tokenContract.interface.encodeFunctionData('burn', [
params.amount
]);
return {
to: params.contractAddress,
data,
value: '0', // No ETH is sent with this transaction
};
}
/**
* Create an unsigned transaction for approving another address to spend tokens
* @param contractAddress The address of the token contract
* @param spender The address being approved to spend tokens
* @param amount The amount of tokens to approve
* @returns An unsigned transaction object that can be sent to a wallet for signing
*/
async createApproveTransaction(contractAddress, spender, amount) {
const tokenContract = new ethers_1.ethers.Contract(contractAddress, tokenAbi, this.provider);
// Encode the approve function call
const data = tokenContract.interface.encodeFunctionData('approve', [
spender,
amount
]);
return {
to: contractAddress,
data,
value: '0', // No ETH is sent with this transaction
};
}
// TODO: Add a function to validate a transfer
//check if the recipient is whitelisted
/**
* Create an unsigned transaction for transferring tokens
* @param contractAddress The address of the token contract
* @param recipient The address to receive the tokens
* @param amount The amount of tokens to transfer
* @returns An unsigned transaction object that can be sent to a wallet for signing
*/
async createTransferTransaction(contractAddress, recipient, amount) {
const tokenContract = new ethers_1.ethers.Contract(contractAddress, tokenAbi, this.provider);
// Encode the transfer function call
const data = tokenContract.interface.encodeFunctionData('transfer', [
recipient,
amount
]);
return {
to: contractAddress,
data,
value: '0', // No ETH is sent with this transaction
};
}
}
exports.TokenModule = TokenModule;