UNPKG

stellar-plus

Version:

beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain

491 lines (490 loc) 17.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SorobanTokenHandler = void 0; const tslib_1 = require("tslib"); const constants_1 = require("../../../stellar-plus/asset/soroban-token/constants"); const types_1 = require("../../../stellar-plus/asset/types"); const contract_engine_1 = require("../../../stellar-plus/core/contract-engine"); class SorobanTokenHandler extends contract_engine_1.ContractEngine { /** * * @args args * @param {NetworkConfig} args.networkConfig - Network to connect to * @param args.contractParameters - Contract parameters * @param {Spec=} args.contractParameters.spec - Contract specification * @param {string=} args.contractParameters.contractId - Contract ID * @param {Buffer=} args.wasm - Contract WASM file as Buffer * @param {string=} args.wasmHash - Contract WASM hash identifier * @param {Options=} args.options - Contract options * @param {SorobanTransactionPipelineOptions=} args.options.sorobanTransactionPipeline - Soroban transaction pipeline options. Allows for customizing how transaction pipeline will be executed for this contract. * * @description Create a new SorobanTokenHandler instance to interact with a Soroban Token contract. * This class is a subclass of ContractEngine and implements the Soroban token interface. * The contract spec is set to the default Soroban Token spec. When initializing the contract, the spec can be overridden with a custom spec. * The contract ID, WASM file, and WASM hash can be provided to initialize the contract with the given parameters. At least one of these parameters must be provided. * */ constructor(args) { var _a; super(Object.assign(Object.assign({}, args), { contractParameters: Object.assign(Object.assign({}, args.contractParameters), { spec: ((_a = args.contractParameters) === null || _a === void 0 ? void 0 : _a.spec) || constants_1.spec }) })); this.type = types_1.AssetTypes.token; } /** * * @args args * @param {string} args.admin - Admin account public key * @param {u32} args.decimal - Number of decimals * @param {string} args.name - Token name * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Initialize the contract instance with the given parameters. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} */ initialize(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.initialize, methodArgs: { admin: args.admin, decimal: args.decimal, name: args.name, symbol: args.symbol, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } //========================================== // Admin Methods - Require Admin / Issuer //========================================== /** * * @args args * @param {string} args.id - Account public key * @param {string} args.new_admin - New admin account public key * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Set a new admin account for the contract. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} * * @requires {args.id} to be the current admin account */ setAdmin(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.set_admin, methodArgs: { id: args.id, new_admin: args.new_admin, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {EnvelopeHeader} args.header - Transaction envelope header * * @description Get the admin account public key. * * @returns {Promise<string>} * */ admin(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.admin, methodArgs: {}, header: args.header, })); }); } /** * * @args args * @param {string} args.id - Account public key * @param {boolean} args.authorize - Whether to authorize or deauthorize the account * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Authorize or deauthorize an account to interact with the contract. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} * * @requires - Authorization from the admin account */ setAuthorized(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.set_authorized, methodArgs: { id: args.id, authorize: args.authorize, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {string} args.to - Account public key * @param {i128} args.amount - Amount to mint * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Mint tokens to an account. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} * * @requires - Authorization from the admin account */ mint(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.mint, methodArgs: { to: args.to, amount: args.amount, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {string} args.from - Account public key * @param {i128} args.amount - Amount to clawback * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Clawback tokens from an account. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} * * @requires - Authorization from the admin account */ clawback(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.clawback, methodArgs: { from: args.from, amount: args.amount, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } //========================================== // User Methods - Do not require Admin / Issuer //========================================== // // /** * * @args args * @param {string} args.from - Account public key * @param {string} args.spender - Spender account public key * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Get the amount of tokens that the spender is allowed to spend on behalf of the account. * * @returns {Promise<i128>} */ allowance(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.allowance, methodArgs: { from: args.from, spender: args.spender, }, header: args.header, })); }); } /** * * @args args * @param {string} args.from - Account public key * @param {string} args.spender - Spender account public key * @param {i128} args.amount - Amount to approve * @param {u32} args.expiration_ledger - Ledger number until the approval is valid * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Approve a spender to spend tokens on behalf of the account. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} */ approve(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.approve, methodArgs: { from: args.from, spender: args.spender, amount: args.amount, expiration_ledger: args.expiration_ledger, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {string} args.id - Account public key * * @param {EnvelopeHeader} args.header - Transaction envelope header * * @description Get the balance of the account. * * @returns {Promise<i128>} */ balance(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.balance, methodArgs: { id: args.id, }, header: args.header, })); }); } /** * * @args args * @param {string} args.id - Account public key * * @param {EnvelopeHeader} args.header - Transaction envelope header * * @description Get the spendable balance of the account. * * @returns {Promise<i128>} */ spendableBalance(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.spendable_balance, methodArgs: { id: args.id, }, header: args.header, })); }); } /** * * @args args * @param {string} args.from - Sender public key * @param {string} args.to - Recipient account public key * @param {i128} args.amount - Amount to transfer * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Transfer tokens from the sender to the recipient. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} */ transfer(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.transfer, methodArgs: { from: args.from, to: args.to, amount: args.amount, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {string} args.spender - Spender account public key * @param {string} args.from - Sender public key * @param {string} args.to - Recipient account public key * @param {i128} args.amount - Amount to transfer * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Transfer tokens from the sender to the recipient on behalf of the spender. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} */ transferFrom(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.transfer_from, methodArgs: { spender: args.spender, from: args.from, to: args.to, amount: args.amount, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {string} args.from - Account public key * @param {i128} args.amount - Amount to burn * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Burn tokens from an account. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} */ burn(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.burn, methodArgs: { from: args.from, amount: args.amount, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * @param {string} args.spender - Spender account public key * @param {string} args.from - Account public key * @param {i128} args.amount - Amount to burn * * @param {EnvelopeHeader} args.header - Transaction envelope header * @param {AccountHandler[]} args.signers - Transaction signers * @param {FeeBumpHeader=} args.feeBump - Fee bump configuration * * @description Burn tokens from an account on behalf of the spender. * * @returns {Promise<SorobanTransactionPipelineOutputSimple>} */ burnFrom(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.invokeContract({ method: constants_1.methods.burn_from, methodArgs: { spender: args.spender, from: args.from, amount: args.amount, }, signers: args.signers, header: args.header, feeBump: args.feeBump, })); }); } /** * * @args args * * @param {EnvelopeHeader} args.header - Transaction envelope header * * @description Get the number of decimals. * * @returns {Promise<u32>} */ decimals(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.decimals, methodArgs: {}, header: args.header, })); }); } /** * * @args args * * @param {EnvelopeHeader} args.header - Transaction envelope header * * @description Get the token name. * * @returns {Promise<string>} */ name(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.name, methodArgs: {}, header: args.header, })); }); } /** * * @args args * * @param {EnvelopeHeader} args.header - Transaction envelope header * * @description Get the token symbol. * * @returns {Promise<string>} */ symbol(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { return (yield this.readFromContract({ method: constants_1.methods.symbol, methodArgs: {}, header: args.header, })); }); } } exports.SorobanTokenHandler = SorobanTokenHandler;