Create an Object

Create an Object via bepro-js in 6 simple steps

Become a super hero in bepro development

The example below shows an Object X considering it uses an ERC20Contract

1 - Create your X.sol object/objects at contracts/X.sol

pragma solidity >=0.6.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
/**
* @title X Contract
* @dev A Contract that stores 3 variables
*/
contract X{
string public variable1;
string public variable2;
ERC20 public erc20Token;
constructor(
string memory _variable1,
string memory _variable2,
ERC20 _tokenAddress) public {
//constructor info
variable1 = _variable1;
variable2 = _variable2;
erc20Token = _tokenAddress;
}
}

2 - Export the interface x at src/interfaces/index.js

/* eslint-disable global-require , import/no-unresolved, import/no-extraneous-dependencies */
const index = {
...
x: require('../contracts/x.json'),
};
module.exports = index;

3 - Create the javascript wrapper at src/models/X.js

A default version is shown below on how to use the system - you can use it to start your Javascript Wrapper

/* src/models/X.js */
import { x } from '../../interfaces';
/**
* X Object
* @class X
* @param {X~Options} options
*/
class X extends IContract {
constructor(params = {}) {
super({ abi: x, ...params });
}
/**
* Asserts a new {@link ERC20Contract} on the current address
* @function
* @return {Promise<void>}
* @throws {Error} Contract is not deployed, first deploy it and provide a contract address
*/
__assert = async () => {
if (!this.getAddress()) {
throw new Error(
'Contract is not deployed, first deploy it and provide a contract address',
);
}
// Use ABI
this.params.contract.use(x, this.getAddress());
// Set Token Address Contract for easy access
this.params.ERC20Contract = new ERC20Contract({
web3Connection: this.web3Connection,
contractAddress: params.tokenAddress,
});
// Assert Token Contract
await this.params.ERC20Contract.__assert();
};
/**
* Action1
* @function
* @param {Object} params Parameters
* @param {number} params.itemId Item Id
* @returns {Promise<Transaction>} Transaction
*/
action1 = async ({ itemId }) => await this.__sendTx(
this.params.contract.getContract().methods.action1(itemId),
);
/**
* User deploys the contract
* @function
* @param {Object} params Parameters
* @param {string} params.variable1 Variable 1 of the Contract
* @param {string} params.variable2 Variable 2 of the Contract
* @param {Address} params.tokenAddress token Address of the purchase Token in use
* @returns {Promise<boolean>} Success the Tx Object if operation was successful
*/
deploy = async ({
variable1, variable2, tokenAddress, callback,
}) => {
const params = [variable1, variable2, tokenAddress];
const res = await this.__deploy(params, callback);
this.params.contractAddress = res.contractAddress;
/* Call to Backend API */
await this.__assert();
return res;
};
/**
* @function
* @return ERC20Contract|undefined
*/
getERC20Contract = () => this.params.ERC20Contract;
}
export default X;

4 - Export the Object at src/models/index.js

src/models/index.js
...
import X from './X';
export {
...
X
}

5 - Create Unit tests tests/x.js

The Template below should be used to start the object of unit tests

tests/x.js
import { expect, assert } from "chai";
import moment from "moment";
import delay from "delay";
import { mochaAsync } from "./utils";
import { ERC20Contract, X } from "..";
import Numbers from "../src/utils/Numbers";
const testConfig = {test : true, localtest : true};
context("X Contract", async () => {
let erc20Contract;
let xContract;
let deployed_tokenAddress, contractAddress, deployed_contractAddress;
let userAddress;
before(async () => {
erc20Lock = new xContract(testConfig);
userAddress = await xContract.getUserAddress(); // local test with ganache
});
it(
"should deploy a new ERC20Contract",
mochaAsync(async () => {
// Create Contract
erc20Contract = new ERC20Contract(testConfig);
expect(erc20Contract).to.not.equal(null);
// Deploy
let res = await erc20Contract.deploy({
name: "test",
symbol: "test",
cap: Numbers.toSmartContractDecimals(100000000, 18),
distributionAddress: userAddress,
});
await erc20Contract.__assert();
deployed_tokenAddress = erc20Contract.getAddress();
expect(res).to.not.equal(false);
expect(deployed_tokenAddress).to.equal(res.contractAddress);
})
);
it(
"should start the X",
mochaAsync(async () => {
xContract = new X(testConfig);
expect(xContract).to.not.equal(null);
})
);
it(
"should deploy X Contract",
mochaAsync(async () => {
// Create Contract
let testConfig2 = { ...testConfig, tokenAddress: deployed_tokenAddress };
xContract = new X(testConfig2);
// Deploy
let res = await x.deploy();
await x.__assert();
contractAddress = erc20Lock.getAddress();
deployed_contractAddress = erc20Lock.getAddress();
assert.equal(
deployed_tokenAddress,
erc20Lock.getERC20Contract().getAddress(),
"token address should match"
);
expect(res).to.not.equal(false);
})
);
it(
"should set do action1",
mochaAsync(async () => {
// maxTokenAmount = 7000;
let res = await xContract.action1({
itemId: 1,
});
expect(res).to.not.equal(false);
})
);
}

6 - Import them at tests/index.js

src/models/index.js
context("Unit Tests", async () => {
...
require("./x");
});

7 - To test open 2 terminals

Terminal 1 (Running Ganache Local EVM)
npm run ganache:start
Terminal 2 (Running the unit tests created)
npm run test