UNPKG

stellar-plus

Version:

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

180 lines (179 loc) 9.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CertificateOfDepositClient = void 0; const tslib_1 = require("tslib"); const contract_engine_1 = require("../../../../stellar-plus/core/contract-engine"); const constants_1 = require("../../../../stellar-plus/soroban/contracts/certificate-of-deposit/constants"); class CertificateOfDepositClient extends contract_engine_1.ContractEngine { /** * * @param {string} contractId - The contract ID of the deployed Certificate of Deposit to use. * @param {NetworkConfig} networkConfig - The network to use. * @param {RpcHandler} rpcHandler - The RPC handler to use. * * @description - The certificate of deposit client is used for interacting with the certificate of deposit contract. * */ constructor(args) { const contractSpec = args.contractParameters.spec || constants_1.spec; const contractParameters = Object.assign(Object.assign({}, args.contractParameters), { spec: contractSpec }); super(Object.assign(Object.assign({}, args), { contractParameters })); this.methods = constants_1.Methods; } /** * @args {DepositArgs} args - The arguments to pass to the deposit method. * @param {string} args.address - The address to deposit from. * @param {number} args.amount - The amount to deposit. * @param {string[]} args.signers - The signers to authorize this transaction. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @param {SorobanFeeBumpTransaction=} args.feeBump - The fee bump to use for this transaction. This is optional. * * @returns {void} * @description - Performs a deposit to the certificate of deposit contract, opening a position. * * */ deposit(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const amount = args.amount; const address = args.address; yield this.invokeContract(Object.assign({ method: this.methods.deposit, methodArgs: { amount, address } }, args)); }); } /** * @args {WithdrawArgs} args - The arguments to pass to the withdraw method. * @param {string} args.address - The address of the account withdrawing. * @param {boolean} args.acceptPrematureWithdraw - Whether to accept premature withdraw or not. When true, the withdraw will be accepted even if the time left is greater than 0 and the contract penalty will be applied to the amount withdrawn. When false, the withdraw will only be accepted if the time left is 0. * @param {string[]} args.signers - The signers to authorize this transaction. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @param {SorobanFeeBumpTransaction=} args.feeBump - The fee bump to use for this transaction. This is optional. * * @returns {void} * @description - Performs a withdraw from the certificate of deposit contract, closing a position. * * */ withdraw(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const address = args.address; const accept_premature_withdraw = args.acceptPrematureWithdraw; yield this.invokeContract({ method: this.methods.withdraw, methodArgs: { address, accept_premature_withdraw }, signers: args.signers, header: args.header, feeBump: args.feeBump, }); }); } /** * @args {GetEstimatedYieldArgs} args - The arguments to pass to the getEstimatedYield method. * @param {string} args.address - The address of the account to get the estimated yield for. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @returns {number} The estimated yield for the account. * @description - Gets the current estimated yield accrued for the account's position so far. */ getEstimatedYield(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const address = args.address; const result = (yield this.readFromContract({ method: this.methods.getEstimatedYield, methodArgs: { address }, header: args.header, })); return Number(result); }); } /** * @args {GetPositionArgs} args - The arguments to pass to the getPosition method. * @param {string} args.address - The address of the account to get the position for. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @returns {number} The position for the account. Includes the original deposit plus the accrued yield. * @description - Gets the current open position for the account. */ getPosition(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const address = args.address; const result = (yield this.readFromContract({ method: this.methods.getPosition, methodArgs: { address }, header: args.header, })); return Number(result); }); } /** * @args {GetEstimatedPrematureWithdrawArgs} args - The arguments to pass to the getEstimatedPrematureWithdraw method. * @param {string} args.address - The address of the account to get the estimated premature withdraw for. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @returns {number} The estimated premature withdraw for the account. * @description - Gets the current estimated premature withdraw for the account. This is the amount that will be received if the account withdraws prematurely, with the penalty applied. */ getEstimatedPrematureWithdraw(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const address = args.address; const result = (yield this.readFromContract({ method: this.methods.getEstimatedPrematureWithdraw, methodArgs: { address }, header: args.header, })); return Number(result); }); } /** * @args {GetTimeLeftArgs} args - The arguments to pass to the getTimeLeft method. * @param {string} args.address - The address of the account to get the time left for. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @returns {number} The time left for the account's position to reach the term. * @description - Gets the current time left for the account. This is the time left until the account can withdraw without penalty. */ getTimeLeft(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const address = args.address; const result = (yield this.readFromContract({ method: this.methods.getTimeLeft, methodArgs: { address }, header: args.header, })); return Number(result); }); } /** * * @param args - The arguments to pass to the initialize method. * @param {string} args.admin - The admin address to set for the contract. * @param {string} args.asset - The asset contract ID to set for the contract. * @param {number} args.term - The term in seconds to set for the contract. * @param {number} args.compoundStep - The compound step in seconds to set for the contract. * @param {number} args.yieldRate - The yield rate in percentage to set for the contract. 1% = 100. Example: 10% = 1000 * @param {number} args.minDeposit - The minimum deposit in stroops to set for the contract. * @param {number} args.penaltyRate - The penalty rate in percentage to set for the contract. 1% = 100. Example: 10% = 1000. This is the penalty applied to the yield in the amount withdrawn if the account withdraws prematurely before the term is reached. * @param {number} args.allowancePeriod - The expiration ledger to set for the contract. This is the final ledger for which the contract will be allowed to access to the funds of its admin to perform withdrawals. * @param {string[]} args.signers - The signers to authorize this transaction. * @param {EnvelopeHeader} args.header - The header to use for this transaction. * @param {SorobanFeeBumpTransaction=} args.feeBump - The fee bump to use for this transaction. This is optional. * * @returns {void} * * @description - Initializes the contract's state. * */ initialize(args) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const { term, compoundStep, yieldRate, minDeposit, penaltyRate } = args; const admin = args.admin; const asset = args.asset; yield this.invokeContract(Object.assign({ method: this.methods.initialize, methodArgs: { admin, asset, term: term, compound_step: compoundStep, yield_rate: yieldRate, min_deposit: minDeposit, penalty_rate: penaltyRate, allowance_period: args.allowancePeriod, } }, args)); }); } } exports.CertificateOfDepositClient = CertificateOfDepositClient;