2.9.DeployingSoliditywithweb3 HTML ArithmeticOperations
2.9.DeployingSoliditywithweb3 HTML ArithmeticOperations
//Deploying Contract
var SimpleObj=new web3.eth.Contract(abi);
var deployContractTx = SimpleObj.deploy({data: bytecode });
var accounts;
web3.eth.getAccounts().then((acc) => accounts = acc);
var contractInstance;
deployContractTx.send({from: accounts[0], gas: 1000000}).then((instance) => contractInstance =
instance);
8. If you could able to get 20 as result it is sucessful. you can go for next step.
At Node Prompt copy this address and you can use this in javascript to communicate with this
contract
> contractInstance.address
'0x4cb24294f3Ac145ef1Ef6Ad3cbCCC1e5c3F6867F'
abi = JSON.parse('[{"constant":true,"inputs":[{"name":"_a","type":"uint256"},
{"name":"_b","type":"uint256"}],"name":"multiply","outputs":
[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"},
{"constant":true,"inputs":[{"name":"_a","type":"uint256"},
{"name":"_b","type":"uint256"}],"name":"arithmetics","outputs":
[{"name":"o_sum","type":"uint256"},
{"name":"o_product","type":"uint256"}],"payable":false,"stateMutability":"pure","type":"function"
}]')
function Arithmetics(){
var val1= document.getElementById("num1").value
var val2= document.getElementById("num2").value
//console.log(val1,val2);
contract.methods.arithmetics(val1,val2).call().then((f) => document.write(f[0],"<br>",f[1]))
}
function Multiply(){
var val1= document.getElementById("num1").value
var val2= document.getElementById("num2").value
contract.methods.multiply(val1,val2).call().then((f) => document.write(f));
}
<!DOCTYPE html>
<html>
<head>
<title>My First DApp</title>
<link href='https://fonts.googleapis.com/css?family=Open Sans:400,700' rel='stylesheet'
type='text/css'>
<link href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' rel='stylesheet'
type='text/css'>
</head>
<body class="container">
<h1>A Simple Dapp</h1>
<form id="frm1">
First Number:<br>
<input type="number" id="num1" value="0"><br>
Second Number:<br>
<input type="number" id="num2" value="0"><br><br>
<a href="#" onclick="Arithmetics()" class="btn btn-primary">Arithmetic</a>
<a href="#" onclick="Multiply()" class="btn btn-primary">Multiply</a>
<p id="demo"></p>
</form>
</body>
<script src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]
beta.37/dist/web3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="./index.js"></script>