Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions contracts/stableswap.clar
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@
(y-amount-scaled (get scaled-y scaled-up-swap-amount))
(y-amount-fees-lps-scaled (/ (* y-amount-scaled swap-fee-lps) u10000))
(y-amount-fees-protocol-scaled (/ (* y-amount-scaled swap-fee-protocol) u10000))
(y-amount-total-fees-scaled (/ (* y-amount total-swap-fee) u10000))
(y-amount-total-fees-scaled (/ (* y-amount-scaled total-swap-fee) u10000))
(updated-y-amount-scaled (- y-amount-scaled y-amount-total-fees-scaled))
(updated-y-balance-scaled (+ current-balance-y-scaled updated-y-amount-scaled))
(new-x-scaled (get-x updated-y-balance-scaled current-balance-x-scaled updated-y-amount-scaled (* (get amplification-coefficient pair-data) number-of-tokens)))
Expand Down Expand Up @@ -239,7 +239,7 @@
(x-amount-scaled (get scaled-x scaled-up-swap-amount))
(x-amount-fees-lps-scaled (/ (* x-amount-scaled swap-fee-lps) u10000))
(x-amount-fees-protocol-scaled (/ (* x-amount-scaled swap-fee-protocol) u10000))
(x-amount-total-fees-scaled (/ (* x-amount total-swap-fee) u10000))
(x-amount-total-fees-scaled (/ (* x-amount-scaled total-swap-fee) u10000))
(updated-x-amount-scaled (- x-amount-scaled x-amount-total-fees-scaled))
(updated-x-balance-scaled (+ current-balance-x-scaled updated-x-amount-scaled))
(new-y-scaled (get-y updated-x-balance-scaled current-balance-y-scaled updated-x-amount-scaled (* (get amplification-coefficient pair-data) number-of-tokens)))
Expand Down Expand Up @@ -1032,4 +1032,3 @@
(ok staking-contract)
)
)

31 changes: 30 additions & 1 deletion tests/stableswap_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,35 @@ Clarinet.test({
},
});

// Test get-dx returns the same amount as swap-y-for-x for mixed-decimal pairs
Clarinet.test({
name: "Ensure get-dx matches swap-y-for-x after scaled fees",
async fn(chain: Chain, accounts: Map<string, Account>) {
const deployer = accounts.get("deployer")!;

chain.mineBlock([
Tx.contractCall("usda-token", "mint", [types.uint(100000000000000), types.principal(deployer.address)], deployer.address)
]);

chain.mineBlock([
Tx.contractCall("susdt-token", "mint", [types.uint(10000000000000000), types.principal(deployer.address)], deployer.address)
]);

chain.mineBlock([
Tx.contractCall("stableswap", "create-pair", [types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.susdt-token"), types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.usda-token"), types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.usda-susdt-lp-token"), types.uint(100), types.ascii("test"), types.uint(1000000000000000), types.uint(10000000000000)], deployer.address)
]);

const quote = chain.callReadOnlyFn("stableswap", "get-dx", [types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.usda-token"), types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.susdt-token"), types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.usda-susdt-lp-token"), types.uint(100000000)], deployer.address);

const block = chain.mineBlock([
Tx.contractCall("stableswap", "swap-y-for-x", [types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.usda-token"), types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.susdt-token"), types.principal("ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM.usda-susdt-lp-token"), types.uint(100000000), types.uint(0)], deployer.address)
]);

quote.result.expectOk().expectUint(9994998022);
block.receipts[0].result.expectOk().expectUint(9994998022);
},
});


// Test swap y for x 9 times and get the same amount
Clarinet.test({
Expand Down Expand Up @@ -1675,4 +1704,4 @@ Clarinet.test({
block.receipts[0].result.expectErr()
console.log(JSON.stringify(block.receipts));
},
});
});