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
```
68 changes: 68 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const Web3 = require('web3');
const fs = require('fs');
const util = require('util');

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));



//main function
const run = async () => {
const b_addresses = Object.keys(balancesheet);
const b_values = Object.values(balancesheet);
//Deploy CappedMintableToken
const CappedMintableToken = await deployContract('./build/contracts/CappedMintableToken.json', account, [1000000]);

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


await CappedMintableToken.methods.mint(TokenDistribution.options.address, 15000).send({ 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.


try {
await TokenDistribution.methods.distribute(b_addresses, b_values).send({ from: account.address, gas: 800000 });
}
catch (err) {
throw err;
}
for (let i = 0; i < b_addresses.length; i++) {
balance = await CappedMintableToken.methods.balanceOf(b_addresses[i]).call();
}
balance = await CappedMintableToken.methods.balanceOf(TokenDistribution.options.address).call();
};

//helper function to deloy contracts
const deployContract = async (contractPath, account, args) => {
// Read the JSON file contents
const file = util.promisify(fs.readFile);
const contractJsonContent = await file(contractPath);
const jsonOutput = JSON.parse(contractJsonContent);

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

const bytecode = jsonOutput.bytecode;

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

tokenContract.options.data = bytecode;

return tokenContract.deploy({ arguments: args }).send({ from: account.address, gas: 2000000, });
}

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

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

contract CappedMintableToken is ERC20Capped{

constructor(uint256 cap) ERC20Capped(cap) public {}
}
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]);
}
}
}
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