Skip to content
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SmartContract
# Requirements
- Truffle Framework
```
npm install truffle -g
```
- [Ganache](https://truffleframework.com/ganache).

# Instalation
```
npm intall
truffle compile
```
# To Run
- Run Ganache first and after
- Execute command
```
node app.js
```
81 changes: 81 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const Web3 = require('web3');
const fs = require('fs');

const account = {
address: '0x6D2D0C8d05D39B96FEf53a74B61e9974C0029a00',
key:'79f6211271c58baf8f2eb17651881a935584e845f06589dcfde811269f00c3ca'
};

const balancesheet = {
'0xBFc79d03a9B592F93edb677e7815F9DA41C14475': 1000,
'0xF215e07458aa6ad6472961Ec407FAEDb65E5188C': 2000,
'0x422d16efDB1F60379E82a0a5EC8A71C35334790D': 3000,
'0x7158d92Ac4dbAd955E01DDa0a3b0b014224D055e': 4000,
'0x0AEDACa4451B87DB1704b9379f544785b46E2f02': 5000
};

const selectedHost = 'http://127.0.0.1:7545';
const web3 = new Web3(new Web3.providers.HttpProvider(selectedHost));

run();

//main function
async function run(){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

async 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one suggestion; since we're using the functional programming paradigm we really like to follow the principles from the style of programming.

one of the suggestion is to use name binding. This applies for functions, as well.

This can be rewritten like this:

const run = async () => {}

let b_addresses = Object.keys(balancesheet);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer const over let. It's nice to keep reference transparency

let b_values = Object.values(balancesheet);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here const

//Deploy CappedMintableToken
let CappedMintableToken = await deployContract('./build/contracts/CappedMintableToken.json', account, [1000000]);

//Deploy TokenDistribution
let TokenDistribution = await deployContract('./build/contracts/TokenDistribution.json', account, [CappedMintableToken.options.address]);


await CappedMintableToken.methods.mint(TokenDistribution.options.address, 15000).send({from: account.address});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

await CappedMintableToken.methods.mint(TokenDistribution.options.address, 15000, {from: account.address})


let balance = await CappedMintableToken.methods.balanceOf(TokenDistribution.options.address).call();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Balance is used as a variable the value changes.

console.log('Total balance: ' + balance);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

console.log should be avoided in the code :)

try{
await TokenDistribution.methods.distribute(b_addresses,b_values).send({from: account.address, gas: 800000});
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 await TokenDistribution.methods.distribute(b_addresses,b_values, {from: account.address, gas: 800000})

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work without send(), I think it's because of the we3js version.

}catch(err){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch on a new line

console.log(err);
}
for(let i=0; i<b_addresses.length; i++){
balance = await CappedMintableToken.methods.balanceOf(b_addresses[i]).call();
console.log(i+' ' +b_addresses[i] +': '+ balance);
}
balance = await CappedMintableToken.methods.balanceOf(TokenDistribution.options.address).call();
console.log('Total balance: ' + balance);
};

//helper function to deloy contracts
function deployContract(contractPath, account, args){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const deployContract = (contractPath, account, args) => {}

return new Promise((resolve,reject)=>{
// Read the JSON file contents
let contractJsonContent = fs.readFileSync(contractPath, 'utf8');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use promisify https://nodejs.org/dist/latest-v8.x/docs/api/util.html and then use async/await

let jsonOutput = JSON.parse(contractJsonContent);

// Retrieve the ABI
let abi = jsonOutput.abi;

let bytecode = jsonOutput.bytecode;
// console.log(bytecode);

let tokenContract = web3.eth.Contract(abi);

tokenContract.options.data = bytecode;

tokenContract.deploy({
arguments: args
})
.send({
from: account.address,
gas: 2000000,
})
.then((newContractInstance) => {
resolve(newContractInstance);
}).catch(err=>{
console.log(err);
});
});
}

12 changes: 12 additions & 0 deletions contracts/CappedMintableToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity ^0.5.0;

import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Capped.sol";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import "openzeppelin-solidity/token/ERC20/ERC20Capped.sol";


contract CappedMintableToken is ERC20Capped{

constructor(uint256 cap) ERC20Capped(cap) public {}

function mint(address _to, uint256 _amount) public returns (bool){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove this function because it will be inherited from the ERC20Capped

return super.mint(_to,_amount);
}
}
16 changes: 16 additions & 0 deletions contracts/TokenDistribution​.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pragma solidity ^0.5.0;

import "./CappedMintableToken.sol";

contract TokenDistribution{
address addr;

constructor(address _address) public{
addr = _address;
}
function distribute(address[] memory contributors, uint256[] memory balances) public {
for(uint256 i=0; i<contributors.length; i++){
CappedMintableToken(addr).transfer(contributors[i], balances[i]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitespace missing

}
}
}
7 changes: 5 additions & 2 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const Migrations = artifacts.require("Migrations");
const TokenDistribution = artifacts.require("TokenDistribution");
const CappedMintableToken = artifacts.require("CappedMintableToken");

module.exports = function(deployer) {
deployer.deploy(Migrations);
deployer.deploy(CappedMintableToken, 1000000).then(()=>{
deployer.deploy(TokenDistribution, CappedMintableToken.address);
});
};
Loading