manniru
2/21/2018 - 5:04 PM

Token Presentation tests

Token Presentation tests

const EthereumTx = require('ethereumjs-tx');
const privateKeys = require('./truffle-keys').private;
const publicKeys = require('./truffle-keys').public;
const Token = artifacts.require('./Token.sol');

contract('Token (integration)', function(accounts) {
  let contract;
  let owner;
  let web3Contract;

  before(async () => {
    contract = await Token.deployed();
    web3Contract = web3.eth.contract(contract.abi).at(contract.address);
    owner = web3Contract._eth.coinbase;
    let other = web3.eth.accounts[1];

    if (publicKeys[0] !== owner || publicKeys[1] !== other) {
      throw new Error('Use `truffle develop` and /test/truffle-keys.js');
    }

  });

  it('should pass if contract is deployed', async function() {
    let name = await contract.name.call();
    assert.strictEqual(name, 'Token');
  });

  it('should return inital token wei balance of 1*10^27', async function() {
    let ownerBalance = await contract.balanceOf.call(owner);
    ownerBalance = ownerBalance.toString();
    assert.strictEqual(ownerBalance, '1e+27');
  });

  it('should purchase 100 wei tokens with 1 wei ETH', async function() {
    let spenderPublicKey = web3.eth.accounts[1];
    let spenderPrivateKey = privateKeys[1];
    let wei = 1;

    let ownerBalance = await contract.balanceOf.call(owner);
    let spenderBalance = await contract.balanceOf.call(spenderPublicKey);

    data = web3Contract.purchaseToken.getData();

    let result = await rawTransaction(
      spenderPublicKey,
      spenderPrivateKey,
      contract.address,
      data,
      wei
    );

    let ownerBalanceAfter = await contract.balanceOf.call(owner);
    let spenderBalanceAfter = await contract.balanceOf.call(spenderPublicKey);

    // Correct account balances
    // toString() numbers that are too large for js
    assert.strictEqual(
      ownerBalance.toString(),
      ownerBalanceAfter.add(wei * 100).toString()
    );
    assert.strictEqual(
      spenderBalance.add(wei * 100).toString(),
      spenderBalanceAfter.toString()
    );

    // Normal transaction hash, not an error.
    assert.strictEqual(0, result.indexOf('0x'));
  });

});

/*
 * Call a smart contract function from any keyset in which the caller has the
 *     private and public keys.
 * @param {string} senderPublicKey Public key in key pair.
 * @param {string} senderPrivateKey Private key in key pair.
 * @param {string} contractAddress Address of Solidity contract.
 * @param {string} data Data from the function's `getData` in web3.js.
 * @param {number} value Number of Ethereum wei sent in the transaction.
 * @return {Promise}
 */
function rawTransaction(
  senderPublicKey,
  senderPrivateKey,
  contractAddress,
  data,
  value
) {
  return new Promise((resolve, reject) => {

    let key = new Buffer(senderPrivateKey, 'hex');
    let nonce = web3.toHex(web3.eth.getTransactionCount(senderPublicKey));

    let gasPrice = web3.eth.gasPrice;
    let gasPriceHex = web3.toHex(web3.eth.estimateGas({
      from: contractAddress
    }));
    let gasLimitHex = web3.toHex(5500000);

    let rawTx = {
        nonce: nonce,
        gasPrice: gasPriceHex,
        gasLimit: gasLimitHex,
        data: data,
        to: contractAddress,
        value: web3.toHex(value)
    };

    let tx = new EthereumTx(rawTx);
    tx.sign(key);

    let stx = '0x' + tx.serialize().toString('hex');

    web3.eth.sendRawTransaction(stx, (err, hash) => {
      if (err) {
        reject(err);
      } else {
        resolve(hash);
      }
    });

  });
}