Skip to content

feature: update amm contract to use uusdc and uatom instead of symbol #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2024
Merged
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
22 changes: 11 additions & 11 deletions __tests__/contract2.test.ts
Original file line number Diff line number Diff line change
@@ -9,15 +9,15 @@ import { useChain, generateMnemonic } from 'starshipjs';
import { sleep } from '../test-utils/sleep';
import './setup.test';

describe('JSD tests', () => {
describe('Contract 2: AMM contract test', () => {
let wallet, denom, address, queryClient, signingClient;
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
let contractCode, contractIndex;

let wallet2, address2;
let fee;

const denom2 = "uweb", denomATOM = "ATOM", denomUSDC = "USDC";
const denom2 = "uweb", uatom = "uatom", uusdc = "uusdc";

beforeAll(async () => {
({
@@ -54,8 +54,8 @@ describe('JSD tests', () => {

await creditFromFaucet(address, denom);
await creditFromFaucet(address, denom2);
await creditFromFaucet(address, denomATOM);
await creditFromFaucet(address, denomUSDC);
await creditFromFaucet(address, uatom);
await creditFromFaucet(address, uusdc);

await creditFromFaucet(address2, denom);
await creditFromFaucet(address2, denom2);
@@ -118,7 +118,7 @@ describe('JSD tests', () => {
creator: address,
index: contractIndex,
fnName: "addLiquidity",
arg: `{"amount0":50, "amount1":50}`,
arg: `{"amount0":50000000, "amount1":50000000}`,
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
@@ -129,25 +129,25 @@ describe('JSD tests', () => {
});

it('check balance after addLiquidity', async () => {
const usdcBalance = await signingClient.getBalance(address, "USDC");
expect(usdcBalance.amount).toEqual("9999999950");
const usdcBalance = await signingClient.getBalance(address, uusdc);
expect(usdcBalance.amount).toEqual("9950000000");

const atomBalance = await signingClient.getBalance(address, "ATOM");
expect(atomBalance.amount).toEqual("9999999950");
const atomBalance = await signingClient.getBalance(address, uatom);
expect(atomBalance.amount).toEqual("9950000000");
});

it('perform swap eval', async () => {
const msg = jsd.jsd.MessageComposer.fromPartial.eval({
creator: address,
index: contractIndex,
fnName: "swap",
arg: `{"tokenIn":"USDC","amountIn":10}`,
arg: `{"tokenIn":"${uusdc}","amountIn":10000000}`,
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = jsd.jsd.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(response.result).toEqual("8.312489578122396");
expect(response.result).toEqual("9969998.011982398");
});
});
2 changes: 1 addition & 1 deletion configs/ci.yaml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ chains:
binary: jsdd
prefix: hyper
denom: uhyper
coins: 100000000000000uhyper,100000000000000uweb,100000000000000ATOM,100000000000000USDC
coins: 100000000000000uhyper,100000000000000uweb,100000000000000uatom,100000000000000uusdc
hdPath: m/44'/118'/0'/0/0
coinType: 118
repo: https://github.com/cosmology-tech/jsd
2 changes: 1 addition & 1 deletion configs/local.yaml
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ chains:
binary: jsdd
prefix: hyper
denom: uhyper
coins: 100000000000000uhyper,100000000000000uweb,100000000000000ATOM,100000000000000USDC
coins: 100000000000000uhyper,100000000000000uweb,100000000000000uatom,100000000000000uusdc
hdPath: m/44'/118'/0'/0/0
coinType: 118
repo: https://github.com/cosmology-tech/jsd
51 changes: 31 additions & 20 deletions src/contract2/amm.ts
Original file line number Diff line number Diff line change
@@ -27,8 +27,8 @@ export class Contract {
this.reserves = store.reserves;
}

token0: string = "USDC";
token1: string = "ATOM";
token0: string = "uusdc"; // ibc denom for usdc
token1: string = "uatom"; // ibc denom for atom

schema = {
getTotalSupply: {
@@ -75,6 +75,7 @@ export class Contract {
];
}

// swap method adjusted for uusdc and uatom scaling
swap({tokenIn, amountIn}: {tokenIn: string, amountIn: number}) {
const isToken0 = tokenIn == this.token0;
const isToken1 = tokenIn == this.token1;
@@ -91,47 +92,53 @@ export class Contract {
? [this.token0, this.token1, reserve0, reserve1]
: [this.token1, this.token0, reserve1, reserve0];

// Adjust amountIn to account for scaling (from uusdc/uatom)
const adjustedAmountIn = amountIn / 1e6; // Convert back to full tokens
sendCoins(this.msg.sender, this.address, {
[tokenIn]: amountIn,
[tokenIn]: amountIn, // Amount in uusdc/uatom remains unchanged
});

const amountInWithFee = amountIn * 997 / 1000;
const amountInWithFee = adjustedAmountIn * 997 / 1000;
const amountOut = (reserveOut * amountInWithFee) / (reserveIn + amountInWithFee);

sendCoins(this.address, this.msg.sender, {
[tokenOut]: amountOut,
[tokenOut]: amountOut * 1e6, // Convert output back to uusdc/uatom
});

this.#update(
this.#getBankBalance(this.address, this.token0).amount,
this.#getBankBalance(this.address, this.token1).amount,
);

return amountOut;
return amountOut * 1e6; // Return result in uusdc/uatom
}

// addLiquidity method adjusted for uusdc and uatom scaling
addLiquidity({amount0, amount1}: {amount0: number, amount1: number}) {
sendCoins(this.msg.sender, this.address, {
[this.token0]: amount0,
[this.token1]: amount1,
[this.token0]: amount0, // uusdc
[this.token1]: amount1, // uatom
});

const [reserve0, reserve1] = this.reserves.value;

if (reserve0 > 0 || reserve1 > 0) {
if (reserve0 * amount1 != reserve1 * amount0) {
if (reserve0 * (amount1 / 1e6) != reserve1 * (amount0 / 1e6)) {
throw Error("invalid liquidity");
}
}

let shares = 0
let shares = 0;
const adjustedAmount0 = amount0 / 1e6; // Convert to full tokens
const adjustedAmount1 = amount1 / 1e6; // Convert to full tokens

if (this.totalSupply.value > 0) {
shares = Math.sqrt(amount0 * amount1)
shares = Math.sqrt(adjustedAmount0 * adjustedAmount1);
} else {
shares = Math.min(
(amount0 * this.totalSupply.value) / reserve0,
(amount1 * this.totalSupply.value) / reserve1,
)
(adjustedAmount0 * this.totalSupply.value) / reserve0,
(adjustedAmount1 * this.totalSupply.value) / reserve1,
);
}

this.#mint(this.msg.sender, shares);
@@ -144,20 +151,24 @@ export class Contract {
return shares;
}

// removeLiquidity method adjusted for uusdc and uatom scaling
removeLiquidity({shares}: {shares: number}) {
const bal0 = this.#getBankBalance(this.address, this.token0);
const bal1 = this.#getBankBalance(this.address, this.token1);
const totalSupply = this.totalSupply.value;

const amount0 = bal0 * shares / totalSupply;
const amount1 = bal1 * shares / totalSupply;
// Adjust output to uusdc/uatom
const amount0 = (bal0 * shares / totalSupply) * 1e6;
const amount1 = (bal1 * shares / totalSupply) * 1e6;

this.#burn(this.msg.sender, shares);
this.#update(bal0 - amount0, bal1 - amount1);
this.#update(bal0 - amount0 / 1e6, bal1 - amount1 / 1e6);

sendCoins(this.address, this.msg.sender, {
[this.token0]: amount0,
[this.token1]: amount1,
[this.token0]: amount0, // uusdc
[this.token1]: amount1, // uatom
});

return [amount0, amount1];
return [amount0, amount1]; // Return uusdc/uatom
}
}
Loading