PageSourceSearch

https://use.ink/assets/js/941e794a.2e36bad5.js

js use.ink collected 2026-10-03 21:40:10 UTC 5,442 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkink_docs=self.webpackChunkink_docs||[]).push([[219],{65404:(e,s,n)=>{n.d(s,{R:()=>c,x:()=>l});var t=n(36672);const o={},r=t.createContext(o);function c(e){const s=t.useContext(r);return t.useMemo(function(){return"function"==typeof e?e(s):{...s,...e}},[s,e])}function l(e){let s;return s=e.disableParentContext?"function"==typeof e.components?e.components(o):e.components||o:c(e.components),t.createElement(r.Provider,{value:s},e.children)}},95491:(e,s,n)=>{n.r(s),n.d(s,{assets:()=>i,contentTitle:()=>l,default:()=>u,frontMatter:()=>c,metadata:()=>t,toc:()=>a});const t=JSON.parse('{"id":"macros-attributes/selector","title":"#[ink(selector = S:u32)]","description":"Applicable to ink! messages and ink! constructors.","source":"@site/versioned_docs/version-v3/macros-attributes/selector.md","sourceDirName":"macros-attributes","slug":"/macros-attributes/selector","permalink":"/docs/v3/macros-attributes/selector","draft":false,"unlisted":false,"editUrl":"https://github.com/use-ink/ink-docs/edit/master/versioned_docs/version-v3/macros-attributes/selector.md","tags":[],"version":"v3","frontMatter":{"title":"#[ink(selector = S:u32)]","slug":"/macros-attributes/selector"},"sidebar":"reference","previous":{"title":"#[ink(payable)]","permalink":"/docs/v3/macros-attributes/payable"},"next":{"title":"#[ink(storage)]","permalink":"/docs/v3/macros-attributes/storage"}}');var o=n(23420),r=n(65404);const c={title:"#[ink(selector = S:u32)]",slug:"/macros-attributes/selector"},l=void 0,i={},a=[{value:"Examples",id:"examples",level:2},{value:"Controlling the messages selector",id:"controlling-the-messages-selector",level:2}];function d(e){const s={code:"code",h2:"h2",li:"li",p:"p",pre:"pre",ul:"ul",...(0,r.R)(),...e.components};return(0,o.jsxs)(o.Fragment,{children:[(0,o.jsx)(s.p,{children:"Applicable to ink! messages and ink! constructors."}),"\n",(0,o.jsx)(s.p,{children:"By default ink! creates a selector for each message and constructor.\nThis is necessary since the contract is compiled to a Wasm blob and functions are invoked by invoking the\nselector, which identifies a method \u2012 method names are no longer available in these underlying layers."}),"\n",(0,o.jsx)(s.p,{children:"Using this attribute it is possible to specify a concrete dispatch selector for the flagged entity. This allows a contract author to precisely control the selectors of their APIs making it possible to rename their API without breakage."}),"\n",(0,o.jsxs)(s.p,{children:["A selector must be a ",(0,o.jsx)(s.code,{children:"u32"})," decodable integer. For example"]}),"\n",(0,o.jsxs)(s.ul,{children:["\n",(0,o.jsx)(s.li,{children:(0,o.jsx)(s.code,{children:"selector = 0xCAFEBABE"})}),"\n",(0,o.jsx)(s.li,{children:(0,o.jsx)(s.code,{children:"selector = 42"})}),"\n"]}),"\n",(0,o.jsxs)(s.p,{children:["An exception is the fallback selector ",(0,o.jsx)(s.code,{children:"_"}),", allowing contract calls not matching any of the other message selectors to be dispatched to a fallback message. Fallback messages can be ",(0,o.jsx)(s.code,{children:"payable"}),"."]}),"\n",(0,o.jsx)(s.h2,{id:"examples",children:"Examples"}),"\n",(0,o.jsx)(s.pre,{children:(0,o.jsx)(s.code,{className:"language-rust",children:"#[ink(message, selector = 0xC0DECAFE)]\nfn my_message_1(&self) {}\n\n#[ink(message, selector = 42)]\nfn my_message_2(&self) {}\n\n#[ink(message, payable, selector = _)]\nfn my_fallback(&self) {}\n"})}),"\n",(0,o.jsxs)(s.p,{children:["\u2026 then the selector of ",(0,o.jsx)(s.code,{children:"my_message_1"})," is ",(0,o.jsx)(s.code,{children:"[0xC0, 0xDE, 0xCA, 0xFE]"})," and the selector of ",(0,o.jsx)(s.code,{children:"my_message_2"})," is ",(0,o.jsx)(s.code,{children:"[0, 0, 0, 42]"}),"\nsince setting the selector manually overrides the automatically generated selector."]}),"\n",(0,o.jsx)(s.h2,{id:"controlling-the-messages-selector",children:"Controlling the messages selector"}),"\n",(0,o.jsx)(s.p,{children:"Every ink! message and ink! constructor has a selector with which the\nmessage or constructor can be uniquely identified within the ink! smart contract.\nNon-unique message or constructor selector lead to a compile time error.\nThese selectors are mainly used to drive the contract's dispatch upon calling it."}),"\n",(0,o.jsxs)(s.p,{children:["An ink! smart contract author can control the selector of an ink! message or ink!\nconstructor using the ",(0,o.jsx)(s.code,{children:"selector"})," flag. An example is shown below:"]}),"\n",(0,o.jsx)(s.pre,{children:(0,o.jsx)(s.code,{className:"language-rust",children:"use ink_lang as ink;\n\n#[ink::contract]\nmod flipper {\n    #[ink(storage)]\n    pub struct Flipper {\n        value: bool,\n    }\n\n    impl Flipper {\n        #[ink(constructor)]\n        #[ink(selector = 0xDEADBEEF)] // Works on constructors as well.\n        pub fn new(initial_value: bool) -> Self {\n            Flipper { value: initial_value }\n        }\n\n        #[ink(message)]\n        #[ink(selector = 0xCAFEBABE)] // You can either specify selector out-of-line.\n        pub fn flip(&mut self) {\n            self.value = !self.value;\n        }\n\n        #[ink(message, selector = 0xC0DECAFE)] // ...or specify the selector inline.\n        pub fn get(&self) -> bool {\n            self.value\n        }\n    }\n}\n"})})]})}function u(e={}){const{wrapper:s}={...(0,r.R)(),...e.components};return s?(0,o.jsx)(s,{...e,children:(0,o.jsx)(d,{...e})}):d(e)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.