Skip to content

Commit

Permalink
Merge pull request #92 from BirthdayResearch/harsh/finalize-bot
Browse files Browse the repository at this point in the history
feat(bot): save SC logs
  • Loading branch information
pierregee authored Aug 12, 2024
2 parents b38ef18 + 2455617 commit 7da0c4a
Show file tree
Hide file tree
Showing 22 changed files with 735 additions and 242 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Liquid Staking Derivative for DeFi Metachain


## MainNet Addresses

### TimeLock
Expand All @@ -19,9 +18,6 @@ BridgeProxy Contract address: [0x7625924EFb4835E9459a4Ea6bA17ea99FBb7883B](https

BridgeQueue Contract address: [0xDB9d6fbF5b971Ae3C3a333cc31DB7105e1534998](https://meta.defiscan.live/address/0xDB9d6fbF5b971Ae3C3a333cc31DB7105e1534998)




## TestNet Addresses

### TimeLock
Expand Down
4 changes: 2 additions & 2 deletions apps/server/.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost:5432/

# Bot env values
BOT_RUNNING_NETWORKS=""
# Private key extracted from Light wallet
DEFICHAIN_TESTNET_PRIVATE_KEY=""
# Private key extracted from Light wallet for Reward
DEFICHAIN_MAINNET_PRIVATE_KEY=""
DEFICHAIN_TESTNET_PRIVATE_KEY=""
4 changes: 2 additions & 2 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"@defichain/jellyfish-wallet-classic": "^4.0.7",
"@nestjs/schedule": "^4.0.2",
"@prisma/client": "^5.6.0",
"@prisma/client": "^5.16.2",
"@stickyjs/testcontainers": "^1.3.10",
"@waveshq/standard-api-fastify": "^4.0.1",
"@waveshq/walletkit-core": "^1.3.11",
Expand All @@ -48,7 +48,7 @@
"@typescript-eslint/parser": "^6.0.0",
"@waveshq/standard-prettier": "^4.0.1",
"jest": "29.3.1",
"prisma": "^5.6.0",
"prisma": "^5.16.2",
"supertest": "^6.1.3",
"ts-jest": "29.0.3",
"ts-node": "^10.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
-- CreateTable
CREATE TABLE "ScDepositEvents" (
"id" SERIAL NOT NULL,
"txnHash" TEXT NOT NULL,
"network" "Network" NOT NULL,
"owner" TEXT NOT NULL,
"receiver" TEXT NOT NULL,
"assets" TEXT NOT NULL,
"shares" TEXT NOT NULL,
"fees" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "ScDepositEvents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ScWithdrawalRequestEvents" (
"id" SERIAL NOT NULL,
"txnHash" TEXT NOT NULL,
"network" "Network" NOT NULL,
"requestId" TEXT NOT NULL,
"owner" TEXT NOT NULL,
"receiver" TEXT NOT NULL,
"assets" TEXT NOT NULL,
"shares" TEXT NOT NULL,
"fees" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "ScWithdrawalRequestEvents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ScWithdrawalFinalizeEvents" (
"id" SERIAL NOT NULL,
"txnHash" TEXT NOT NULL,
"network" "Network" NOT NULL,
"from" TEXT NOT NULL,
"to" TEXT NOT NULL,
"assets" TEXT NOT NULL,
"shares" TEXT NOT NULL,
"timestamp" TIMESTAMP(3) NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "ScWithdrawalFinalizeEvents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ScWithdrawalClaimEvents" (
"id" SERIAL NOT NULL,
"txnHash" TEXT NOT NULL,
"network" "Network" NOT NULL,
"requestId" TEXT NOT NULL,
"owner" TEXT NOT NULL,
"receiver" TEXT NOT NULL,
"assets" TEXT NOT NULL,
"shares" TEXT NOT NULL,
"fees" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "ScWithdrawalClaimEvents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ScRewardEvents" (
"id" SERIAL NOT NULL,
"txnHash" TEXT NOT NULL,
"network" "Network" NOT NULL,
"owner" TEXT NOT NULL,
"assets" TEXT NOT NULL,
"fees" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "ScRewardEvents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ScUpdateEvents" (
"id" SERIAL NOT NULL,
"txnHash" TEXT NOT NULL,
"network" "Network" NOT NULL,
"name" TEXT NOT NULL,
"oldValue" TEXT NOT NULL,
"newValue" TEXT NOT NULL,
"owner" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3),

CONSTRAINT "ScUpdateEvents_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "ScEventLastSyncedBlock" (
"id" SERIAL NOT NULL,
"network" "Network" NOT NULL,
"blockNumber" TEXT NOT NULL,

CONSTRAINT "ScEventLastSyncedBlock_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "ScDepositEvents_txnHash_network_key" ON "ScDepositEvents"("txnHash", "network");

-- CreateIndex
CREATE UNIQUE INDEX "ScWithdrawalRequestEvents_txnHash_network_key" ON "ScWithdrawalRequestEvents"("txnHash", "network");

-- CreateIndex
CREATE UNIQUE INDEX "ScWithdrawalFinalizeEvents_txnHash_network_key" ON "ScWithdrawalFinalizeEvents"("txnHash", "network");

-- CreateIndex
CREATE UNIQUE INDEX "ScWithdrawalClaimEvents_txnHash_network_key" ON "ScWithdrawalClaimEvents"("txnHash", "network");

-- CreateIndex
CREATE UNIQUE INDEX "ScRewardEvents_txnHash_network_key" ON "ScRewardEvents"("txnHash", "network");

-- CreateIndex
CREATE UNIQUE INDEX "ScUpdateEvents_txnHash_network_key" ON "ScUpdateEvents"("txnHash", "network");

-- CreateIndex
CREATE UNIQUE INDEX "ScEventLastSyncedBlock_network_key" ON "ScEventLastSyncedBlock"("network");
95 changes: 95 additions & 0 deletions apps/server/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,98 @@ model Rewards {
@@unique([txnHash, network])
}

model ScDepositEvents {
id Int @id @default(autoincrement())
txnHash String
network Network
owner String
receiver String
assets String
shares String
fees String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([txnHash, network])
}

model ScWithdrawalRequestEvents {
id Int @id @default(autoincrement())
txnHash String
network Network
requestId String
owner String
receiver String
assets String
shares String
fees String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([txnHash, network])
}

model ScWithdrawalFinalizeEvents {
id Int @id @default(autoincrement())
txnHash String
network Network
from String
to String
assets String
shares String
timestamp DateTime
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([txnHash, network])
}

model ScWithdrawalClaimEvents {
id Int @id @default(autoincrement())
txnHash String
network Network
requestId String
owner String
receiver String
assets String
shares String
fees String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([txnHash, network])
}

model ScRewardEvents {
id Int @id @default(autoincrement())
txnHash String
network Network
owner String
assets String
fees String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([txnHash, network])
}

model ScUpdateEvents {
id Int @id @default(autoincrement())
txnHash String
network Network
name String
oldValue String
newValue String
owner String
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
@@unique([txnHash, network])
}

model ScEventLastSyncedBlock {
id Int @id @default(autoincrement())
network Network @unique()
blockNumber String
}
8 changes: 6 additions & 2 deletions apps/server/src/AppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export function appConfig() {
botNetworks: process.env.BOT_RUNNING_NETWORKS,
defichain: {
[EnvironmentNetwork.MainNet]: {
key: process.env.DEFICHAIN_MAINNET_PRIVATE_KEY,
rewardDistributerKey: process.env.DEFICHAIN_MAINNET_PRIVATE_KEY,
marbleFiContractDeploymentHash:
"0xfb6913340f29733a14917879dc1e2ed9572a28f6b69f8c80d7ec2b2472cc5342",
marbleFiContractAddress: "0x7625924EFb4835E9459a4Ea6bA17ea99FBb7883B",
ethRPCUrl: "https://eth.mainnet.ocean.jellyfishsdk.com",
},
[EnvironmentNetwork.TestNet]: {
key: process.env.DEFICHAIN_TESTNET_PRIVATE_KEY,
rewardDistributerKey: process.env.DEFICHAIN_TESTNET_PRIVATE_KEY,
marbleFiContractAddress: "0x0B52a71A03a47246BD9d9C556B6Df9C42e73462A",
marbleFiContractDeploymentHash:
"0x85d8112666b6a18041a6beb3d71832807403ed7915f35c382317ee5024dfe575",
ethRPCUrl: "https://eth.testnet.ocean.jellyfishsdk.com",
},
whaleURL: process.env.DEFICHAIN_WHALE_URL,
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/bot/BotController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class BotController {
@Cron(CronExpression.EVERY_DAY_AT_11AM)
async initBot(): Promise<void> {
for (let i = 0; i < this.networks.length; i++) {
await this.botService.processTransfer(this.networks[i]);
await this.botService.processRewards(this.networks[i]);
await this.botService.processSmartContractLogs(this.networks[i]);
}
}

Expand Down
56 changes: 51 additions & 5 deletions apps/server/src/bot/BotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { WalletProvider } from "./providers/WalletProvider";
import { WhaleApiClientProvider } from "./providers/WhaleApiClientProvider";
import BigNumber from "bignumber.js";
import { PrismaService } from "../PrismaService";
import { Network } from "@prisma/client";
import { parseEther } from "ethers";
import { SmartContractLogsProvider } from "./providers/SmartContractLogsProvider";

@Controller("bot")
export class BotService {
Expand All @@ -16,15 +16,18 @@ export class BotService {
private prismaService: PrismaService,
) {}

async processTransfer(network: EnvironmentNetwork) {
async processRewards(network: EnvironmentNetwork) {
console.log(`Initializing reward process: ${new Date().toISOString()}`);

const privateKey = this.configService.getOrThrow<EnvironmentNetwork>(
`defichain.${network}.rewardDistributerKey`,
);
const walletProvider = new WalletProvider(
privateKey,
network,
this.whaleApiClient,
this.configService,
);

await walletProvider.initWallet();
// Retrieve UTXO balance and keep 1 UTXO for txn cost
const utxoBalance = await walletProvider.getUTXOBalance();
const conversionAmount = new BigNumber(utxoBalance).minus(1);
Expand Down Expand Up @@ -61,7 +64,7 @@ export class BotService {
if (rewardsTxn) {
await this.prismaService.rewards.create({
data: {
network: network as Network,
network: network,
txnHash: rewardsTxn?.hash,
amount: rewardsTxn?.value.toString(),
fromAddress: rewardsTxn.from,
Expand All @@ -77,4 +80,47 @@ export class BotService {

console.log(`Reward process completed: ${new Date().toISOString()}`);
}

// TODO do not remove this set of code
// async processWithdrawals(network: EnvironmentNetwork) {
// console.log(`Initializing reward process: ${new Date().toISOString()}`);
// const privateKey = this.configService.getOrThrow<EnvironmentNetwork>(
// `defichain.${network}.withdrawFinalizerKey`,
// );
// const walletProvider = new WalletProvider(
// privateKey,
// network,
// this.whaleApiClient,
// this.configService,
// );
// await walletProvider.initWallet();
// walletProvider.finalizeWithdrawalRequest("1", parseEther("1"));
// }

async processSmartContractLogs(network: EnvironmentNetwork) {
const scLogsProvider = new SmartContractLogsProvider(
network,
this.configService,
this.prismaService,
);
let fromBlockNumber = await scLogsProvider.getLastSyncBlockNumber();
const latestBlock = await scLogsProvider.getLatestBlock();
do {
const fromBlock = new BigNumber(fromBlockNumber).minus(1);
const toBlock = Math.min(
new BigNumber(fromBlock).plus(2000).toNumber(),
latestBlock,
);
const logs = await scLogsProvider.getLogs(
new BigNumber(fromBlock).toNumber(),
new BigNumber(toBlock).toNumber(),
);
const saveLogsPromise = (logs ?? []).map((each) =>
scLogsProvider.saveLogs(each),
);
await Promise.all(saveLogsPromise);
await scLogsProvider.updateLastSyncBlockNumber(toBlock.toString());
fromBlockNumber = toBlock;
} while (new BigNumber(fromBlockNumber).lt(latestBlock));
}
}
Loading

0 comments on commit 7da0c4a

Please sign in to comment.