Blockchain & Solidity Program Lab Manual
ISBN 9788119221646

Highlights

Notes

  

Prog. 17:: Ethereum program on Solidity to check account details

Step 2: Install the truffle in the project directory

Step 3: Open the Sublime Text 3 editor

Step 4: Unbox the all dependencies files in the transaction directory

Step 5: Write the Solidity Programs MySwap.sol

pragma solidity 0.5.16; 
import “./Vikram.sol”;

contract MySwap {

string public name = “MySwap Ultimate Exchange”; 
Vikram public token;

uint public rate = 100;

event TokenPurchased 
(address account, address token,

uint amount, uint rate

);

function buyTokens(address) public payable

{

// Calculate the number of tokens to buy uint tokenAmount = msg.value*rate;

// Require that myswap has enough tokens

require(token.balanceOf(address(this)) >= tokenAmount);

// Transfer tokens to the user token.transfer(msg.sender, tokenAmount);

// Emit an event

emit TokenPurchased(msg.sender, address(token), tokenAmount, rate);

}

Vikram.sol

pragma solidity 0.5.16;

contract Vikram

{

string public name = “Vikram”; 
string public symbol = “SBN”; 
uint256 public decimals = 18;

uint256 public totalSupply = 1000000000000000000000000;

event Transfer(

address indexed _from, 
address indexed _to, 
uint256 _value

);

event Approval(

address indexed _owner, address indexed _spender, 
uint256 _value

);

mapping(address => uint256) public balanceOf;

mapping(address => mapping(address => uint256)) public allowance;

constructor() public {balanceOf[msg.sender] = totalSupply;

}

function transfer(address _to, uint256 _value) public returns (bool success)

{

require(balanceOf[msg.sender] >= _value);

balanceOf[msg.sender] -= _value; balanceOf[_to] += _value;

emit Transfer(msg.sender, _to, _value); 
return true;

}

function approve(address _spender, uint256 _value) public returns (bool success)

{

allowance[msg.sender][_spender] = _value; 
emit Approval(msg.sender, _spender, _value);

return true;

}

function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)

{

require(_value <= balanceOf[_from]);

require(_value <= allowance[_from][msg.sender]);

balanceOf[_from] -= _value; balanceOf[_to] += _value;

allowance[_from][msg.sender] -= _value; emit Transfer(_from, _to, _value);

return true;

}

}

Test>MyTest.js

const {assert} = require(‘chai’);

const Vikram= artifacts.require(“Vikram”);

const MySwap = artifacts.require(“MySwap”);

require(‘chai’)

.use(require(‘chai-as-promised’))

.should()

function tokens(n)

{

return web3.utils.toWei(n, ‘ether’);

}

contract(‘MySwap’, (accounts) =>

{

let token, myswap

before(async() =>

{

token = await Vikram.new()

myswap = await MySwap.new()

await token.transfer(myswap.address, tokens(‘1000000’))

})

describe(‘Vikram deployment’, async() =>

{

it(‘contract has a name’, async() =>

{

const name = await token.name() 
assert.equal(name, ‘Saubaan 2’)

})

})

describe(‘MySwap deployment’, async() =>

{

it(‘contract has a name’, async() =>

{

const name = await myswap.name() 
assert.equal(name, ‘MySwap Ultimate Exchange’)

})

it(‘contract has tokens’, async() =>

{

let balance = await token.balanceOf(myswap.address) 
assert.equal(balance.toString(), tokens(‘1000000’))

})

})

describe (‘buyTokens()’, async() =>

{

let result

before(async() =>

{

// pUrchase tokens before each example

result = await myswap.buyTokens

({

from: accounts[1], value: web3.utils.toWei(‘1’, ‘ether’)

})

})

it(‘Allows user to instantly buy tokens from myswap for a fixed price ‘,async() =>

{

let investorBalance = await token.balanceOf(investor) 
assert.equal(investorBalance.toString(), tokens(‘100’))

// Check myswap balance after purchase let myswapBalance

myswapBalance = await token.balanceOf(myswap.address) 
assert.equal(myswapBalance.toString(), tokens(‘99900’)) 
myswapbalance = await web3.eth.getBalance(myswap.address) 
assert.equal(myswapBalance.toString(), web3.utils.toWei(‘1’, ‘ether’))

})

})

})

Step 5: Write the migration files

Step 6: Compile the contracts

Step 7: Deploy the Contract on Ethereum