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: multichain indexer ( part 1 ) #293

Merged
merged 12 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import { http, createPublicClient, PublicClient, createWalletClient } from 'viem'
import { sepolia } from 'viem/chains'
import { appenv } from '../config'
import { eosEvmTestnet } from 'app-env'

export const client: PublicClient = createPublicClient({
chain: eosEvmTestnet,
transport: http(),
})

export const walletClient = createWalletClient({
chain: eosEvmTestnet,
transport: http(),
key: appenv.evm.issuerKey,
account: appenv.evm.issuerAccount,
})

export const sepoliaClient: PublicClient = createPublicClient({
chain: {
...sepolia,
rpcUrls: {
default: {
http: [appenv.evm.sepoliaApi],
},
},
},
transport: http(),
})

export async function getCurrentBlockHeight() {
try {
const client = createPublicClient({
chain: eosEvmTestnet,
transport: http(),
})
const blockNumber = await client.getBlockNumber()
return blockNumber
} catch (error) {
Expand Down
8 changes: 6 additions & 2 deletions apps/indexer/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs/promises'
import { erc20Abi } from 'abitype/abis'
import { client } from './evm-client'
import { Abi, Address, parseUnits } from 'viem'
import {Abi, Address, parseUnits, http, createPublicClient} from 'viem';
import {eosEvmTestnet} from 'app-env';

export async function writeToFile(data: string, filePath: string) {
try {
Expand Down Expand Up @@ -38,6 +38,10 @@ export function runPromisesInSeries<T>(
}

export async function getTokenDetails({ address }: { address: Address }) {
const client = createPublicClient({
chain: eosEvmTestnet,
transport: http(),
})
const results = await client.multicall({
contracts: [
{
Expand Down
12 changes: 8 additions & 4 deletions apps/indexer/src/modules/auction/auction-indexer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
import { Log, stringify } from 'viem'
import { client } from '~/lib/evm-client'
import {Log, stringify, http, createPublicClient} from 'viem';
import { TestnetEasyAuction } from 'app-contracts'
import {
bigintToPostgresTimestamp,
getEvents,
getTokenDetails,
runPromisesInSeries,
} from '~/lib/utils'
import { NewAuctionEvent, NewSellOrderEvent, NewUserEvent } from '~/modules/auction/auction.type'

import { NewAuctionEvent, NewSellOrderEvent, NewUserEvent } from './auction.type'
import BN from 'bn.js'
import { eosEvmTestnet } from 'app-env'
import { upsertAuctionDetail, upsertOrder } from '~/lib/supabase-client'

export async function startAuctionIndexer() {
console.log('indexing starting')

// TODO: create client for
const client = createPublicClient({
chain: eosEvmTestnet,
transport: http(),
})

// await writeToFile(stringify(TestnetEasyAuction.getEvents(), null, 2), './events.json')
// Get historical event logs
const blockNumber = await client.getBlockNumber()
Expand Down
9 changes: 7 additions & 2 deletions apps/indexer/src/modules/auction/easyauction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getContract } from 'viem'
import { client } from '../../lib/evm-client'
import {getContract, http, createPublicClient} from 'viem';
import { TestnetEasyAuction } from 'app-contracts'
import {eosEvmTestnet} from 'app-env';

const client = createPublicClient({
chain: eosEvmTestnet,
transport: http(),
})

export const easyAuction = getContract({
...TestnetEasyAuction,
Expand Down
4 changes: 2 additions & 2 deletions apps/indexer/src/modules/presale/eos-contributions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { smartsaleEnv } from 'app-env'
import { stringify } from 'viem/utils'
import { createFirehoseSubscription } from '~/lib/dfuse-client'
import { issueTokens } from './presale-issuer'
import { issuePresaleTokens } from './presale-issuer'

// https://docs.dfuse.eosnation.io/platform/public-apis/search-query-language/
// https://docs.dfuse.eosnation.io/eosio/public-apis/reference/search/terms/
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function listenToEosContributions(env: 'test' | 'prod' = 'test') {
async function handleDeposit(data: { trxId: string; from: string; quantity: string }) {
console.log('handle deposit', data)

const response = await issueTokens(
const response = await issuePresaleTokens(
'0x7472312e4e1a373df751f84bd871a4c7a16128fa',
BigInt(data.quantity),
)
Expand Down
19 changes: 8 additions & 11 deletions apps/indexer/src/modules/presale/evm-contributions.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import { EVMTokenContractData, SepoliaUSDT, TestnetUSDT } from 'app-contracts'
import { EVMTokenContractData, appContracts } from 'app-contracts'
import { runPromisesInSeries } from '~/lib/utils'

import { Address, Log, PublicClient, createPublicClient, http, parseAbiItem, stringify } from 'viem'
import { TransferEvent } from '~/modules/auction/auction.type'
import { sepolia } from 'viem/chains'
import { smartsaleChains } from 'app-env'

const tokens: EVMTokenContractData[] = [SepoliaUSDT, TestnetUSDT]
const presaleWallet = '0xf7bb6BD787FFbA43539219560E3B8162Ba8EEF09'
const tokens: EVMTokenContractData[] = appContracts.dev.tokens.evm

export async function listenToEvmContributions() {
console.log('subscribing to evm usdt transfers ...')
tokens.map(listenToEvmTransfersFn)
}

async function listenToEvmTransfersFn(token: EVMTokenContractData) {
const chain = smartsaleChains.dev.get(token.chainId)
if (!chain) return
console.log(`listening usdt transfers for token ${token.symbol} on chain ${chain.name}`)
console.log(`listening usdt transfers for token ${token.symbol} on chain ${token.chain.name}`)
const client: PublicClient = createPublicClient({
chain,
chain: token.chain,
transport: http(),
})
try {
Expand All @@ -28,7 +25,7 @@ async function listenToEvmTransfersFn(token: EVMTokenContractData) {
'event Transfer(address indexed from, address indexed to, uint256 value)',
),
args: {
to: '0x2C9DAAb3F463d6c6D248aCbeaAEe98687936374a',
to: presaleWallet,
},
fromBlock: BigInt(token.indexFromBlock),
})
Expand All @@ -43,7 +40,7 @@ async function listenToEvmTransfersFn(token: EVMTokenContractData) {
'event Transfer(address indexed from, address indexed to, uint256 value)',
),
args: {
to: '0x2C9DAAb3F463d6c6D248aCbeaAEe98687936374a',
to: presaleWallet,
},
onLogs: (logs) => {
console.log('real time transfer', stringify(logs, null, 2))
Expand Down Expand Up @@ -102,7 +99,7 @@ async function handleTransfer(log: TransferEvent) {
// console.log('result', result)
// if (result.usdcred_trx || data.from === '0x0000000000000000000000000000000000000000') return

// const usdcred_trx = (await issueTokens(data.from, data.amount)) as Address
// const usdcred_trx = (await issuePresaleTokens(data.from, data.amount)) as Address

// if (!usdcred_trx) return

Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/src/modules/presale/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { listenToEosContributions } from './eos-contributions'
import { listenToEvmContributions } from './evm-contributions'

export function startPresaleService() {
listenToEosContributions()
// listenToEosContributions()
listenToEvmContributions()
}
2 changes: 1 addition & 1 deletion apps/indexer/src/modules/presale/presale-issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createWalletClient } from 'viem'
import { appenv } from '~/config'
import { Address, http } from 'viem'

export async function issueTokens(to: Address, amount: bigint) {
export async function issuePresaleTokens(to: Address, amount: bigint) {
console.log('issueTokens', {
args: [to, amount],
})
Expand Down
8 changes: 4 additions & 4 deletions apps/indexer/src/modules/swaps/evm-transfers.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { EVMTokenContractData, SepoliaUSDT, TestnetUSDT } from 'app-contracts'
import { EVMTokenContractData, SepoliaUSDT, TestnetUSDT, appContracts } from 'app-contracts'
import { runPromisesInSeries } from '~/lib/utils'

import { Address, Log, PublicClient, createPublicClient, http, parseAbiItem, stringify } from 'viem'
import { TransferEvent } from '~/modules/auction/auction.type'
import { sepolia } from 'viem/chains'
import { smartsaleChains } from 'app-env'
import { appChains } from 'app-env'

const tokens: EVMTokenContractData[] = [SepoliaUSDT, TestnetUSDT]
const tokens: EVMTokenContractData[] = appContracts.dev.tokens.evm

export async function listenToEvmTransfers() {
console.log('subscribing to evm usdt transfers ...')
tokens.map(listenToEvmTransfersFn)
}

async function listenToEvmTransfersFn(token: EVMTokenContractData) {
const chain = smartsaleChains.dev.get(token.chainId)
const chain = appChains.dev.get(token.chainId)
if (!chain) return
console.log(`listening usdt transfers for token ${token.symbol} on chain ${chain.name}`)
const client: PublicClient = createPublicClient({
Expand Down
2 changes: 1 addition & 1 deletion apps/indexer/src/routes/healthcheck.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express'
import { getCurrentBlockHeight } from '../lib/evm-client'
import { getCurrentBlockHeight } from '../lib/issuer-client'

export function startExpress() {
const app = express()
Expand Down
Binary file modified bun.lockb
Binary file not shown.
Binary file added hardhat/erc20-token/bun.lockb
Binary file not shown.
35 changes: 35 additions & 0 deletions hardhat/erc20-token/contracts/BLPLToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

import "@openzeppelin/contracts/access/Ownable.sol";

contract BLPLToken is ERC20, Ownable {
constructor(uint256 initialSupply) ERC20("Bitlauncher Prelaunch Token", "BLPL") {
_mint(msg.sender, initialSupply);
}

// use 6 decimals
function decimals() public view virtual override returns (uint8) {
return 6;
}

/**
* @dev Issues a specific amount of tokens.
* @param recipient The address to which the issued tokens will be sent.
* @param amount The amount of tokens to be issued.
*/
function issue(address recipient, uint256 amount) public onlyOwner {
_mint(recipient, amount);
}

/**
* @dev Burns a specific amount of tokens.
* @param amount The amount of tokens to be burned.
*/
function burn(uint256 amount) public {
_burn(msg.sender, amount);
}
}
6 changes: 5 additions & 1 deletion hardhat/erc20-token/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"deployUSDCredMainnet": " hardhat run scripts/deployUSDCred.js --network eosevm_mainnet",
"deployMBOTSPLTestnet": " hardhat run scripts/deployMBOTSPLWithFaucet.js --network eosevm_testnet",
"deployUSDTSepolia": " hardhat run scripts/deployUSDTWithFaucet.js --network sepolia",
"deployUSDTTestnet": " hardhat run scripts/deployUSDTWithFaucet.js --network eosevm_testnet"
"deployUSDTTestnet": " hardhat run scripts/deployUSDTWithFaucet.js --network eosevm_testnet",
"deployBLPL": "hardhat run scripts/deployBLPL.js --network eosevm_testnet"
},
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
Expand All @@ -15,5 +16,8 @@
"ethereum-waffle": "^3.4.0",
"ethers": "^5.7.2",
"hardhat": "^2.6.4"
},
"dependencies": {
"@openzeppelin/contracts": "^3.3.0"
}
}
16 changes: 16 additions & 0 deletions hardhat/erc20-token/scripts/deployBLPL.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

const { ethers } = require("hardhat");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using ES6 import syntax.

Since the project uses TypeScript with NodeNext module resolution, consider using ES6 import syntax instead of CommonJS require.

- const { ethers } = require("hardhat");
+ import { ethers } from "hardhat";
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { ethers } = require("hardhat");
import { ethers } from "hardhat";


async function main() {
const BLPLToken = await ethers.getContractFactory("BLPLToken");
const token = await BLPLToken.deploy(ethers.utils.parseUnits("1000000", 18));

await token.deployed();

console.log("BLPLToken deployed to:", token.address);
}

main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
8 changes: 8 additions & 0 deletions hardhat/erc20-token/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
// ... existing options ...
"module": "NodeNext",
"moduleResolution": "NodeNext"
},
// ... existing options ...
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { EVMContractData } from "../types";
import { sepolia } from "viem/chains";
import { EVMContractData } from "../../types";

export const TestnetAllowList: EVMContractData = {
address: "0x3D18904711fe451356eBA461B7747EA3Abff6c93",
indexFromBlock: 12241046,
chainId: 15557,
chainType: "evm",
chainName: "Sepolia",
chain: sepolia,
abi: [
{
inputs: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { EVMContractData } from "../types";
import { sepolia } from "viem/chains";
import { EVMContractData } from "../../types";

export const TestnetDepositOrder: EVMContractData = {
address: "0x4faA684A1E0Cdd7cb271b5424a12A2D039624D78",
indexFromBlock: 12240840,
chainId: 15557,
chainType: "evm",
chainName: "EOS EVM Tesnet",
chain: sepolia,
abi: [
{
inputs: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { EVMContractData } from "../types";
import { sepolia } from "viem/chains";
import { EVMContractData } from "../../types";

export const TestnetEasyAuction: EVMContractData = {
address: "0x8d37219725eB0088360f744A5d894035D0f88F82",
indexFromBlock: 12239067,
chainId: 15557,
chainType: "evm",
chainName: "EOS EVM Tesnet",
chain: sepolia,
abi: [
{
inputs: [],
Expand Down
Loading
Loading