<commit_before>var assert = require("chai").assert;
var Artifactor = require("../");
var contract = require("truffle-contract");
var Schema = require("truffle-contract-schema");
var temp = require("temp").track();
var path = require("path");
var fs = require("fs");
var requireNoCache = require("require-nocache")(module);
var Compile = require("truffle-compile");
var TestRPC = require("ganache-cli");
var Web3 = require("web3");
const { promisify } = require("util");

describe("artifactor + require", function() {
  var Example;
  var accounts;
  var abi;
  var binary;
  var network_id;
  var artifactor;
  var provider = TestRPC.provider();
  var web3 = new Web3();
  web3.setProvider(provider);

  before(function() {
    return web3.eth.net.getId().then(id => (network_id = id));
  });

  before(async function() {
    this.timeout(20000);

    const sourcePath = path.join(__dirname, "Example.sol");
    const sources = {
      Example: fs.readFileSync(sourcePath, { encoding: "utf8" })
    };

    const options = {
      contracts_directory: path.join(__dirname),
      compilers: {
        solc: {
          version: "0.5.0",
          settings: {}
        }
      }
    };

    // Compile first
    var result = await promisify(Compile)(sources, options);

    // Clean up after solidity. Only remove solidity's listener,
    // which happens to be the first.
    process.removeListener(
      "uncaughtException",
      process.listeners("uncaughtException")[0] || function() {}
    );

    var compiled = Schema.normalize(result["Example"]);
    abi = compiled.abi;
    binary = compiled.bytecode;

    // Setup
    var dirPath = temp.mkdirSync({
      dir: path.resolve("./"),
      prefix: "tmp-test-contract-"
    });

    var expected_filepath = path.join(dirPath, "Example.json");

    artifactor = new Artifactor(dirPath);

    await artifactor
      .save({
        contractName: "Example",
        abi: abi,
        binary: binary,
        address: "0xe6e1652a0397e078f434d6dda181b218cfd42e01",
        network_id: network_id
      })
      .then(function() {
        var json = requireNoCache(expected_filepath);
        Example = contract(json);
        Example.setProvider(provider);
      });
  });

  before(function() {
    return web3.eth.getAccounts().then(accs => {
      accounts = accs;

      Example.defaults({
        from: accounts[0]
      });
    });
  });

  after(function(done) {
    temp.cleanupSync();
    done();
  });

  it("should set the transaction hash of contract instantiation", function() {
    return Example.new(1, { gas: 3141592 }).then(function(example) {
      assert(example.transactionHash, "transactionHash should be non-empty");
    });
  });

  it("should get and set values via methods and get values via .call", function(done) {
    var example;
    Example.new(1, { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(value.valueOf(), 1, "Starting value should be 1");
        return example.setValue(5);
      })
      .then(() => {
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(parseInt(value), 5, "Ending value should be five");
      })
      .then(done)
      .catch(done);
  });

  it("shouldn't synchronize constant functions", function(done) {
    var example;
    Example.new(5, { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.getValue();
      })
      .then(function(value) {
        assert.equal(
          value.valueOf(),
          5,
          "Value should have been retrieved without explicitly calling .call()"
        );
      })
      .then(done)
      .catch(done);
  });

  it("should allow BigNumbers as input parameters, and not confuse them as transaction objects", function(done) {
    // BigNumber passed on new()
    var example = null;
    Example.new("30", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(parseInt(value), 30, "Starting value should be 30");
        // BigNumber passed in a transaction.
        return example.setValue("25", { gas: 3141592 });
      })
      .then(() => {
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(parseInt(value), 25, "Ending value should be twenty-five");
        // BigNumber passed in a call.
        return example.parrot.call(865);
      })
      .then(function(parrot_value) {
        assert.equal(
          parseInt(parrot_value),
          865,
          "Parrotted value should equal 865"
        );
      })
      .then(done)
      .catch(done);
  });

  it("should return transaction hash, logs and receipt when using synchronised transactions", function(done) {
    var example = null;
    Example.new("1", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.triggerEvent();
      })
      .then(function(result) {
        assert.isDefined(result.tx, "transaction hash wasn't returned");
        assert.isDefined(
          result.logs,
          "synchronized transaction didn't return any logs"
        );
        assert.isDefined(
          result.receipt,
          "synchronized transaction didn't return a receipt"
        );
        assert.isOk(result.tx.length > 42, "Unexpected transaction hash"); // There has to be a better way to do this.
        assert.equal(
          result.tx,
          result.receipt.transactionHash,
          "Transaction had different hash than receipt"
        );
        assert.equal(result.logs.length, 1, "logs array expected to be 1");

        var log = result.logs[0];

        assert.equal("ExampleEvent", log.event);
        assert.equal(accounts[0], log.args._from);
        assert.equal(8, log.args.num); // 8 is a magic number inside Example.sol
      })
      .then(done)
      .catch(done);
  });

  it("should trigger the fallback function when calling sendTransaction()", function() {
    var example = null;
    return Example.new("1", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.fallbackTriggered();
      })
      .then(function(triggered) {
        assert(
          triggered == false,
          "Fallback should not have been triggered yet"
        );
        return example.sendTransaction({
          value: web3.utils.toWei("1", "ether")
        });
      })
      .then(() => {
        return new Promise(function(accept, reject) {
          return web3.eth.getBalance(example.address, function(err, balance) {
            if (err) return reject(err);
            accept(balance);
          });
        });
      })
      .then(function(balance) {
        assert(balance == web3.utils.toWei("1", "ether"));
      });
  });

  it("should trigger the fallback function when calling send() (shorthand notation)", function() {
    var example = null;
    return Example.new("1", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.fallbackTriggered();
      })
      .then(function(triggered) {
        assert(
          triggered == false,
          "Fallback should not have been triggered yet"
        );
        return example.send(web3.utils.toWei("1", "ether"));
      })
      .then(() => {
        return new Promise(function(accept, reject) {
          return web3.eth.getBalance(example.address, function(err, balance) {
            if (err) return reject(err);
            accept(balance);
          });
        });
      })
      .then(function(balance) {
        assert(balance == web3.utils.toWei("1", "ether"));
      });
  });

  it("errors when setting an invalid provider", function(done) {
    try {
      Example.setProvider(null);
      assert.fail("setProvider() should have thrown an error");
    } catch (e) {
      // Do nothing with the error.
    }
    done();
  });

  it("creates a network object when an address is set if no network specified", function(done) {
    var NewExample = contract({
      abi: abi,
      unlinked_binary: binary
    });

    NewExample.setProvider(provider);
    NewExample.defaults({
      from: accounts[0]
    });

    assert.equal(NewExample.network_id, null);

    NewExample.new(1, { gas: 3141592 })
      .then(function(instance) {
        // We have a network id in this case, with new(), since it was detected,
        // but no further configuration.
        assert.equal(NewExample.network_id, network_id);
        assert.equal(NewExample.toJSON().networks[network_id], null);

        NewExample.address = instance.address;

        assert.equal(
          NewExample.toJSON().networks[network_id].address,
          instance.address
        );

        done();
      })
      .catch(done);
  });

  it("doesn't error when calling .links() or .events() with no network configuration", function(done) {
    var event_abi = {
      anonymous: false,
      inputs: [
        {
          indexed: true,
          name: "nameHash",
          type: "bytes32"
        },
        {
          indexed: true,
          name: "releaseHash",
          type: "bytes32"
        }
      ],
      name: "PackageRelease",
      type: "event"
    };

    var MyContract = contract({
      contractName: "MyContract",
      abi: [event_abi],
      binary: "0x12345678"
    });

    MyContract.setNetwork(5);

    var expected_event_topic = web3.utils.sha3(
      "PackageRelease(bytes32,bytes32)"
    );

    // We want to make sure these don't throw when a network configuration doesn't exist.
    // While we're at it, lets make sure we still get the event we expect.
    assert.deepEqual(MyContract.links, {});
    assert.deepEqual(MyContract.events[expected_event_topic], event_abi);

    done();
  });
});
<commit_msg>update contracts.js<commit_after>var assert = require("chai").assert;
var Artifactor = require("../");
var contract = require("truffle-contract");
var Schema = require("truffle-contract-schema");
var temp = require("temp").track();
var path = require("path");
var fs = require("fs");
var requireNoCache = require("require-nocache")(module);
var Compile = require("truffle-compile");
var TestRPC = require("ganache-cli");
var Web3 = require("web3");
const { promisify } = require("util");

