Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Payload exploration #120

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
71 changes: 41 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ members = [
"single_offer_router",
"events",
"timelock",
"token"
"token",
"token_with_payload"
]

[profile.release]
Expand All @@ -35,10 +36,10 @@ soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "3b2994
soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "3b299468" }
soroban-auth = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "3b299468" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "3b299468" }
soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "a51888cf" }
soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "a51888cf" }
soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "a51888cf" }
soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "a51888cf" }
soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "a51888cf" }
soroban-env-common = { git = "https://github.com/stellar/rs-soroban-env", rev = "5d57d88" }
soroban-env-guest = { git = "https://github.com/stellar/rs-soroban-env", rev = "5d57d88" }
soroban-env-host = { git = "https://github.com/stellar/rs-soroban-env", rev = "5d57d88" }
soroban-env-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "5d57d88" }
soroban-native-sdk-macros = { git = "https://github.com/stellar/rs-soroban-env", rev = "5d57d88" }
stellar-xdr = { git = "https://github.com/stellar/rs-stellar-xdr", rev = "469efc9" }
wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", rev = "a61b6df" }
25 changes: 25 additions & 0 deletions token_with_payload/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "soroban-token-with-payload"
description = "Soroban token with payloads"
version = "0.0.4"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["export"]
export = []
testutils = ["soroban-sdk/testutils", "soroban-auth/testutils", "dep:ed25519-dalek"]

[dependencies]
ed25519-dalek = { version = "1.0.1", optional = true }
num-bigint = { version = "0.4", optional = true }
soroban-sdk = { version = "0.0.4" }
soroban-auth = { version = "0.0.4" }

[dev-dependencies]
soroban-sdk = { version = "0.0.4", features = ["testutils"] }
soroban-auth = { version = "0.0.4", features = ["testutils"] }
ed25519-dalek = { version = "1.0.1" }
rand = { version = "0.7.3" }
25 changes: 25 additions & 0 deletions token_with_payload/src/admin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::storage_types::DataKey;
use soroban_auth::{Identifier, Signature};
use soroban_sdk::Env;

pub fn has_administrator(e: &Env) -> bool {
let key = DataKey::Admin;
e.contract_data().has(key)
}

fn read_administrator(e: &Env) -> Identifier {
let key = DataKey::Admin;
e.contract_data().get_unchecked(key).unwrap()
}

pub fn write_administrator(e: &Env, id: Identifier) {
let key = DataKey::Admin;
e.contract_data().set(key, id);
}

pub fn check_admin(e: &Env, auth: &Signature) {
let auth_id = auth.get_identifier(&e);
if auth_id != read_administrator(&e) {
panic!("not authorized by admin")
}
}
25 changes: 25 additions & 0 deletions token_with_payload/src/allowance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::storage_types::{AllowanceDataKey, DataKey};
use soroban_auth::Identifier;
use soroban_sdk::{BigInt, Env};

pub fn read_allowance(e: &Env, from: Identifier, spender: Identifier) -> BigInt {
let key = DataKey::Allowance(AllowanceDataKey { from, spender });
if let Some(allowance) = e.contract_data().get(key) {
allowance.unwrap()
} else {
BigInt::zero(e)
}
}

pub fn write_allowance(e: &Env, from: Identifier, spender: Identifier, amount: BigInt) {
let key = DataKey::Allowance(AllowanceDataKey { from, spender });
e.contract_data().set(key, amount);
}

pub fn spend_allowance(e: &Env, from: Identifier, spender: Identifier, amount: BigInt) {
let allowance = read_allowance(e, from.clone(), spender.clone());
if allowance < amount {
panic!("insufficient allowance");
}
write_allowance(e, from, spender, allowance - amount);
}
Loading