Switch
Switch on and off a connected object with a smart contract.
How to control a connected object with a smart contract?
    1.
    write the on/off state of the object in the smart contract
    2.
    make the connected object repeatedly read its state from the smart contract
Indeed the object must be able to actively read the contract storage on a regular basis because the blockchain is passive by nature.
Werenode is a french startup that leverages the power of the Tezos blockchain in order to simplify and reduce the cost of the electric vehicle charging process. Werenode is currently developing a Tezos-based solution to switch on the electric vehicle supply equipment (EVSE).
Werenode
GitHub
The basic idea is to deploy a smart contract that pilots the state of the EVSE:
The smart contract should provide an entry point start which sets the status on. Of course, this should be done if the sufficient amount of tezis is transferred to the contract for the EVSE owner to buy the electricity and pay the maintenance of the equipment.
Rather than a boolean state, the state should be materialized as a date: if this date is in the future the object is on; it is off otherwise.
Basically the start entry converts the transferred tezis to a duration (according to a rate) and the duration to a date in the future.
Say for example the rate is 1.2, meaning the EVSE is switched on for 1h12m for each tezis transferred.
The following code implements this switch:
1
archetype switch
2
3
variable owner : address = @tz1XZ7s6uStC2hZVpPQhXgcdXPwxifByF3Ao
4
5
variable interruption : date = 2020-06-18
6
7
variable rate : rational = 1.2 // in time_unit / tez_unit
8
9
variable time_unit : duration = 1h
10
variable tez_unit : tez = 1tz
11
12
variable user : option<address> = none
13
14
variable read_interval : duration = 5s
15
16
// UTILS
17
18
function get_rate_in_s_by_utz () : rational {
19
var d : int = time_unit;
20
var t : int = tez_unit;
21
return (rate * d / t)
22
}
23
24
function get_return_tz () : tez {
25
var res : int = 1 / get_rate_in_s_by_utz() * (interruption - now);
26
return (res * 1utz)
27
}
28
29
// ENTRIES
30
entry start () {
31
require {
32
r1: now > interruption;
33
}
34
effect {
35
var t : int = transferred;
36
var dur : duration = (get_rate_in_s_by_utz() * t)*1s;
37
if dur > read_interval then (
38
interruption := now + dur + read_interval;
39
user := some(caller)
40
)
41
}
42
}
43
44
entry interrupt () {
45
require {
46
r2: caller = opt_get(user) and now < interruption
47
}
48
effect {
49
transfer (get_return_tz()) to caller;
50
interruption := now - read_interval;
51
}
52
}
53
54
entry collect () {
55
called by owner
56
effect {
57
var keep = 0tz;
58
if now < interruption then
59
keep := get_return_tz();
60
if balance - keep > 0tz then
61
transfer (balance - keep) to owner
62
}
63
}
64
65
entry setunits (dunit : duration, tunit : tez) {
66
called by owner
67
effect {
68
time_unit := dunit;
69
tez_unit := tunit;
70
}
71
}
72
Copied!
The contract is available on the Carthage net to play with:
https://you.better-call.dev/carthagenet/KT1NzLPSERC8Uwsrr8sHRgXmNQ6m724Jdw9Q/storage
you.better-call.dev
This connected online bulb listens to this contract ... Feel free to switch it on and off.
Last modified 1yr ago
Export as PDF
Copy link