import "@stdlib/deploy";
message CounterValue {
value: Int as uint32;
}
////////////////////////////////////////////////////////////////////////////
// this is our famous Counter contract, we've seen it before
// this contract is very annoying, it only allows to increment +1 at a time!
contract Counter with Deployable {
val: Int as uint32;
init() {
self.val = 0;
}
// step 6: this contract allows anyone to ask it to increment by 1 (ie. the other contract)
receive("increment") {
self.val = self.val + 1;
reply(CounterValue{value: self.val}.toCell());
}
// step 3: this contract replies with its current value to anyone asking (ie. the other contract)
receive("query") {
reply(CounterValue{value: self.val}.toCell());
}
get fun value(): Int {
return self.val;
}
}
message Reach {
target_value: Int as uint32;
}
////////////////////////////////////////////////////////////////////////////
// let's write a second helper contract to make our lives a little easier
// it will keep incrementing the previous contract as many times as we need!
contract BulkAdder with Deployable {
target_value: Int as uint32;
init() {
self.target_value = 10;
}
// step 1: users will send this message to tell us what target value we need to reach
receive(msg: Reach) {
self.target_value = msg.target_value;
let CounterAddressInit: StateInit = initOf Counter();
// step 2: this contract will query the current counter value from the other contract
send(SendParameters{
to: contractAddress(CounterAddressInit),
value: 0, /// TODO: https://github.com/tact-lang/tact/issues/31
mode: SendRemainingValue + SendIgnoreErrors, /// TODO: issues/31
body: "query".asComment(),
data: CounterAddressInit.data,
code: CounterAddressInit.code
});
}
// step 4: the other contract will tell us what is its current value by sending us this message
receive(msg: CounterValue) {
if (msg.value < self.target_value) {
// step 5: if its value is too low, send it another message to increment it by +1 more
send(SendParameters{
to: sender(),
value: 0, /// TODO: same issue 31
mode: SendRemainingValue + SendIgnoreErrors, /// TODO: https://github.com/tact-lang/tact/issues/31
body: "increment".asComment()
});
}
}
}