Rule-based smart contract

Smart contracts a rule-based systems. The Second State smart contract platform supports a formal rules language inside smart contracts. The rules language makes it easy to write rigorous and correct smart contracts, and in turn, smart contracts make business rules transparent and more collaborative. The complete source code for this example is available here.

Access the BUIDL IDE from your browser: https://buidl.secondstate.io/

The rule-based smart contract in BUIDL

Let’s first see how the DApp works. It show a contract that calculates rebates for a customer based on rules. The customer gets 5% rebate, up to $100, for every purchase that is $100 or more. You can click on the Add button to simulate a purchase made by this user. The contract automatically computes the rebate as soon as a new purchase is made.

Step 1: Copy and paste the following code into contract tab

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

The important code segment is at the end of the contract, where we specify how the rebates should be given in a set of formal rules named computeRebate. The contract uses factInsert, factDelete, and fireAllRules built-in functions to manage the data and execution of this rule. For more details, you can read the Lity rules documentation.

Compile and deploy the smart contract via the Compile and Deploy buttons. Notice that the contract requires a constructor parameter. The parameter is a person's name. This contract represents his or her shopping history.

Step 2: Copy and paste the follow HTML code into the dapp -> HTML tab

<!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>

The HTML page presents a UI that allows users to add purchases for this person. With each purchase, the rebate is re-calculated according to the rule.

Step 3: Copy and paste the following into the dapp -> JS tab

/* Don't modify */
var abi = [{"constant":true,"inputs":[],"name":"getInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"int256"},{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_price","type":"int256"}],"name":"buy","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_name","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];
var bytecode = '608060405234801561001057600080fd5b50604051611107380380611107833981018060405281019080805182019291905050508060009080519060200190610049929190610142565b5060408051908101604052806000815260200160008152506003600082015181600001556020820151816001015590505060037f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d6554156100a557fe5b807f1478fcf6491d782c04fad7bec38b59739195d42968b9636c0e1bbff8e1f86966187f92919971c96557dd80d467a7a3aa1c76c5084e9c5e36475d375f96bc71084e509055807f92919971c96557dd80d467a7a3aa1c76c5084e9c5e36475d375f96bc71084e50805460010180915590817f92919971c96557dd80d467a7a3aa1c76c5084e9c5e36475d375f96bc71084e5001555050506101e7565b82805460018160011615610100020d166002900490600052602060002090601f016020900481019282601f1061018357805160ff19168380011785556101b1565b828001600101855582156101b1579182015b828111156101b0578251825591602001919060010190610195565b5b5090506101be91906101c2565b5090565b6101e491905b808211156101e05760008160009055506001016101c8565b5090565b90565b610f11806101f66000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635a9b0b8914610051578063e9403013146100ef575b600080fd5b34801561005d57600080fd5b50610066610bfa565b6040518080602001848152602001838152602001828103825285818151815260200191508051906020019080838360005b838110156100b2578082015181840152602081019050610097565b50505050905090810190601f1680156100df5780820d805160018360200d6101000a0d1916815260200191505b5094505050505060405180910390f35b3480156100fb57600080fd5b5061011a60048036038101908080359060200190929190505050610cb4565b005b60606040519081016040527faf9cba60e1567612cf154e188b8cac218f3b6bcab4b2094e233f5fc1c5dc7f41556101516101e5565b7faf9cba60e1567612cf154e188b8cac218f3b6bcab4b2094e233f5fc1c5dc7f4154806020015160005b8181106001186101bc578281602002905101517f477123eff2f24747a3852f6d785698bd4ee71f1971f13a82f5e9484d485b637c186000905560010161017b565b50505060007f85ea6a8417c1a3040114bffb390741801ff1f43b7c4805046b8bfee2935be26b55565b6108df565b60606040519081016040527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8db557ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8da54806020015160005b8181106001186103155782816020029051015160648160010154121561030c577ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8db548181602001518260400151146001186102e8578160400151600101600202829080826040015260200260405190810160405281518183528260200151602002919060005b838110156102e25780820151818401526020810190506102c7565b50505050505b81806020015160010180916020015282906001900360200290510190816000015250505b50600101610241565b505050565b60606040519081016040527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d8557ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d754806020015160005b81811060011861045a578281602002905101516064816000015412158160010160009054906101000a900460ff16151615610451577ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d85481816020015182604001511460011861042d578160400151600101600202829080826040015260200260405190810160405281518183528260200151602002919060005b8381101561042757808201518184015260208101905061040c565b50505050505b81806020015160010180916020015282906001900360200290510190816000015250505b50600101610371565b505050565b60606040519081016040527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8da5560007f92919971c96557dd80d467a7a3aa1c76c5084e9c5e36475d375f96bc71084e50545b81811115610595577f92919971c96557dd80d467a7a3aa1c76c5084e9c5e36475d375f96bc71084e508260010101547ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8da54908160200151826040015114600118610567578160400151600101600202829080826040015260200260405190810160405281518183528260200151602002919060005b83811015610561578082015181840152602081019050610546565b50505050505b81806020015160010180916020015282906001900360200290510190816000015250508160010191506104b1565b5050565b60606040519081016040527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d75560007f4164101274fa86e8bdab904be403956c23377d6ce0b8a53d31714ce7d4fc9c35545b818111156106cf577f4164101274fa86e8bdab904be403956c23377d6ce0b8a53d31714ce7d4fc9c358260010101547ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d7549081602001518260400151146001186106a1578160400151600101600202829080826040015260200260405190810160405281518183528260200151602002919060005b8381101561069b578082015181840152602081019050610680565b50505050505b81806020015160010180916020015282906001900360200290510190816000015250508160010191506105eb565b5050565b60606040519081016040527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8dc557ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8db54806020015160005b81811060011861084e578281602002905101517ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d954806020015160005b81811060011861084257828160200290510180600001519050600115610839577ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8dc548186826020015183604001511460011861080f578260400151600101600202839080826040015260400260405190810160405281518183528260200151604002919060005b838110156108095780820151818401526020810190506107ee565b50505050505b82806020015160010180916020015283906001900360400290510190816020015290816000015250505b50600101610767565b5050505060010161072a565b505050565b7ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d8547ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d955565b7ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8dc547ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8dd55565b6108e7610599565b6108ef61031a565b6108f7610853565b6108ff61045f565b6109076101ea565b61090f6106d3565b610917610899565b7ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8dd54806020015160005b818110600118610bf557828160400290510160009060007f45be2df21cc5ec523e109cca409f7aeda3a5f06b7b2cedff9120c0698323541655808060000151908060200151905060018260010160006101000a81548160ff0219169083151502179055506064600583600001540e8115156109b857fe5b058160010160008282540c92505081905550817f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d65546109f357fe5b5060017f45be2df21cc5ec523e109cca409f7aeda3a5f06b7b2cedff9120c0698323541655807f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d6554610a4157fe5b5060017f45be2df21cc5ec523e109cca409f7aeda3a5f06b7b2cedff9120c069832354165550507f45be2df21cc5ec523e109cca409f7aeda3a5f06b7b2cedff9120c069832354165415610be857604081207f477123eff2f24747a3852f6d785698bd4ee71f1971f13a82f5e9484d485b637c187f85ea6a8417c1a3040114bffb390741801ff1f43b7c4805046b8bfee2935be26b557ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8da546000816020015260008160400152600090527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d7546000816020015260008160400152600090527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8db546000816020015260008160400152600090527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8d8546000816020015260008160400152600090527ffee65f3aabbcae05825bae01c71a89a25321d8bb875f54a10063a3404766d8dd54600081602001526000816040015260009052610be36101e5565b600191505b50610bf557600101610941565b505050565b6060600080600060036000015460036001015482805460018160011615610100020d166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020d16600290048015610ca25780601f10610c7757610100808354040283529160200191610ca2565b820191906000526020600020905b815481529060010190602001808311610c855782900d601f168201915b50505050509250925092509250909192565b600060408051908101604052808381526020016000151581525060016000820151816000015560208201518160010160006101000a81548160ff02191690831515021790555090505081600360000160008282540c9250508190555060017f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d655415610d3b57fe5b807f1478fcf6491d782c04fad7bec38b59739195d42968b9636c0e1bbff8e1f86966187f4164101274fa86e8bdab904be403956c23377d6ce0b8a53d31714ce7d4fc9c359055807f4164101274fa86e8bdab904be403956c23377d6ce0b8a53d31714ce7d4fc9c35805460010180915590817f4164101274fa86e8bdab904be403956c23377d6ce0b8a53d31714ce7d4fc9c3501555090507f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d655415610dfc57fe5b60017f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d6555610e2861011c565b60007f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d6555807f47ad463029d43bc9ebc5cf4a8070d2c2ec6666ff91ceb7f2b657239dee553d655415610e7657fe5b807f1478fcf6491d782c04fad7bec38b59739195d42968b9636c0e1bbff8e1f86966185460005b8154811015610ebb57818160010101548314610ebd57600101610e9d565bfe5b81546001900382906001010154828282816001018301555050505080546001900390555050505600a165627a7a72305820cba471b8c5808adac80898f67a3f43a1be17e325cc9e67e317f74065eff2d5e20029';
var contract = web3.ss.contract(abi);
var instance = contract.at('0x01e5a4f78e6a074fd9856773811ed070d90a823d');
/* Don't modify */
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);
}

BUIDL pre-fills the JavaScript with the smart contract's compiled artifacts (ABI and bytecode), and its deployed address on the blockchain. The script starts by making a getInfo() call against the contract instance (i.e., a person) via web3 to get the current state of the contract.

The buy() method prompts the user to enter a purchase amount to add to this person. It waits for 2 seconds and then calls getInfo() again to get the updated purchase and rebate information for this person from the blockchain smart contract.

Step 4: Hit the Run button to launch the DApp

You will see the web app running inside the right panel. You can now add purchases to the person, and see his or her rebates change.

There is also a more complex example of the rebate rules, which allows you to create new persons (one contract per person), and then manage all persons in a table. Try the source code here.