Skip to content

Commit

Permalink
Add truffle-v4 example
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor committed Apr 16, 2020
1 parent 3946dcc commit 7d34010
Show file tree
Hide file tree
Showing 14 changed files with 952 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol linguist-language=Solidity
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ packages/target-truffle-v5-test/build
packages/target-truffle-v5-test/contracts
packages/typechain/README.md
examples/web3-v1/types
examples/truffle-v4/types
examples/truffle-v4/build
22 changes: 22 additions & 0 deletions examples/truffle-v4/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2018 Truffle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

8 changes: 8 additions & 0 deletions examples/truffle-v4/contracts/ConvertLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
pragma solidity >=0.4.25 <0.7.0;

library ConvertLib{
function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount)
{
return amount * conversionRate;
}
}
34 changes: 34 additions & 0 deletions examples/truffle-v4/contracts/MetaCoin.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
pragma solidity >=0.4.25 <0.7.0;

import "./ConvertLib.sol";

// This is just a simple example of a coin-like contract.
// It is not standards compatible and cannot be expected to talk to other
// coin/token contracts. If you want to create a standards-compliant
// token, see: https://github.com/ConsenSys/Tokens. Cheers!

contract MetaCoin {
mapping (address => uint) balances;

event Transfer(address indexed _from, address indexed _to, uint256 _value);

constructor() public {
balances[tx.origin] = 10000;
}

function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
if (balances[msg.sender] < amount) return false;
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Transfer(msg.sender, receiver, amount);
return true;
}

function getBalanceInEth(address addr) public view returns(uint){
return ConvertLib.convert(getBalance(addr),2);
}

function getBalance(address addr) public view returns(uint) {
return balances[addr];
}
}
18 changes: 18 additions & 0 deletions examples/truffle-v4/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pragma solidity >=0.4.25 <0.7.0;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}

constructor() public {
owner = msg.sender;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
}
8 changes: 8 additions & 0 deletions examples/truffle-v4/migrations/1_initial_migration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const Migrations = artifacts.require('Migrations')

module.exports = function(deployer) {
deployer.deploy(Migrations)
} as Truffle.Migration

// because of https://stackoverflow.com/questions/40900791/cannot-redeclare-block-scoped-variable-in-unrelated-files
export {}
11 changes: 11 additions & 0 deletions examples/truffle-v4/migrations/2_deploy_contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const ConvertLib = artifacts.require('ConvertLib')
const MetaCoin = artifacts.require('MetaCoin')

module.exports = function(deployer) {
deployer.deploy(ConvertLib)
deployer.link(ConvertLib, MetaCoin)
deployer.deploy(MetaCoin)
} as Truffle.Migration

// because of https://stackoverflow.com/questions/40900791/cannot-redeclare-block-scoped-variable-in-unrelated-files
export {}
17 changes: 17 additions & 0 deletions examples/truffle-v4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "truffle-v4",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"generate-types": "typechain --target=truffle-v4 'build/contracts/*.json'",
"postinstall": "truffle compile && yarn generate-types",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@typechain/truffle-v4": "^1.0.0-beta",
"truffle": "4.1.17",
"typechain": "2.0.0-beta1",
"typescript": "^3.8.3"
}
}
40 changes: 40 additions & 0 deletions examples/truffle-v4/test/metacoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const MetaCoin = artifacts.require("MetaCoin");

contract('MetaCoin', (accounts) => {
it('should put 10000 MetaCoin in the first account', async () => {
const metaCoinInstance = await MetaCoin.deployed();
const balance = await metaCoinInstance.getBalance(accounts[0]);

assert.equal(balance.toString(), "10000", "10000 wasn't in the first account");
});
it('should call a function that depends on a linked library', async () => {
const metaCoinInstance = await MetaCoin.deployed();
const metaCoinBalance = (await metaCoinInstance.getBalance(accounts[0])).toNumber();
const metaCoinEthBalance = (await metaCoinInstance.getBalanceInEth(accounts[0])).toNumber();

assert.equal(metaCoinEthBalance, 2 * metaCoinBalance, 'Library function returned unexpected function, linkage may be broken');
});
it('should send coin correctly', async () => {
const metaCoinInstance = await MetaCoin.deployed();

// Setup 2 accounts.
const accountOne = accounts[0];
const accountTwo = accounts[1];

// Get initial balances of first and second account.
const accountOneStartingBalance = (await metaCoinInstance.getBalance(accountOne)).toNumber();
const accountTwoStartingBalance = (await metaCoinInstance.getBalance(accountTwo)).toNumber();

// Make transaction from first account to second.
const amount = 10;
await metaCoinInstance.sendCoin(accountTwo, amount, { from: accountOne });

// Get balances of first and second account after the transactions.
const accountOneEndingBalance = (await metaCoinInstance.getBalance(accountOne)).toNumber();
const accountTwoEndingBalance = (await metaCoinInstance.getBalance(accountTwo)).toNumber();


assert.equal(accountOneEndingBalance, accountOneStartingBalance - amount, "Amount wasn't correctly taken from the sender");
assert.equal(accountTwoEndingBalance, accountTwoStartingBalance + amount, "Amount wasn't correctly sent to the receiver");
});
});
14 changes: 14 additions & 0 deletions examples/truffle-v4/truffle-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require('ts-node/register/transpile-only')

module.exports = {
test_file_extension_regexp: /.*\.ts$/,
migrations_file_extension_regexp: /.*\.ts$/,

networks: {
development: {
network_id: 1337,
host: 'localhost',
port: 8545,
},
},
}
File renamed without changes.
Loading

0 comments on commit 7d34010

Please sign in to comment.