Skip to content

Commit

Permalink
Merge pull request #1 from script3/phase-2
Browse files Browse the repository at this point in the history
Phase 2
  • Loading branch information
mootz12 authored Apr 2, 2024
2 parents 41a447c + 9cc641b commit b03cb32
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 42 deletions.
50 changes: 25 additions & 25 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ codegen-units = 1
lto = true

[workspace.dependencies.soroban-sdk]
version = "20.0.0"
version = "20.5.0"
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 9 additions & 7 deletions mock-sep-41/src/allowance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
}
}
6 changes: 3 additions & 3 deletions mock-sep-41/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
27 changes: 26 additions & 1 deletion mock-sep-41/src/test.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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));
});
}
2 changes: 1 addition & 1 deletion sep-41/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
repository = "https://github.com/script3/sep-40-oracle"
Expand Down
32 changes: 32 additions & 0 deletions sep-41/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand All @@ -18,3 +23,30 @@ You can optionally include the `testutils` feature in your `dev-dependencies` to
[dev_dependencies]
sep-40-token = { version = "<desired 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"),
);
```
4 changes: 2 additions & 2 deletions sep-41/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified sep-41/src/testutils/mock_sep_41_token.wasm
Binary file not shown.

0 comments on commit b03cb32

Please sign in to comment.