How to use the @arkecosystem/crypto.TRANSACTION_TYPES.VOTE function in @arkecosystem/crypto

To help you get started, we’ve selected a few @arkecosystem/crypto 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 ArkEcosystem / core / packages / core-database / lib / wallet-manager.js View on Github external
async applyTransaction (transaction) {
    const transactionData = transaction.data
    const recipientId = transactionData.recipientId

    const sender = this.getWalletByPublicKey(transactionData.senderPublicKey)
    let recipient = recipientId ? this.getWalletByAddress(recipientId) : null

    if (!recipient && recipientId) { // cold wallet
      recipient = new Wallet(recipientId)
      this.walletsByAddress[recipientId] = recipient
      emitter.emit('wallet:cold:created', recipient)
    } else if (transactionData.type === TRANSACTION_TYPES.DELEGATE_REGISTRATION && this.walletsByUsername[transactionData.asset.delegate.username.toLowerCase()]) {
      logger.error(`Delegate transction sent by ${sender.address}`, JSON.stringify(transactionData))

      throw new Error(`Can't apply transaction ${transactionData.id}: delegate name already taken`)
    } else if (transactionData.type === TRANSACTION_TYPES.VOTE && !this.walletsByPublicKey[transactionData.asset.votes[0].slice(1)].username) {
      logger.error(`Vote transaction sent by ${sender.address}`, JSON.stringify(transactionData))

      throw new Error(`Can't apply transaction ${transactionData.id}: voted delegate does not exist`)
    } else if (config.network.exceptions[transactionData.id]) {
      logger.warn('Transaction forcibly applied because it has been added as an exception:', transactionData)
    } else if (!sender.canApply(transactionData)) {
      logger.error(`Can't apply transaction for ${sender.address}`, JSON.stringify(transactionData))
      logger.debug('Audit', JSON.stringify(sender.auditApply(transactionData), null, 2))

      throw new Error(`Can't apply transaction ${transactionData.id}`)
    }

    sender.applyTransactionToSender(transactionData)

    if (recipient && transactionData.type === TRANSACTION_TYPES.TRANSFER) {
      recipient.applyTransactionToRecipient(transactionData)
github ArkEcosystem / core / packages / core-transaction-pool / lib / pool-wallet-manager.js View on Github external
`[PoolWalletManager] Can't apply transaction ${
          data.id
        }: delegate name already taken.`,
        JSON.stringify(data),
      )

      throw new Error(
        `[PoolWalletManager] Can't apply transaction ${
          data.id
        }: delegate name already taken.`,
      )

      // NOTE: We use the vote public key, because vote transactions
      // have the same sender and recipient.
    } else if (
      type === TRANSACTION_TYPES.VOTE &&
      !database.walletManager.__isDelegate(asset.votes[0].slice(1))
    ) {
      logger.error(
        `[PoolWalletManager] Can't apply vote transaction: delegate ${
          asset.votes[0]
        } does not exist.`,
        JSON.stringify(data),
      )

      throw new Error(
        `[PoolWalletManager] Can't apply transaction ${data.id}: delegate ${
          asset.votes[0]
        } does not exist.`,
      )
    } else if (this.__isException(data)) {
      logger.warn(
github ArkEcosystem / core / packages / core-test-utils / lib / generators / transactions.js View on Github external
const address = testAddress || crypto.getAddress(crypto.getKeys(passphrase).publicKey)

    let builder
    if (type === TRANSACTION_TYPES.TRANSFER) {
      builder = client.getBuilder().transfer()
        .recipientId(address)
        .amount(amount)
        .vendorField(`Test Transaction ${i + 1}`)
    } else if (type === TRANSACTION_TYPES.SECOND_SIGNATURE) {
      builder = client.getBuilder().secondSignature()
        .signatureAsset(passphrase)
    } else if (type === TRANSACTION_TYPES.DELEGATE_REGISTRATION) {
      const username = superheroes.random().toLowerCase().replace(/[^a-z0-9]/g, '_')
      builder = client.getBuilder().delegateRegistration()
        .usernameAsset(username)
    } else if (type === TRANSACTION_TYPES.VOTE) {
      const publicKey = crypto.getKeys(config.passphrase).publicKey
      builder = client.getBuilder().vote()
        .votesAsset([`+${publicKey}`])
    } else {
      continue
    }

    const transaction = builder
      .sign(passphrase)
      .build()

    transactions.push(transaction)
  }

  return transactions
}
github ArkEcosystem / core / packages / core-test-utils / lib / generators / transactions.js View on Github external
module.exports = (network, type, testWallet, testAddress, amount = 2, quantity = 10) => {
  network = network || 'testnet'
  type = type || TRANSACTION_TYPES.TRANSFER
  amount = amount * Math.pow(10, 8)

  assert.ok(['mainnet', 'devnet', 'testnet'].includes(network), 'Invalid network')
  assert.ok([
    TRANSACTION_TYPES.TRANSFER,
    TRANSACTION_TYPES.SECOND_SIGNATURE,
    TRANSACTION_TYPES.DELEGATE_REGISTRATION,
    TRANSACTION_TYPES.VOTE
  ].includes(type), 'Invalid transaction type')

  client.getConfigManager().setFromPreset('ark', network)

  const transactions = []
  for (let i = 0; i < quantity; i++) {
    const passphrase = testWallet ? testWallet.passphrase : config.passphrase
    const address = testAddress || crypto.getAddress(crypto.getKeys(passphrase).publicKey)

    let builder
    if (type === TRANSACTION_TYPES.TRANSFER) {
      builder = client.getBuilder().transfer()
        .recipientId(address)
        .amount(amount)
        .vendorField(`Test Transaction ${i + 1}`)
    } else if (type === TRANSACTION_TYPES.SECOND_SIGNATURE) {
github ArkEcosystem / core / packages / core-api / lib / repositories / transactions.js View on Github external
async allVotesBySender(senderPublicKey, parameters = {}) {
    return this.findAll({
      ...{ senderPublicKey, type: TRANSACTION_TYPES.VOTE },
      ...parameters,
    })
  }
github ArkEcosystem / core / packages / core-api / lib / versions / 2 / methods / votes.js View on Github external
const index = async request => {
  const transactions = await transactionsRepository.findAllByType(
    TRANSACTION_TYPES.VOTE,
    {
      ...request.query,
      ...utils.paginate(request),
    },
  )

  return utils.toPagination(request, transactions, 'transaction')
}
github ArkEcosystem / core / packages / core-api / lib / versions / 2 / handlers / statistics.js View on Github external
async handler (request, h) {
    let transactions = await database.transactions.findAllByDateAndType(TRANSACTION_TYPES.VOTE, request.query.from, request.query.to)
    transactions = transactions.filter(transaction => transaction.asset.votes[0].startsWith('-'))

    return {
      data: {
        count: transactions.length,
        amount: _.sumBy(transactions, 'amount'),
        fees: _.sumBy(transactions, 'fee')
      }
    }
  },
  options: {
github ArkEcosystem / core / packages / core-database-sequelize / lib / repositories / transactions.js View on Github external
async allVotesBySender (senderPublicKey, params = {}) {
    return this.findAll({...{senderPublicKey, type: TRANSACTION_TYPES.VOTE}, ...params})
  }
github ArkEcosystem / core / packages / core-api / lib / versions / 2 / handlers / statistics.js View on Github external
async handler (request, h) {
    let transactions = await database.transactions.findAllByDateAndType(TRANSACTION_TYPES.VOTE, request.query.from, request.query.to)
    transactions = transactions.filter(transaction => transaction.asset.votes[0].startsWith('+'))

    return {
      data: {
        count: transactions.length,
        amount: _.sumBy(transactions, 'amount'),
        fees: _.sumBy(transactions, 'fee')
      }
    }
  },
  options: {
github ArkEcosystem / core / packages / core-database-sequelize / lib / spv.js View on Github external
async __buildVotes () {
    const data = await this.query
      .select('sender_public_key', 'serialized')
      .from('transactions')
      .where('type', TRANSACTION_TYPES.VOTE)
      .orderBy('created_at', 'DESC')
      .all()

    data.forEach(row => {
      const wallet = this.walletManager.findByPublicKey(row.senderPublicKey)

      if (!wallet.voted) {
        const vote = Transaction.deserialize(row.serialized.toString('hex')).asset.votes[0]
        if (vote.startsWith('+')) {
          wallet.vote = vote.slice(1)
        }
        wallet.voted = true
      }
    })

    this.walletManager.updateDelegates()