-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[x/programs] rewording for better dev ui (#446)
* Context -> State * programInvoke -> invokeProgram * expose -> public * remove mandatory init * lint * rename and expose macro from sdk * lint + merge imports * remove simulator * rust lint test * remove server files
- Loading branch information
Showing
26 changed files
with
131 additions
and
139 deletions.
There are no files selected for viewing
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
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
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
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package runtime | ||
|
||
import "errors" | ||
|
||
var ErrMissingExportedFunction = errors.New("failed to find exported function") |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,21 @@ | ||
use expose_macro::expose; | ||
use wasmlanche_sdk::store::Context; | ||
use wasmlanche_sdk::types::Address; | ||
use wasmlanche_sdk::{public, store::State, types::Address}; | ||
|
||
/// Initializes the program. This program maps addresses with a count. | ||
#[expose] | ||
fn init(ctx: Context) -> bool { | ||
ctx.store_value("counter", &0_i64).is_ok() | ||
#[public] | ||
fn init(state: State) -> bool { | ||
state.store_value("counter", &0_i64).is_ok() | ||
} | ||
|
||
/// Increments the count at the address by the amount. | ||
#[expose] | ||
fn inc(ctx: Context, to: Address, amount: i64) -> bool { | ||
let counter = amount + value(ctx, to); | ||
#[public] | ||
fn inc(state: State, to: Address, amount: i64) -> bool { | ||
let counter = amount + value(state, to); | ||
// dont check for error/ok | ||
ctx.store_map_value("counts", &to, &counter).is_ok() | ||
state.store_map_value("counts", &to, &counter).is_ok() | ||
} | ||
|
||
/// Gets the count at the address. | ||
#[expose] | ||
fn value(ctx: Context, of: Address) -> i64 { | ||
ctx.get_map_value("counts", &of).unwrap_or(0) | ||
#[public] | ||
fn value(state: State, of: Address) -> i64 { | ||
state.get_map_value("counts", &of).unwrap_or(0) | ||
} |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,51 @@ | ||
use wasmlanche_sdk::store::Context; | ||
use wasmlanche_sdk::types::Address; | ||
|
||
use expose_macro::expose; | ||
use wasmlanche_sdk::{public, store::State, types::Address}; | ||
|
||
/// Initializes the program with a name, symbol, and total supply. | ||
#[expose] | ||
pub fn init(ctx: Context) -> bool { | ||
ctx.store_value("total_supply", &123456789_i64) | ||
#[public] | ||
pub fn init(state: State) -> bool { | ||
state | ||
.store_value("total_supply", &123456789_i64) | ||
.store_value("name", "WasmCoin") | ||
.store_value("symbol", "WACK") | ||
.is_ok() | ||
} | ||
|
||
/// Gets total supply or -1 on error. | ||
#[expose] | ||
pub fn get_total_supply(ctx: Context) -> i64 { | ||
ctx.get_value("total_supply").unwrap() | ||
#[public] | ||
pub fn get_total_supply(state: State) -> i64 { | ||
state.get_value("total_supply").unwrap() | ||
} | ||
|
||
/// Adds amount coins to the recipients balance. | ||
#[expose] | ||
pub fn mint_to(ctx: Context, recipient: Address, amount: i64) -> bool { | ||
let amount = amount + ctx.get_map_value("balances", &recipient).unwrap_or(0); | ||
ctx.store_map_value("balances", &recipient, &amount).is_ok() | ||
#[public] | ||
pub fn mint_to(state: State, recipient: Address, amount: i64) -> bool { | ||
let amount = amount + state.get_map_value("balances", &recipient).unwrap_or(0); | ||
state | ||
.store_map_value("balances", &recipient, &amount) | ||
.is_ok() | ||
} | ||
|
||
/// Transfers amount coins from the sender to the recipient. Returns whether successful. | ||
#[expose] | ||
pub fn transfer(ctx: Context, sender: Address, recipient: Address, amount: i64) -> bool { | ||
#[public] | ||
pub fn transfer(state: State, sender: Address, recipient: Address, amount: i64) -> bool { | ||
// require sender != recipient | ||
if sender == recipient { | ||
return false; | ||
} | ||
// ensure the sender has adequate balance | ||
let sender_balance: i64 = ctx.get_map_value("balances", &sender).unwrap_or(0); | ||
let sender_balance: i64 = state.get_map_value("balances", &sender).unwrap_or(0); | ||
if amount < 0 || sender_balance < amount { | ||
return false; | ||
} | ||
let recipient_balance: i64 = ctx.get_map_value("balances", &recipient).unwrap_or(0); | ||
ctx.store_map_value("balances", &sender, &(sender_balance - amount)) | ||
let recipient_balance: i64 = state.get_map_value("balances", &recipient).unwrap_or(0); | ||
state | ||
.store_map_value("balances", &sender, &(sender_balance - amount)) | ||
.store_map_value("balances", &recipient, &(recipient_balance + amount)) | ||
.is_ok() | ||
} | ||
|
||
/// Gets the balance of the recipient. | ||
#[expose] | ||
pub fn get_balance(ctx: Context, recipient: Address) -> i64 { | ||
ctx.get_map_value("balances", &recipient).unwrap_or(0) | ||
#[public] | ||
pub fn get_balance(state: State, recipient: Address) -> i64 { | ||
state.get_map_value("balances", &recipient).unwrap_or(0) | ||
} |
2 changes: 1 addition & 1 deletion
2
x/programs/rust/expose_macro/Cargo.toml → x/programs/rust/sdk_macros/Cargo.toml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "expose_macro" | ||
name = "sdk_macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
|
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
Oops, something went wrong.