# SNS validate and execute functions in Motoko

**URL:** <https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432>\
**Category:** Motoko\
**Created:** [July 16, 2023, 7:29pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432 "2023-07-16T19:29:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mitch](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mitch/32/4826_2.png) [@Mitch](https://forum.dfinity.org/u/Mitch)\
**Post date:** [July 16, 2023, 7:29pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/1 "2023-07-16T19:29:28Z")

</div>

Hey everyone, I’m working on an SNS test flight I have a few questions around executing code via proposals. Let’s say my goal is to run a greet function from a proposal:

```auto
public shared func greet() : async Result.Result<Text,Text>{
        
       
        return #ok("hello");
    };

```

It is my understanding from the [documentation](https://internetcomputer.org/docs/current/developer-docs/integrations/sns/testing/testing-on-mainnet#test-executing-code-on-sns-managed-canisters-via-sns-proposals) that the canister containing the greet function should also contain a validate and execute function, is this correct?

Is there an example in Motoko available anywhere, I’ve only found [this one in rust](https://github.com/dfinity/sns-testing/blob/main/test/src/lib.rs)?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [July 17, 2023, 8:20am UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/2 "2023-07-17T08:20:12Z")

</div>

The ‘execute function’ is the one you’ve already written. And yes, you need to also add a validation function, although it doesn’t need to be on the same canister IIRC, even though that is by far the easiest way to do it.

Since your function does not take any arguments the validation function is pretty useless, but the interface requires it. In your case I’d add this function:

```plaintext
public shared func greet_validate() : async Result.Result<Text,Text>{
        return #ok("unit argument");
    };

```

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mitch/32/4826_2.png) [@Mitch](https://forum.dfinity.org/u/Mitch)\
**Post date:** [July 17, 2023, 2:53pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/3 "2023-07-17T14:53:19Z")

</div>

Ah I see that clears things up for empty arguments.

Let’s say the function did take an argument, is the purpose to validate the payload for parameters I’ve decided on, for example let’s say I want to be sure the payload is only 5 chars long?

Then in the case of a payload, greet\_validate would intake the payload as a string and return an error if the payload is greater than 5 chars?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [July 17, 2023, 2:56pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/4 "2023-07-17T14:56:55Z")

</div>

Yes, you got it 👍

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mitch/32/4826_2.png) [@Mitch](https://forum.dfinity.org/u/Mitch)\
**Post date:** [July 17, 2023, 3:05pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/5 "2023-07-17T15:05:11Z")

</div>

Awesome, out of curiosity, is there some technical concern why we couldn’t just validate within the function it self?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [July 17, 2023, 3:35pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/6 "2023-07-17T15:35:16Z")

</div>

Not 100% sure on that one, but I think part of creating the proposal is to validate the arguments already. There’s no purpose to voting on a proposal that’s invalid. Also, I think the `Ok(string)` is used to display the thing you’re voting on. So e.g. for a new canister wasm you’d feed the wasm into the validate function, which could e.g. check if the wasm is valid, and then return the hex encoding of the hash of the wasm. This way people can verify that you’re indeed voting on the right piece of code, and not some arbitrary other wasm

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mitch/32/4826_2.png) [@Mitch](https://forum.dfinity.org/u/Mitch)\
**Post date:** [July 18, 2023, 1:06am UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/7 "2023-07-18T01:06:08Z")

</div>

Is it possible this is the ok vs Ok issue between rust and motoko?

```auto
The request is being processed...
(
  record {
    command = opt variant {
      Error = record {
        error_message = "1 defects in Proposal:\nError decoding reply from proposal payload validate and render call: Fail to decode argument 0 from table0 to variant { Ok : text; Err : text }";
        error_type = 15 : int32;
      }
    };
  },
)

```

Here is the motoko function I am testing for reference:

```auto
public shared func testSNSFunctionCalls() : async Text {
       
       return "testing 1..2..3..";
    };

    public shared func validate_testSNSFunctionCalls() : async Result.Result<Text, Text> {
       return #ok("testing 1..2..3..");
    };

```

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [July 18, 2023, 6:09am UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/8 "2023-07-18T06:09:37Z")

</div>

Most likely, yes. I think you ‘just’ have to declare your own result type that contains the upper case version…

---

<div class="post-metadata">

**Author:** ![Mitch](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/mitch/32/4826_2.png) [@Mitch](https://forum.dfinity.org/u/Mitch)\
**Post date:** [July 18, 2023, 1:57pm UTC](https://forum.dfinity.org/t/sns-validate-and-execute-functions-in-motoko/21432/9 "2023-07-18T13:57:41Z")

</div>

Worked, thanks again 🙏
