Skip to content

Commit

Permalink
Merge pull request #2 from sg-milad/fix/dockedile
Browse files Browse the repository at this point in the history
fix docker file & upgrade wallet version
  • Loading branch information
sg-milad authored Nov 2, 2024
2 parents a86c9db + 51883ae commit 58e687d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ WALLET_MNEMONIC="test test test test test test test test test test test junk"
MAIN_WALLET="UQBwOYkda287IyhqEucTl9_ne4JZecMRnwS9Vle7I9Aobhf4"
WALLET_SEED_PASSPHRASE="password"

DATABASE_URL="mongodb://root:pass@mongo:27017/prisma-mongo?authSource=admin&retryWrites=true&w=majority&directConnection=true"
DATABASE_URL="mongodb://root:pass@mongodb:27017/prisma-mongo?authSource=admin&retryWrites=true&w=majority&directConnection=true"
# redis
REDIS_HOST=redis
REDIS_PORT=6379
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ FROM base AS dependencies

WORKDIR /app
COPY package.json pnpm-lock.yaml ./
COPY prisma/ /app/prisma/
RUN pnpm install
FROM base AS build

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"prisma": {
"schema": "./prisma/schema.prisma"
},
"jest": {
"moduleFileExtensions": [
"js",
Expand Down
30 changes: 26 additions & 4 deletions src/ton/ton.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InjectQueue } from "@nestjs/bullmq";
import { Injectable, Logger, OnModuleInit } from "@nestjs/common";
import { Cron, CronExpression } from "@nestjs/schedule";
import { getHttpEndpoint } from "@orbs-network/ton-access";
import { Address, fromNano, internal, SendMode, toNano, TonClient, WalletContractV3R1 } from "@ton/ton";
import { Address, fromNano, internal, SendMode, toNano, TonClient } from "@ton/ton";
import { Queue } from "bullmq";
import { PaymentService } from "src/payment/payment.service";
import { WalletService } from "src/wallet/wallet.service";
Expand All @@ -20,7 +20,7 @@ export class TonService implements OnModuleInit {
private PaymentService: PaymentService,
private configService: ConfigService,
@InjectQueue(PaymentQUEUE) private paymentQueue: Queue,
) {}
) { }

async onModuleInit() {
try {
Expand All @@ -30,37 +30,52 @@ export class TonService implements OnModuleInit {
this.client = new TonClient({ endpoint });
} catch (error) {
this.logger.error("Failed to initialize TonClient", error);
process.exit(1);
}
}

@Cron(CronExpression.EVERY_MINUTE)
async transactionProcess(): Promise<void> {
const getInitPayment = await this.PaymentService.findInitPayment();

this.logger.log("find init Payment...", getInitPayment);

if (getInitPayment.length === 0) return;

for (const payment of getInitPayment) {

const wallet = await this.walletService.createWallet(payment.walletAccount, 0, payment.walletIndex);

const publicKey = this.walletService.keyPairToPublicKey(wallet);

const transactions = await this.checkTransactions(publicKey);

for (const transaction of transactions) {
this.logger.log("Transactions", { publicKey, transaction });
this.logger.log("Transactions", { paymentId: payment.id, publicKey, transaction });
await this.paymentQueue.add(PaymentJob, { transaction, payment }, { removeOnComplete: true });
}
}
}
@Cron(CronExpression.EVERY_MINUTE)
async checkPendingPayments() {
const getPendingPayment = await this.PaymentService.findPendingPayment();

this.logger.log("Check pending payments", getPendingPayment);

for (const payment of getPendingPayment) {

if (payment.walletDes) {
const balance = await this.getBalance(payment.walletDes);
if (balance > toNano("0.1")) {

if (balance > toNano("0.01")) {
this.logger.log("Transfer remaining balance", { balance: toNano(balance), payment });

const wallet = await this.walletService.createWallet(payment.walletAccount, 0, payment.walletIndex);

return await this.transfer(wallet);
}
await this.PaymentService.updatePayment({ id: payment.id }, { paymentStatus: "ACCEPT" });

this.logger.log("Payment accepted", payment);
}
}
Expand All @@ -70,11 +85,14 @@ export class TonService implements OnModuleInit {
const transactions = await this.client.getTransactions(Address.parse(publicKey), {
limit: 5,
});

this.logger.log("Check transactions", { publicKey, transactions });

const results: Array<TransactionType> = [];

for (const tx of transactions) {
if (tx.inMessage && tx.inMessage.info.type === "internal") {

const value = fromNano(tx.inMessage.info.value.coins);
const source = tx.inMessage.info.src.toString();

Expand All @@ -94,9 +112,13 @@ export class TonService implements OnModuleInit {
}
async transfer(keyPair: KeyPair) {
try {

const wallet = this.walletService.createContractWallet(keyPair);

const contract = this.client.open(wallet);

const seqno = await contract.getSeqno();

return await contract.sendTransfer({
seqno,
secretKey: keyPair.secretKey,
Expand Down
8 changes: 4 additions & 4 deletions src/wallet/wallet.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Injectable, OnModuleInit } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { KeyPair, keyPairFromSeed } from "@ton/crypto";
import { WalletContractV3R1 } from "@ton/ton";
import { WalletContractV3R1, WalletContractV5R1 } from "@ton/ton";
import { initWasm, WalletCore } from "@trustwallet/wallet-core";
import { PrivateKey } from "@trustwallet/wallet-core/dist/src/wallet-core";

@Injectable()
export class WalletService implements OnModuleInit {
private walletCore: WalletCore;

constructor(private configService: ConfigService) {}
constructor(private configService: ConfigService) { }
async onModuleInit() {
this.walletCore = await initWasm();
if (!this.walletCore) {
Expand All @@ -35,7 +35,7 @@ export class WalletService implements OnModuleInit {
}

keyPairToPublicKey(wallet: KeyPair): string {
const publicKey = WalletContractV3R1.create({ publicKey: wallet.publicKey, workchain: 0 });
const publicKey = WalletContractV5R1.create({ publicKey: wallet.publicKey, workChain: 0 });
return publicKey.address.toString({ urlSafe: true, bounceable: false });
}

Expand All @@ -44,6 +44,6 @@ export class WalletService implements OnModuleInit {
return this.keyPairToPublicKey(keyPair);
}
createContractWallet(keyPair: KeyPair) {
return WalletContractV3R1.create({ publicKey: keyPair.publicKey, workchain: 0 });
return WalletContractV5R1.create({ publicKey: keyPair.publicKey, workChain: 0 });
}
}

0 comments on commit 58e687d

Please sign in to comment.