Skip to content
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

Feat/fix edge case #351

Merged
merged 15 commits into from
Dec 11, 2024
2 changes: 2 additions & 0 deletions Dockerfile.data-backend.railway
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ RUN npm install -g pnpm
# Copy the entire repository into the Docker container
COPY . .

# Install open ssl
RUN apk add --no-cache openssl
# Install all dependencies for the workspace, including common and data-backend
RUN pnpm install --force

Expand Down
1 change: 1 addition & 0 deletions apps/data-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"bcryptjs": "^2.4.3",
"cloudinary": "^2.5.1",
"cors": "^2.8.5",
"data-backend": "link:",
"dotenv": "^16.4.5",
"fastify": "^5.1.0",
"fastify-plugin": "^5.0.1",
Expand Down
2 changes: 2 additions & 0 deletions apps/data-backend/src/routes/indexer/all-transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"

import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
2 changes: 2 additions & 0 deletions apps/data-backend/src/routes/indexer/buy-coin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"

import { HTTPStatus } from "../../utils/http";

async function buyCoinRoute(fastify: FastifyInstance, options: RouteOptions) {
Expand Down
1 change: 1 addition & 0 deletions apps/data-backend/src/routes/indexer/deploy-launch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"
import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
2 changes: 2 additions & 0 deletions apps/data-backend/src/routes/indexer/deploy-token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"

import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
1 change: 1 addition & 0 deletions apps/data-backend/src/routes/indexer/graph.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"
import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
2 changes: 2 additions & 0 deletions apps/data-backend/src/routes/indexer/holdings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"

import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
2 changes: 2 additions & 0 deletions apps/data-backend/src/routes/indexer/token_stats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"

import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
2 changes: 2 additions & 0 deletions apps/data-backend/src/routes/indexer/transactions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { FastifyInstance, RouteOptions } from "fastify";
import prisma from "indexer-prisma"
// import {prisma} from "indexer-prisma"

import { HTTPStatus } from "../../utils/http";
import { isValidStarknetAddress } from "../../utils/starknet";

Expand Down
15 changes: 0 additions & 15 deletions onchain/cairo/launchpad/.snfoundry_cache/.prev_tests_failed
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_launch_token
afk_launchpad::tests::launchpad_tests::launchpad_tests::launchpad_test_calculation
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_launch_token_with_uncreated_token
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_set_protocol_fee_percent_non_admin
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_sell_coin_for_invalid_coin
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_get_coin_launch
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_sell_coin_when_quote_amount_is_greater_than_liquidity_raised
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_sell_coin_when_share_too_low
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_get_all_launch_tokens_and_coins
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_get_coin_amount_by_quote_amount_for_buy_steps
afk_launchpad::tests::launchpad_tests::launchpad_tests::launchpad_buy_all
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_buy_coin_with_different_supply
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_buy_coin_with_different_supply_and_amount_check
afk_launchpad::tests::launchpad_tests::launchpad_tests::launchpad_end_to_end
afk_launchpad::tests::launchpad_tests::launchpad_tests::test_launchpad_end_to_end
10 changes: 0 additions & 10 deletions onchain/cairo/launchpad/src/launchpad/calcul/launch.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,6 @@ pub fn get_amount_by_type_of_coin_or_quote(
is_decreased: bool,
is_quote_amount: bool,
) -> u256 {
let mut total_supply = pool.total_token_holded.clone();
let mut final_supply = total_supply + amount;

if is_decreased {
final_supply = total_supply - amount;
}

// let mut actual_supply = total_supply;
// let mut starting_price = pool.starting_price.clone();
// let step_increase_linear = pool.slope.clone();
let bonding_type = pool.bonding_curve_type.clone();
match bonding_type {
BondingType::Linear => {
Expand Down
36 changes: 24 additions & 12 deletions onchain/cairo/launchpad/src/launchpad/launchpad.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,6 @@ pub mod LaunchpadMarketplace {
// let threshold_liquidity = self.threshold_liquidity.read();
let threshold_liquidity = pool_coin.threshold_liquidity.clone();

//new liquidity after purchase
let new_liquidity = pool_coin.liquidity_raised + quote_amount;

//assertion
assert(new_liquidity <= threshold_liquidity, 'threshold liquidity exceeded');

// TODO erc20 token transfer
let token_quote = old_launch.token_quote.clone();
Expand All @@ -627,28 +622,39 @@ pub mod LaunchpadMarketplace {
let old_price = pool_coin.price.clone();
let mut amount_protocol_fee: u256 = total_price * protocol_fee_percent / BPS;
let mut remain_liquidity = total_price - amount_protocol_fee;
let mut remain_quote_to_liquidity = total_price - amount_protocol_fee;

println!("amount quote to send {:?}", quote_amount);
println!("remain_quote_to_liquidity {:?}", remain_quote_to_liquidity);

//new liquidity after purchase
let new_liquidity = pool_coin.liquidity_raised + remain_quote_to_liquidity;

//assertion
assert(new_liquidity <= threshold_liquidity, 'threshold liquidity exceeded');
// let mut amount = 0;
// Pay with quote token
// Transfer quote & coin
// TOdo fix issue price
let mut amount = get_amount_by_type_of_coin_or_quote(
pool_coin.clone(), coin_address.clone(), remain_liquidity.clone(), false, true
pool_coin.clone(), coin_address.clone(), remain_quote_to_liquidity.clone(), false, true
);
// remain_liquidity = total_price - amount_protocol_fee;
// TODO check available to buy

println!("amount memecoin to receive {:?}", amount);
println!("amount quote to receive {:?}", quote_amount);

// TODO readd this check and check why it's broken
// assert(amount <= pool_coin.available_supply, 'no available supply');
println!("transfer protocol fees {:?}", amount_protocol_fee);

erc20
.transfer_from(
get_caller_address(), self.protocol_fee_destination.read(), amount_protocol_fee
);
// println!("remain_liquidity {:?}", remain_liquidity);
erc20.transfer_from(get_caller_address(), get_contract_address(), remain_liquidity);
println!("transfer remain_liquidity {:?}", remain_quote_to_liquidity);

erc20.transfer_from(get_caller_address(), get_contract_address(), remain_quote_to_liquidity);
// In case the user want to buy more than the threshold
// Give the available supply
// if total_price + old_launch.liquidity_raised.clone() > threshold_liquidity {
Expand Down Expand Up @@ -734,12 +740,14 @@ pub mod LaunchpadMarketplace {
// pool_coin.price = total_price / amount;

// Check if liquidity threshold raise
let threshold = self.threshold_liquidity.read();
// let threshold = self.threshold_liquidity.read();
let threshold = pool_coin.threshold_liquidity.clone();
let threshold_liq = self.threshold_liquidity.read();
let threshold_mc = self.threshold_market_cap.read();
// println!("threshold {:?}", threshold);
// println!("pool_coin.liquidity_raised {:?}", pool_coin.liquidity_raised);

let mc = (pool_coin.price * total_supply_memecoin);
// let mc = (pool_coin.price * total_supply_memecoin);
// TODO add liquidity launch
// TOTAL_SUPPLY / 5
// 20% go the liquidity
Expand Down Expand Up @@ -834,6 +842,8 @@ pub mod LaunchpadMarketplace {
let quote_amount_protocol_fee: u256 = quote_amount_total * protocol_fee_percent / BPS;

let quote_amount = quote_amount_total - quote_amount_protocol_fee;
println!("quote_amount {:?}", quote_amount);

assert(old_pool.liquidity_raised >= quote_amount, 'liquidity <= amount');

// TODO fix this function
Expand Down Expand Up @@ -885,10 +895,11 @@ pub mod LaunchpadMarketplace {
);

// Transfer protocol fee to the designated destination
println!("transfer fees protocol");
if quote_amount_protocol_fee > 0 {
erc20.transfer(self.protocol_fee_destination.read(), quote_amount_protocol_fee);
}

println!("transfer quote amount");
// Transfer the remaining quote amount to the user
if quote_amount > 0 {
erc20.transfer(caller, quote_amount);
Expand All @@ -905,6 +916,7 @@ pub mod LaunchpadMarketplace {

// TODO fix amount owned and sellable.
// Update share user coin

share_user.amount_owned -= remain_coin_amount;
share_user.amount_sell += remain_coin_amount;

Expand Down
2 changes: 1 addition & 1 deletion onchain/cairo/launchpad/src/tests/launchpad_tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ mod launchpad_tests {
println!("buy coin {:?}", THRESHOLD_LIQUIDITY);

run_buy_by_amount(launchpad, erc20, memecoin, 1, token_address, sender_address,);
println!("get share user {:?}");
println!("get share user");

let share_user = launchpad
.get_share_of_user_by_contract(sender_address, memecoin.contract_address);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build:indexer-prisma":"cd packages/indexer-prisma && pnpm run build",
"build:prisma-db":"cd packages/prisma-db && pnpm pull && pnpm generate",
"build:backend":"cd apps/data-backend && pnpm run build",
"build:backend:all":"pnpm run build:indexer-prisma && pnpm run build:prisma-db && cd apps/data-backend && pnpm install && pnpm run build:all",
"build:backend:all":"pnpm run build:prisma-db && pnpm run build:indexer-prisma && cd apps/data-backend && pnpm install && pnpm run build:all",
"run:backend":"cd apps/data-backend && pnpm run start",
"build:backend:prisma":"cd apps/data-backend && pnpm run prisma:setup",
"run:backend:prod":"cd apps/data-backend && pnpm run start:prod",
Expand Down
6 changes: 6 additions & 0 deletions packages/indexer-prisma/dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { PrismaClient } from '@prisma/client';
declare const prisma: PrismaClient<import("@prisma/client").Prisma.PrismaClientOptions, never, import("@prisma/client/runtime/library").DefaultArgs>;
export * from '@prisma/client';
export default prisma;
export { PrismaClient };
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions packages/indexer-prisma/dist/index.d.ts.map

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

32 changes: 32 additions & 0 deletions packages/indexer-prisma/dist/index.js

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

1 change: 1 addition & 0 deletions packages/indexer-prisma/dist/index.js.map

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

28 changes: 28 additions & 0 deletions packages/indexer-prisma/package copy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "indexer-prisma",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"types": "src/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx prisma generate && npx prisma db pull && tsc",
"build:push": "npx prisma generate && npx prisma db push && tsc",
"generate":"npx prisma generate",
"pull":"npx prisma db pull",
"push":"npx prisma db push" ,
"prisma:setup": "npx prisma generate && npx prisma db push"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@prisma/client": "^5.18.0"
},
"devDependencies": {
"prisma": "^5.18.0",
"starknet": "^6.6.6",
"tsup": "^8.0.2",
"typescript": "~5.3.3"
}
}
2 changes: 1 addition & 1 deletion packages/indexer-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "indexer-prisma",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx prisma generate && npx prisma db pull && tsc",
Expand Down
20 changes: 20 additions & 0 deletions packages/indexer-prisma/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()
// Test the connection
// prisma.$connect()
// .then(() => console.log('Successfully connected to database'))
// .catch((e: any) => console.error('Failed to connect to database:', e))

// Export everything from @prisma/client for types
export * from '@prisma/client'

export default prisma
export { PrismaClient }

// import { PrismaClient } from '@prisma/client'

// export const prisma = new PrismaClient()

// // Export any other types or utilities you need
// export * from '@prisma/client'
21 changes: 21 additions & 0 deletions packages/indexer-prisma/tsconfig copy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"outDir": "./dist", // Output directory for compiled JS files
"module": "CommonJS", // Use CommonJS for Node.js compatibility
"target": "ESNext",
"lib": ["dom", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
},
"exclude": ["node_modules", "dist"],
"include": ["src/**/*", "**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"]
}
18 changes: 8 additions & 10 deletions packages/indexer-prisma/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"compilerOptions": {
"outDir": "./dist", // Output directory for compiled JS files
"module": "CommonJS", // Use CommonJS for Node.js compatibility
"target": "ESNext",
"lib": ["dom", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"target": "es2020",
"module": "commonjs",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"exclude": ["node_modules", "dist"],
"include": ["src/**/*", "**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx"]
Expand Down
1 change: 1 addition & 0 deletions packages/prisma-db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"types": "src/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx prisma generate && npx prisma db pull && tsc",
Expand Down
Loading
Loading