Skip to content

Commit

Permalink
regarding market cap
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRanomial committed Feb 18, 2025
1 parent b10f598 commit ba47999
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
14 changes: 13 additions & 1 deletion apps/nestjs-indexer/src/services/buy-token/buy-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ export class BuyTokenService {
return;
}

const newSupply =
let newSupply =
Number(tokenLaunchRecord.current_supply ?? 0) - Number(data.amount);
let newLiquidityRaised =
Number(tokenLaunchRecord.liquidity_raised ?? 0) +
Number(data.quoteAmount);

if (newSupply < 0) {
this.logger.warn(
`Buy amount ${data.amount} would exceed remaining supply ${tokenLaunchRecord.current_supply}. Setting supply to 0.`,
);
newSupply = 0;
}

newLiquidityRaised = newLiquidityRaised - Number(data?.protocolFee);

console.log('newLiquidityRaised', newLiquidityRaised);
Expand Down Expand Up @@ -77,6 +84,10 @@ export class BuyTokenService {
// priceBuy = 0;
// }
price = priceBuy;
const marketCap = (
(Number(tokenLaunchRecord.total_supply ?? 0) - newSupply) *
price
).toString();

console.log('price calculation', price);
await this.prismaService.token_launch.update({
Expand All @@ -89,6 +100,7 @@ export class BuyTokenService {
liquidity_raised: newLiquidityRaised.toString(),
total_token_holded: newTotalTokenHolded.toString(),
price: price?.toString(),
market_cap: marketCap,
},
// update: {
// current_supply: newSupply.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,19 @@ export class SellTokenService {
price = priceAfterSell;
console.log('price calculation', price);

const marketCap = (
(Number(tokenLaunchRecord.total_supply ?? 0) - newSupply) *
price
).toString();

await this.prismaService.token_launch.update({
where: { transaction_hash: tokenLaunchRecord.transaction_hash },
data: {
current_supply: newSupply.toString(),
liquidity_raised: newLiquidityRaised.toString(),
total_token_holded: newTotalTokenHolded.toString(),
price: price?.toString(),
market_cap: marketCap,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TokenLaunch } from './interfaces';
@Injectable()
export class TokenLaunchService {
private readonly logger = new Logger(TokenLaunchService.name);
constructor(private readonly prismaService: PrismaService) { }
constructor(private readonly prismaService: PrismaService) {}

async create(data: TokenLaunch) {
try {
Expand All @@ -21,23 +21,25 @@ export class TokenLaunchService {
return;
}

const bondingType = data.bondingType === '0'
? 'Linear'
: data.bondingType === '1'
? 'Exponential'
: null;

const bondingType =
data.bondingType === '0'
? 'Linear'
: data.bondingType === '1'
? 'Exponential'
: null;

// TODO: add this in the event
const initalPoolSupply = Number(data.totalSupply) / 5;
const initPoolSupply = Number(initalPoolSupply ?? 0);
const liquidityInQuoteToken = Number(0);
const tokensInPool = Number(initPoolSupply);
// Avoid division by zero
let priceBuy = tokensInPool > 0 ? tokensInPool / liquidityInQuoteToken : 0; // Price in memecoin per ETH
let priceBuy =
tokensInPool > 0 ? tokensInPool / liquidityInQuoteToken : 0; // Price in memecoin per ETH
if (priceBuy < 0) {
priceBuy = 0;
}

await this.prismaService.token_launch.create({
data: {
network: data.network,
Expand All @@ -51,25 +53,26 @@ export class TokenLaunchService {
total_supply: data.totalSupply,
current_supply: data.totalSupply,
is_liquidity_added: false,
liquidity_raised: "0",
liquidity_raised: '0',
threshold_liquidity: data.thresholdLiquidity,
owner_address: data.ownerAddress,
bonding_type: bondingType,
initial_pool_supply_dex: initPoolSupply?.toString(),
market_cap: '0',
},
});

try {
await this.prismaService.token_deploy.updateMany({
where: {
memecoin_address: data.memecoinAddress
memecoin_address: data.memecoinAddress,
},
data: {
is_launched: true
}
is_launched: true,
},
});
} catch (error) {
console.log("Errpr Update the Token model to launched", error)
console.log('Errpr Update the Token model to launched', error);
}
} catch (error) {
this.logger.error(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- AlterTable
ALTER TABLE "token_launch" ADD COLUMN "market_cap" TEXT;

-- CreateTable
CREATE TABLE "token_metadata" (
"transaction_hash" TEXT NOT NULL,
"network" TEXT,
"block_hash" TEXT,
"block_number" BIGINT,
"contract_address" TEXT,
"block_timestamp" TIMESTAMP(6),
"memecoin_address" TEXT,
"url" TEXT,
"nostr_id" TEXT,
"name" TEXT,
"symbol" TEXT,
"_cursor" BIGINT,
"time_stamp" TIMESTAMP(6),
"created_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "token_metadata_pkey" PRIMARY KEY ("transaction_hash")
);

0 comments on commit ba47999

Please sign in to comment.