Skip to content

Commit

Permalink
Token interface updates (#224)
Browse files Browse the repository at this point in the history
* Rename token function names to match the interface

* fix for admin parameter removal
  • Loading branch information
sisuresh authored Apr 6, 2023
1 parent 9613590 commit 22e1827
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 136 deletions.
44 changes: 33 additions & 11 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
8 changes: 4 additions & 4 deletions account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<BytesN<32>, i128>::new(&env);
Expand Down Expand Up @@ -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(());
}

Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions account/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -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()
Expand All @@ -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();
Expand All @@ -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();
Expand Down
16 changes: 8 additions & 8 deletions atomic_multiswap/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions atomic_swap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
4 changes: 2 additions & 2 deletions atomic_swap/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 9 additions & 9 deletions liquidity_pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ 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);
}

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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions liquidity_pool/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 22e1827

Please sign in to comment.