Created
December 27, 2021 18:35
-
-
Save villesundell/df23e391c55517a82c3fc3f0645ac4fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2021 Solarius Intellectual Properties Ky | |
// Authors: Ville Sundell | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
module TaoHe::Outbox { | |
use Std::Event; | |
use Std::Signer; | |
use Std::Vector; | |
#[test_only] | |
use TaoHe::Torch; | |
struct Item<Content: key + store> has key, store { | |
from: address, | |
to: address, | |
content: Content | |
} | |
struct Outbox<Content: key + store> has key, store { | |
content: vector<Item<Content>> | |
} | |
struct Put<phantom Content> has key, drop, store { | |
} | |
struct EventHandle<phantom Content: drop + store> has key, store { | |
event_handle: Event::EventHandle<Content> | |
} | |
public fun create<Content: key + store>(account: &signer) { | |
move_to<Outbox<Content>>(account, Outbox<Content> { content: Vector::empty<Item<Content>>() }); | |
move_to<EventHandle<Put<Content>>>(account, EventHandle<Put<Content>> { event_handle: Event::new_event_handle<Put<Content>>(account) } ); | |
} | |
public fun put<Content: key + store>(account: &signer, from: address, to: address, content: Content) acquires EventHandle, Outbox { | |
let outbox_owner = Signer::address_of(account); | |
let event_handle = borrow_global_mut<EventHandle<Put<Content>>>(outbox_owner); | |
let outbox = borrow_global_mut<Outbox<Content>>(outbox_owner); | |
assert!(to != @0x0, 123); | |
Vector::push_back<Item<Content>>(&mut outbox.content, Item<Content>{ from, to, content }); | |
Event::emit_event<Put<Content>>(&mut event_handle.event_handle, Put<Content> {}); | |
} | |
#[test(account = @0x123)] | |
fun test_put(account: signer) acquires EventHandle, Outbox { | |
create<Torch::Torch>(&account); | |
put<Torch::Torch>(&account, @0x123, @0x123, Torch::new()); | |
} | |
public fun get<Content: key + store>(account: &signer, outbox_owner: address, index: u64): Content acquires Outbox { | |
let account_addr = Signer::address_of(account); | |
let outbox = borrow_global_mut<Outbox<Content>>(outbox_owner); | |
let Item<Content>{from, to, content} = Vector::swap_remove<Item<Content>>(&mut outbox.content, index); | |
assert!(from == account_addr || to == account_addr, 123); | |
content | |
} | |
#[test(account = @0x123)] | |
fun test_get(account: signer) acquires EventHandle, Outbox { | |
create<Torch::Torch>(&account); | |
put<Torch::Torch>(&account, @0x123, @0x123, Torch::new()); | |
put<Torch::Torch>(&account, @0x123, @0x123, Torch::new()); | |
let torch1 = get<Torch::Torch>(&account, @0x123, 0); | |
Torch::destroy(torch1); | |
let torch2 = get<Torch::Torch>(&account, @0x123, 0); | |
Torch::destroy(torch2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment