Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
61 changes: 61 additions & 0 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Build/Deploy

on:
push:
branches:
- main
- test
- '*'
pull_request:
branches:
- main
- test

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write

if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/test')
environment: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref_name }}

env:
DESTINATION_BRANCH: ${{ github.event_name == 'pull_request' && github.base_ref || github.ref_name }}

steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_wrapper: false

- name: Terraform Init
run: terraform init -backend-config="environments/${{ env.DESTINATION_BRANCH }}.backend.tfvars"
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
working-directory: ./terraform

- name: Set Version
if: github.ref == 'refs/heads/main'
run: echo "PROJECT_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV

- name: Terraform Deploy
run: |
terraform apply -auto-approve \
-var-file="environments/${{ env.DESTINATION_BRANCH }}.tfvars" \
-var="account_id=${{ vars.AWS_ACCOUNT_ID }}" \
-var="region=${{ vars.AWS_REGION }}" \
-var="project_version=${{ env.PROJECT_VERSION }}" \
-var="tesnet_provider_url=${{ secrets.TESTNET_PROVIDER_URL }}" \
-var="mainnet_provider_key=${{ secrets.MAINNET_PROVIDER_KEY }}"
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
PROJECT_VERSION: ${{ env.PROJECT_VERSION }}
working-directory: ./terraform
2 changes: 1 addition & 1 deletion database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { env } from "../env";

import { integer, text, boolean, pgSchema } from "drizzle-orm/pg-core";

export const schema = pgSchema("test" as string); //TODO: change to env.DB_SCHEMA
export const schema = pgSchema(env.DB_SCHEMA || "test");

export const fractalityTokenMigrations = schema.table(
"fractality_token_migrations",
Expand Down
1 change: 1 addition & 0 deletions env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const envSchema = z.object({
DB_PORT: z.coerce.number().optional(),
DB_USER: z.string().optional(),
DB_PASSWORD: z.string().optional(),
DB_SCHEMA: z.string().optional(),
BLOCKCHAIN_ENVIRONMENT: z.string(),
BLOCK_START_NUMBER: z.string(),
SAFETY_CUSHION_NUMBER_OF_BLOCKS: z.coerce.number(),
Expand Down
15 changes: 15 additions & 0 deletions handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type Context, type Handler } from 'aws-lambda'

import { main } from './migrationService'

export const handler: Handler<void, void> = async (
event: void,
context: Context
): Promise<void> => {
try {
await main(false)
} catch (error) {
console.error('Error With Migration Service:', error)
throw error
}
}
7 changes: 3 additions & 4 deletions interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export interface HLMigration {
}

export enum MigrationStatus {
FOUND_ON_ARBITRUM = "FOUND_ON_ARBITRUM",
PREPPED_FOR_HL = "PREPPED_FOR_HL",
SENT_TO_HL = "SENT_TO_HL",
ERRORED_IN_SENDING_TO_HL = "ERRORED_IN_SENDING_TO_HL",
FOUND_ON_ARBITRUM = "FOUND_ON_ARBITRUM", //Just found on arbitrum, not filtered.
SENT_TO_HL = "SENT_TO_HL", //Sent to HL
ERRORED_IN_SENDING_TO_HL = "ERRORED_IN_SENDING_TO_HL", //Errored in sending to HL
}
101 changes: 60 additions & 41 deletions index.ts → migrationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { initializeDatabaseConnection } from "./database";
import cron from "node-cron";
import { PreviousBlockManager } from "./libs/PreviousBlockManager";
import { HyperliquidManager } from "./libs/HyperliquidManager";
async function main() {

export async function main(runWithCron: boolean) {
await privateKeyManager.init();
await initializeDatabaseConnection();

Expand Down Expand Up @@ -48,51 +49,71 @@ async function main() {
() => blockchainConnectionProvider.getCurrentBlockNumber()
);

console.log("starting cron job for migrations, running every 5 minutes");
cron.schedule("* * * * *", async () => {
const fromBlock = await blockManager.getFromBlockForScan();
const toBlock = await blockManager.setFromBlockForScanToCurrentBlock();
console.log(
`looking for migrations from block ${fromBlock} to block ${toBlock}`
if (runWithCron) {
console.log("starting cron job for migrations, running every 5 minutes");
cron.schedule("* * * * *", async () => {
await coreMigrationService(
blockManager,
blockchainConnectionProvider,
hlManager
);
});
} else {
await coreMigrationService(
blockManager,
blockchainConnectionProvider,
hlManager
);
}
}

const y2kMigrations = await blockchainConnectionProvider.scanMigrations(
env.Y2K_TOKEN_MIGRATION_ADDRESS as Address,
fromBlock,
toBlock
);
const frctRMigrations = await blockchainConnectionProvider.scanMigrations(
env.FRCT_R_MIGRATION_ADDRESS as Address,
fromBlock,
toBlock
);
await addMigrationsToDatabase([...y2kMigrations, ...frctRMigrations]);
export async function coreMigrationService(
blockManager: PreviousBlockManager,
blockchainConnectionProvider: BlockchainConnectionProvider,
hlManager: HyperliquidManager
) {
const fromBlock = await blockManager.getFromBlockForScan();
const toBlock = await blockManager.setFromBlockForScanToCurrentBlock();
console.log(
`looking for migrations from block ${fromBlock} to block ${toBlock}`
);

const y2kMigrations = await blockchainConnectionProvider.scanMigrations(
env.Y2K_TOKEN_MIGRATION_ADDRESS as Address,
fromBlock,
toBlock
);
const frctRMigrations = await blockchainConnectionProvider.scanMigrations(
env.FRCT_R_MIGRATION_ADDRESS as Address,
fromBlock,
toBlock
);
await addMigrationsToDatabase([...y2kMigrations, ...frctRMigrations]);

//get migrations that still have not been sent to hyperliquid
const unmigratedMigrations: TokenMigration[] =
await getUnmigratedFractalityTokenMigrations();
//get migrations that still have not been sent to hyperliquid
const unmigratedMigrations: TokenMigration[] =
await getUnmigratedFractalityTokenMigrations();

//calcualate the amount of tokens to send to hyperliquid
const hlMigrations = await prepForHLMigration(
hlManager,
unmigratedMigrations
);
console.log("hlMigrations", hlMigrations);
//calcualate the amount of tokens to send to hyperliquid
const hlMigrations = await prepForHLMigration(
hlManager,
unmigratedMigrations
);
console.log("hlMigrations", hlMigrations);

const { successes, failures } = await hlManager.sendHLMigrations(
hlMigrations
);
console.log("successes", successes);
console.log("failures", failures);
const { successes, failures } = await hlManager.sendHLMigrations(
hlMigrations
);
console.log("successes", successes);
console.log("failures", failures);

try {
await finalizeHlMigrations(successes);
} catch (error) {
console.error("FATAL ERROR: Error finalizing HL migrations", error);
}
try {
await finalizeHlMigrations(successes);
} catch (error) {
console.error("FATAL ERROR: Error finalizing HL migrations", error);
}

console.log("done");
});
console.log("done");
}

async function prepForHLMigration(
Expand Down Expand Up @@ -145,5 +166,3 @@ async function addMigrationsToDatabase(migrations: MigrationRegisteredEvent[]) {
console.error("Error adding migration to database", e);
}
}

main();
84 changes: 82 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading