Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3552 from trufflesuite/usenums
Browse files Browse the repository at this point in the history
Enhancement: Add enum objects to contract objects
  • Loading branch information
haltman-at authored Nov 23, 2020
2 parents 5ec6903 + 98f6916 commit aa88a41
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 5 deletions.
22 changes: 22 additions & 0 deletions packages/contract-tests/test/enums.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var assert = require("chai").assert;
var util = require("./util");

describe("Enumerations", function () {
var Example;

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

Example = await util.createExample();
});

it("Sets up enumeration objects on contracts", async function () {
const expected = {
ExampleZero: 0,
ExampleOne: 1,
ExampleTwo: 2
};
assert.deepEqual(Example.enums.ExampleEnum, expected);
assert.deepEqual(Example.ExampleEnum, expected);
});
});
2 changes: 2 additions & 0 deletions packages/contract-tests/test/sources/Example.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ contract Example {
event SpecialEvent();
event NumberEvent(int numA, int indexed numB, address addrC, uint numD, uint);

enum ExampleEnum { ExampleZero, ExampleOne, ExampleTwo }

constructor(uint val) public {
// Constructor revert
require(val != 13);
Expand Down
46 changes: 46 additions & 0 deletions packages/contract/lib/contract/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const execute = require("../execute");
const debug = require("debug")("contract:contract:bootstrap");

module.exports = fn => {
// Add our static methods
Expand All @@ -16,5 +17,50 @@ module.exports = fn => {
fn["new"].estimateGas = execute.estimateDeployment.bind(fn);
fn["new"].request = execute.requestDeployment.bind(fn);

//add enumerations. (probably these should go in
//constructorMethods.js, but this is easier to modify... we'll
//redo all this in the rewrite anyway)
if (fn._json) {
//getters will throw otherwise!
if (fn.ast) {
//note this was set up earlier
const node = locateNode(fn.contractName, fn.ast); //name also set up earlier
if (node) {
fn.enums = extractEnums(node);
for (const [name, enumeration] of Object.entries(fn.enums)) {
//enum is a reserved word :P
if (!(name in fn)) {
//don't overwrite anything!
fn[name] = enumeration;
}
}
}
}
}

return fn;
};

function locateNode(name, ast) {
if (ast.nodeType === "SourceUnit") {
return ast.nodes.find(
node => node.nodeType === "ContractDefinition" && node.name === name
);
} else {
return undefined;
}
}

function extractEnums(node) {
return Object.assign(
{},
...node.nodes
.filter(definition => definition.nodeType === "EnumDefinition")
.map(definition => ({
[definition.name]: Object.assign(
{},
...definition.members.map((member, index) => ({[member.name]: index}))
)
}))
);
}
10 changes: 5 additions & 5 deletions packages/contract/lib/contract/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const debug = require("debug")("contract:contract"); // eslint-disable-line no-unused-vars
const debug = require("debug")("contract:contract");
let Web3 = require("web3");
const webUtils = require("web3-utils");
const execute = require("../execute");
Expand All @@ -12,7 +12,7 @@ if (typeof Web3 === "object" && Object.keys(Web3).length === 0) {
Web3 = global.Web3;
}

(function(module) {
(function (module) {
// Accepts a contract object created with web3.eth.Contract or an address.
function Contract(contract) {
var instance = this;
Expand All @@ -38,15 +38,15 @@ if (typeof Web3 === "object" && Object.keys(Web3).length === 0) {
}

// User defined methods, overloaded methods, events
instance.abi.forEach(function(item) {
instance.abi.forEach(function (item) {
switch (item.type) {
case "function":
var isConstant =
["pure", "view"].includes(item.stateMutability) || item.constant; // new form // deprecated case

var signature = webUtils._jsonInterfaceMethodToString(item);

var method = function(constant, web3Method) {
var method = function (constant, web3Method) {
var fn;

constant
Expand Down Expand Up @@ -127,7 +127,7 @@ if (typeof Web3 === "object" && Object.keys(Web3).length === 0) {
// Prefer user defined `send`
if (!instance.send) {
instance.send = (value, txParams = {}) => {
const packet = Object.assign({ value: value }, txParams);
const packet = Object.assign({value: value}, txParams);
return instance.sendTransaction(packet);
};
}
Expand Down

0 comments on commit aa88a41

Please sign in to comment.