Rule-based smart contract
Last updated
Was this helpful?
Was this helpful?
pragma lity ^1.2.6;
contract Person {
struct Purchase {
int price;
bool rebated;
}
struct Total {
int paid;
int rebate;
}
string name;
Purchase purchase;
Total total;
constructor (string _name) public {
name = _name;
total = Total (0,0);
factInsert total;
}
function buy (int _price) public {
purchase = Purchase (_price, false);
total.paid += _price;
uint256 idx = factInsert purchase;
fireAllRules;
factDelete idx;
}
function getInfo () view public returns (string, int, int) {
return (name, total.paid, total.rebate);
}
rule "computeRebate" when {
p: Purchase(price >= 100, !rebated);
t: Total(rebate < 100);
} then {
p.rebated = true;
t.rebate += p.price * 5 / 100;
update p;
update t;
}
}<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Rule-based rebate</title>
</head>
<body>
<div class="container">
<p><br/>Rebate rules</p>
<ul>
<li>Purchases of $100 or more gets 5% rebate</li>
<li>Each purchase can only get one rebate</li>
<li>Max rebate is $100 per person</li>
</ul>
<p>Use the <b>Add</b> button to add a purchase, and update the rebate.</p>
<h3>Record for <span id="name"></span></h3>
<table class="table">
<tbody id="tbody">
<tr>
<td>Purchases</td>
<td id="purchase"></td>
<td><button class='btn btn-info btn-sm' onclick='buy(this)'>Add</button></td>
</tr>
<tr>
<td>Rebate</td>
<td id="rebate"></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</body>
</html>var contract = window.web3 && web3.ss && web3.ss.contract(abi);
var instance = contract && contract.at(cAddr);
window.addEventListener('web3Ready', function() {
contract = web3.ss.contract(abi);
instance = contract.at(cAddr);
reload();
});
instance.getInfo.call (function (e, r) {
if (e) {
console.log(e);
return;
} else {
console.log(r);
document.querySelector("#name").innerHTML = r[0];
document.querySelector("#purchase").innerHTML = r[1];
document.querySelector("#rebate").innerHTML = r[2];
}
});
function buy (element) {
element.innerHTML = "Wait ...";
var n = window.prompt("Amount paid for purchase:");
n && instance.buy(n);
setTimeout(function () {
instance.getInfo.call (function (e, r) {
if (e) {
console.log(e);
return;
} else {
document.querySelector("#purchase").innerHTML = r[1];
document.querySelector("#rebate").innerHTML = r[2];
element.innerHTML = "Add";
}
});
}, 2 * 1000);
}