Magnet
  • Welcome to Magnet
  • Overview
    • Magnet Network Features
      • Magnet Coretime
      • Coretime Order Control and Assurance
      • Workflows of Magnet Network
      • Profit Calcuation and Distribution
    • Magnet Stack Features
      • Magnet Stack Coretime Modules
  • Product Guides
    • Setup a Local Node
      • Installing Substrate Dependencies
      • Launching the Local Relay Chain
      • Build the Magnet Node
      • Prepare the parachain Collator
      • Register on the Local Relay Chain
    • Deploy Contracts on Magnet Network
      • EVM Contract Deployment
      • WASM Contract Deployment
      • MoveVM Contract Deployment
    • Bridge Asset
      • Bridging Asset between EVM and Substrate
      • Bridging Asset with XCM
    • Develop Coretime-Ready chain with Magnet Stack
      • Preparation: Reserve a ParaID
      • Preparation: Generate the Collator's Address
      • Preparation: Purchase Bulk Core
      • Using Magnet Stack : Coretime Configuration
      • Using Magnet Stack : Virtual Machine Configuration
      • Using Magnet Stack : Functional Models
      • Using Magnet Stack : Summary
      • Deployment & Connection to Rocococ
        • Define the collator's address
Powered by GitBook
On this page
  1. Product Guides
  2. Deploy Contracts on Magnet Network

WASM Contract Deployment

PreviousEVM Contract DeploymentNextMoveVM Contract Deployment

Last updated 9 months ago

Smart Contract Runtime Environment

Magnet runtimes are based on Substrate, incorporate pallet-contracts, a sandboxed environment used to deploy and execute WebAssembly smart contracts. Any language that compiles to Wasm may be deployed and run on this Wasm Virtual Machine, however, the code should be compatible with the pallet-contracts .

To avoid unnecessary complexity, and writing boilerplate code, the most appropriate method of building will involve the use of an eDSL specifically targeting pallet-contracts, such as [ink!] (based on Rust), or [ask!] (based on AssemblyScript), or possibly others as the ecosystem grows.

After compilation, a Wasm blob can then be deployed and stored on-chain.

Execution Engine

Pallet-contracts uses as a Wasm interpreter to execute Wasm smart contract blobs. Although there is a faster JIT interpreter such as available in the native runtime, smart contracts are an untrusted environment which require a higher degree of correctness of interpretation, which makes wasmi a more suitable option.

Two-step Deployment of Contracts

The contract code (Wasm blob), and contract address and storage are decoupled from one another other, so require two steps to deploy a new contract on-chain:

  1. First, upload the Wasm contract on-chain (every contract Wasm code has a code_hash as an identifier).

  2. Second, instantiate the contract - it will create an address and storage for that contract.

  3. Anyone can instantiate a contract based on its code_hash.

There are several benefits of decoupling the contract code from the address/storage:

  • To save space on-chain. Since a contract can have several constructors and instantiations, a redeployment will create a new instance based on the same underlying code. Think about standardized tokens, that will have one code_hash & blob living on-chain, and as many instantiations as are needed, rather than having to upload code with each new instantiation (for example, on Ethereum).

  • To instantiate a new contract using code within an existing smart contract (see the delegator example), code_hash is all that is needed.

  • Some standard contracts will only be uploaded on-chain once, preventing users from having to pay gas costs for uploading new code.

  • Update contract code for an address: replace the contract code at the specified address with new code. Storage and balances will be preserved.

API
wasmi
wasmtime