describe("artifactor + require", function() {
  var Example;
  var accounts;
  var abi;
  var binary;
  var network_id;
  var artifactor;
  var provider = TestRPC.provider();
  var web3 = new Web3();
  web3.setProvider(provider);

  before(function() {
    return web3.eth.net.getId().then(id => (network_id = id));
  });

  before(async function() {
    this.timeout(20000);

    const sourcePath = path.join(__dirname, "Example.sol");
    const sources = {
      Example: fs.readFileSync(sourcePath, { encoding: "utf8" })
    };

    const options = {
      contracts_directory: path.join(__dirname),
      compilers: {
        solc: {
          version: "0.5.0",
          settings: {
            optimizer: {
              enabled: false,
              runs: 200
            },
            evmVersion: "byzantium"
          }
        }
      }
    };

    // Compile first
    var result = await promisify(Compile)(sources, options);

    // Clean up after solidity. Only remove solidity's listener,
    // which happens to be the first.
    process.removeListener(
      "uncaughtException",
      process.listeners("uncaughtException")[0] || function() {}
    );

    var compiled = Schema.normalize(result["Example"]);
    abi = compiled.abi;
    binary = compiled.bytecode;

    // Setup
    var dirPath = temp.mkdirSync({
      dir: path.resolve("./"),
      prefix: "tmp-test-contract-"
    });

    var expected_filepath = path.join(dirPath, "Example.json");

    artifactor = new Artifactor(dirPath);

    await artifactor
      .save({
        contractName: "Example",
        abi: abi,
        binary: binary,
        address: "0xe6e1652a0397e078f434d6dda181b218cfd42e01",
        network_id: network_id
      })
      .then(function() {
        var json = requireNoCache(expected_filepath);
        Example = contract(json);
        Example.setProvider(provider);
      });
  });

  before(function() {
    return web3.eth.getAccounts().then(accs => {
      accounts = accs;

      Example.defaults({
        from: accounts[0]
      });
    });
  });

  after(function(done) {
    temp.cleanupSync();
    done();
  });

  it("should set the transaction hash of contract instantiation", function() {
    return Example.new(1, { gas: 3141592 }).then(function(example) {
      assert(example.transactionHash, "transactionHash should be non-empty");
    });
  });

  it("should get and set values via methods and get values via .call", function(done) {
    var example;
    Example.new(1, { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(value.valueOf(), 1, "Starting value should be 1");
        return example.setValue(5);
      })
      .then(() => {
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(parseInt(value), 5, "Ending value should be five");
      })
      .then(done)
      .catch(done);
  });

  it("shouldn't synchronize constant functions", function(done) {
    var example;
    Example.new(5, { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.getValue();
      })
      .then(function(value) {
        assert.equal(
          value.valueOf(),
          5,
          "Value should have been retrieved without explicitly calling .call()"
        );
      })
      .then(done)
      .catch(done);
  });

  it("should allow BigNumbers as input parameters, and not confuse them as transaction objects", function(done) {
    // BigNumber passed on new()
    var example = null;
    Example.new("30", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(parseInt(value), 30, "Starting value should be 30");
        // BigNumber passed in a transaction.
        return example.setValue("25", { gas: 3141592 });
      })
      .then(() => {
        return example.value.call();
      })
      .then(function(value) {
        assert.equal(parseInt(value), 25, "Ending value should be twenty-five");
        // BigNumber passed in a call.
        return example.parrot.call(865);
      })
      .then(function(parrot_value) {
        assert.equal(
          parseInt(parrot_value),
          865,
          "Parrotted value should equal 865"
        );
      })
      .then(done)
      .catch(done);
  });

  it("should return transaction hash, logs and receipt when using synchronised transactions", function(done) {
    var example = null;
    Example.new("1", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.triggerEvent();
      })
      .then(function(result) {
        assert.isDefined(result.tx, "transaction hash wasn't returned");
        assert.isDefined(
          result.logs,
          "synchronized transaction didn't return any logs"
        );
        assert.isDefined(
          result.receipt,
          "synchronized transaction didn't return a receipt"
        );
        assert.isOk(result.tx.length > 42, "Unexpected transaction hash"); // There has to be a better way to do this.
        assert.equal(
          result.tx,
          result.receipt.transactionHash,
          "Transaction had different hash than receipt"
        );
        assert.equal(result.logs.length, 1, "logs array expected to be 1");

        var log = result.logs[0];

        assert.equal("ExampleEvent", log.event);
        assert.equal(accounts[0], log.args._from);
        assert.equal(8, log.args.num); // 8 is a magic number inside Example.sol
      })
      .then(done)
      .catch(done);
  });

  it("should trigger the fallback function when calling sendTransaction()", function() {
    var example = null;
    return Example.new("1", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.fallbackTriggered();
      })
      .then(function(triggered) {
        assert(
          triggered == false,
          "Fallback should not have been triggered yet"
        );
        return example.sendTransaction({
          value: web3.utils.toWei("1", "ether")
        });
      })
      .then(() => {
        return new Promise(function(accept, reject) {
          return web3.eth.getBalance(example.address, function(err, balance) {
            if (err) return reject(err);
            accept(balance);
          });
        });
      })
      .then(function(balance) {
        assert(balance == web3.utils.toWei("1", "ether"));
      });
  });

  it("should trigger the fallback function when calling send() (shorthand notation)", function() {
    var example = null;
    return Example.new("1", { gas: 3141592 })
      .then(function(instance) {
        example = instance;
        return example.fallbackTriggered();
      })
      .then(function(triggered) {
        assert(
          triggered == false,
          "Fallback should not have been triggered yet"
        );
        return example.send(web3.utils.toWei("1", "ether"));
      })
      .then(() => {
        return new Promise(function(accept, reject) {
          return web3.eth.getBalance(example.address, function(err, balance) {
            if (err) return reject(err);
            accept(balance);
          });
        });
      })
      .then(function(balance) {
        assert(balance == web3.utils.toWei("1", "ether"));
      });
  });

  it("errors when setting an invalid provider", function(done) {
    try {
      Example.setProvider(null);
      assert.fail("setProvider() should have thrown an error");
    } catch (e) {
      // Do nothing with the error.
    }
    done();
  });

  it("creates a network object when an address is set if no network specified", function(done) {
    var NewExample = contract({
      abi: abi,
      unlinked_binary: binary
    });

    NewExample.setProvider(provider);
    NewExample.defaults({
      from: accounts[0]
    });

    assert.equal(NewExample.network_id, null);

    NewExample.new(1, { gas: 3141592 })
      .then(function(instance) {
        // We have a network id in this case, with new(), since it was detected,
        // but no further configuration.
        assert.equal(NewExample.network_id, network_id);
        assert.equal(NewExample.toJSON().networks[network_id], null);

        NewExample.address = instance.address;

        assert.equal(
          NewExample.toJSON().networks[network_id].address,
          instance.address
        );

        done();
      })
      .catch(done);
  });

  it("doesn't error when calling .links() or .events() with no network configuration", function(done) {
    var event_abi = {
      anonymous: false,
      inputs: [
        {
          indexed: true,
          name: "nameHash",
          type: "bytes32"
        },
        {
          indexed: true,
          name: "releaseHash",
          type: "bytes32"
        }
      ],
      name: "PackageRelease",
      type: "event"
    };

    var MyContract = contract({
      contractName: "MyContract",
      abi: [event_abi],
      binary: "0x12345678"
    });

    MyContract.setNetwork(5);

    var expected_event_topic = web3.utils.sha3(
      "PackageRelease(bytes32,bytes32)"
    );

    // We want to make sure these don't throw when a network configuration doesn't exist.
    // While we're at it, lets make sure we still get the event we expect.
    assert.deepEqual(MyContract.links, {});
    assert.deepEqual(MyContract.events[expected_event_topic], event_abi);

    done();
  });
});

{'fraction_non_alphanumeric': 0.13428813813064525, 'fraction_numerical': 0.017915065622037027, 'mean_word_length': 2.3031152134498103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3824069', 'n_tokens_mistral': 6034, 'n_tokens_neox': 5759, 'n_words': 1410}