Skip to content

Commit

Permalink
Called-on-contract example (TrueFiEng#288)
Browse files Browse the repository at this point in the history
Fix contracts
  • Loading branch information
VladStarostenko authored May 28, 2020
1 parent adf8d24 commit 617e519
Show file tree
Hide file tree
Showing 10 changed files with 8,166 additions and 1 deletion.
5 changes: 5 additions & 0 deletions examples/called-on-contract/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const baseConfig = require('../../.eslintrc.json')

module.exports = {
...baseConfig,
}
2 changes: 2 additions & 0 deletions examples/called-on-contract/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
build
27 changes: 27 additions & 0 deletions examples/called-on-contract/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "example-basic",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "export NODE_ENV=test && mocha",
"build": "waffle waffle.json",
"lint": "eslint '{src,test}/**/*.ts'",
"lint:fix": "eslint --fix '{src,test}/**/*.ts'"
},
"devDependencies": {
"@types/chai": "^4.2.3",
"@types/mocha": "^5.2.7",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"chai": "^4.2.0",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.20.2",
"ethereum-waffle": "2.5.0",
"ethers": "4.0.47",
"mocha": "^7.1.2",
"ts-node": "^8.9.1",
"typescript": "^3.8.3",
"@openzeppelin/contracts": "^3.0.1"
}
}
22 changes: 22 additions & 0 deletions examples/called-on-contract/src/AmIRichAlready.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
pragma solidity ^0.6.2;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract BasicToken is ERC20 {
constructor(uint256 initialBalance) ERC20("Basic", "BSC") public {
_mint(msg.sender, initialBalance);
}
}

contract AmIRichAlready {
BasicToken private tokenContract;
uint private constant RICHNESS = 1000000 * 10 ** 18;
constructor (BasicToken _tokenContract) public {
tokenContract = _tokenContract;
}

function check() public view returns (bool) {
uint balance = tokenContract.balanceOf(msg.sender);
return balance > RICHNESS;
}
}
28 changes: 28 additions & 0 deletions examples/called-on-contract/test/AmIRichAlready.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {expect, use} from 'chai';
import {Contract, utils} from 'ethers';
import {deployContract, MockProvider, solidity} from 'ethereum-waffle';

import BasicToken from '../build/BasicToken.json';
import AmIRichAlready from '../build/AmIRichAlready.json';

use(solidity);

describe('Am I Rich Already', () => {
let ERC20: Contract;
let contract: Contract;
const [wallet] = new MockProvider().getWallets();

it('returns false if the wallet has less then 1000000 coins', async () => {
ERC20 = await deployContract(wallet, BasicToken, [utils.parseEther('999999')]);
contract = await deployContract(wallet, AmIRichAlready, [ERC20.address]);
expect(await contract.check()).to.be.equal(false);
expect('balanceOf').to.be.calledOnContract(ERC20);
});

it('returns true if the wallet has at least 1000000 coins', async () => {
ERC20 = await deployContract(wallet, BasicToken, [utils.parseEther('1000001')]);
contract = await deployContract(wallet, AmIRichAlready, [ERC20.address]);
expect(await contract.check()).to.equal(true);
expect('balanceOf').to.be.calledOnContractWith(ERC20, [wallet.address]);
});
});
4 changes: 4 additions & 0 deletions examples/called-on-contract/test/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r ts-node/register/transpile-only
--timeout 50000
--no-warnings
test/**/*.test.{js,ts}
3 changes: 3 additions & 0 deletions examples/called-on-contract/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}
6 changes: 6 additions & 0 deletions examples/called-on-contract/waffle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerType": "solcjs",
"compilerVersion": "0.6.2",
"sourceDirectory": "./src",
"outputDirectory": "./build"
}
Loading

0 comments on commit 617e519

Please sign in to comment.