Skip to content

Commit

Permalink
Add unruggable memecoins launch and transfers indexers (#250)
Browse files Browse the repository at this point in the history
Co-authored-by: 0xChqrles <[email protected]>
Co-authored-by: Uğur Eren <[email protected]>
  • Loading branch information
3 people authored Jun 6, 2024
1 parent 24c6e49 commit 366cec5
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 0 deletions.
14 changes: 14 additions & 0 deletions packages/indexers/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Notice that the base image is build from scratch (not an OS like Ubuntu),
# so the binary is in a location that depends on the build.
# For this reason we stick to a specific version and architecture.
#
# When updating the image you also need to update the entrypoint below.
#
# - docker image pull quay.io/apibara/sink-postgres:0.7.0-x86_64
# - docker image inspect quay.io/apibara/sink-postgres:0.7.0-x86_64 | jq '.[].Config.Entrypoint'
FROM quay.io/apibara/sink-postgres:0.7.0-x86_64

WORKDIR /app
COPY ./src/* /app

ENTRYPOINT ["/nix/store/rh1g8pb7wfnyr527jfmkkc5lm3sa1f0l-apibara-sink-postgres-0.7.0/bin/apibara-sink-postgres"]
45 changes: 45 additions & 0 deletions packages/indexers/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
version: '3.8'

services:
postgres:
image: postgres:latest
environment:
POSTGRES_DB: indexer
POSTGRES_USER: admin
POSTGRES_PASSWORD: password
ports:
- '5432:5432'
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
networks:
- backend

unruggableMemecoin-launch-indexer:
environment:
- AUTH_TOKEN=${AUTH_TOKEN}
image: quay.io/apibara/sink-postgres:latest
command: 'run ./indexer/unruggableMemecoin-launch.indexer.ts --connection-string postgresql://admin:password@postgres:5432/indexer -A ${AUTH_TOKEN}'
volumes:
- ./src:/indexer
depends_on:
- postgres
networks:
- backend
restart: on-failure

unruggableMemecoin-transfers-indexer:
environment:
- AUTH_TOKEN=${AUTH_TOKEN}
image: quay.io/apibara/sink-postgres:latest
command: 'run ./indexer/unruggableMemecoin-transfers.indexer.ts --connection-string postgresql://admin:password@postgres:5432/indexer -A ${AUTH_TOKEN}'
volumes:
- ./src:/indexer
depends_on:
- postgres
networks:
- backend
restart: on-failure

networks:
backend:
driver: bridge
19 changes: 19 additions & 0 deletions packages/indexers/envs.example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
apiVersion: v1
kind: Secret
metadata:
namespace: default
name: apibara-api-key
stringData:
production: dna_XXX # replace with your production key

---
apiVersion: v1
kind: Secret
metadata:
namespace: default
name: database-connection-string
stringData:
production: your_postgres_connection_string # replace with postgres connection string

# To apply :
# kubectl apply -f envs.yaml
27 changes: 27 additions & 0 deletions packages/indexers/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
create table unrugmeme_transfers(
network text,
block_hash text,
block_number bigint,
block_timestamp timestamp,
transaction_hash text,
transfer_id text unique primary key,
from_address text,
to_address text,
memecoin_address text,
amount text,
created_at timestamp default current_timestamp,
_cursor bigint
);

create table unrugmeme_launch(
network text,
block_hash text,
block_number bigint,
block_timestamp timestamp,
transaction_hash text,
memecoin_address text unique primary key,
quote_token text,
exchange_name text,
created_at timestamp default current_timestamp,
_cursor bigint
);
4 changes: 4 additions & 0 deletions packages/indexers/src/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { ec, hash, uint256, shortString } from 'https://esm.sh/[email protected]'
export { formatUnits } from 'https://esm.sh/[email protected]'

export type { Block, FieldElement, Filter } from 'https://esm.sh/@apibara/[email protected]/starknet'
52 changes: 52 additions & 0 deletions packages/indexers/src/unruggableMemecoin-launch.indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Block, hash, shortString } from './deps.ts'
import { FACTORY_ADDRESS, STARTING_BLOCK } from './unruggableMemecoin.ts'

const filter = {
header: {
weak: true,
},
events: [
{
fromAddress: FACTORY_ADDRESS,
keys: [hash.getSelectorFromName('MemecoinLaunched')],
includeReceipt: false,
},
],
}

export const config = {
streamUrl: 'https://mainnet.starknet.a5a.ch',
startingBlock: STARTING_BLOCK,
network: 'starknet',
finality: 'DATA_STATUS_ACCEPTED',
filter,
sinkType: 'postgres',
sinkOptions: {
connectionString: '',
tableName: 'unrugmeme_launch',
},
}

export default function DecodeUnruggableMemecoinLaunch({ header, events }: Block) {
const { blockNumber, blockHash, timestamp } = header!

return (events ?? []).map(({ event, transaction }) => {
const transactionHash = transaction.meta.hash

const [memecoin_address, quote_token, exchange_name] = event.data

const exchange_name_decoded = shortString.shortString(exchange_name)

return {
network: 'starknet-mainnet',
block_hash: blockHash,
block_number: +blockNumber,
block_timestamp: timestamp,
transaction_hash: transactionHash,
memecoin_address: memecoin_address,
quote_token: quote_token,
exchange_name: exchange_name_decoded,
created_at: new Date().toISOString(),
}
})
}
69 changes: 69 additions & 0 deletions packages/indexers/src/unruggableMemecoin-transfers.indexer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Block, hash, uint256 } from './deps.ts'
import { FACTORY_ADDRESS, STARTING_BLOCK } from './unruggableMemecoin.ts'

export const config = {
filter: {
header: { weak: true },
events: [
{
fromAddress: FACTORY_ADDRESS,
keys: [hash.getSelectorFromName('MemecoinLaunched')],
includeReceipt: false,
},
],
},
streamUrl: 'https://mainnet.starknet.a5a.ch',
startingBlock: STARTING_BLOCK,
network: 'starknet',
finality: 'DATA_STATUS_ACCEPTED',
sinkType: 'postgres',
sinkOptions: {
connectionString: '',
tableName: 'unrugmeme_transfers',
},
}

export function factory({ header, events }) {
const launchEvents = (events ?? []).map(({ event }) => {
const memecoin_address = event.data[0]
return {
fromAddress: memecoin_address,
keys: [hash.getSelectorFromName('Transfer')],
includeReceipt: false,
}
})

return {
filter: {
header: { weak: true },
events: launchEvents,
},
}
}

export default function DecodeUnruggableMemecoinLaunch({ header, events }: Block) {
const { blockNumber, blockHash, timestamp } = header!

return (events ?? []).map(({ event, transaction }) => {
const transactionHash = transaction.meta.hash
const transferId = `${transactionHash}_${event.index ?? 0}`
const fromAddress = event.keys[1]
const toAddress = event.keys[2]
const amount = uint256.uint256ToBN({ low: event.data[0], high: event.data[1] })
const memecoin_address = event.fromAddress

return {
network: 'starknet-mainnet',
block_hash: blockHash,
block_number: +blockNumber,
block_timestamp: timestamp,
transaction_hash: transactionHash,
transfer_id: transferId,
from_address: fromAddress,
to_address: toAddress,
memecoin_address: memecoin_address,
amount: amount.toString(10),
created_at: new Date().toISOString(),
}
})
}
2 changes: 2 additions & 0 deletions packages/indexers/src/unruggableMemecoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const FACTORY_ADDRESS = '0x01a46467a9246f45c8c340f1f155266a26a71c07bd55d36e8d1c7d0d438a2dbc'
export const STARTING_BLOCK = 615556

0 comments on commit 366cec5

Please sign in to comment.