Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 2b07fc1

Browse files
token-swap: Assess swap fee on input token (#562)
* token-swap: Assess swap fee on input token * Update token-swap/program/src/curve.rs Co-authored-by: Tyera Eulberg <[email protected]> * Update token-swap/program/src/curve.rs Co-authored-by: Tyera Eulberg <[email protected]> * Fix new var name everywhere Co-authored-by: Tyera Eulberg <[email protected]>
1 parent b33bf9c commit 2b07fc1

File tree

7 files changed

+38
-12
lines changed

7 files changed

+38
-12
lines changed

bpf-sdk-install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env bash
22
set -e
33

4-
channel=${1:-v1.3.13}
4+
channel=${1:-v1.3.14}
55
installDir="$(dirname "$0")"/bin
66
cacheDir=~/.cache/solana-bpf-sdk/"$channel"
77

memo/program/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ program = ["solana-sdk/program"]
1717
default = ["solana-sdk/default"]
1818

1919
[dependencies]
20-
solana-sdk = { version = "1.3.12", default-features = false, optional = true }
20+
solana-sdk = { version = "1.3.14", default-features = false, optional = true }
2121

2222
[lib]
2323
name = "spl_memo"

token-swap/js/cli/token-swap-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ let tokenAccountB: PublicKey;
3939
const BASE_AMOUNT = 1000;
4040
// Amount passed to swap instruction
4141
const SWAP_AMOUNT_IN = 100;
42-
const SWAP_AMOUNT_OUT = 69;
42+
const SWAP_AMOUNT_OUT = 70;
4343
// Pool token amount minted on init
4444
const DEFAULT_POOL_TOKEN_AMOUNT = 1000000000;
4545
// Pool token amount to withdraw / deposit

token-swap/program/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ arrayref = "0.3.6"
2222
num-derive = "0.3"
2323
num-traits = "0.2"
2424
remove_dir_all = "=0.5.0"
25-
solana-sdk = { version = "1.3.12", default-features = false, optional = true }
25+
solana-sdk = { version = "1.3.14", default-features = false, optional = true }
2626
spl-token = { path = "../../token/program", default-features = false, optional = true }
2727
thiserror = "1.0"
2828

token-swap/program/src/curve.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,19 @@ impl SwapResult {
2727
fee_denominator: u64,
2828
) -> Option<SwapResult> {
2929
let invariant = swap_source_amount.checked_mul(swap_destination_amount)?;
30-
let new_source_amount = swap_source_amount.checked_add(source_amount)?;
31-
let new_destination_amount = invariant.checked_div(new_source_amount)?;
32-
let remove = swap_destination_amount.checked_sub(new_destination_amount)?;
33-
let fee = remove
30+
31+
// debit the fee to calculate the amount swapped
32+
let fee = source_amount
3433
.checked_mul(fee_numerator)?
3534
.checked_div(fee_denominator)?;
36-
let new_destination_amount = new_destination_amount.checked_add(fee)?;
37-
let amount_swapped = remove.checked_sub(fee)?;
35+
let new_source_amount_less_fee = swap_source_amount
36+
.checked_add(source_amount)?
37+
.checked_sub(fee)?;
38+
let new_destination_amount = invariant.checked_div(new_source_amount_less_fee)?;
39+
let amount_swapped = swap_destination_amount.checked_sub(new_destination_amount)?;
40+
41+
// actually add the whole amount coming in
42+
let new_source_amount = swap_source_amount.checked_add(source_amount)?;
3843
Some(SwapResult {
3944
new_source_amount,
4045
new_destination_amount,
@@ -162,4 +167,25 @@ mod tests {
162167
check_pool_token_a_rate(5, u64::MAX, 5, 10, Some(2));
163168
check_pool_token_a_rate(u64::MAX, u64::MAX, 5, 10, None);
164169
}
170+
171+
#[test]
172+
fn swap_calculation() {
173+
// calculation on https://github.com/solana-labs/solana-program-library/issues/341
174+
let swap_source_amount: u64 = 1000;
175+
let swap_destination_amount: u64 = 50000;
176+
let fee_numerator: u64 = 1;
177+
let fee_denominator: u64 = 100;
178+
let source_amount: u64 = 100;
179+
let result = SwapResult::swap_to(
180+
source_amount,
181+
swap_source_amount,
182+
swap_destination_amount,
183+
fee_numerator,
184+
fee_denominator,
185+
)
186+
.unwrap();
187+
assert_eq!(result.new_source_amount, 1100);
188+
assert_eq!(result.amount_swapped, 4505);
189+
assert_eq!(result.new_destination_amount, 45495);
190+
}
165191
}

token/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"/lib",
2323
"/module.flow.js"
2424
],
25-
"testnetDefaultChannel": "v1.3.13",
25+
"testnetDefaultChannel": "v1.3.14",
2626
"scripts": {
2727
"build": "rollup -c",
2828
"start": "babel-node cli/main.js",

token/program-v3/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ default = ["solana-sdk/default"]
2121
num-derive = "0.3"
2222
num-traits = "0.2"
2323
remove_dir_all = "=0.5.0"
24-
solana-sdk = { version = "1.3.12", default-features = false, optional = true }
24+
solana-sdk = { version = "1.3.14", default-features = false, optional = true }
2525
thiserror = "1.0"
2626
arrayref = "0.3.6"
2727
num_enum = "0.5.1"

0 commit comments

Comments
 (0)