From 9cc641b86e124d5fe785fe9a8503f2d2e7477b8f Mon Sep 17 00:00:00 2001 From: mootz12 Date: Mon, 1 Apr 2024 14:11:39 -0400 Subject: [PATCH] chore: upgrade to soroban 20.5 and update mock sep-41 token contract --- Cargo.lock | 50 ++++++++++---------- Cargo.toml | 2 +- README.md | 8 +++- mock-sep-41/src/allowance.rs | 16 ++++--- mock-sep-41/src/storage.rs | 6 +-- mock-sep-41/src/test.rs | 27 ++++++++++- sep-41/Cargo.toml | 2 +- sep-41/README.md | 32 +++++++++++++ sep-41/src/lib.rs | 4 +- sep-41/src/testutils/mock_sep_41_token.wasm | Bin 6194 -> 6213 bytes 10 files changed, 105 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 585e347..0e6effd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -849,7 +849,7 @@ checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" [[package]] name = "sep-41-token" -version = "0.3.0" +version = "1.0.0" dependencies = [ "soroban-sdk", ] @@ -953,9 +953,9 @@ checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" [[package]] name = "soroban-builtin-sdk-macros" -version = "20.0.0" +version = "20.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b42487d6b0268748f753feeb579c6f7908dbb002faf20b703e6a7185b12f0527" +checksum = "7cc32c6e817f3ca269764ec0d7d14da6210b74a5bf14d4e745aa3ee860558900" dependencies = [ "itertools", "proc-macro2", @@ -965,9 +965,9 @@ dependencies = [ [[package]] name = "soroban-env-common" -version = "20.0.0" +version = "20.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bb493483fa3e3ebfb4c081472495d14b0abcfbe04ba142a56ff63056cc62700" +checksum = "c14e18d879c520ff82612eaae0590acaf6a7f3b977407e1abb1c9e31f94c7814" dependencies = [ "arbitrary", "crate-git-revision", @@ -983,9 +983,9 @@ dependencies = [ [[package]] name = "soroban-env-guest" -version = "20.0.0" +version = "20.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f31a738ef5faf4084c4b1824a8e3f93dfff0261a3909e86060f818e728479a3" +checksum = "5122ca2abd5ebcc1e876a96b9b44f87ce0a0e06df8f7c09772ddb58b159b7454" dependencies = [ "soroban-env-common", "static_assertions", @@ -993,9 +993,9 @@ dependencies = [ [[package]] name = "soroban-env-host" -version = "20.0.0" +version = "20.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bdd1172a76c0bc2ce67ec7f28ca37dddbe9fefabe583f80434f5f60aaee3547e" +checksum = "114a0fa0d0cc39d0be16b1ee35b6e5f4ee0592ddcf459bde69391c02b03cf520" dependencies = [ "backtrace", "curve25519-dalek", @@ -1020,9 +1020,9 @@ dependencies = [ [[package]] name = "soroban-env-macros" -version = "20.0.0" +version = "20.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c0536648cea69ab3bae1801d35f92c0a31e7449cd2c7d14a18fb5e413f43279" +checksum = "b13e3f8c86f812e0669e78fcb3eae40c385c6a9dd1a4886a1de733230b4fcf27" dependencies = [ "itertools", "proc-macro2", @@ -1035,9 +1035,9 @@ dependencies = [ [[package]] name = "soroban-ledger-snapshot" -version = "20.0.0" +version = "20.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37960eec21d7dc5dbd976fa16e38c056429663a89243798486b07afbb263c9b5" +checksum = "61a54708f44890e0546180db6b4f530e2a88d83b05a9b38a131caa21d005e25a" dependencies = [ "serde", "serde_json", @@ -1049,9 +1049,9 @@ dependencies = [ [[package]] name = "soroban-sdk" -version = "20.0.0" +version = "20.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f08e1fdb18dbee88160ea6640962faf021a49f22eb1bd212c4d8b0cef32c582c" +checksum = "84fc8be9068dd4e0212d8b13ad61089ea87e69ac212c262914503a961c8dc3a3" dependencies = [ "arbitrary", "bytes-lit", @@ -1069,9 +1069,9 @@ dependencies = [ [[package]] name = "soroban-sdk-macros" -version = "20.0.0" +version = "20.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c5cae44f304f2fd32f9cfa9a31a9b58eb1c10aa07a7d5b591921cf7fa649e44" +checksum = "db20def4ead836663633f58d817d0ed8e1af052c9650a04adf730525af85b964" dependencies = [ "crate-git-revision", "darling", @@ -1089,9 +1089,9 @@ dependencies = [ [[package]] name = "soroban-spec" -version = "20.0.0" +version = "20.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7539cfa0abe36f3d33c49fe1253f6b652c91c9a9841fe83dedc1799b7f4bb55f" +checksum = "3eefeb5d373b43f6828145d00f0c5cc35e96db56a6671ae9614f84beb2711cab" dependencies = [ "base64 0.13.1", "stellar-xdr", @@ -1101,9 +1101,9 @@ dependencies = [ [[package]] name = "soroban-spec-rust" -version = "20.0.0" +version = "20.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb6189ef3ede0061db14b0cf9fa2692a2cb6c6e8d941689f0c9ca82b68c47ab2" +checksum = "3152bca4737ef734ac37fe47b225ee58765c9095970c481a18516a2b287c7a33" dependencies = [ "prettyplease", "proc-macro2", @@ -1117,9 +1117,9 @@ dependencies = [ [[package]] name = "soroban-wasmi" -version = "0.31.1-soroban.20.0.0" +version = "0.31.1-soroban.20.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1aaa682a67cbd2173f1d60cb1e7b951d490d7c4e0b7b6f5387cbb952e963c46" +checksum = "710403de32d0e0c35375518cb995d4fc056d0d48966f2e56ea471b8cb8fc9719" dependencies = [ "smallvec", "spin", @@ -1163,9 +1163,9 @@ dependencies = [ [[package]] name = "stellar-xdr" -version = "20.0.0" +version = "20.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9595b775539e475da4179fa46212b11e4575f526d57b13308989a8c1dd59238c" +checksum = "e59cdf3eb4467fb5a4b00b52e7de6dca72f67fac6f9b700f55c95a5d86f09c9d" dependencies = [ "arbitrary", "base64 0.13.1", diff --git a/Cargo.toml b/Cargo.toml index 10732bf..7b0f2ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ codegen-units = 1 lto = true [workspace.dependencies.soroban-sdk] -version = "20.0.0" +version = "20.5.0" diff --git a/README.md b/README.md index b38965c..1824b68 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ -# sep-41-token -SEP-0041 Trait, Client, and mock contract. +# SEP-0041-token +SEP-0041 Trait, Client, and mock testing contract. SEP-0041 Definition: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0041.md +## Safety +This is **experimental software** and is provided on an "as is" and "as available" basis. + +We do **not give any warranties** and **will not be liable for any loss** incurred through any use of this codebase. diff --git a/mock-sep-41/src/allowance.rs b/mock-sep-41/src/allowance.rs index 1daca93..f03cd06 100644 --- a/mock-sep-41/src/allowance.rs +++ b/mock-sep-41/src/allowance.rs @@ -20,11 +20,13 @@ pub fn spend_allowance(e: &Env, from: &Address, spender: &Address, amount: i128) if allowance.amount < amount || e.ledger().sequence() > allowance.expiration_ledger { panic_with_error!(e, TokenError::AllowanceError); } - storage::set_allowance( - e, - from, - spender, - allowance.amount - amount, - allowance.expiration_ledger, - ); + if amount > 0 { + storage::set_allowance( + e, + from, + spender, + allowance.amount - amount, + allowance.expiration_ledger, + ); + } } diff --git a/mock-sep-41/src/storage.rs b/mock-sep-41/src/storage.rs index 0f43065..f1b7882 100644 --- a/mock-sep-41/src/storage.rs +++ b/mock-sep-41/src/storage.rs @@ -4,11 +4,11 @@ use soroban_sdk::{ }; pub(crate) const DAY_IN_LEDGERS: u32 = 17280; -pub(crate) const INSTANCE_BUMP_AMOUNT: u32 = 8 * DAY_IN_LEDGERS; +pub(crate) const INSTANCE_BUMP_AMOUNT: u32 = 31 * DAY_IN_LEDGERS; pub(crate) const INSTANCE_LIFETIME_THRESHOLD: u32 = INSTANCE_BUMP_AMOUNT - DAY_IN_LEDGERS; -pub(crate) const BALANCE_BUMP_AMOUNT: u32 = 31 * DAY_IN_LEDGERS - 1; -pub(crate) const BALANCE_LIFETIME_THRESHOLD: u32 = BALANCE_BUMP_AMOUNT - DAY_IN_LEDGERS; +pub(crate) const BALANCE_BUMP_AMOUNT: u32 = 120 * DAY_IN_LEDGERS; +pub(crate) const BALANCE_LIFETIME_THRESHOLD: u32 = BALANCE_BUMP_AMOUNT - 20 * DAY_IN_LEDGERS; const METADATA_KEY: Symbol = symbol_short!("METADATA"); const ADMIN_KEY: Symbol = symbol_short!("ADMIN"); diff --git a/mock-sep-41/src/test.rs b/mock-sep-41/src/test.rs index de6ebd6..175731b 100644 --- a/mock-sep-41/src/test.rs +++ b/mock-sep-41/src/test.rs @@ -1,7 +1,11 @@ #![cfg(test)] extern crate std; -use crate::{contract::MockToken, MockTokenClient}; +use crate::{ + contract::MockToken, + storage::{AllowanceDataKey, DataKey}, + MockTokenClient, +}; use soroban_sdk::{ symbol_short, testutils::{Address as _, AuthorizedFunction, AuthorizedInvocation}, @@ -254,3 +258,24 @@ fn decimal_is_over_max() { &"symbol".into_val(&e), ); } + +#[test] +fn test_zero_allowance() { + // Here we test that transfer_from with a 0 amount does not create an empty allowance + let e = Env::default(); + e.mock_all_auths(); + + let admin = Address::generate(&e); + let spender = Address::generate(&e); + let from = Address::generate(&e); + let token = create_token(&e, &admin); + + token.transfer_from(&spender, &from, &spender, &0); + e.as_contract(&token.address, || { + let key = DataKey::Allowance(AllowanceDataKey { + from: from.clone(), + spender: spender.clone(), + }); + assert!(!e.storage().temporary().has(&key)); + }); +} diff --git a/sep-41/Cargo.toml b/sep-41/Cargo.toml index 8612d88..da084c0 100644 --- a/sep-41/Cargo.toml +++ b/sep-41/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sep-41-token" -version = "0.3.0" +version = "1.0.0" description = "SEP-0041 Standard Token trait, client, and mock implementation" authors = ["Script3 "] repository = "https://github.com/script3/sep-40-oracle" diff --git a/sep-41/README.md b/sep-41/README.md index c10e993..2386d47 100644 --- a/sep-41/README.md +++ b/sep-41/README.md @@ -3,6 +3,11 @@ Exposes the interface of the SEP-0041 Token alongside a mock token contract. SEP-0041 Definition: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0041.md +## Safety +This is **experimental software** and is provided on an "as is" and "as available" basis. + +We do **not give any warranties** and **will not be liable for any loss** incurred through any use of this codebase. + ## Getting Started Add the package to your `Cargo.toml`: @@ -18,3 +23,30 @@ You can optionally include the `testutils` feature in your `dev-dependencies` to [dev_dependencies] sep-40-token = { version = "", features = ["testutils"] } ``` + +### Client and Traits +This package exposes 3 different token clients based on your usage. + +* `TokenClient` is the `SEP-0041` standard and is derived from the trait `Token` +* `StellarAssetClient` exposes the functions implemented by the Stellar Asset Contract and is derived from the trait `StellarAssetExtension` + +### Mock Token +This package exposes an example Soroban token implementation of the `SEP-0041` standard that can be used to test protocol interactions with Soroban tokens. This is important to test as interacting with Soroban tokens has a much larger cost impact than interacting with the Stellar Asset Contract. + +A WASM version of the contract can be deployed as follows: +```rust +use sep_41_token::testutils::{MockTokenClient, MockTokenWASM}; +use soroban_sdk::{testutils::Address as _, Address, Env, String}; + +let env = Env::default(); + +let admin = Address::generate(&env); +let token_id = env.register_contract_wasm(None, MockTokenWASM); +let token_client = MockTokenClient::new(&env, &token_id); +token_client.initialize( + &admin, + &7, + &String::from_str(&env, "Name"), + &String::from_str(&env, "Symbol"), +); +``` \ No newline at end of file diff --git a/sep-41/src/lib.rs b/sep-41/src/lib.rs index 27ed6a6..6177b01 100644 --- a/sep-41/src/lib.rs +++ b/sep-41/src/lib.rs @@ -194,10 +194,10 @@ impl TokenEvents { from: Address, spender: Address, amount: i128, - expiration_ledger: u32, + live_until_ledger: u32, ) { let topics = (symbol_short!("approve"), from, spender); - env.events().publish(topics, (amount, expiration_ledger)); + env.events().publish(topics, (amount, live_until_ledger)); } /// Emitted when an amount is transferred from one address to another diff --git a/sep-41/src/testutils/mock_sep_41_token.wasm b/sep-41/src/testutils/mock_sep_41_token.wasm index e71df574aa5014063b35028468ee2f63505f0b32..5a83d89925a2781a1c866c1bbfae63a2151b0353 100644 GIT binary patch delta 415 zcmXX=Jxe1|6utMoH!~(L!6`&`5``OMB`7nK%&J(JH-(MGE`owqW;6(yLM$zk8Stao zCh+=Y%PrOwO|dN4SS;+n5IY5b!4|78>TM3^o^v?o+W+GZ4vJRaTn4+?zdAZbc0R42 z@OSnT8PBlKT7;^wUIxM%C#VVDA}(4iQ%GVSV1}tXSSv$?nDSHgj7O^YNwf^Rg*RGx zqxbLBTQ6HiT}ew!8jR8sj&LEB7x6QmkQLkKr!{MWxWd#_3WeR8FeXWsuJgm{*=QK+ z*FjM_BWA%k#=2zoxs&>mhf6Gm4dO9u68~H^ON>He#Cqfq?~#4cBg_3DO^Uj+FfQYh zJkJN{db%new6wlD4iLEG75&}X!Ax^|r;y(~g3AB?2fc6qLQhXQbER+pczkN2`QVEf zZaBNl{D*}t^oGUaDsw#v^0nF~HYyx$mww3kXu+>9cf!4fABIV@5k*NDHrtEsrJ&gf S8c90}7C#1^P7*Ye1YQ81;9dOy delta 372 zcmXYsKTN_<5XRr1zE)_7O>`&(%(JAzLNYC-*wJ0 zA#KtjC0YUu(uPz5$&Hj_f_fMbNX-mFY39D6attMa4O0Y|Yqs3zmKbI318TX6Kl?6h zd^5&?cx!=1RH1gD;a|-Ls))tSlmyh1PkJdTxGFp^(gS?PFKLoLQfrlh(g#H}4L}|S zY1;34s1n0S^}1mg4zO+@Ra}K(0!RQ9m>w6w(46Os(ublTK>B2YPq7Pr&nh_J)AEio zewlU0ufUJwTWs(pWkDN!Bd;g&!tfN28cLN2{v6rH#%LzGPIN>4Um}BeR*#SNwJSAa pX3e=3%kmoq-_JGjg=OFOigv;2Ay{QwEtR-*s_