From 4621b7fba4edf5d3d2abf3088d3af02dceef7dbc Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 4 Apr 2023 15:07:27 -0700 Subject: [PATCH 1/2] Rename token function names to match the interface --- Cargo.lock | 44 ++++++++++++++++++++++++--------- Cargo.toml | 4 +-- account/src/lib.rs | 8 +++--- account/src/test.rs | 12 ++++----- atomic_swap/src/lib.rs | 6 ++--- liquidity_pool/src/lib.rs | 16 ++++++------ single_offer/src/lib.rs | 14 +++++------ single_offer/src/test.rs | 2 +- soroban_token_spec.wasm | Bin 5730 -> 5808 bytes timelock/src/lib.rs | 4 +-- token/src/contract.rs | 30 +++++++++++------------ token/src/event.rs | 12 ++++----- token/src/test.rs | 50 +++++++++++++++++++------------------- 13 files changed, 112 insertions(+), 90 deletions(-) mode change 100644 => 100755 soroban_token_spec.wasm diff --git a/Cargo.lock b/Cargo.lock index c4baf312..eb4f1041 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "arbitrary" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d098ff73c1ca148721f37baad5ea6a465a13f9573aba8641fbbbae8164a54e" +dependencies = [ + "derive_arbitrary", +] + [[package]] name = "assert_unordered" version = "0.3.5" @@ -282,6 +291,17 @@ dependencies = [ "syn", ] +[[package]] +name = "derive_arbitrary" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cdeb9ec472d588e539a818b2dee436825730da08ad0017c4b1a17676bdc8b7" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "digest" version = "0.9.0" @@ -859,7 +879,7 @@ dependencies = [ [[package]] name = "soroban-auth" version = "0.7.0" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=8abd3353c728f09ee1c8a2544f67a853e915afc2#8abd3353c728f09ee1c8a2544f67a853e915afc2" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=29b505cf95f2fd4fbe639fdb163b606c44920837#29b505cf95f2fd4fbe639fdb163b606c44920837" dependencies = [ "soroban-env-host", "soroban-sdk", @@ -910,8 +930,9 @@ dependencies = [ [[package]] name = "soroban-env-common" version = "0.0.15" -source = "git+https://github.com/stellar/rs-soroban-env?rev=cf83838ae8a07a00224f0a5a3500eb7969bac7cf#cf83838ae8a07a00224f0a5a3500eb7969bac7cf" +source = "git+https://github.com/stellar/rs-soroban-env?rev=5ef87058f07ad67eeba4c350a6b6af82bd17f916#5ef87058f07ad67eeba4c350a6b6af82bd17f916" dependencies = [ + "arbitrary", "crate-git-revision", "ethnum", "serde", @@ -924,7 +945,7 @@ dependencies = [ [[package]] name = "soroban-env-guest" version = "0.0.15" -source = "git+https://github.com/stellar/rs-soroban-env?rev=cf83838ae8a07a00224f0a5a3500eb7969bac7cf#cf83838ae8a07a00224f0a5a3500eb7969bac7cf" +source = "git+https://github.com/stellar/rs-soroban-env?rev=5ef87058f07ad67eeba4c350a6b6af82bd17f916#5ef87058f07ad67eeba4c350a6b6af82bd17f916" dependencies = [ "soroban-env-common", "static_assertions", @@ -933,7 +954,7 @@ dependencies = [ [[package]] name = "soroban-env-host" version = "0.0.15" -source = "git+https://github.com/stellar/rs-soroban-env?rev=cf83838ae8a07a00224f0a5a3500eb7969bac7cf#cf83838ae8a07a00224f0a5a3500eb7969bac7cf" +source = "git+https://github.com/stellar/rs-soroban-env?rev=5ef87058f07ad67eeba4c350a6b6af82bd17f916#5ef87058f07ad67eeba4c350a6b6af82bd17f916" dependencies = [ "backtrace", "curve25519-dalek", @@ -955,7 +976,7 @@ dependencies = [ [[package]] name = "soroban-env-macros" version = "0.0.15" -source = "git+https://github.com/stellar/rs-soroban-env?rev=cf83838ae8a07a00224f0a5a3500eb7969bac7cf#cf83838ae8a07a00224f0a5a3500eb7969bac7cf" +source = "git+https://github.com/stellar/rs-soroban-env?rev=5ef87058f07ad67eeba4c350a6b6af82bd17f916#5ef87058f07ad67eeba4c350a6b6af82bd17f916" dependencies = [ "itertools", "proc-macro2", @@ -998,7 +1019,7 @@ dependencies = [ [[package]] name = "soroban-ledger-snapshot" version = "0.7.0" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=8abd3353c728f09ee1c8a2544f67a853e915afc2#8abd3353c728f09ee1c8a2544f67a853e915afc2" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=29b505cf95f2fd4fbe639fdb163b606c44920837#29b505cf95f2fd4fbe639fdb163b606c44920837" dependencies = [ "serde", "serde_json", @@ -1024,7 +1045,7 @@ dependencies = [ [[package]] name = "soroban-native-sdk-macros" version = "0.0.15" -source = "git+https://github.com/stellar/rs-soroban-env?rev=cf83838ae8a07a00224f0a5a3500eb7969bac7cf#cf83838ae8a07a00224f0a5a3500eb7969bac7cf" +source = "git+https://github.com/stellar/rs-soroban-env?rev=5ef87058f07ad67eeba4c350a6b6af82bd17f916#5ef87058f07ad67eeba4c350a6b6af82bd17f916" dependencies = [ "itertools", "proc-macro2", @@ -1035,7 +1056,7 @@ dependencies = [ [[package]] name = "soroban-sdk" version = "0.7.0" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=8abd3353c728f09ee1c8a2544f67a853e915afc2#8abd3353c728f09ee1c8a2544f67a853e915afc2" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=29b505cf95f2fd4fbe639fdb163b606c44920837#29b505cf95f2fd4fbe639fdb163b606c44920837" dependencies = [ "bytes-lit", "ed25519-dalek", @@ -1050,7 +1071,7 @@ dependencies = [ [[package]] name = "soroban-sdk-macros" version = "0.7.0" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=8abd3353c728f09ee1c8a2544f67a853e915afc2#8abd3353c728f09ee1c8a2544f67a853e915afc2" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=29b505cf95f2fd4fbe639fdb163b606c44920837#29b505cf95f2fd4fbe639fdb163b606c44920837" dependencies = [ "darling", "itertools", @@ -1073,7 +1094,7 @@ dependencies = [ [[package]] name = "soroban-spec" version = "0.7.0" -source = "git+https://github.com/stellar/rs-soroban-sdk?rev=8abd3353c728f09ee1c8a2544f67a853e915afc2#8abd3353c728f09ee1c8a2544f67a853e915afc2" +source = "git+https://github.com/stellar/rs-soroban-sdk?rev=29b505cf95f2fd4fbe639fdb163b606c44920837#29b505cf95f2fd4fbe639fdb163b606c44920837" dependencies = [ "base64", "darling", @@ -1151,8 +1172,9 @@ dependencies = [ [[package]] name = "stellar-xdr" version = "0.0.15" -source = "git+https://github.com/stellar/rs-stellar-xdr?rev=4655b635c698bb3bbc628bdba456df627c42ee3a#4655b635c698bb3bbc628bdba456df627c42ee3a" +source = "git+https://github.com/stellar/rs-stellar-xdr?rev=bcf6f4c3a9dd32822a20af89880650a421d10e7f#bcf6f4c3a9dd32822a20af89880650a421d10e7f" dependencies = [ + "arbitrary", "base64", "crate-git-revision", "hex", diff --git a/Cargo.toml b/Cargo.toml index f642ac9e..ce55aa14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,12 +40,12 @@ lto = true [workspace.dependencies.soroban-sdk] version = "0.7.0" git = "https://github.com/stellar/rs-soroban-sdk" -rev = "8abd3353c728f09ee1c8a2544f67a853e915afc2" +rev = "29b505cf95f2fd4fbe639fdb163b606c44920837" [workspace.dependencies.soroban-auth] version = "0.7.0" git = "https://github.com/stellar/rs-soroban-sdk" -rev = "8abd3353c728f09ee1c8a2544f67a853e915afc2" +rev = "29b505cf95f2fd4fbe639fdb163b606c44920837" # [patch."https://github.com/stellar/rs-soroban-sdk"] # soroban-sdk = { path = "../rs-soroban-sdk/soroban-sdk" } diff --git a/account/src/lib.rs b/account/src/lib.rs index 593b3e09..99f383f2 100644 --- a/account/src/lib.rs +++ b/account/src/lib.rs @@ -38,7 +38,7 @@ pub enum AccError { UnknownSigner = 4, } -const XFER_FN: Symbol = Symbol::short("xfer"); +const TRANSFER_FN: Symbol = Symbol::short("transfer"); #[contractimpl] impl AccountContract { @@ -104,7 +104,7 @@ impl AccountContract { let curr_contract_id = env.current_contract_id(); // This is a map for tracking the token spend limits per token. This - // makes sure that if e.g. multiple `xfer` calls are being authorized + // makes sure that if e.g. multiple `transfer` calls are being authorized // for the same token we still respect the limit for the total // transferred amount (and not the 'per-call' limits). let mut spend_left_per_token = Map::, i128>::new(&env); @@ -165,7 +165,7 @@ fn verify_authorization_policy( } // Otherwise, we're only interested in functions that spend tokens. - if context.fn_name != XFER_FN && context.fn_name != Symbol::new(env, "incr_allow") { + if context.fn_name != TRANSFER_FN && context.fn_name != Symbol::new(env, "increase_allowance") { return Ok(()); } @@ -183,7 +183,7 @@ fn verify_authorization_policy( // 'None' means that the contract is outside of the policy. if let Some(spend_left) = spend_left { - // 'amount' is the third argument in both `approve` and `xfer`. + // 'amount' is the third argument in both `approve` and `transfer`. // If the contract has a different signature, it's safer to panic // here, as it's expected to have the standard interface. let spent: i128 = context diff --git a/account/src/test.rs b/account/src/test.rs index ea6f6296..baea65bf 100644 --- a/account/src/test.rs +++ b/account/src/test.rs @@ -74,7 +74,7 @@ fn test_token_auth() { &vec![&env, sign(&env, &signers[0], &payload)], &vec![ &env, - token_auth_context(&env, &token, Symbol::new(&env, "xfer"), 1000), + token_auth_context(&env, &token, Symbol::new(&env, "transfer"), 1000), ], ) .unwrap(); @@ -84,7 +84,7 @@ fn test_token_auth() { &vec![&env, sign(&env, &signers[0], &payload)], &vec![ &env, - token_auth_context(&env, &token, Symbol::new(&env, "xfer"), 1000), + token_auth_context(&env, &token, Symbol::new(&env, "transfer"), 1000), ], ) .unwrap(); @@ -111,7 +111,7 @@ fn test_token_auth() { &vec![&env, sign(&env, &signers[0], &payload)], &vec![ &env, - token_auth_context(&env, &token, Symbol::new(&env, "xfer"), 1001) + token_auth_context(&env, &token, Symbol::new(&env, "transfer"), 1001) ], ) .err() @@ -126,7 +126,7 @@ fn test_token_auth() { &vec![&env, sign(&env, &signers[0], &payload)], &vec![ &env, - token_auth_context(&env, &token, Symbol::new(&env, "incr_allow"), 1001) + token_auth_context(&env, &token, Symbol::new(&env, "increase_allowance"), 1001) ], ) .err() @@ -142,7 +142,7 @@ fn test_token_auth() { &vec![&env, sign(&env, &signers[0], &payload)], &vec![ &env, - token_auth_context(&env, &token, Symbol::new(&env, "incr_allow"), 1000), + token_auth_context(&env, &token, Symbol::new(&env, "increase_allowance"), 1000), ], ) .unwrap(); @@ -157,7 +157,7 @@ fn test_token_auth() { ], &vec![ &env, - token_auth_context(&env, &token, Symbol::new(&env, "xfer"), 10000), + token_auth_context(&env, &token, Symbol::new(&env, "transfer"), 10000), ], ) .unwrap(); diff --git a/atomic_swap/src/lib.rs b/atomic_swap/src/lib.rs index 7d083a1a..927d3bfd 100644 --- a/atomic_swap/src/lib.rs +++ b/atomic_swap/src/lib.rs @@ -58,15 +58,15 @@ fn move_token( from: &Address, to: &Address, approve_amount: i128, - xfer_amount: i128, + transfer_amount: i128, ) { let token = token::Client::new(&env, &token); let contract_address = env.current_contract_address(); // This call needs to be authorized by `from` address. Since it increases // the allowance on behalf of the contract, `from` doesn't need to know `to` // at the signature time. - token.incr_allow(&from, &contract_address, &approve_amount); - token.xfer_from(&contract_address, &from, to, &xfer_amount); + token.increase_allowance(&from, &contract_address, &approve_amount); + token.transfer_from(&contract_address, &from, to, &transfer_amount); } mod test; diff --git a/liquidity_pool/src/lib.rs b/liquidity_pool/src/lib.rs index 99d9c2bf..841d2661 100644 --- a/liquidity_pool/src/lib.rs +++ b/liquidity_pool/src/lib.rs @@ -108,7 +108,7 @@ fn mint_shares(e: &Env, to: Address, amount: i128) { } fn transfer(e: &Env, contract_id: BytesN<32>, to: Address, amount: i128) { - token::Client::new(e, &contract_id).xfer(&e.current_contract_address(), &to, &amount); + token::Client::new(e, &contract_id).transfer(&e.current_contract_address(), &to, &amount); } fn transfer_a(e: &Env, to: Address, amount: i128) { @@ -160,10 +160,10 @@ pub trait LiquidityPoolTrait { // If "buy_a" is true, the swap will buy token_a and sell token_b. This is flipped if "buy_a" is false. // "out" is the amount being bought, with in_max being a safety to make sure you receive at least that amount. - // swap will xfer the selling token "to" to this contract, and then the contract will xfer the buying token to "to". + // swap will transfer the selling token "to" to this contract, and then the contract will transfer the buying token to "to". fn swap(e: Env, to: Address, buy_a: bool, out: i128, in_max: i128); - // xfers share_amount of pool share tokens to this contract, burns all pools share tokens in this contracts, and sends the + // transfers share_amount of pool share tokens to this contract, burns all pools share tokens in this contracts, and sends the // corresponding amount of token_a and token_b to "to". // Returns amount of both tokens withdrawn fn withdraw(e: Env, to: Address, share_amount: i128, min_a: i128, min_b: i128) -> (i128, i128); @@ -212,8 +212,8 @@ impl LiquidityPoolTrait for LiquidityPool { let token_a_client = token::Client::new(&e, &get_token_a(&e)); let token_b_client = token::Client::new(&e, &get_token_b(&e)); - token_a_client.xfer(&to, &e.current_contract_address(), &amounts.0); - token_b_client.xfer(&to, &e.current_contract_address(), &amounts.1); + token_a_client.transfer(&to, &e.current_contract_address(), &amounts.0); + token_b_client.transfer(&to, &e.current_contract_address(), &amounts.1); // Now calculate how many new pool shares to mint let (balance_a, balance_b) = (get_balance_a(&e), get_balance_b(&e)); @@ -251,14 +251,14 @@ impl LiquidityPoolTrait for LiquidityPool { panic!("in amount is over max") } - // Xfer the amount being sold to the contract + // Transfer the amount being sold to the contract let sell_token = if buy_a { get_token_b(&e) } else { get_token_a(&e) }; let sell_token_client = token::Client::new(&e, &sell_token); - sell_token_client.xfer(&to, &e.current_contract_address(), &sell_amount); + sell_token_client.transfer(&to, &e.current_contract_address(), &sell_amount); let (balance_a, balance_b) = (get_balance_a(&e), get_balance_b(&e)); @@ -304,7 +304,7 @@ impl LiquidityPoolTrait for LiquidityPool { // First transfer the pool shares that need to be redeemed let share_token_client = token::Client::new(&e, &get_token_share(&e)); - share_token_client.xfer(&to, &e.current_contract_address(), &share_amount); + share_token_client.transfer(&to, &e.current_contract_address(), &share_amount); let (balance_a, balance_b) = (get_balance_a(&e), get_balance_b(&e)); let balance_shares = get_balance_shares(&e); diff --git a/single_offer/src/lib.rs b/single_offer/src/lib.rs index 82afcc0d..58f2531d 100644 --- a/single_offer/src/lib.rs +++ b/single_offer/src/lib.rs @@ -81,7 +81,7 @@ impl SingleOffer { // defined by the price. // `min_sell_amount` defines a lower bound on the price that the buyer would // accept. - // Buyer needs to authorize the `trade` call and internal `xfer` call to + // Buyer needs to authorize the `trade` call and internal `transfer` call to // the contract address. pub fn trade(e: Env, buyer: Address, buy_token_amount: i128, min_sell_token_amount: i128) { // Buyer needs to authorize the trade. @@ -104,22 +104,22 @@ impl SingleOffer { let contract = e.current_contract_address(); - // Perform the trade in 3 `xfer` steps. + // Perform the trade in 3 `transfer` steps. // Note, that we don't need to verify any balances - the contract would // just trap and roll back in case if any of the transfers fails for // any reason, including insufficient balance. // Transfer the `buy_token` from buyer to this contract. - // This `xfer` call should be authorized by buyer. + // This `transfer` call should be authorized by buyer. // This could as well be a direct transfer to the seller, but sending to // the contract address allows building more transparent signature // payload where the buyer doesn't need to worry about sending token to // some 'unknown' third party. - buy_token_client.xfer(&buyer, &contract, &buy_token_amount); + buy_token_client.transfer(&buyer, &contract, &buy_token_amount); // Transfer the `sell_token` from contract to buyer. - sell_token_client.xfer(&contract, &buyer, &sell_token_amount); + sell_token_client.transfer(&contract, &buyer, &sell_token_amount); // Transfer the `buy_token` to the seller immediately. - buy_token_client.xfer(&contract, &offer.seller, &buy_token_amount); + buy_token_client.transfer(&contract, &offer.seller, &buy_token_amount); } // Sends amount of token from this contract to the seller. @@ -130,7 +130,7 @@ impl SingleOffer { pub fn withdraw(e: Env, token: BytesN<32>, amount: i128) { let offer = load_offer(&e); offer.seller.require_auth(); - token::Client::new(&e, &token).xfer(&e.current_contract_address(), &offer.seller, &amount); + token::Client::new(&e, &token).transfer(&e.current_contract_address(), &offer.seller, &amount); } // Updates the price. diff --git a/single_offer/src/test.rs b/single_offer/src/test.rs index 51bb5c6e..e94449eb 100644 --- a/single_offer/src/test.rs +++ b/single_offer/src/test.rs @@ -62,7 +62,7 @@ fn test() { sell_token.mint(&token_admin, &seller, &1000); buy_token.mint(&token_admin, &buyer, &1000); // Deposit 100 sell_token from seller into offer. - sell_token.xfer(&seller, &offer.address(), &100); + sell_token.transfer(&seller, &offer.address(), &100); // Try trading 20 buy_token for at least 11 sell_token - that wouldn't // succeed because the offer price would result in 10 sell_token. diff --git a/soroban_token_spec.wasm b/soroban_token_spec.wasm old mode 100644 new mode 100755 index 3e62eefc775223aa0e6a75e9528b1720847ac2e6..b9a65b907770db34c6cd4b587671e3f36280c993 GIT binary patch delta 326 zcmaE)vq5*lqxvQ$QMTOF-29?SCI-&LoSgjf#JuEG24N-BmR zJ9|=M4p?w>gdR79ZEMRTH{4wN9SVQ9s#BK!H`ixF}hFbG|?dBeNpI=E>ItlZCN3 zaq=7?Utuh+1d7*+U~wM9CN^|xA delta 260 zcmdm>`$%WPqqsYaqHMXTx%owvObncfIXU^|iFwJX49r}adC5ibU;zeJu9Q?bhn+ns zF$b)Mle4%WH7_MGDJPYIn=7%jBqP5lvnn-(ftRHsEwzY&pR;0e0;4P^M{#ONJV<(T z592L9uJ=cK|2?_8=j>;O$pr!mtTDw!$uXO!3ZyVH%QJLO{v((yh-^0l5HL@EASyn& rU&ufZ*&zs-_d*E>w@gkDj^~6p0H)JlM289J7EzI3NN!OOoxlhH{UBBT diff --git a/timelock/src/lib.rs b/timelock/src/lib.rs index 29c08cc0..017ddfc7 100644 --- a/timelock/src/lib.rs +++ b/timelock/src/lib.rs @@ -76,7 +76,7 @@ impl ClaimableBalanceContract { from.require_auth(); // Transfer token from `from` to this contract address. - token::Client::new(&env, &token).xfer(&from, &env.current_contract_address(), &amount); + token::Client::new(&env, &token).transfer(&from, &env.current_contract_address(), &amount); // Store all the necessary info to allow one of the claimants to claim it. env.storage().set( &DataKey::Balance, @@ -112,7 +112,7 @@ impl ClaimableBalanceContract { // Transfer the stored amount of token to claimant after passing // all the checks. - token::Client::new(&env, &claimable_balance.token).xfer( + token::Client::new(&env, &claimable_balance.token).transfer( &env.current_contract_address(), &claimant, &claimable_balance.amount, diff --git a/token/src/contract.rs b/token/src/contract.rs index 3aa67e50..017bb868 100644 --- a/token/src/contract.rs +++ b/token/src/contract.rs @@ -15,19 +15,19 @@ pub trait TokenTrait { fn allowance(e: Env, from: Address, spender: Address) -> i128; - fn incr_allow(e: Env, from: Address, spender: Address, amount: i128); + fn increase_allowance(e: Env, from: Address, spender: Address, amount: i128); - fn decr_allow(e: Env, from: Address, spender: Address, amount: i128); + fn decrease_allowance(e: Env, from: Address, spender: Address, amount: i128); fn balance(e: Env, id: Address) -> i128; - fn spendable(e: Env, id: Address) -> i128; + fn spendable_balance(e: Env, id: Address) -> i128; fn authorized(e: Env, id: Address) -> bool; - fn xfer(e: Env, from: Address, to: Address, amount: i128); + fn transfer(e: Env, from: Address, to: Address, amount: i128); - fn xfer_from(e: Env, spender: Address, from: Address, to: Address, amount: i128); + fn transfer_from(e: Env, spender: Address, from: Address, to: Address, amount: i128); fn burn(e: Env, from: Address, amount: i128); @@ -35,7 +35,7 @@ pub trait TokenTrait { fn clawback(e: Env, admin: Address, from: Address, amount: i128); - fn set_auth(e: Env, admin: Address, id: Address, authorize: bool); + fn set_authorized(e: Env, admin: Address, id: Address, authorize: bool); fn mint(e: Env, admin: Address, to: Address, amount: i128); @@ -73,7 +73,7 @@ impl TokenTrait for Token { read_allowance(&e, from, spender) } - fn incr_allow(e: Env, from: Address, spender: Address, amount: i128) { + fn increase_allowance(e: Env, from: Address, spender: Address, amount: i128) { from.require_auth(); check_nonnegative_amount(amount); @@ -84,10 +84,10 @@ impl TokenTrait for Token { .expect("Updated allowance doesn't fit in an i128"); write_allowance(&e, from.clone(), spender.clone(), new_allowance); - event::incr_allow(&e, from, spender, amount); + event::increase_allowance(&e, from, spender, amount); } - fn decr_allow(e: Env, from: Address, spender: Address, amount: i128) { + fn decrease_allowance(e: Env, from: Address, spender: Address, amount: i128) { from.require_auth(); check_nonnegative_amount(amount); @@ -98,14 +98,14 @@ impl TokenTrait for Token { } else { write_allowance(&e, from.clone(), spender.clone(), allowance - amount); } - event::decr_allow(&e, from, spender, amount); + event::decrease_allowance(&e, from, spender, amount); } fn balance(e: Env, id: Address) -> i128 { read_balance(&e, id) } - fn spendable(e: Env, id: Address) -> i128 { + fn spendable_balance(e: Env, id: Address) -> i128 { read_balance(&e, id) } @@ -113,7 +113,7 @@ impl TokenTrait for Token { is_authorized(&e, id) } - fn xfer(e: Env, from: Address, to: Address, amount: i128) { + fn transfer(e: Env, from: Address, to: Address, amount: i128) { from.require_auth(); check_nonnegative_amount(amount); @@ -122,7 +122,7 @@ impl TokenTrait for Token { event::transfer(&e, from, to, amount); } - fn xfer_from(e: Env, spender: Address, from: Address, to: Address, amount: i128) { + fn transfer_from(e: Env, spender: Address, from: Address, to: Address, amount: i128) { spender.require_auth(); check_nonnegative_amount(amount); @@ -157,11 +157,11 @@ impl TokenTrait for Token { event::clawback(&e, admin, from, amount); } - fn set_auth(e: Env, admin: Address, id: Address, authorize: bool) { + fn set_authorized(e: Env, admin: Address, id: Address, authorize: bool) { check_admin(&e, &admin); admin.require_auth(); write_authorization(&e, id.clone(), authorize); - event::set_auth(&e, admin, id, authorize); + event::set_authorized(&e, admin, id, authorize); } fn mint(e: Env, admin: Address, to: Address, amount: i128) { diff --git a/token/src/event.rs b/token/src/event.rs index 64b505fb..638850f3 100644 --- a/token/src/event.rs +++ b/token/src/event.rs @@ -1,12 +1,12 @@ use soroban_sdk::{Address, Env, Symbol}; -pub(crate) fn incr_allow(e: &Env, from: Address, to: Address, amount: i128) { - let topics = (Symbol::new(e, "incr_allow"), from, to); +pub(crate) fn increase_allowance(e: &Env, from: Address, to: Address, amount: i128) { + let topics = (Symbol::new(e, "increase_allowance"), from, to); e.events().publish(topics, amount); } -pub(crate) fn decr_allow(e: &Env, from: Address, to: Address, amount: i128) { - let topics = (Symbol::new(e, "decr_allow"), from, to); +pub(crate) fn decrease_allowance(e: &Env, from: Address, to: Address, amount: i128) { + let topics = (Symbol::new(e, "decrease_allowance"), from, to); e.events().publish(topics, amount); } @@ -25,8 +25,8 @@ pub(crate) fn clawback(e: &Env, admin: Address, from: Address, amount: i128) { e.events().publish(topics, amount); } -pub(crate) fn set_auth(e: &Env, admin: Address, id: Address, authorize: bool) { - let topics = (Symbol::short("set_auth"), admin, id); +pub(crate) fn set_authorized(e: &Env, admin: Address, id: Address, authorize: bool) { + let topics = (Symbol::new(e, "set_authorized"), admin, id); e.events().publish(topics, authorize); } diff --git a/token/src/test.rs b/token/src/test.rs index 9a8b6b7a..db07c8df 100644 --- a/token/src/test.rs +++ b/token/src/test.rs @@ -33,45 +33,45 @@ fn test() { ); assert_eq!(token.balance(&user1), 1000); - token.incr_allow(&user2, &user3, &500); + token.increase_allowance(&user2, &user3, &500); assert_eq!( e.recorded_top_authorizations(), std::vec![( user2.clone(), token.contract_id.clone(), - Symbol::new(&e, "incr_allow"), + Symbol::new(&e, "increase_allowance"), (&user2, &user3, 500_i128).into_val(&e), )] ); assert_eq!(token.allowance(&user2, &user3), 500); - token.xfer(&user1, &user2, &600); + token.transfer(&user1, &user2, &600); assert_eq!( e.recorded_top_authorizations(), std::vec![( user1.clone(), token.contract_id.clone(), - Symbol::short("xfer"), + Symbol::short("transfer"), (&user1, &user2, 600_i128).into_val(&e), )] ); assert_eq!(token.balance(&user1), 400); assert_eq!(token.balance(&user2), 600); - token.xfer_from(&user3, &user2, &user1, &400); + token.transfer_from(&user3, &user2, &user1, &400); assert_eq!( e.recorded_top_authorizations(), std::vec![( user3.clone(), token.contract_id.clone(), - Symbol::short("xfer_from"), + Symbol::new(&e, "transfer_from"), (&user3, &user2, &user1, 400_i128).into_val(&e), )] ); assert_eq!(token.balance(&user1), 800); assert_eq!(token.balance(&user2), 200); - token.xfer(&user1, &user3, &300); + token.transfer(&user1, &user3, &300); assert_eq!(token.balance(&user1), 500); assert_eq!(token.balance(&user3), 300); @@ -86,19 +86,19 @@ fn test() { )] ); - token.set_auth(&admin2, &user2, &false); + token.set_authorized(&admin2, &user2, &false); assert_eq!( e.recorded_top_authorizations(), std::vec![( admin2.clone(), token.contract_id.clone(), - Symbol::short("set_auth"), + Symbol::new(&e, "set_authorized"), (&admin2, &user2, false).into_val(&e), )] ); assert_eq!(token.authorized(&user2), false); - token.set_auth(&admin2, &user3, &true); + token.set_authorized(&admin2, &user3, &true); assert_eq!(token.authorized(&user3), true); token.clawback(&admin2, &user3, &100); @@ -114,15 +114,15 @@ fn test() { assert_eq!(token.balance(&user3), 200); // Increase by 400, with an existing 100 = 500 - token.incr_allow(&user2, &user3, &400); + token.increase_allowance(&user2, &user3, &400); assert_eq!(token.allowance(&user2, &user3), 500); - token.decr_allow(&user2, &user3, &501); + token.decrease_allowance(&user2, &user3, &501); assert_eq!( e.recorded_top_authorizations(), std::vec![( user2.clone(), token.contract_id.clone(), - Symbol::new(&e, "decr_allow"), + Symbol::new(&e, "decrease_allowance"), (&user2, &user3, 501_i128).into_val(&e), )] ); @@ -141,7 +141,7 @@ fn test_burn() { token.mint(&admin, &user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.incr_allow(&user1, &user2, &500); + token.increase_allowance(&user1, &user2, &500); assert_eq!(token.allowance(&user1, &user2), 500); token.burn_from(&user2, &user1, &500); @@ -174,7 +174,7 @@ fn test_burn() { #[test] #[should_panic(expected = "insufficient balance")] -fn xfer_insufficient_balance() { +fn transfer_insufficient_balance() { let e: Env = Default::default(); let admin = Address::random(&e); let user1 = Address::random(&e); @@ -184,12 +184,12 @@ fn xfer_insufficient_balance() { token.mint(&admin, &user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.xfer(&user1, &user2, &1001); + token.transfer(&user1, &user2, &1001); } #[test] #[should_panic(expected = "can't receive when deauthorized")] -fn xfer_receive_deauthorized() { +fn transfer_receive_deauthorized() { let e: Env = Default::default(); let admin = Address::random(&e); let user1 = Address::random(&e); @@ -199,13 +199,13 @@ fn xfer_receive_deauthorized() { token.mint(&admin, &user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.set_auth(&admin, &user2, &false); - token.xfer(&user1, &user2, &1); + token.set_authorized(&admin, &user2, &false); + token.transfer(&user1, &user2, &1); } #[test] #[should_panic(expected = "can't spend when deauthorized")] -fn xfer_spend_deauthorized() { +fn transfer_spend_deauthorized() { let e: Env = Default::default(); let admin = Address::random(&e); let user1 = Address::random(&e); @@ -215,13 +215,13 @@ fn xfer_spend_deauthorized() { token.mint(&admin, &user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.set_auth(&admin, &user1, &false); - token.xfer(&user1, &user2, &1); + token.set_authorized(&admin, &user1, &false); + token.transfer(&user1, &user2, &1); } #[test] #[should_panic(expected = "insufficient allowance")] -fn xfer_from_insufficient_allowance() { +fn transfer_from_insufficient_allowance() { let e: Env = Default::default(); let admin = Address::random(&e); let user1 = Address::random(&e); @@ -232,10 +232,10 @@ fn xfer_from_insufficient_allowance() { token.mint(&admin, &user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.incr_allow(&user1, &user3, &100); + token.increase_allowance(&user1, &user3, &100); assert_eq!(token.allowance(&user1, &user3), 100); - token.xfer_from(&user3, &user1, &user2, &101); + token.transfer_from(&user3, &user1, &user2, &101); } #[test] From f11f76720ea42054d060d5a031e6e4182cc581c0 Mon Sep 17 00:00:00 2001 From: Siddharth Suresh Date: Tue, 4 Apr 2023 15:53:04 -0700 Subject: [PATCH 2/2] fix for admin parameter removal --- atomic_multiswap/src/test.rs | 16 ++++++++-------- atomic_swap/src/test.rs | 4 ++-- liquidity_pool/src/lib.rs | 2 +- liquidity_pool/src/test.rs | 4 ++-- single_offer/src/lib.rs | 6 +++++- single_offer/src/test.rs | 4 ++-- soroban_token_spec.wasm | Bin 5808 -> 5551 bytes timelock/src/test.rs | 2 +- token/src/admin.rs | 8 +------- token/src/contract.rs | 26 +++++++++++++------------- token/src/test.rs | 32 ++++++++++++++++---------------- 11 files changed, 51 insertions(+), 53 deletions(-) diff --git a/atomic_multiswap/src/test.rs b/atomic_multiswap/src/test.rs index e3526051..374901a3 100644 --- a/atomic_multiswap/src/test.rs +++ b/atomic_multiswap/src/test.rs @@ -62,13 +62,13 @@ fn test_atomic_multi_swap() { let token_a = create_token_contract(&env, &token_admin); let token_b = create_token_contract(&env, &token_admin); - token_a.mint(&token_admin, &swaps_a[0].address, &2000); - token_a.mint(&token_admin, &swaps_a[1].address, &3000); - token_a.mint(&token_admin, &swaps_a[2].address, &4000); + token_a.mint(&swaps_a[0].address, &2000); + token_a.mint(&swaps_a[1].address, &3000); + token_a.mint(&swaps_a[2].address, &4000); - token_b.mint(&token_admin, &swaps_b[0].address, &300); - token_b.mint(&token_admin, &swaps_b[1].address, &295); - token_b.mint(&token_admin, &swaps_b[2].address, &400); + token_b.mint(&swaps_b[0].address, &300); + token_b.mint(&swaps_b[1].address, &295); + token_b.mint(&swaps_b[2].address, &400); let contract = create_atomic_multiswap_contract(&env); @@ -194,8 +194,8 @@ fn test_multi_swap_with_duplicate_account() { let token_a = create_token_contract(&env, &token_admin); let token_b = create_token_contract(&env, &token_admin); - token_a.mint(&token_admin, &address_a, &3000); - token_b.mint(&token_admin, &address_b, &291); + token_a.mint(&address_a, &3000); + token_b.mint(&address_b, &291); let contract = create_atomic_multiswap_contract(&env); diff --git a/atomic_swap/src/test.rs b/atomic_swap/src/test.rs index 4178f039..09ce22d7 100644 --- a/atomic_swap/src/test.rs +++ b/atomic_swap/src/test.rs @@ -28,8 +28,8 @@ fn test_atomic_swap() { let token_a = create_token_contract(&env, &token_admin); let token_b = create_token_contract(&env, &token_admin); - token_a.mint(&token_admin, &a, &1000); - token_b.mint(&token_admin, &b, &5000); + token_a.mint(&a, &1000); + token_b.mint(&b, &5000); let contract = create_atomic_swap_contract(&env); diff --git a/liquidity_pool/src/lib.rs b/liquidity_pool/src/lib.rs index 841d2661..8141824e 100644 --- a/liquidity_pool/src/lib.rs +++ b/liquidity_pool/src/lib.rs @@ -102,7 +102,7 @@ fn mint_shares(e: &Env, to: Address, amount: i128) { let total = get_total_shares(e); let share_contract_id = get_token_share(e); - token::Client::new(e, &share_contract_id).mint(&e.current_contract_address(), &to, &amount); + token::Client::new(e, &share_contract_id).mint(&to, &amount); put_total_shares(e, total + amount); } diff --git a/liquidity_pool/src/test.rs b/liquidity_pool/src/test.rs index 66fb45f9..e2a2946a 100644 --- a/liquidity_pool/src/test.rs +++ b/liquidity_pool/src/test.rs @@ -51,10 +51,10 @@ fn test() { let contract_share: [u8; 32] = liqpool.share_id().into(); let token_share = token::Client::new(&e, &contract_share); - token1.mint(&admin1, &user1, &1000); + token1.mint(&user1, &1000); assert_eq!(token1.balance(&user1), 1000); - token2.mint(&admin2, &user1, &1000); + token2.mint(&user1, &1000); assert_eq!(token2.balance(&user1), 1000); liqpool.deposit(&user1, &100, &100, &100, &100); diff --git a/single_offer/src/lib.rs b/single_offer/src/lib.rs index 58f2531d..c20c7f21 100644 --- a/single_offer/src/lib.rs +++ b/single_offer/src/lib.rs @@ -130,7 +130,11 @@ impl SingleOffer { pub fn withdraw(e: Env, token: BytesN<32>, amount: i128) { let offer = load_offer(&e); offer.seller.require_auth(); - token::Client::new(&e, &token).transfer(&e.current_contract_address(), &offer.seller, &amount); + token::Client::new(&e, &token).transfer( + &e.current_contract_address(), + &offer.seller, + &amount, + ); } // Updates the price. diff --git a/single_offer/src/test.rs b/single_offer/src/test.rs index e94449eb..c41fe967 100644 --- a/single_offer/src/test.rs +++ b/single_offer/src/test.rs @@ -59,8 +59,8 @@ fn test() { 2, ); // Give some sell_token to seller and buy_token to buyer. - sell_token.mint(&token_admin, &seller, &1000); - buy_token.mint(&token_admin, &buyer, &1000); + sell_token.mint(&seller, &1000); + buy_token.mint(&buyer, &1000); // Deposit 100 sell_token from seller into offer. sell_token.transfer(&seller, &offer.address(), &100); diff --git a/soroban_token_spec.wasm b/soroban_token_spec.wasm index b9a65b907770db34c6cd4b587671e3f36280c993..ccc3ac8fb3d40e7bd3cb7640bd1dce0c97e45d5b 100755 GIT binary patch delta 214 zcmdm>yIz}%A+b1@k%57MF>fN5I~!A7JwpQH#7GZzCT=D!CN74JD?AyQ&P#9p%*evY z=s#JWPs~7p!JJt@fk}bMQ6dXS2xJK?Z)j*>U~*7k(qLlXWl&&r`p4KD;0k0hI5G&# zn7oive6kLo9pjzJMx1$!vw-|i#y4R091xrDPve0G27z@TLm0SSCQo6rWt=y8BCE*c z=WO=Ct}+$>@$P@Gy4pO})HnFsU$5HL=*5UT(H D6l^$W delta 396 zcmZ3ly+N0YA+b1@k%57Mv0x&XI|p-JT|GkrQ{BXH4|W!A7FHHkrj5%y8JYgcZ2ri| z!dYLz$XKu300az<2N<%Im{q`R1!e_Cb7lh|#bC~?p}_1Ykp(0yK!iq?z;cjsCI bool { e.storage().has(&key) } -fn read_administrator(e: &Env) -> Address { +pub fn read_administrator(e: &Env) -> Address { let key = DataKey::Admin; e.storage().get_unchecked(&key).unwrap() } @@ -15,9 +15,3 @@ pub fn write_administrator(e: &Env, id: &Address) { let key = DataKey::Admin; e.storage().set(&key, id); } - -pub fn check_admin(e: &Env, admin: &Address) { - if admin != &read_administrator(e) { - panic!("not authorized by admin") - } -} diff --git a/token/src/contract.rs b/token/src/contract.rs index 017bb868..037d12e4 100644 --- a/token/src/contract.rs +++ b/token/src/contract.rs @@ -1,6 +1,6 @@ //! This contract demonstrates a sample implementation of the Soroban token //! interface. -use crate::admin::{check_admin, has_administrator, write_administrator}; +use crate::admin::{has_administrator, read_administrator, write_administrator}; use crate::allowance::{read_allowance, spend_allowance, write_allowance}; use crate::balance::{is_authorized, write_authorization}; use crate::balance::{read_balance, receive_balance, spend_balance}; @@ -33,13 +33,13 @@ pub trait TokenTrait { fn burn_from(e: Env, spender: Address, from: Address, amount: i128); - fn clawback(e: Env, admin: Address, from: Address, amount: i128); + fn clawback(e: Env, from: Address, amount: i128); - fn set_authorized(e: Env, admin: Address, id: Address, authorize: bool); + fn set_authorized(e: Env, id: Address, authorize: bool); - fn mint(e: Env, admin: Address, to: Address, amount: i128); + fn mint(e: Env, to: Address, amount: i128); - fn set_admin(e: Env, admin: Address, new_admin: Address); + fn set_admin(e: Env, new_admin: Address); fn decimals(e: Env) -> u32; @@ -149,31 +149,31 @@ impl TokenTrait for Token { event::burn(&e, from, amount) } - fn clawback(e: Env, admin: Address, from: Address, amount: i128) { + fn clawback(e: Env, from: Address, amount: i128) { check_nonnegative_amount(amount); - check_admin(&e, &admin); + let admin = read_administrator(&e); admin.require_auth(); spend_balance(&e, from.clone(), amount); event::clawback(&e, admin, from, amount); } - fn set_authorized(e: Env, admin: Address, id: Address, authorize: bool) { - check_admin(&e, &admin); + fn set_authorized(e: Env, id: Address, authorize: bool) { + let admin = read_administrator(&e); admin.require_auth(); write_authorization(&e, id.clone(), authorize); event::set_authorized(&e, admin, id, authorize); } - fn mint(e: Env, admin: Address, to: Address, amount: i128) { + fn mint(e: Env, to: Address, amount: i128) { check_nonnegative_amount(amount); - check_admin(&e, &admin); + let admin = read_administrator(&e); admin.require_auth(); receive_balance(&e, to.clone(), amount); event::mint(&e, admin, to, amount); } - fn set_admin(e: Env, admin: Address, new_admin: Address) { - check_admin(&e, &admin); + fn set_admin(e: Env, new_admin: Address) { + let admin = read_administrator(&e); admin.require_auth(); write_administrator(&e, &new_admin); event::set_admin(&e, admin, new_admin); diff --git a/token/src/test.rs b/token/src/test.rs index db07c8df..26e15ab7 100644 --- a/token/src/test.rs +++ b/token/src/test.rs @@ -21,14 +21,14 @@ fn test() { let user3 = Address::random(&e); let token = create_token(&e, &admin1); - token.mint(&admin1, &user1, &1000); + token.mint(&user1, &1000); assert_eq!( e.recorded_top_authorizations(), std::vec![( admin1.clone(), token.contract_id.clone(), Symbol::short("mint"), - (&admin1, &user1, 1000_i128).into_val(&e), + (&user1, 1000_i128).into_val(&e), )] ); assert_eq!(token.balance(&user1), 1000); @@ -75,40 +75,40 @@ fn test() { assert_eq!(token.balance(&user1), 500); assert_eq!(token.balance(&user3), 300); - token.set_admin(&admin1, &admin2); + token.set_admin(&admin2); assert_eq!( e.recorded_top_authorizations(), std::vec![( admin1.clone(), token.contract_id.clone(), Symbol::short("set_admin"), - (&admin1, &admin2).into_val(&e), + (&admin2,).into_val(&e), //THIS DOESN'T WORK )] ); - token.set_authorized(&admin2, &user2, &false); + token.set_authorized(&user2, &false); assert_eq!( e.recorded_top_authorizations(), std::vec![( admin2.clone(), token.contract_id.clone(), Symbol::new(&e, "set_authorized"), - (&admin2, &user2, false).into_val(&e), + (&user2, false).into_val(&e), )] ); assert_eq!(token.authorized(&user2), false); - token.set_authorized(&admin2, &user3, &true); + token.set_authorized(&user3, &true); assert_eq!(token.authorized(&user3), true); - token.clawback(&admin2, &user3, &100); + token.clawback(&user3, &100); assert_eq!( e.recorded_top_authorizations(), std::vec![( admin2.clone(), token.contract_id.clone(), Symbol::short("clawback"), - (&admin2, &user3, 100_i128).into_val(&e), + (&user3, 100_i128).into_val(&e), )] ); assert_eq!(token.balance(&user3), 200); @@ -138,7 +138,7 @@ fn test_burn() { let user2 = Address::random(&e); let token = create_token(&e, &admin); - token.mint(&admin, &user1, &1000); + token.mint(&user1, &1000); assert_eq!(token.balance(&user1), 1000); token.increase_allowance(&user1, &user2, &500); @@ -181,7 +181,7 @@ fn transfer_insufficient_balance() { let user2 = Address::random(&e); let token = create_token(&e, &admin); - token.mint(&admin, &user1, &1000); + token.mint(&user1, &1000); assert_eq!(token.balance(&user1), 1000); token.transfer(&user1, &user2, &1001); @@ -196,10 +196,10 @@ fn transfer_receive_deauthorized() { let user2 = Address::random(&e); let token = create_token(&e, &admin); - token.mint(&admin, &user1, &1000); + token.mint(&user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.set_authorized(&admin, &user2, &false); + token.set_authorized(&user2, &false); token.transfer(&user1, &user2, &1); } @@ -212,10 +212,10 @@ fn transfer_spend_deauthorized() { let user2 = Address::random(&e); let token = create_token(&e, &admin); - token.mint(&admin, &user1, &1000); + token.mint(&user1, &1000); assert_eq!(token.balance(&user1), 1000); - token.set_authorized(&admin, &user1, &false); + token.set_authorized(&user1, &false); token.transfer(&user1, &user2, &1); } @@ -229,7 +229,7 @@ fn transfer_from_insufficient_allowance() { let user3 = Address::random(&e); let token = create_token(&e, &admin); - token.mint(&admin, &user1, &1000); + token.mint(&user1, &1000); assert_eq!(token.balance(&user1), 1000); token.increase_allowance(&user1, &user3, &100);