PHP WebShell

Текущая директория: /usr/lib/node_modules/bitgo/node_modules/xrpl/dist/npm/snippets/src

Просмотр файла: bridgeTransfer.js

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const src_1 = require("../../src");
function sleep(sec) {
    return __awaiter(this, void 0, void 0, function* () {
        return new Promise((resolve) => {
            setTimeout(resolve, sec * 1000);
        });
    });
}
const lockingClient = new src_1.Client('wss://s.devnet.rippletest.net:51233');
const issuingClient = new src_1.Client('wss://sidechain-net2.devnet.rippletest.net:51233');
const MAX_LEDGERS_WAITED = 5;
const LEDGER_CLOSE_TIME = 4;
void bridgeTransfer();
function bridgeTransfer() {
    return __awaiter(this, void 0, void 0, function* () {
        yield lockingClient.connect();
        yield issuingClient.connect();
        const lockingChainDoor = 'rnQAXXWoFNN6PEqwqsdTngCtFPCrmfuqFJ';
        const accountObjectsRequest = {
            command: 'account_objects',
            account: lockingChainDoor,
            type: 'bridge',
        };
        const lockingAccountObjects = (yield lockingClient.request(accountObjectsRequest)).result.account_objects;
        const bridgeData = lockingAccountObjects.filter((obj) => obj.LedgerEntryType === 'Bridge' &&
            obj.XChainBridge.LockingChainIssue.currency === 'XRP')[0];
        const bridge = bridgeData.XChainBridge;
        console.log(bridge);
        console.log('Creating wallet on the locking chain via the faucet...');
        const { wallet: wallet1 } = yield lockingClient.fundWallet();
        console.log(wallet1);
        const wallet2 = src_1.Wallet.generate();
        console.log(`Creating ${wallet2.classicAddress} on the issuing chain via the bridge...`);
        const fundTx = {
            TransactionType: 'XChainAccountCreateCommit',
            Account: wallet1.classicAddress,
            XChainBridge: bridge,
            SignatureReward: bridgeData.SignatureReward,
            Destination: wallet2.classicAddress,
            Amount: (parseInt(bridgeData.MinAccountCreateAmount, 10) * 2).toString(),
        };
        const fundResponse = yield lockingClient.submitAndWait(fundTx, {
            wallet: wallet1,
        });
        console.log(fundResponse);
        console.log('Waiting for the attestation to go through... (usually 8-12 seconds)');
        let ledgersWaited = 0;
        let initialBalance = 0;
        while (ledgersWaited < MAX_LEDGERS_WAITED) {
            yield sleep(LEDGER_CLOSE_TIME);
            try {
                initialBalance = yield issuingClient.getXrpBalance(wallet2.classicAddress);
                console.log(`Wallet ${wallet2.classicAddress} has been funded with a balance of ${initialBalance} XRP`);
                break;
            }
            catch (_error) {
                ledgersWaited += 1;
                if (ledgersWaited === MAX_LEDGERS_WAITED) {
                    throw Error('Destination account creation via the bridge failed.');
                }
            }
        }
        console.log(`Transferring funds from ${wallet1.classicAddress} on the locking chain to ` +
            `${wallet2.classicAddress} on the issuing_chain...`);
        console.log('Step 1: Fetching the claim ID for the transfer...');
        const claimIdTx = {
            TransactionType: 'XChainCreateClaimID',
            Account: wallet2.classicAddress,
            XChainBridge: bridge,
            SignatureReward: bridgeData.SignatureReward,
            OtherChainSource: wallet1.classicAddress,
        };
        const claimIdResult = yield issuingClient.submitAndWait(claimIdTx, {
            wallet: wallet2,
        });
        console.log(claimIdResult);
        const xchainClaimId = (0, src_1.getXChainClaimID)(claimIdResult.result.meta);
        if (xchainClaimId == null) {
            throw Error('Could not extract XChainClaimID');
        }
        console.log(`Claim ID for the transfer: ${xchainClaimId}`);
        console.log('Step 2: Locking the funds on the locking chain with an XChainCommit transaction...');
        const commitTx = {
            TransactionType: 'XChainCommit',
            Account: wallet1.classicAddress,
            Amount: (0, src_1.xrpToDrops)(1),
            XChainBridge: bridge,
            XChainClaimID: xchainClaimId,
            OtherChainDestination: wallet2.classicAddress,
        };
        const commitResult = yield lockingClient.submitAndWait(commitTx, {
            wallet: wallet1,
        });
        console.log(commitResult);
        console.log('Waiting for the attestation to go through... (usually 8-12 seconds)');
        ledgersWaited = 0;
        while (ledgersWaited < MAX_LEDGERS_WAITED) {
            yield sleep(LEDGER_CLOSE_TIME);
            const currentBalance = yield issuingClient.getXrpBalance(wallet2.classicAddress);
            console.log(initialBalance, currentBalance);
            if (currentBalance > initialBalance) {
                console.log('Transfer is complete');
                console.log(`New balance of ${wallet2.classicAddress} is ${currentBalance} XRP`);
                break;
            }
            ledgersWaited += 1;
            if (ledgersWaited === MAX_LEDGERS_WAITED) {
                throw Error('Bridge transfer failed.');
            }
        }
        yield lockingClient.disconnect();
        yield issuingClient.disconnect();
    });
}
//# sourceMappingURL=bridgeTransfer.js.map

Выполнить команду


Для локальной разработки. Не используйте в интернете!