Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For the Tact code in 03 - Contract Message, here is my suggestion: #1

Open
howardpen9 opened this issue Mar 19, 2023 · 1 comment
Open

Comments

@howardpen9
Copy link
Collaborator

Target:

  • the original code didn't properly set with target as TargetValue(since it is Int)
  • the second contract with message Reach will need parse the counter contract address. To better understanding and implement / test on test-net. here is my update:

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()
            });
        }
    }
}
@howardpen9
Copy link
Collaborator Author

Basically, we deployed the Counter contract initially, and send the query and successfully reply a series of reply until the value of counter contract is higher the target_value

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant