aptos/contracts: implement simple example contract

This commit is contained in:
Csongor Kiss 2022-10-14 01:16:59 +00:00 committed by jumpsiegel
parent da39940b13
commit 70ccb69e86
4 changed files with 116 additions and 0 deletions

7
aptos/examples/Makefile Normal file
View File

@ -0,0 +1,7 @@
SUBDIRS := $(wildcard */)
TARGETS := build test
.PHONY: $(TARGETS)
$(TARGETS):
$(foreach dir,$(SUBDIRS),make -C $(dir) $@ &&) true

View File

@ -0,0 +1,14 @@
-include ../../../Makefile.help
.PHONY: artifacts
artifacts: build
.PHONY: build
## Build contract
build:
aptos move compile --save-metadata
.PHONY: test
## Run tests
test:
aptos move test

View File

@ -0,0 +1,11 @@
[package]
name = 'core_messages'
version = '1.0.0'
[dependencies]
Wormhole = { local = "../../wormhole/" }
[addresses]
wormhole = "0xde0036a9600559e295d5f6802ef6f3f802f510366e0c23912b0655d972166017"
deployer = "0x277fa055b6a73c42c0662d5236c65c864ccbf2d4abd21f174a30c8b786eab84b"
core_messages = "0x277fa055b6a73c42c0662d5236c65c864ccbf2d4abd21f174a30c8b786eab84b"

View File

@ -0,0 +1,84 @@
/// A simple contracts that demonstrates how to send messages with wormhole.
module core_messages::sender {
use wormhole::wormhole;
use aptos_framework::coin;
struct State has key {
emitter_cap: wormhole::emitter::EmitterCapability,
}
entry fun init_module(core_messages: &signer) {
// Register ourselves as a wormhole emitter. This gives back an
// `EmitterCapability` which will be required to send messages through
// wormhole.
let emitter_cap = wormhole::register_emitter();
move_to(core_messages, State { emitter_cap });
}
#[test_only]
/// Initialise module for testing.
public fun init_module_test() {
use aptos_framework::account;
// recover the signer for the module's account
let signer_cap = account::create_test_signer_cap(@core_messages);
let signer = account::create_signer_with_capability(&signer_cap);
// then call the initialiser
init_module(&signer)
}
public entry fun send_message(user: &signer, payload: vector<u8>) acquires State {
// Retrieve emitter capability from the state
let emitter_cap = &mut borrow_global_mut<State>(@core_messages).emitter_cap;
// Set nonce to 0 (this field is not interesting for regular messages,
// only batch VAAs)
let nonce: u64 = 0;
let message_fee = wormhole::state::get_message_fee();
let fee_coins = coin::withdraw(user, message_fee);
let _sequence = wormhole::publish_message(
emitter_cap,
nonce,
payload,
fee_coins
);
}
}
#[test_only]
module core_messages::sender_test {
use wormhole::wormhole;
use core_messages::sender;
use aptos_framework::account;
use aptos_framework::aptos_coin::{Self, AptosCoin};
use aptos_framework::coin;
use aptos_framework::signer;
use aptos_framework::timestamp;
#[test(aptos_framework = @aptos_framework, user = @0x111)]
public fun test_send_message(aptos_framework: &signer, user: &signer) {
let message_fee = 100;
timestamp::set_time_has_started_for_testing(aptos_framework);
wormhole::init_test(
22,
1,
x"0000000000000000000000000000000000000000000000000000000000000004",
x"beFA429d57cD18b7F8A4d91A2da9AB4AF05d0FBe",
message_fee
);
sender::init_module_test();
let (burn_cap, mint_cap) = aptos_coin::initialize_for_test(aptos_framework);
// create user account and airdrop coins
account::create_account_for_test(signer::address_of(user));
coin::register<AptosCoin>(user);
coin::deposit(signer::address_of(user), coin::mint(message_fee, &mint_cap));
sender::send_message(user, b"hi mom");
coin::destroy_mint_cap(mint_cap);
coin::destroy_burn_cap(burn_cap);
}
}