I enjoy writing custom pallets so I would like to know if instead of writing an ink! smart contract and then compiling it to .wasm and deploying it to the Contracts module/pallet of a Substrate-based chain, would it be possible to instead write the same logic in a custom pallet and then compile that to .wasm, and then deploy it to the Contracts pallet/module of a Substrate-based chain?
From the ink! docs:
ink! is just standard Rust in a well defined "contract format" with specialized #[ink(…)] attribute macros. These attribute macros tell ink! what the different parts of your Rust smart contract represent, and ultimately allow ink! to do all the magic needed to create Substrate compatible Wasm bytecode!
In my opinion is a very interesting question, I hope to see some good ideas answering your question. Here is mine:
As far as I know there is not a direct way to do that, as Luke suggested in the comment one option would be to create a tool that translate the pallet runtime code into the ink! code, and then we will be able to generate a
Wasm binary, a
metadata file (which contains the contract's ABI) and a
.contract file which bundles both.
A few months ago I did that exercise manually, I built a pallet and I convert it into a smart contract in ink!. I was able to reuse most of the code and replace the FRAME Macros with the ink! macros was not a big deal. However I found a couple of stoppers where the translation was not that trivial:
- ensure_root in ink!. (It is possible now. The ink! team implemented a few weeks ago).
- BoundedVec on ink!. (A workaround in the answer).
So keep in mind that some of the things that you can do on pallets will need a very special treatment using ink!, and example might be interacting with other pallets in the runtime.