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

Add Prisma as ORM [DRAFT] #1

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM node:18 as build
FROM node:18 AS build

WORKDIR /app

# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
ENV PATH=/app/node_modules/.bin:$PATH

# Generate build
COPY package.json /app
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ PGUSER=
PGDATABASE=
PGPASSWORD=
PGPORT=5432

DATABASE_URL="postgresql://johndoe:randompassword@localhost:5432/mydb?schema=public"
```

## Usage
Expand Down
20 changes: 11 additions & 9 deletions bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestNetWallet, Wallet, TokenMintRequest, BalanceResponse } from "mainne
import { bigIntToVmNumber, binToHex } from '@bitauth/libauth';
import { ethers } from "ethers";
import {
writeInfoToDb,
writeBurnInfoToDb,
getAllBridgeInfo,
getRecentBridgeInfo,
checkAmountBridgedDb,
Expand All @@ -12,8 +12,10 @@ import {
import abi from "./abi.json" assert { type: 'json' }
import express from "express";
import cors from "cors";
import 'dotenv/config'
import * as dotenv from "dotenv";
import { BurnInfo } from "./interfaces/interfaces.js";

dotenv.config();
const tokenId = process.env.TOKENID;
const network = process.env.NETWORK;
const derivationPathAddress = process.env.DERIVATIONPATH;
Expand Down Expand Up @@ -106,13 +108,13 @@ reapersContract.on("Transfer", (from, to, amount, event) => {
if(to != burnAddress && to !=burnAddress2) return
console.log(`${ from } burnt reaper #${nftNumber}`);
const timeBurned = new Date().toISOString();
const burnInfo = {
const burnInfo:BurnInfo = {
timeBurned,
txIdSmartBCH: event?.transactionHash,
nftNumber,
sbchOriginAddress: from
}
writeInfoToDb(burnInfo);
writeBurnInfoToDb(burnInfo);
});

async function tryBridging(
Expand All @@ -126,9 +128,9 @@ async function tryBridging(
try{
bridgingNft = true;
const infoAddress = await bridgeInfoEthAddress(sbchOriginAddress);
const listNftItems = infoAddress.filter(item => !item.timebridged)
const listNftNumbers = listNftItems.map(item => item.nftnumber)
if(!listNftNumbers.length) throw("empty list!")
const listNftItems = infoAddress?.filter(item => !item.timebridged)
const listNftNumbers = listNftItems?.map(item => item.nftnumber)
if(!listNftNumbers?.length) throw(`Empty list of NftNumbers that can be bridged for sbch address ${sbchOriginAddress}`)
const txid = await bridgeNFTs(listNftNumbers, destinationAddress, signatureProof);
bridgingNft = false;
return txid
Expand Down Expand Up @@ -158,7 +160,7 @@ async function bridgeNFTs(
})
mintRequests.push(mintNftOutput);
})
const { txId } = await wallet.tokenMint( tokenId as string, mintRequests );
const { txId } = await wallet.tokenMint( tokenId as string, mintRequests ) ;
console.log(txId)
nftsBridged += listNftNumbers.length;
// create db entries
Expand All @@ -168,7 +170,7 @@ async function bridgeNFTs(
const bridgeInfo = {
timeBridged,
signatureProof,
txIdBCH: txId,
txIdBCH: txId as string,
destinationAddress
}
addBridgeInfoToNFT(nftNumber, bridgeInfo);
Expand Down
74 changes: 0 additions & 74 deletions database.js

This file was deleted.

92 changes: 92 additions & 0 deletions database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { PrismaClient } from '@prisma/client'
import { BridgedInfoObj, BurnInfo } from './interfaces/interfaces.js';

const prisma = new PrismaClient()

export async function writeBurnInfoToDb(burnInfoObj:BurnInfo){
try {
await prisma.bridge.create({
data: {
timeburned: burnInfoObj.timeBurned,
txidsmartbch: burnInfoObj.txIdSmartBCH,
sbchoriginaddress: burnInfoObj.sbchOriginAddress,
nftnumber: burnInfoObj.nftNumber,
}
})
} catch (e) {
console.log(e);
}
}

export async function getAllBridgeInfo(){
try {
const result = await prisma.bridge.findMany({
orderBy: {
id: 'desc',
}
})
return result;
} catch (e) {
console.log(e);
}
}

export async function getRecentBridgeInfo(){
try {
const result = await prisma.bridge.findMany({
orderBy: {
id: 'desc',
},
take: 20
})
return result;
} catch (e) {
console.log(e);
}
}

export async function bridgeInfoEthAddress(ethAddress:string){
try {
const result = await prisma.bridge.findMany({
where: {
sbchoriginaddress: ethAddress
}
})
return result;
} catch (e) {
console.log(e);
}
}

export async function checkAmountBridgedDb() {
try {
const result = await prisma.bridge.findMany({
where: {
NOT: {
txidbch: null
}
}
})
return result.length;
} catch (e) {
console.log(e);
}
}

export async function addBridgeInfoToNFT(nftNumber:number, bridgedInfoObj:BridgedInfoObj) {
try {
await prisma.bridge.update({
where: {
nftnumber: nftNumber,
},
data: {
timebridged: bridgedInfoObj.timeBridged,
signatureproof: bridgedInfoObj.signatureProof,
txidbch: bridgedInfoObj.txIdBCH,
destinationaddress: bridgedInfoObj.timeBridged,
},
})
} catch (e) {
console.log(e);
}
}
11 changes: 0 additions & 11 deletions db.sql

This file was deleted.

13 changes: 13 additions & 0 deletions interfaces/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface BurnInfo {
timeBurned: string
txIdSmartBCH: string
sbchOriginAddress: string
nftNumber: number
}

export interface BridgedInfoObj {
timeBridged: string
txIdBCH: string
destinationAddress: string
signatureProof: string
}
5 changes: 5 additions & 0 deletions kube/reapers-bridge-deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ spec:
spec:
containers:
- env:
- name: DATABASE_URL
valueFrom:
secretKeyRef:
name: reapers-bridge-postgres
key: database_url
- name: PGUSER
valueFrom:
secretKeyRef:
Expand Down
Loading