From fff68c1c2de6595bec628f2559b26e3b855f30cc Mon Sep 17 00:00:00 2001 From: Himanshu Singh <129299316+TheRanomial@users.noreply.github.com> Date: Mon, 17 Feb 2025 22:35:48 +0530 Subject: [PATCH] Addressed launchpad issue (#497) * Addressed launchpad issue * continuing the function --- .../services/buy-token/buy-token.service.ts | 136 ++++++---- .../20250216172107_init2/migration.sql | 252 ++++++++++++++++++ .../prisma/migrations/migration_lock.toml | 3 + 3 files changed, 333 insertions(+), 58 deletions(-) create mode 100644 packages/indexer-prisma/prisma/migrations/20250216172107_init2/migration.sql create mode 100644 packages/indexer-prisma/prisma/migrations/migration_lock.toml diff --git a/apps/nestjs-indexer/src/services/buy-token/buy-token.service.ts b/apps/nestjs-indexer/src/services/buy-token/buy-token.service.ts index 42e557ad..7644ba84 100644 --- a/apps/nestjs-indexer/src/services/buy-token/buy-token.service.ts +++ b/apps/nestjs-indexer/src/services/buy-token/buy-token.service.ts @@ -5,21 +5,24 @@ import { BuyToken } from './interfaces'; @Injectable() export class BuyTokenService { private readonly logger = new Logger(BuyTokenService.name); - constructor(private readonly prismaService: PrismaService) { } + constructor(private readonly prismaService: PrismaService) {} async create(data: BuyToken) { try { - const tokenLaunchRecord = await this.prismaService.token_launch.findFirst( - { where: { memecoin_address: data.memecoinAddress } }, - ); + await this.prismaService.$transaction(async (prisma) => { + const tokenLaunchRecord = await prisma.token_launch.findFirst({ + where: { memecoin_address: data.memecoinAddress }, + }); - let price = tokenLaunchRecord?.price ?? 0; + let price = tokenLaunchRecord?.price ?? 0; + + if (!tokenLaunchRecord) { + this.logger.warn( + `Record with memecoin address ${data.memecoinAddress} doesn't exist`, + ); + return; + } - if (!tokenLaunchRecord) { - this.logger.warn( - `Record with memecoin address ${data.memecoinAddress} doesn't exists`, - ); - } else { const newSupply = Number(tokenLaunchRecord.current_supply ?? 0) - Number(data.amount); let newLiquidityRaised = @@ -28,7 +31,7 @@ export class BuyTokenService { newLiquidityRaised = newLiquidityRaised - Number(data?.protocolFee); - console.log("newLiquidityRaised", newLiquidityRaised); + console.log('newLiquidityRaised', newLiquidityRaised); const maxLiquidityRaised = tokenLaunchRecord?.threshold_liquidity; if (Number(newLiquidityRaised) > Number(maxLiquidityRaised)) { @@ -43,22 +46,25 @@ export class BuyTokenService { // Calculate price based on liquidity and token supply // TODO better to do it with ETH per memecoin or Memecoin per ETH? - // + // // Price = ETH liquidity / Fixed token supply in pool - const initPoolSupply = Number(tokenLaunchRecord?.initial_pool_supply_dex ?? 0); // Fixed memecoin supply + const initPoolSupply = Number( + tokenLaunchRecord?.initial_pool_supply_dex ?? 0, + ); // Fixed memecoin supply const liquidityInQuoteToken = Number(newLiquidityRaised); // ETH liquidity that increases on buy, decreases on sell const tokensInPool = Number(initPoolSupply); // Fixed token supply - // Memecoin per ETH - let priceBuy = tokensInPool > 0 ? liquidityInQuoteToken / tokensInPool : 0; - // ETH per Memecoin + // Memecoin per ETH + const priceBuy = + tokensInPool > 0 ? liquidityInQuoteToken / tokensInPool : 0; + // ETH per Memecoin // let priceBuy = liquidityInQuoteToken > 0 && tokensInPool > 0 ? liquidityInQuoteToken / tokensInPool : 0; - + // if (priceBuy < 0) { // priceBuy = 0; // } price = priceBuy; - console.log("price calculation", price); + console.log('price calculation', price); await this.prismaService.token_launch.update({ where: { transaction_hash: tokenLaunchRecord.transaction_hash }, data: { @@ -68,7 +74,7 @@ export class BuyTokenService { // }, liquidity_raised: newLiquidityRaised.toString(), total_token_holded: newTotalTokenHolded.toString(), - price: price?.toString() + price: price?.toString(), }, // update: { // current_supply: newSupply.toString(), @@ -77,53 +83,67 @@ export class BuyTokenService { // price: price?.toString() // } }); - } - - await this.prismaService.shares_token_user.upsert({ - where: { - id: `${data.ownerAddress}_${data.memecoinAddress}`, - owner: data.ownerAddress, - token_address: data.memecoinAddress, - }, - update: { - amount_owned: { - increment: data.amount, + + const sharesTokenUser = await prisma.shares_token_user.findUnique({ + where: { + id: `${data.ownerAddress}_${data.memecoinAddress}`, }, - }, - create: { - id: `${data.ownerAddress}_${data.memecoinAddress}`, - owner: data.ownerAddress, - token_address: data.memecoinAddress, - amount_owned: data.amount, - }, - }); + }); + + let newAmountOwned = sharesTokenUser + ? Number(sharesTokenUser.amount_owned) + Number(data.amount) + : Number(data.amount); - await this.prismaService.token_transactions.upsert({ - where: { transfer_id: data.transferId }, - update: {}, - create: { - transfer_id: data.transferId, - network: data.network, - block_hash: data.blockHash, - block_number: data.blockNumber, - block_timestamp: data.blockTimestamp, - transaction_hash: data.transactionHash, - memecoin_address: data.memecoinAddress, - owner_address: data.ownerAddress, - last_price: data.lastPrice, - quote_amount: data.quoteAmount, - price: price?.toString() ?? data.price, - amount: data.amount, - protocol_fee: data.protocolFee, - time_stamp: data.timestamp, - transaction_type: data.transactionType, - }, + if (newAmountOwned > newTotalTokenHolded) { + this.logger.warn( + `Amount owned (${newAmountOwned}) exceeds total token held (${newTotalTokenHolded}). Adjusting amount owned to total token held.`, + ); + newAmountOwned = newTotalTokenHolded; + } + + await prisma.shares_token_user.upsert({ + where: { + id: `${data.ownerAddress}_${data.memecoinAddress}`, + }, + update: { + amount_owned: newAmountOwned.toString(), + }, + create: { + id: `${data.ownerAddress}_${data.memecoinAddress}`, + owner: data.ownerAddress, + token_address: data.memecoinAddress, + amount_owned: data.amount.toString(), + }, + }); + + await prisma.token_transactions.upsert({ + where: { transfer_id: data.transferId }, + update: {}, + create: { + transfer_id: data.transferId, + network: data.network, + block_hash: data.blockHash, + block_number: data.blockNumber, + block_timestamp: data.blockTimestamp, + transaction_hash: data.transactionHash, + memecoin_address: data.memecoinAddress, + owner_address: data.ownerAddress, + last_price: data.lastPrice, + quote_amount: data.quoteAmount, + price: price?.toString() ?? data.price, + amount: data.amount, + protocol_fee: data.protocolFee, + time_stamp: data.timestamp, + transaction_type: data.transactionType, + }, + }); }); } catch (error) { this.logger.error( `Error creating buy token record: ${error.message}`, error.stack, ); + throw error; } } } diff --git a/packages/indexer-prisma/prisma/migrations/20250216172107_init2/migration.sql b/packages/indexer-prisma/prisma/migrations/20250216172107_init2/migration.sql new file mode 100644 index 00000000..7ca3dfb2 --- /dev/null +++ b/packages/indexer-prisma/prisma/migrations/20250216172107_init2/migration.sql @@ -0,0 +1,252 @@ +-- CreateTable +CREATE TABLE "token_deploy" ( + "transaction_hash" TEXT NOT NULL, + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "memecoin_address" TEXT, + "owner_address" TEXT, + "name" TEXT, + "symbol" TEXT, + "initial_supply" TEXT, + "total_supply" TEXT, + "_cursor" BIGINT, + "time_stamp" TIMESTAMP(6), + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "is_launched" BOOLEAN, + + CONSTRAINT "token_deploy_pkey" PRIMARY KEY ("transaction_hash") +); + +-- CreateTable +CREATE TABLE "token_launch" ( + "transaction_hash" TEXT NOT NULL, + "network" TEXT, + "block_hash" TEXT, + "owner_address" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "memecoin_address" TEXT, + "quote_token" TEXT, + "exchange_name" TEXT, + "total_supply" TEXT, + "threshold_liquidity" TEXT, + "current_supply" TEXT, + "liquidity_raised" TEXT, + "is_liquidity_added" BOOLEAN, + "total_token_holded" TEXT, + "price" TEXT, + "bonding_type" TEXT, + "_cursor" BIGINT, + "time_stamp" TIMESTAMP(6), + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "initial_pool_supply_dex" TEXT, + + CONSTRAINT "token_launch_pkey" PRIMARY KEY ("transaction_hash") +); + +-- CreateTable +CREATE TABLE "unrugmeme_deploy" ( + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT, + "memecoin_address" TEXT NOT NULL, + "owner_address" TEXT, + "name" TEXT, + "symbol" TEXT, + "initial_supply" TEXT, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "_cursor" BIGINT, + + CONSTRAINT "unrugmeme_deploy_pkey" PRIMARY KEY ("memecoin_address") +); + +-- CreateTable +CREATE TABLE "unrugmeme_launch" ( + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT, + "memecoin_address" TEXT NOT NULL, + "quote_token" TEXT, + "exchange_name" TEXT, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "_cursor" BIGINT, + + CONSTRAINT "unrugmeme_launch_pkey" PRIMARY KEY ("memecoin_address") +); + +-- CreateTable +CREATE TABLE "unrugmeme_transfers" ( + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT, + "transfer_id" TEXT NOT NULL, + "from_address" TEXT, + "to_address" TEXT, + "memecoin_address" TEXT, + "amount" TEXT, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "_cursor" BIGINT, + + CONSTRAINT "unrugmeme_transfers_pkey" PRIMARY KEY ("transfer_id") +); + +-- CreateTable +CREATE TABLE "token_transactions" ( + "transfer_id" TEXT NOT NULL, + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT, + "memecoin_address" TEXT, + "owner_address" TEXT, + "last_price" TEXT, + "quote_amount" TEXT, + "coin_received" TEXT, + "initial_supply" TEXT, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "total_supply" TEXT, + "current_supply" TEXT, + "liquidity_raised" TEXT, + "price" TEXT, + "protocol_fee" TEXT, + "amount" DECIMAL, + "_cursor" BIGINT, + "transaction_type" TEXT NOT NULL, + "time_stamp" TIMESTAMP(6), + + CONSTRAINT "token_transactions_pkey" PRIMARY KEY ("transfer_id") +); + +-- CreateTable +CREATE TABLE "renew_subscription" ( + "owner_address" TEXT, + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT NOT NULL, + "name" TEXT, + "old_name" TEXT, + "paid" TEXT, + "quote_address" TEXT, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "_cursor" BIGINT, + "time_stamp" TEXT, + + CONSTRAINT "renew_subscription_pkey" PRIMARY KEY ("transaction_hash") +); + +-- CreateTable +CREATE TABLE "username_changed" ( + "owner_address" TEXT, + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT NOT NULL, + "name" TEXT, + "old_name" TEXT, + "paid" TEXT, + "quote_address" TEXT, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "_cursor" BIGINT, + "time_stamp" TEXT, + + CONSTRAINT "username_changed_pkey" PRIMARY KEY ("transaction_hash") +); + +-- CreateTable +CREATE TABLE "username_claimed" ( + "owner_address" TEXT, + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT NOT NULL, + "expiry" TIMESTAMP(6), + "username" TEXT, + "name" TEXT, + "symbol" TEXT, + "paid" TEXT, + "quote_address" TEXT, + "_cursor" BIGINT, + "time_stamp" TIMESTAMP(6), + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "username_claimed_pkey" PRIMARY KEY ("transaction_hash") +); + +-- CreateTable +CREATE TABLE "shares_token_user" ( + "id" TEXT NOT NULL, + "owner" TEXT NOT NULL, + "token_address" TEXT NOT NULL, + "amount_owned" DECIMAL DEFAULT 0, + "is_claimable" BOOLEAN DEFAULT false, + "amount_claimed" DECIMAL DEFAULT 0, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "shares_token_user_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "tip_deposit" ( + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT, + "deposit_id" TEXT NOT NULL, + "sender" TEXT NOT NULL, + "nostr_recipient" TEXT NOT NULL, + "starknet_recipient" TEXT, + "token_address" TEXT NOT NULL, + "amount" DECIMAL, + "gas_amount" DECIMAL, + "gas_token_address" TEXT, + "is_cancelled" BOOLEAN NOT NULL DEFAULT false, + "is_claimed" BOOLEAN NOT NULL DEFAULT false, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(6), + + CONSTRAINT "tip_deposit_pkey" PRIMARY KEY ("deposit_id") +); + +-- CreateTable +CREATE TABLE "tip_transfer" ( + "network" TEXT, + "block_hash" TEXT, + "block_number" BIGINT, + "block_timestamp" TIMESTAMP(6), + "transaction_hash" TEXT NOT NULL, + "sender" TEXT NOT NULL, + "nostr_recipient" TEXT NOT NULL, + "starknet_recipient" TEXT, + "token_address" TEXT NOT NULL, + "amount" DECIMAL, + "created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP, + "updated_at" TIMESTAMP(6), + + CONSTRAINT "tip_transfer_pkey" PRIMARY KEY ("transaction_hash") +); + +-- CreateTable +CREATE TABLE "IndexerStats" ( + "id" SERIAL NOT NULL, + "lastBlockScraped" INTEGER NOT NULL, + "lastTx" TEXT NOT NULL, + "lastTimestamp" TIMESTAMP(3) NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" TIMESTAMP(3) NOT NULL, + + CONSTRAINT "IndexerStats_pkey" PRIMARY KEY ("id") +); diff --git a/packages/indexer-prisma/prisma/migrations/migration_lock.toml b/packages/indexer-prisma/prisma/migrations/migration_lock.toml new file mode 100644 index 00000000..648c57fd --- /dev/null +++ b/packages/indexer-prisma/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (e.g., Git) +provider = "postgresql" \ No newline at end of file