Skip to content

Commit e90db9f

Browse files
authored
Replace increase/decrease_allowance functions with approve (#1022)
Update sdk to match SAC changes. I tried to add the following documentation to the token interface for the approve function, but I was getting a `LengthExceedsMax` macro error. This should be added to our docs instead. ``` /// # A note on modifying allowances /// /// The approve function overwrites the previous value with `amount`, so it's /// possible for the previous allowance to be spent in an earlier transaction /// before `amount` is written in a later transaction. The result of this is /// that `spender` can spend more than intended. This issue can be avoided by /// first setting the allowance to 0, verifying that the spender didn't spend /// any portion of the previous allowance, and then setting the allowance to the /// new desired amount. You can read more about this issue here - ethereum/EIPs#20 (comment). ```
1 parent 9bfbfa7 commit e90db9f

6 files changed

Lines changed: 31 additions & 44 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ soroban-token-sdk = { version = "0.8.4", path = "soroban-token-sdk" }
4040
[workspace.dependencies.soroban-env-common]
4141
version = "0.0.16"
4242
git = "https://github.com/stellar/rs-soroban-env"
43-
rev = "aff56a8cbf5c2f3c23fb0174da6edeb070322028"
43+
rev = "422ce4110f26097e94835e0a70a4ed8c10b5bc16"
4444

4545
[workspace.dependencies.soroban-env-guest]
4646
version = "0.0.16"
4747
git = "https://github.com/stellar/rs-soroban-env"
48-
rev = "aff56a8cbf5c2f3c23fb0174da6edeb070322028"
48+
rev = "422ce4110f26097e94835e0a70a4ed8c10b5bc16"
4949

5050
[workspace.dependencies.soroban-env-host]
5151
version = "0.0.16"
5252
git = "https://github.com/stellar/rs-soroban-env"
53-
rev = "aff56a8cbf5c2f3c23fb0174da6edeb070322028"
53+
rev = "422ce4110f26097e94835e0a70a4ed8c10b5bc16"
5454

5555
[workspace.dependencies.stellar-strkey]
5656
version = "0.0.7"

soroban-ledger-snapshot/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct LedgerSnapshot {
3232
pub base_reserve: u32,
3333
pub min_persistent_entry_expiration: u32,
3434
pub min_temp_entry_expiration: u32,
35+
pub max_entry_expiration: u32,
3536
pub ledger_entries: Vec<(Box<LedgerKey>, Box<LedgerEntry>)>,
3637
}
3738

@@ -74,6 +75,7 @@ impl LedgerSnapshot {
7475
base_reserve: self.base_reserve,
7576
min_persistent_entry_expiration: self.min_persistent_entry_expiration,
7677
min_temp_entry_expiration: self.min_temp_entry_expiration,
78+
max_entry_expiration: self.max_entry_expiration,
7779
}
7880
}
7981

@@ -164,6 +166,7 @@ impl Default for LedgerSnapshot {
164166
ledger_entries: Vec::default(),
165167
min_persistent_entry_expiration: Default::default(),
166168
min_temp_entry_expiration: Default::default(),
169+
max_entry_expiration: Default::default(),
167170
}
168171
}
169172
}

soroban-sdk/src/env.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ impl Env {
526526
base_reserve: 0,
527527
min_persistent_entry_expiration: 4096,
528528
min_temp_entry_expiration: 16,
529+
max_entry_expiration: 6_312_000,
529530
});
530531

531532
env

soroban-sdk/src/tests/token_client.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ impl TestContract {
3434
get_token(&e)
3535
}
3636

