當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Operation.createAccount方法代碼示例

本文整理匯總了TypeScript中stellar-sdk.Operation.createAccount方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Operation.createAccount方法的具體用法?TypeScript Operation.createAccount怎麽用?TypeScript Operation.createAccount使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在stellar-sdk.Operation的用法示例。


在下文中一共展示了Operation.createAccount方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: co

    return co(function *() {
      const [userKey, backupKey] = this.initiateRecovery(params);
      const isKrsRecovery = params.backupKey.startsWith('G') && !params.userKey.startsWith('G');
      const isUnsignedSweep = params.backupKey.startsWith('G') && params.userKey.startsWith('G');

      if (!stellar.StrKey.isValidEd25519PublicKey(params.rootAddress)) {
        throw new Error(`Invalid wallet address: ${ params.rootAddress }`);
      }

      const accountDataUrl = `${ this.getHorizonUrl() }/accounts/${ params.rootAddress }`;
      const destinationUrl = `${ this.getHorizonUrl() }/accounts/${ params.recoveryDestination }`;

      let accountData;
      try {
        accountData = yield request.get(accountDataUrl).result();
      } catch (e) {
        throw new Error('Unable to reach the Stellar network via Horizon.');
      }

      // Now check if the destination account is empty or not
      let unfundedDestination = false;
      try {
        yield request.get(destinationUrl);
      } catch (e) {
        if (e.status === 404) {
          // If the destination account does not yet exist, horizon responds with 404
          unfundedDestination = true;
        }
      }

      if (!accountData.sequence || !accountData.balances) {
        throw new Error('Horizon server error - unable to retrieve sequence ID or account balance');
      }

      const account = new stellar.Account(params.rootAddress, accountData.sequence);

      // Stellar supports multiple assets on chain, we're only interested in the balances entry whose type is "native" (XLM)
      const nativeBalanceInfo = accountData.balances.find(assetBalance => assetBalance['asset_type'] === 'native');

      if (!nativeBalanceInfo) {
        throw new Error('Provided wallet has a balance of 0 XLM, recovery aborted');
      }

      const walletBalance = this.bigUnitsToBaseUnits(nativeBalanceInfo.balance);
      const minimumReserve = yield this.getMinimumReserve();
      const baseTxFee = yield this.getBaseTransactionFee();
      const recoveryAmount = walletBalance - minimumReserve - baseTxFee;
      const formattedRecoveryAmount = (this.baseUnitsToBigUnits(recoveryAmount)).toString();

      let txBuilder = new stellar.TransactionBuilder(account);
      let operation;
      if (unfundedDestination) { // In this case, we need to create the account
        operation = stellar.Operation.createAccount({
          destination: params.recoveryDestination,
          startingBalance: formattedRecoveryAmount
        });
      } else { // Otherwise if the account already exists, we do a normal send
        operation = stellar.Operation.payment({
          destination: params.recoveryDestination,
          asset: stellar.Asset.native(),
          amount: formattedRecoveryAmount
        });
      }
      txBuilder = txBuilder.addOperation(operation).build();

      if (!isUnsignedSweep) {
        txBuilder.sign(userKey);
      }

      if (!isKrsRecovery && !isUnsignedSweep) {
        txBuilder.sign(backupKey);
      }

      const transaction: any = {
        tx: txBuilder.toEnvelope().toXDR('base64'),
        recoveryAmount
      };

      if (isKrsRecovery) {
        transaction.backupKey = params.backupKey;
        transaction.coin = this.getChain();
      }

      return transaction;
    }).call(this);
開發者ID:BitGo,項目名稱:BitGoJS,代碼行數:85,代碼來源:xlm.ts


注:本文中的stellar-sdk.Operation.createAccount方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。