Skip to content

Commit

Permalink
Addressed launchpad issue (#497)
Browse files Browse the repository at this point in the history
* Addressed launchpad issue

* continuing the function
  • Loading branch information
TheRanomial authored Feb 17, 2025
1 parent 848436f commit fff68c1
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 58 deletions.
136 changes: 78 additions & 58 deletions apps/nestjs-indexer/src/services/buy-token/buy-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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)) {
Expand All @@ -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: {
Expand All @@ -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(),
Expand All @@ -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;
}
}
}
Loading

0 comments on commit fff68c1

Please sign in to comment.