• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hicommonwealth / commonwealth / 12284359451

11 Dec 2024 08:28PM UTC coverage: 47.172% (+0.01%) from 47.159%
12284359451

Pull #10016

github

web-flow
Merge d127b8066 into 373f274d5
Pull Request #10016: Add full end to end integration tests for web3

1190 of 2833 branches covered (42.0%)

Branch coverage included in aggregate %.

2 of 7 new or added lines in 3 files covered. (28.57%)

3 existing lines in 1 file now uncovered.

2454 of 4892 relevant lines covered (50.16%)

31.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.63
/libs/model/src/policies/handleLaunchpadTrade.ts
1
import { logger } from '@hicommonwealth/core';
2
import { commonProtocol as cp } from '@hicommonwealth/evm-protocols';
3
import { chainEvents, events } from '@hicommonwealth/schemas';
4
import { BigNumber } from 'ethers';
5
import Web3 from 'web3';
6
import { z } from 'zod';
7
import { models } from '../database';
8
import { commonProtocol } from '../services';
9

10
const log = logger(import.meta);
26✔
11

12
export async function handleLaunchpadTrade(
13
  event: z.infer<typeof events.ChainEventCreated>,
14
) {
15
  const {
16
    0: traderAddress,
17
    1: tokenAddress,
18
    2: isBuy,
19
    3: communityTokenAmount,
20
    4: ethAmount,
21
    // 5: protocolEthAmount,
22
    6: floatingSupply,
23
  } = event.parsedArgs as z.infer<typeof chainEvents.LaunchpadTrade>;
×
24

25
  const token = await models.Token.findOne({
×
26
    where: {
27
      token_address: tokenAddress.toLowerCase(),
28
    },
29
  });
30

31
  if (!token) {
×
32
    throw new Error('Token not found');
×
33
  }
34

35
  const chainNode = await models.ChainNode.scope('withPrivateData').findOne({
×
36
    where: {
37
      id: event.eventSource.chainNodeId,
38
    },
39
  });
40

41
  if (!chainNode) {
×
42
    // TODO: throw custom error with no retries -> straight to deadletter
UNCOV
43
    throw new Error('Unsupported chain');
×
44
  }
45

NEW
46
  const web3 = new Web3(chainNode.private_url! || chainNode.url!);
×
NEW
UNCOV
47
  const block = await web3.eth.getBlock(event.rawLog.blockHash);
×
48

NEW
UNCOV
49
  await models.LaunchpadTrade.findOrCreate({
×
50
    where: {
51
      eth_chain_id: chainNode.eth_chain_id!,
52
      transaction_hash: event.rawLog.transactionHash,
53
    },
54
    defaults: {
55
      eth_chain_id: chainNode.eth_chain_id!,
56
      transaction_hash: event.rawLog.transactionHash,
57
      token_address: tokenAddress.toLowerCase(),
58
      trader_address: traderAddress,
59
      is_buy: isBuy,
60
      community_token_amount: BigNumber.from(communityTokenAmount).toBigInt(),
61
      price:
62
        Number(
63
          (BigNumber.from(ethAmount).toBigInt() * BigInt(1e18)) /
64
            BigNumber.from(communityTokenAmount).toBigInt(),
65
        ) / 1e18,
66
      floating_supply: BigNumber.from(floatingSupply).toBigInt(),
67
      timestamp: Number(block.timestamp),
68
    },
69
  });
70

71
  const contracts =
72
    cp.factoryContracts[chainNode!.eth_chain_id as cp.ValidChains];
×
73
  let lpBondingCurveAddress: string;
NEW
74
  if (contracts && 'lpBondingCurve' in contracts) {
×
75
    lpBondingCurveAddress = contracts.lpBondingCurve;
×
76
  } else {
77
    log.error('No lpBondingCurve address found for chain', undefined, {
×
78
      eth_chain_id: chainNode.eth_chain_id,
79
    });
80
    return;
×
81
  }
82

83
  if (
×
84
    !token.liquidity_transferred &&
×
85
    BigNumber.from(floatingSupply).toBigInt() ===
86
      BigInt(token.launchpad_liquidity)
87
  ) {
88
    const onChainTokenData = await commonProtocol.launchpadHelpers.getToken({
×
89
      rpc: chainNode.private_url!,
90
      tokenAddress,
91
      lpBondingCurveAddress,
92
    });
93

94
    if (!onChainTokenData.funded) {
×
95
      await commonProtocol.launchpadHelpers.transferLiquidityToUniswap({
×
96
        rpc: chainNode.private_url!,
97
        tokenAddress,
98
        lpBondingCurveAddress,
99
      });
100
    }
101

102
    token.liquidity_transferred = true;
×
103
    await token.save();
×
104
  }
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc