How to use the @liskhq/lisk-transactions.utils.BigNum function in @liskhq/lisk-transactions

To help you get started, we’ve selected a few @liskhq/lisk-transactions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github LiskHQ / lisk-sdk-examples / transport / transactions / solutions / register-packet.js View on Github external
store.account.set(sender.address, updatedSender);

            /* --- Modify packet account --- */
            /**
             * Update the packet account:
             * - Add the postage to the packet account balance
             * - Add all important data about the packet inside the asset field:
             *   - recipient: ID of the packet recipient
             *   - sender: ID of the packet sender
             *   - carrier: ID of the packet carrier
             *   - security: Number of tokens the carrier needs to lock during the transport of the packet
             *   - postage: Number of tokens the sender needs to pay for transportation of the packet
             *   - minTrust: Minimal trust that is needed to be carrier for the packet
             *   - status: Status of the transport (pending|ongoing|success|fail)
             */
            const packetBalanceWithPostage = new utils.BigNum(packet.balance).add(
                new utils.BigNum(this.asset.postage)
            );

            const updatedPacketAccount = {
                ...packet,
                ...{
                    balance: packetBalanceWithPostage.toString(),
                    asset: {
                        recipient: this.recipientId,
                        sender: this.senderId,
                        security: this.asset.security,
                        postage: this.asset.postage,
                        minTrust: this.asset.minTrust,
                        status: 'pending',
                        carrier: null
                    }
github LiskHQ / lisk-sdk-examples / transport / transactions / finish-transport.js View on Github external
undoAsset(store) {
        const errors = [];
        const packet = store.account.get(this.recipientId);
        const carrier = store.account.get(packet.carrier);
        const sender = store.account.get(packet.sender);
        /* --- Revert successful transport --- */
        if ( this.asset.status === "success") {
            /* --- Revert carrier account --- */
            const carrierBalanceWithoutSecurityAndPostage = new utils.BigNum(carrier.balance).sub(new utils.BigNum(packet.asset.security)).sub(new utils.BigNum(packet.asset.postage));

            carrier.balance = carrierBalanceWithoutSecurityAndPostage.toString();
            carrier.asset.lockedSecurity = packet.asset.security;
            carrier.asset.trust--;

            store.account.set(carrier.address, carrier);

        /* --- Revert failed transport --- */
        } else {
            /* --- Revert sender account --- */
            const senderBalanceWithoutSecurityAndPostage = new utils.BigNum(sender.balance).sub(new utils.BigNum(packet.asset.security)).sub(new utils.BigNum(packet.asset.postage));
            sender.balance = senderBalanceWithoutSecurityAndPostage.toString();
            store.account.set(sender.address, sender);
            /* --- Revert carrier account --- */
            carrier.asset.trust++;
            carrier.asset.lockedSecurity = packet.asset.security;
github LiskHQ / lisk-sdk-examples / transport / transactions / solutions / register-packet.js View on Github external
applyAsset(store) {
        const errors = [];
        const packet = store.account.get(this.asset.packetId);

        if (!packet.asset.status) {
            /* --- Modify sender account --- */
            /**
             * Update the sender account:
             * - Deduct the postage from senders' account balance
             */
            const sender = store.account.get(this.senderId);
            const senderBalancePostageDeducted = new utils.BigNum(sender.balance).sub(
                new utils.BigNum(this.asset.postage)
            );
            const updatedSender = {
                ...sender,
                balance: senderBalancePostageDeducted.toString(),
            };
            store.account.set(sender.address, updatedSender);

            /* --- Modify packet account --- */
            /**
             * Update the packet account:
             * - Add the postage to the packet account balance
             * - Add all important data about the packet inside the asset field:
             *   - recipient: ID of the packet recipient
             *   - sender: ID of the packet sender
             *   - carrier: ID of the packet carrier
             *   - security: Number of tokens the carrier needs to lock during the transport of the packet
github LiskHQ / lisk-sdk-examples / transport / transactions / start-transport.js View on Github external
applyAsset(store) {
        const errors = [];
        const packet = store.account.get(this.recipientId);
        if (packet.asset.status === "pending"){
            const carrier = store.account.get(this.senderId);
            // If the carrier has the trust to transport the packet
            const carrierTrust = carrier.asset.trust ? carrier.asset.trust : 0;
            const carrierBalance = new utils.BigNum(carrier.balance);
            const packetSecurity = new utils.BigNum(packet.asset.security);
            if (packet.asset.minTrust <= carrierTrust && carrierBalance.gte(packetSecurity)) {
                /**
                 * Update the Carrier account:
                 * - Lock security inside the account
                 * - Remove the security form balance
                 * - initialize carriertrust, if not present already
                 */
                const carrierBalanceWithoutSecurity = carrierBalance.sub(packetSecurity);
                const carrierTrust = carrier.asset.trust ? carrier.asset.trust : 0;
                const updatedCarrier = { /* Write your code here */ };
                store.account.set(carrier.address, updatedCarrier);
                /**
                 * Update the Packet account:
                 * - Set status to "ongoing"
                 * - set carrier to ID of the carrier
                 */
github LiskHQ / lisk-sdk-examples / transport / transactions / finish-transport.js View on Github external
*/
                /* Write your own code here */
                /**
                 * Update the Packet account:
                 * - Remove postage from balance
                 * - Change status to "success"
                 */
                /* Write your own code here */
                return errors;
            }
            // if the transport failed
            /**
             * Update the Sender account:
             * - Add postage and security to balance
             */
            const senderBalanceWithSecurityAndPostage = new utils.BigNum(sender.balance).add(new utils.BigNum(packet.asset.security)).add(new utils.BigNum(packet.asset.postage));

            sender.balance = senderBalanceWithSecurityAndPostage.toString();

            store.account.set(sender.address, sender);
            /**
             * Update the Carrier account:
             * - Reduce trust by 1
             * - Set lockedSecurity to 0
             */
            carrier.asset.trust = carrier.asset.trust ? --carrier.asset.trust : -1;
            carrier.asset.lockedSecurity = null;

            store.account.set(carrier.address, carrier);
            /**
             * Update the Packet account:
             * - set status to "fail"
github LiskHQ / lisk-sdk-examples / transport / transactions / register-packet.js View on Github external
applyAsset(store) {
        const errors = [];
        const packet = store.account.get(this.asset.packetId);

        if (!packet.asset.status) {
            /* --- Modify sender account --- */
            /**
             * Update the sender account:
             * - Deduct the postage from senders' account balance
             */
            const sender = store.account.get(this.senderId);
            const senderBalancePostageDeducted = new utils.BigNum(sender.balance).sub(
                new utils.BigNum(this.asset.postage)
            );
            const updatedSender = {
                ...sender,
                balance: senderBalancePostageDeducted.toString(),
            };
            store.account.set(sender.address, updatedSender);

            /* --- Modify packet account --- */
            /**
             * Update the packet account:
             * - Add the postage to the packet account balance
             * - Add all important data about the packet inside the asset field:
             *   - recipient: ID of the packet recipient
             *   - sender: ID of the packet sender
             *   - carrier: ID of the packet carrier
github LiskHQ / lisk-sdk-examples / transport / transactions / register-packet.js View on Github external
undoAsset(store) {
        const errors = [];

        /* --- Revert sender account --- */
        const sender = store.account.get(this.senderId);
        const senderBalanceWithPostage = new utils.BigNum(sender.balance).add(
            new utils.BigNum(this.asset.postage)
        );
        const updatedSender = {
            ...sender,
            balance: senderBalanceWithPostage.toString()
        };
        store.account.set(sender.address, updatedSender);

        /* --- Revert packet account --- */
        const packet = store.account.get(this.asset.packetId);
        const originalPacketAccount = /* Task: The missing UndoAsset logic comes here */
        store.account.set(packet.address, originalPacketAccount);

        return errors;
    }
github LiskHQ / lisk-sdk-examples / delivery / transactions / register-packet.js View on Github external
applyAsset(store) {
        const errors = [];
        const packet = store.account.get(this.asset.packetId);
        const owner = store.account.get(this.senderId);

        const ownerBalanceWithoutPorto = new utils.BigNum(owner.balance).sub(
            new utils.BigNum(this.asset.porto)
        );
        const packetBalanceWithPorto = new utils.BigNum(packet.balance).add(
            new utils.BigNum(this.asset.porto)
        );
        const updatedOwner = {
            ...owner,
            balance: ownerBalanceWithoutPorto.toString()
        };
        store.account.set(owner.address, updatedOwner);
        const newObj = {
            ...packet,
            balance : packetBalanceWithPorto.toString(),
            asset: {
                receipientId: this.asset.receipientId,
                receipientLocation: this.asset.receipientLocation,
                ownerId: this.senderId,
                ownerLocation: this.asset.senderLocation,
                security: this.asset.security,
github LiskHQ / lisk-sdk-examples / delivery / transactions / register-packet.js View on Github external
undoAsset(store) {
        const packet = store.account.get(this.asset.packetId);
        const oldObj = { ...packet, balance: 0, asset: null };
        store.account.set(packet.address, oldObj);
        const owner = store.account.get(this.senderId);
        const ownerBalanceWithPorto = new utils.BigNum(owner.balance).add(
            new utils.BigNum(this.asset.porto)
        );
        const updatedOwner = {
            ...owner,
            balance: ownerBalanceWithPorto.toString()
        };
        store.account.set(owner.address, updatedOwner);
        return [];
    }
github LiskHQ / lisk-sdk-examples / delivery / transactions / register-packet.js View on Github external
applyAsset(store) {
        const errors = [];
        const packet = store.account.get(this.asset.packetId);
        const owner = store.account.get(this.senderId);

        const ownerBalanceWithoutPorto = new utils.BigNum(owner.balance).sub(
            new utils.BigNum(this.asset.porto)
        );
        const packetBalanceWithPorto = new utils.BigNum(packet.balance).add(
            new utils.BigNum(this.asset.porto)
        );
        const updatedOwner = {
            ...owner,
            balance: ownerBalanceWithoutPorto.toString()
        };
        store.account.set(owner.address, updatedOwner);
        const newObj = {
            ...packet,
            balance : packetBalanceWithPorto.toString(),
            asset: {
                receipientId: this.asset.receipientId,
                receipientLocation: this.asset.receipientLocation,