37-
pub fn increase_allowance(e: Env, from: Address, spender: Address, amount: i128) {
38-
TokenClient::new(&e, &get_token(&e)).increase_allowance(&from, &spender, &amount);
37+
pub fn approve(e: Env, from: Address, spender: Address, amount: i128, expiration_ledger: u32) {
38+
TokenClient::new(&e, &get_token(&e)).approve(&from, &spender, &amount, &expiration_ledger);
3939
}
4040

4141
pub fn allowance(e: Env, from: Address, spender: Address) -> i128 {
@@ -61,9 +61,7 @@ fn test_mock_all_auth() {
6161
let from = Address::random(&env);
6262
let spender = Address::random(&env);
6363

64-
client
65-
.mock_all_auths()
66-
.increase_allowance(&from, &spender, &20);
64+
client.mock_all_auths().approve(&from, &spender, &20, &200);
6765

6866
assert_eq!(
6967
env.auths(),
@@ -72,8 +70,8 @@ fn test_mock_all_auth() {
7270
AuthorizedInvocation {
7371
function: AuthorizedFunction::Contract((
7472
token_contract_id.clone(),
75-
Symbol::new(&env, "increase_allowance"),
76-
(&from, &spender, 20_i128).into_val(&env)
73+
Symbol::new(&env, "approve"),
74+
(&from, &spender, 20_i128, 200_u32).into_val(&env)
7775
)),
7876
sub_invocations: std::vec![]
7977
}
@@ -106,12 +104,12 @@ fn test_mock_auth() {
106104
address: &from,
107105
invoke: &MockAuthInvoke {
108106
contract: &token_contract_id,
109-
fn_name: "increase_allowance",
110-
args: (&from, &spender, 20_i128).into_val(&env),
107+
fn_name: "approve",
108+
args: (&from, &spender, 20_i128, 200_u32).into_val(&env),
111109
sub_invokes: &[],
112110
},
113111
}])
114-
.increase_allowance(&from, &spender, &20);
112+
.approve(&from, &spender, &20, &200);
115113

116114
assert_eq!(client.allowance(&from, &spender), 20);
117115
}

soroban-sdk/src/token.rs

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,25 @@ pub trait Interface {
3030
/// * `spender` - The address spending the tokens held by `from`.
3131
fn allowance(env: Env, from: Address, spender: Address) -> i128;
3232

33-
/// Increase the allowance by `amount` for `spender` to transfer/burn from
33+
/// Set the allowance by `amount` for `spender` to transfer/burn from
3434
/// `from`.
3535
///
3636
/// # Arguments
3737
///
3838
/// * `from` - The address holding the balance of tokens to be drawn from.
3939
/// * `spender` - The address being authorized to spend the tokens held by
4040
/// `from`.
41-
/// * `amount` - The additional tokens to be made availabe to `spender`.
41+
/// * `amount` - The tokens to be made availabe to `spender`.
42+
/// * `expiration_ledger` - The ledger number where this allowance expires. Cannot
43+
/// be less than the current ledger number unless the amount is being set to 0.
44+
/// An expired entry (where expiration_ledger < the current ledger number)
45+
/// should be treated as a 0 amount allowance.
4246
///
4347
/// # Events
4448
///
45-
/// Emits an event with topics `["increase_allowance", from: Address,
46-
/// spender: Address], data = [amount: i128]`
47-
fn increase_allowance(env: Env, from: Address, spender: Address, amount: i128);
48-
49-
/// Decrease the allowance by `amount` for `spender` to transfer/burn from
50-
/// `from`.
51-
///
52-
/// # Arguments
53-
///
54-
/// * `from` - The address holding the balance of tokens to be drawn from.
55-
/// * `spender` - The address being (de-)authorized to spend the tokens held
56-
/// by `from`.
57-
/// * `amount` - The tokens which are no longer availabe for use by
58-
/// `spender`. If `amount` is greater than the current allowance, set the
59-
/// allowance to 0.
60-
///
61-
/// # Events
62-
///
63-
/// Emits an event with topics `["decrease_allowance", from: Address,
64-
/// spender: Address], data = [amount: i128]`
65-
fn decrease_allowance(env: Env, from: Address, spender: Address, amount: i128);
49+
/// Emits an event with topics `["approve", from: Address,
50+
/// spender: Address], data = [amount: i128, expiration_ledger: u32]`
51+
fn approve(env: Env, from: Address, spender: Address, amount: i128, expiration_ledger: u32);
6652

6753
/// Returns the balance of `id`.
6854
///
@@ -244,13 +230,12 @@ pub struct StellarAssetSpec;
244230
pub(crate) const SPEC_XDR_INPUT: &[&[u8]] = &[
245231
&StellarAssetSpec::spec_xdr_allowance(),
246232
&StellarAssetSpec::spec_xdr_authorized(),
233+
&StellarAssetSpec::spec_xdr_approve(),
247234
&StellarAssetSpec::spec_xdr_balance(),
248235
&StellarAssetSpec::spec_xdr_burn(),
249236
&StellarAssetSpec::spec_xdr_burn_from(),
250237
&StellarAssetSpec::spec_xdr_clawback(),
251238
&StellarAssetSpec::spec_xdr_decimals(),
252-
&StellarAssetSpec::spec_xdr_decrease_allowance(),
253-
&StellarAssetSpec::spec_xdr_increase_allowance(),
254239
&StellarAssetSpec::spec_xdr_mint(),
255240
&StellarAssetSpec::spec_xdr_name(),
256241
&StellarAssetSpec::spec_xdr_set_admin(),
@@ -261,7 +246,7 @@ pub(crate) const SPEC_XDR_INPUT: &[&[u8]] = &[
261246
&StellarAssetSpec::spec_xdr_transfer_from(),
262247
];
263248

264-
pub(crate) const SPEC_XDR_LEN: usize = 5900;
249+
pub(crate) const SPEC_XDR_LEN: usize = 5572;
265250

266251
impl StellarAssetSpec {
267252
/// Returns the XDR spec for the Token contract.

0 commit comments

Comments
 (0)