UNPKG

@logosnetwork/logos-webwallet-sdk

Version:

Create Logos wallets with or without a full Logos node

603 lines (602 loc) 22.8 kB
import Account, { AccountOptions, AccountJSON } from './Account'; import { Request as RpcRequest, Transaction } from '@logosnetwork/logos-rpc-client/api'; import { Request } from './Requests'; import { IssuanceOptions } from './Requests/Issuance'; import { IssueAdditionalJSON } from './Requests/IssueAdditional'; import { ChangeSettingOptions } from './Requests/ChangeSetting'; import { ImmuteSettingOptions } from './Requests/ImmuteSetting'; import { RevokeOptions } from './Requests/Revoke'; import { AdjustUserStatusOptions } from './Requests/AdjustUserStatus'; import { AdjustFeeOptions } from './Requests/AdjustFee'; import { UpdateIssuerInfoOptions } from './Requests/UpdateIssuerInfo'; import { UpdateControllerOptions } from './Requests/UpdateController'; import { BurnOptions } from './Requests/Burn'; import { DistributeOptions } from './Requests/Distribute'; import { WithdrawFeeOptions } from './Requests/WithdrawFee'; import { WithdrawLogosOptions } from './Requests/WithdrawLogos'; import TokenAccount from './TokenAccount'; export interface LogosAccountOptions extends AccountOptions { index?: number; privateKey?: string; tokens?: string[]; tokenBalances?: TokenBalances; pendingTokenBalances?: TokenBalances; } export interface LogosAccountJSON extends AccountJSON { privateKey?: string; tokenBalances?: TokenBalances; tokens?: string[]; type?: string; index?: number; } interface TokenBalances { [tokenID: string]: string; } export interface SyncedResponse { account?: string; synced?: boolean; type?: string; remove?: boolean; } /** * ## Logos Account * This class is the base class of an account on the Logos Network. * The most common uses for this account is to check the balance, history, and create new requests from this account as the origin. */ export default class LogosAccount extends Account { private _index; private _privateKey; private _tokens; private _tokenBalances; private _pendingTokenBalances; /** * ### Instantiating * ```typescript * const LogosAccount = new LogosAccount({ * label: null, * address: null, * publicKey: null, * balance: '0', * pendingBalance: '0', * wallet: null, * chain: [], * receiveChain: [], * pendingChain: [], * privateKey: null * tokenBalances: {}, * tokens: [], * pendingTokenBalances: {}, * index: null * }) * ``` * * All logos account options are optional defaults are shown in the example above * * |Account Options| Description | * |--|--| * | label | Account label e.g. Checking Account | * | address | Address is the lgs_ string | * | publicKey | Public key of the account | * | balance | Balance of the account in the minor unit of Logos | * | pendingBalance | balance of the account including pending transaction in the minor unit of Logos | * | wallet | reference back to the parent wallet class | * | chain | Array of [[Request]] that are confirmed on this account's send chain | * | receiveChain | Array of [[Request]] that are confirmed on this account's receive chain | * | pendingChain | Array of [[Request]] that are *not* confirmed on this account's send chain | * | [[privateKey]] | Private key of the account used to sign transactions | * | [[tokenBalances]] | Balances tokens that this account has in their | * | [[tokens]] | Array of token addresses associated with this account | * | [[pendingTokenBalances]] | Unconfirmed balances of the tokens | * | [[index]] | index of the account | */ constructor(options?: LogosAccountOptions); /** * The type of the account (LogosAccount or TokenAccount) * #### Example * ```typescript * const type = logosAccount.type * ``` */ readonly type: 'LogosAccount'; /** * The index of the account * #### Example * ```typescript * const index = logosAccount.index * ``` */ readonly index: number; /** * The private key of the account * #### Example * ```typescript * const privateKey = logosAccount.privateKey * ``` */ readonly privateKey: string; /** * Array of associated token ids to this account (full list available only with fullsync) * #### Example * ```typescript * const tokens = logosAccount.tokens * ``` */ readonly tokens: string[]; /** * The balance of the tokens in the minor token unit * #### Example * ```typescript * const tokenBalances = logosAccount.tokenBalances * ``` */ readonly tokenBalances: TokenBalances; /** * The pending token balance of the account in the minor token unit * * pending token balance is balance minus the token sends that are pending * * #### Example * ```typescript * const pendingTokenBalances = logosAccount.pendingTokenBalances * ``` */ readonly pendingTokenBalances: TokenBalances; /** * The balance of the given token in the minor unit and major unit (if available) * @param {string} tokenID - Token ID of the token in question, you can also send the token account address * @returns {{minor: string;major?: string}} The balance in minor unit or converted units * #### Example * ```typescript * const tokenBalance = logosAccount.tokenBalance('lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd') * ``` */ tokenBalance(token: string): { minor: string; major?: string; }; /** * Adds a token to the accounts associated tokens if it doesn't already exist * * @param {string} tokenID - The TokenID you are associating with this account (this will be converted into a token account when stored) * @returns {string[]} Array of all the associated tokens * #### Example * ```typescript * const token = await logosAccount.addToken('lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd') * ``` */ addToken(tokenID: string): Promise<string[]>; /** * Checks if the account is synced * @returns {Promise<SyncedResponse>} * #### Example * ```typescript * const isSynced = await logosAccount.isSynced() * ``` */ isSynced(): Promise<SyncedResponse>; /** * Scans the account history using RPC and updates the local chain * * @returns {Promise<Account>} * #### Example * ```typescript * const isSynced = await logosAccount.sync() * ``` */ sync(): Promise<Account>; /** * Updates the balances of the account by traversing the chain * @returns {void} * #### Example * ```typescript * logosAccount.updateBalancesFromChain() * ``` */ updateBalancesFromChain(): void; /** * Updates the balances of the account by doing math on the previous balance when given a new request * Also updates the pending balance based on the new balance and the pending chain * @param {Request} request - request that is being calculated on * #### Example * ```typescript * logosAccount.updateBalancesFromRequest() * ``` */ updateBalancesFromRequest(request: Request): void; /** * Creates a request object from the mqtt info and adds the request to the appropriate chain * * @param {RequestOptions} requestInfo - Request information from the RPC or MQTT * #### Example * ```typescript * logosAccount.addConfirmedRequest([[RpcRequest]]) * ``` */ addConfirmedRequest(requestInfo: RpcRequest): Promise<Request>; /** * Removes all pending requests from the pending chain * #### Example * ```typescript * logosAccount.removePendingRequests() * ``` */ removePendingRequests(): void; /** * Validates that the account has enough funds at the current time to publish the request * * @param {Request} request - Request Class * #### Example * ```typescript * await logosAccount.validateRequest(REQUEST) * ``` */ validateRequest(request: Request): Promise<boolean>; /** * Adds the request to the pending chain and publishes it * * @param {Request} request - Request information from the RPC or MQTT * @throws An exception if the pending balance is less than the required amount to adjust a users status * #### Example * ```typescript * const request = await logosAccount.addRequest(REQUEST) * ``` */ addRequest(request: Request): Promise<Request>; /** * Creates a request from the specified information * * @param {Transaction[]} transactions - The account destinations and amounts you wish to send them * @throws An exception if the account has not been synced * @throws An exception if the pending balance is less than the required amount to do a send * @throws An exception if the request is rejected by the RPC * #### Example * ```typescript * const request = await logosAccount.createSendRequest([ * { * destination: 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo', * amount: '1' * } * ]) * ``` */ createSendRequest(transactions: Transaction[]): Promise<Request>; /** * Creates a request from the specified information * * @param {TokenIssuanceOptions} options - The options for the token creation * @throws An exception if the account has not been synced * @throws An exception if the pending balance is less than the required amount to do a token issuance * @throws An exception if the request is rejected by the RPC * #### Example * ```typescript * const request = await logosAccount.createTokenIssuanceRequest( * { * name: `UnitTestCoin`, * symbol: `UTC`, * totalSupply: '1000', * feeRate: '1', * issuerInfo: '{"decimals":0,"website":"https://github.com/LogosNetwork/logos-webwallet-sdk"}', * settings: { * issuance: true, * modify_issuance: true, * revoke: true, * modify_revoke: true, * freeze: true, * modify_freeze: true, * adjust_fee: true, * modify_adjust_fee: true, * whitelist: false, * modify_whitelist: true * }, * controllers: [{ * account: 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo', * privileges: { * change_issuance: true, * change_modify_issuance: true, * change_revoke: true, * change_modify_revoke: true, * change_freeze: true, * change_modify_freeze: true, * change_adjust_fee: true, * change_modify_adjust_fee: true, * change_whitelist: true, * change_modify_whitelist: true, * issuance: true, * revoke: true, * freeze: true, * adjust_fee: true, * whitelist: true, * update_issuer_info: true, * update_controller: true, * burn: true, * distribute: true, * withdraw_fee: true, * withdraw_logos: true * } * }] * } * ) * ``` */ createTokenIssuanceRequest(options: IssuanceOptions): Promise<Request>; /** * Gets tokenAccount * * @param {TokenRequest} options - Object contained the tokenID or tokenAccount * @throws An exception if no tokenID or tokenAccount * #### Example * ```typescript * const request = await logosAccount.getTokenAccount('lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd') * ``` */ getTokenAccount(token: string | { token_id?: string; tokenID?: string; tokenAccount?: string; token_account?: string; }): Promise<TokenAccount>; /** * Creates a request from the specified information * * @param {string} token - The token address or token id * @param {Transaction} transactions - The account destinations and amounts you wish to send them * @throws An exception if the account has not been synced * @throws An exception if the pending balance is less than the required amount to do a send * @throws An exception if the request is rejected by the RPC * #### Example * ```typescript * const request = await logosAccount.createTokenSendRequest('lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', [{ * destination: 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo', * amount: '1' * }]) * ``` */ createTokenSendRequest(token: string, transactions: Transaction[]): Promise<Request>; /** * Creates a IssueAdditional Token Request from the specified information * * @param {IssueAdditionalOptions} options - The Token ID & amount * @throws An exception if the token account balance is less than the required amount to do a issue additional token request * #### Example * ```typescript * const request = await logosAccount.createIssueAdditionalRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * amount: '1' * }) * ``` */ createIssueAdditionalRequest(options: IssueAdditionalJSON): Promise<Request>; /** * Creates a ChangeSetting Token Request from the specified information * * @param {ChangeSettingOptions} options - Token ID, setting, value * @throws An exception if the token account balance is less than the required amount to do a change setting token request * #### Example * ```typescript * const request = await logosAccount.createChangeSettingRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * setting: 'issuance', * value: true * }) * ``` */ createChangeSettingRequest(options: ChangeSettingOptions): Promise<Request>; /** * Creates a ImmuteSetting Token Request from the specified information * * @param {ImmuteSettingOptions} options - Token ID, setting * @throws An exception if the token account balance is less than the required amount to do a immute setting token request * #### Example * ```typescript * const request = await logosAccount.createImmuteSettingRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * setting: 'issuance' * }) * ``` */ createImmuteSettingRequest(options: ImmuteSettingOptions): Promise<Request>; /** * Creates a Revoke Token Request from the specified information * * @param {RevokeOptions} options - Token ID, transaction, source * @throws An exception if the token account balance is less than the required amount to do a Revoke token request * #### Example * ```typescript * const request = await logosAccount.createRevokeRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * source: 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo', * transaction: { * amount: '1', * destination: 'lgs_3mjbkiwijkbt3aqz8kzm5nmsfhtrbjwkmnyeqi1aoscc46t4xdnfdaunerr6' * } * }) * ``` */ createRevokeRequest(options: RevokeOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {AdjustUserStatusOptions} options - The Token ID, account, and status * @throws An exception if the pending balance is less than the required amount to adjust a users status * #### Example * ```typescript * const request = await logosAccount.createAdjustUserStatusRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * account: 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo', * status: 'frozen' * }) * ``` */ createAdjustUserStatusRequest(options: AdjustUserStatusOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {AdjustFeeOptions} options - The Token ID, feeRate, and feeType * @throws An exception if the pending balance is less than the required amount to do a token distibution * #### Example * ```typescript * const request = await logosAccount.createAdjustFeeRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * feeType: 'flat', * feeRate: '0' * }) * ``` */ createAdjustFeeRequest(options: AdjustFeeOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {UpdateIssuerInfoOptions} options - The Token ID and issuerInfo * @throws An exception if the pending balance is less than the required amount to Update Issuer Info * #### Example * ```typescript * const request = await logosAccount.createUpdateIssuerInfoRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * issuerInfo: '{"decimals":0,"website":"https://github.com/LogosNetwork/logos-webwallet-sdk"}' * }) * ``` */ createUpdateIssuerInfoRequest(options: UpdateIssuerInfoOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {UpdateControllerOptions} options - The Token ID, action ('add' or 'remove'), and controller * @throws An exception if the pending balance is less than the required amount to Update Controller * #### Example * ```typescript * const request = await logosAccount.createUpdateControllerRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * action: 'add', * controller: { * account: 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo', * privileges: { * change_issuance: true, * change_modify_issuance: true, * change_revoke: true, * change_modify_revoke: true, * change_freeze: true, * change_modify_freeze: true, * change_adjust_fee: true, * change_modify_adjust_fee: true, * change_whitelist: true, * change_modify_whitelist: true, * issuance: true, * revoke: true, * freeze: true, * adjust_fee: true, * whitelist: true, * update_issuer_info: true, * update_controller: true, * burn: true, * distribute: true, * withdraw_fee: true, * withdraw_logos: true * } * } * }) * ``` */ createUpdateControllerRequest(options: UpdateControllerOptions): Promise<Request>; /** * Creates a Burn Token Request from the specified information * * @param {BurnOptions} options - The Token ID & amount * @throws An exception if the token account balance is less than the required amount to do a burn token request * #### Example * ```typescript * const request = await logosAccount.createBurnRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * amount: '1' * }) * ``` */ createBurnRequest(options: BurnOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {TokenDistributeOptions} options - The Token ID & transaction * @throws An exception if the pending balance is less than the required amount to do a token distibution * #### Example * ```typescript * const request = await logosAccount.createDistributeRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * transaction: { * amount: '1', * destination: 'lgs_3mjbkiwijkbt3aqz8kzm5nmsfhtrbjwkmnyeqi1aoscc46t4xdnfdaunerr6' * } * }) * ``` */ createDistributeRequest(options: DistributeOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {WithdrawFeeOptions} options - The Token ID & transaction * @throws An exception if the pending balance is less than the required amount to do a withdraw fee request * #### Example * ```typescript * const request = await logosAccount.createWithdrawFeeRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * transaction: { * amount: '1', * destination: 'lgs_3mjbkiwijkbt3aqz8kzm5nmsfhtrbjwkmnyeqi1aoscc46t4xdnfdaunerr6' * } * }) * ``` */ createWithdrawFeeRequest(options: WithdrawFeeOptions): Promise<Request>; /** * Creates a request from the specified information * * @param {WithdrawLogosOptions} options - The Token ID & transaction * @throws An exception if the pending balance is less than the required amount to do a withdraw logos request * #### Example * ```typescript * const request = await logosAccount.createWithdrawLogosRequest({ * tokenAccount: 'lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd', * transaction: { * amount: '1', * destination: 'lgs_3mjbkiwijkbt3aqz8kzm5nmsfhtrbjwkmnyeqi1aoscc46t4xdnfdaunerr6' * } * }) * ``` */ createWithdrawLogosRequest(options: WithdrawLogosOptions): Promise<Request>; /** * Confirms the request in the local chain * * @param {MQTTRequestOptions} requestInfo The request from MQTT * #### Example * ```typescript * await logosAccount.processRequest( * RpcRequest * ) * ``` */ processRequest(requestInfo: RpcRequest): Promise<void>; /** * Determines if you shold combine requests * * Returns true if the pending chain has x sends and * the count of total transactions is <= (x-minimumSaved) * 8 * * @param {number} minimumSaved The minimum amount of requests saved in order to combine defaults to 1 * @returns {boolean} */ private shouldCombine; /** * Batchs send requests * * @returns {Promise<void>} */ private combineRequests; /** * Returns the logos account JSON * #### Example * ```typescript * const logosAccountJSON = await logosAccount.toJSON() * ``` */ toJSON(): LogosAccountJSON; } export {};