Create an Object
Create an Object via bepro-js in 6 simple steps
Become a super hero in bepro-js development
The example below shows an Object X considering it uses an ERC20Contract
1 - Create your X.sol object/objects at contracts/X.sol
1
pragma solidity >=0.6.0;
2
3
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
4
5
/**
6
* @title X Contract
7
* @dev A Contract that stores 3 variables
8
*/
9
contract X{
10
11
string public variable1;
12
string public variable2;
13
ERC20 public erc20Token;
14
15
constructor(
16
string memory _variable1,
17
string memory _variable2,
18
ERC20 _tokenAddress) public {
19
20
//constructor info
21
variable1 = _variable1;
22
variable2 = _variable2;
23
erc20Token = _tokenAddress;
24
}
25
26
}
Copied!
2 - Export the interface x at src/interfaces/index.js
1
/* eslint-disable global-require , import/no-unresolved, import/no-extraneous-dependencies */
2
const index = {
3
...
4
x: require('../contracts/x.json'),
5
};
6
7
module.exports = index;
8
Copied!
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
1
/* src/models/X.js */
2
3
import { x } from '../../interfaces';
4
5
/**
6
* X Object
7
* @class X
8
* @param {X~Options} options
9
*/
10
class X extends IContract {
11
constructor(params = {}) {
12
super({ abi: x, ...params });
13
}
14
15
16
/**
17
* Asserts a new {@link ERC20Contract} on the current address
18
* @function
19
* @return {Promise<void>}
20
* @throws {Error} Contract is not deployed, first deploy it and provide a contract address
21
*/
22
__assert = async () => {
23
if (!this.getAddress()) {
24
throw new Error(
25
'Contract is not deployed, first deploy it and provide a contract address',
26
);
27
}
28
29
// Use ABI
30
this.params.contract.use(x, this.getAddress());
31
32
// Set Token Address Contract for easy access
33
this.params.ERC20Contract = new ERC20Contract({
34
web3Connection: this.web3Connection,
35
contractAddress: params.tokenAddress,
36
});
37
38
// Assert Token Contract
39
await this.params.ERC20Contract.__assert();
40
};
41
42
/**
43
* Action1
44
* @function
45
* @param {Object} params Parameters
46
* @param {number} params.itemId Item Id
47
* @returns {Promise<Transaction>} Transaction
48
*/
49
action1 = async ({ itemId }) => await this.__sendTx(
50
this.params.contract.getContract().methods.action1(itemId),
51
);
52
53
/**
54
* User deploys the contract
55
* @function
56
* @param {Object} params Parameters
57
* @param {string} params.variable1 Variable 1 of the Contract
58
* @param {string} params.variable2 Variable 2 of the Contract
59
* @param {Address} params.tokenAddress token Address of the purchase Token in use
60
* @returns {Promise<boolean>} Success the Tx Object if operation was successful
61
*/
62
deploy = async ({
63
variable1, variable2, tokenAddress, callback,
64
}) => {
65
const params = [variable1, variable2, tokenAddress];
66
const res = await this.__deploy(params, callback);
67
this.params.contractAddress = res.contractAddress;
68
/* Call to Backend API */
69
await this.__assert();
70
return res;
71
};
72
73
/**
74
* @function
75
* @return ERC20Contract|undefined
76
*/
77
getERC20Contract = () => this.params.ERC20Contract;
78
79
}
80
81
export default X;
Copied!
4 - Export the Object at src/models/index.js
src/models/index.js
1
...
2
import X from './X';
3
4
export {
5
...
6
X
7
}
Copied!
5 - Create Unit tests tests/x.js
The Template below should be used to start the object of unit tests
tests/x.js
1
import { expect, assert } from "chai";
2
import moment from "moment";
3
import delay from "delay";
4
import { mochaAsync } from "./utils";
5
import { ERC20Contract, X } from "..";
6
import Numbers from "../src/utils/Numbers";
7
8
const testConfig = {test : true, localtest : true};
9
10
context("X Contract", async () => {
11
let erc20Contract;
12
let xContract;
13
let deployed_tokenAddress, contractAddress, deployed_contractAddress;
14
let userAddress;
15
16
before(async () => {
17
erc20Lock = new xContract(testConfig);
18
userAddress = await xContract.getUserAddress(); // local test with ganache
19
});
20
21
it(
22
"should deploy a new ERC20Contract",
23
mochaAsync(async () => {
24
// Create Contract
25
erc20Contract = new ERC20Contract(testConfig);
26
expect(erc20Contract).to.not.equal(null);
27
// Deploy
28
let res = await erc20Contract.deploy({
29
name: "test",
30
symbol: "test",
31
cap: Numbers.toSmartContractDecimals(100000000, 18),
32
distributionAddress: userAddress,
33
});
34
await erc20Contract.__assert();
35
deployed_tokenAddress = erc20Contract.getAddress();
36
expect(res).to.not.equal(false);
37
expect(deployed_tokenAddress).to.equal(res.contractAddress);
38
})
39
);
40
41
it(
42
"should start the X",
43
mochaAsync(async () => {
44
xContract = new X(testConfig);
45
expect(xContract).to.not.equal(null);
46
})
47
);
48
49
it(
50
"should deploy X Contract",
51
mochaAsync(async () => {
52
// Create Contract
53
let testConfig2 = { ...testConfig, tokenAddress: deployed_tokenAddress };
54
xContract = new X(testConfig2);
55
// Deploy
56
let res = await x.deploy();
57
await x.__assert();
58
59
contractAddress = erc20Lock.getAddress();
60
deployed_contractAddress = erc20Lock.getAddress();
61
assert.equal(
62
deployed_tokenAddress,
63
erc20Lock.getERC20Contract().getAddress(),
64
"token address should match"
65
);
66
expect(res).to.not.equal(false);
67
})
68
);
69
70
71
it(
72
"should set do action1",
73
mochaAsync(async () => {
74
// maxTokenAmount = 7000;
75
let res = await xContract.action1({
76
itemId: 1,
77
});
78
79
expect(res).to.not.equal(false);
80
})
81
);
82
}
83
Copied!
6 - Import them at tests/index.js
src/models/index.js
1
context("Unit Tests", async () => {
2
...
3
require("./x");
4
});
5
Copied!
7 - To test open 2 terminals
Terminal 1 (Running Ganache Local EVM)
1
npm run ganache:start
Copied!
Terminal 2 (Running the unit tests created)
1
npm run test
Copied!
Last modified 1mo ago
Copy link