|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +// |
| 5 | +// Copyright (c) DUSK NETWORK. All rights reserved. |
| 6 | + |
| 7 | +//! Contract that provides and example use of the constructor. |
| 8 | +
|
| 9 | +#![no_std] |
| 10 | + |
| 11 | +use piecrust_uplink as uplink; |
| 12 | + |
| 13 | +/// Struct that describes the state of the Constructor contract |
| 14 | +pub struct EmptyConstructor { |
| 15 | + value: u8, |
| 16 | +} |
| 17 | + |
| 18 | +impl EmptyConstructor { |
| 19 | + pub fn init(&mut self) { |
| 20 | + self.value = 0x10; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/// State of the EmptyConstructor contract |
| 25 | +static mut STATE: EmptyConstructor = EmptyConstructor { value: 0x00 }; |
| 26 | + |
| 27 | +impl EmptyConstructor { |
| 28 | + /// Read the value of the constructor contract state |
| 29 | + pub fn read_value(&self) -> u8 { |
| 30 | + self.value |
| 31 | + } |
| 32 | + |
| 33 | + /// Increment the value by 1 |
| 34 | + pub fn increment(&mut self) { |
| 35 | + let value = self.value + 1; |
| 36 | + self.value = value; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Expose `EmptyConstructor::read_value()` to the host |
| 41 | +#[no_mangle] |
| 42 | +unsafe fn read_value(arg_len: u32) -> u32 { |
| 43 | + uplink::wrap_call(arg_len, |_: ()| STATE.read_value()) |
| 44 | +} |
| 45 | + |
| 46 | +/// Expose `EmptyConstructor::increment()` to the host |
| 47 | +#[no_mangle] |
| 48 | +unsafe fn increment(arg_len: u32) -> u32 { |
| 49 | + uplink::wrap_call(arg_len, |_: ()| STATE.increment()) |
| 50 | +} |
| 51 | + |
| 52 | +/// Expose `EmptyConstructor::init()` to the host |
| 53 | +#[no_mangle] |
| 54 | +unsafe fn init(arg_len: u32) -> u32 { |
| 55 | + uplink::wrap_call(arg_len, |()| STATE.init()) |
| 56 | +} |
0 commit comments