{"size": 4490, "ext": "js", "max_stars_repo_path": "app/actions/Workspace/Transactions/api.transactions.js", "max_stars_repo_name": "Tezsure/Tezster-GUI", "max_stars_repo_stars_event_min_datetime": "2019-11-14T12:14:13.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-24T05:41:25.000Z", "max_issues_repo_path": "app/actions/Workspace/Transactions/api.transactions.js", "max_issues_repo_name": "Tezsure/Tezster-GUI", "max_issues_repo_issues_event_min_datetime": "2020-03-31T18:42:54.000Z", "max_issues_repo_issues_event_max_datetime": "2020-12-07T08:29:02.000Z", "max_forks_repo_path": "app/actions/Workspace/Transactions/api.transactions.js", "max_forks_repo_name": "Tezsure/Tezster-GUI", "max_forks_repo_forks_event_min_datetime": "2020-02-20T12:32:55.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-18T13:35:01.000Z"}
{"max_stars_count": 12, "max_issues_count": 8, "max_forks_count": 2, "avg_line_length": 27.8881987578, "max_line_length": 79, "alphanum_fraction": 0.6525612472}
/* eslint-disable prettier/prettier */
import { TezosToolkit } from '@taquito/taquito';
import { InMemorySigner } from '@taquito/signer';

const conseiljs = require('conseiljs');

const Config = require('../../../db-config/helper.dbConfig');

export async function ListAccountTransactionsAPI(args, callback) {
  try {
    const platform = 'tezos';
    const { networkId } = args.dashboardHeader;
    const networkName = networkId.split('-')[0];
    const network = networkName.toLowerCase();

    const entity = 'operations';
    const conseilServer = {
      url: Config.GetLocalStorage().ConseilJS[networkName].url,
      apiKey: Config.GetLocalStorage().ConseilJS[networkName].apiKey,
      network,
    };
    let sendQuery = conseiljs.ConseilQueryBuilder.blankQuery();
    sendQuery = conseiljs.ConseilQueryBuilder.addFields(
      sendQuery,
      'block_level',
      'timestamp',
      'source',
      'destination',
      'amount',
      'fee',
      'counter'
    );
    sendQuery = conseiljs.ConseilQueryBuilder.addPredicate(
      sendQuery,
      'kind',
      conseiljs.ConseilOperator.EQ,
      ['transaction'],
      false
    );
    sendQuery = conseiljs.ConseilQueryBuilder.addPredicate(
      sendQuery,
      'source',
      conseiljs.ConseilOperator.EQ,
      [args.accountId],
      false
    );
    sendQuery = conseiljs.ConseilQueryBuilder.addPredicate(
      sendQuery,
      'status',
      conseiljs.ConseilOperator.EQ,
      ['applied'],
      false
    );
    sendQuery = conseiljs.ConseilQueryBuilder.addOrdering(
      sendQuery,
      'block_level',
      conseiljs.ConseilSortDirection.DESC
    );
    sendQuery = conseiljs.ConseilQueryBuilder.setLimit(sendQuery, 100);

    let receiveQuery = conseiljs.ConseilQueryBuilder.blankQuery();
    receiveQuery = conseiljs.ConseilQueryBuilder.addFields(
      receiveQuery,
      'block_level',
      'timestamp',
      'source',
      'destination',
      'amount',
      'fee',
      'counter'
    );
    receiveQuery = conseiljs.ConseilQueryBuilder.addPredicate(
      receiveQuery,
      'kind',
      conseiljs.ConseilOperator.EQ,
      ['transaction'],
      false
    );
    receiveQuery = conseiljs.ConseilQueryBuilder.addPredicate(
      receiveQuery,
      'destination',
      conseiljs.ConseilOperator.EQ,
      [args.accountId],
      false
    );
    receiveQuery = conseiljs.ConseilQueryBuilder.addPredicate(
      receiveQuery,
      'status',
      conseiljs.ConseilOperator.EQ,
      ['applied'],
      false
    );
    receiveQuery = conseiljs.ConseilQueryBuilder.addOrdering(
      receiveQuery,
      'block_level',
      conseiljs.ConseilSortDirection.DESC
    );
    receiveQuery = conseiljs.ConseilQueryBuilder.setLimit(receiveQuery, 100);

    const sendResult = await conseiljs.ConseilDataClient.executeEntityQuery(
      conseilServer,
      platform,
      network,
      entity,
      sendQuery
    );
    const receiveResult = await conseiljs.ConseilDataClient.executeEntityQuery(
      conseilServer,
      platform,
      network,
      entity,
      receiveQuery
    );
    const transactions = sendResult.concat(receiveResult).sort((a, b) => {
      return a.timestamp - b.timestamp;
    });
    return callback(null, transactions);
  } catch (exp) {
    return callback(exp, null);
  }
}

export async function TransferBalanceTransactionAPI(args, callback) {
  try {
    const keys = args.userAccounts.find(
      (elem) => elem.pkh === args.senderAccount
    );
    const tezosNode = Config.GetLocalStorage().apiEndPoints[
      args.dashboardHeader.networkId
    ];
    const keystore = {
      publicKey: keys.pk,
      privateKey: keys.sk,
      publicKeyHash: keys.pkh,
      seed: '',
      storeType: conseiljs.StoreType.Fundraiser,
    };
    const rpc = tezosNode;
    const signer = await InMemorySigner.fromSecretKey(keystore.privateKey);
    const Tezos = new TezosToolkit(rpc);
    Tezos.setProvider({ signer });
    const reciever = args.recieverAccount;
    const amount = parseInt(args.amount, 10);
    const transferParams = {
      from: keystore.publicKey,
      to: reciever,
      amount,
    };
    if (args.gasPrice && parseInt(args.gasPrice, 10) !== 0) {
      transferParams.fee = parseInt(args.gasPrice, 10);
    }
    const operation = await Tezos.contract.transfer(transferParams);
    callback(null, {
      fee: operation.params.fee,
      transaction: operation.hash,
    });
  } catch (error) {
    return callback(error.toString(), null);
  }
}
