Skip to content

Commit

Permalink
update validate sender
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-MinhVu committed Jan 3, 2024
1 parent 82fe5f8 commit 657229b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/database/seeds/common_config.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
import { Seeder, SeederFactoryManager } from 'typeorm-extension';

import { CommonConfig } from '@modules/crawler/entities/common-config.entity';
import { addDecimal } from '@shared/utils/bignumber';

export default class CommonConfigSeeder implements Seeder {
public async run(dataSource: DataSource, factoryManager: SeederFactoryManager): Promise<any> {
Expand All @@ -11,7 +12,7 @@ export default class CommonConfigSeeder implements Seeder {
await repository.insert(
new CommonConfig({
tip: 0.5,
dailyQuota: 50,
dailyQuota: addDecimal(500, 18).toString(),
asset: "ETH"
}),
);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/crawler/entities/common-config.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class CommonConfig extends BaseEntityIncludeTime {
default: 0,
nullable: false
})
dailyQuota: number;
dailyQuota: string;

@Column({
name: 'tip',
Expand Down
21 changes: 21 additions & 0 deletions src/modules/crawler/sender.evmbridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { EventLogRepository } from 'database/repositories/event-log.repository';
import { CommonConfigRepository } from 'database/repositories/common-configuration.repository';
import { ETHBridgeContract } from '@shared/modules/web3/web3.service';
import { calculateFee } from '@shared/utils/bignumber';
import { EError } from '@constants/error.constant';

@Injectable()
export class SenderEVMBridge {
Expand All @@ -23,6 +24,13 @@ export class SenderEVMBridge {
this.commonConfigRepository.getCommonConfig()
])
const { tokenReceivedAddress, txHashLock, receiveAddress, amountFrom } = dataLock

const isPassDailyQuota = await this.isPassDailyQuota(receiveAddress);
if(!isPassDailyQuota) {
await this.eventLogRepository.updateStatusAndRetryEvenLog(dataLock.id, dataLock.retry, EEventStatus.FAILED, EError.OVER_DAILY_QUOTA);
return ;
}

const gasFee = await this.ethBridgeContract.getEstimateGas(tokenReceivedAddress, BigNumber(amountFrom), txHashLock, receiveAddress, 0)
const protocolFee = calculateFee(amountFrom, 0 , gasFee, configTip.tip)
const result = await this.ethBridgeContract.unlock(tokenReceivedAddress, BigNumber(amountFrom), txHashLock, receiveAddress, protocolFee)
Expand All @@ -38,5 +46,18 @@ export class SenderEVMBridge {
}

}

private async isPassDailyQuota(address: string): Promise<boolean> {
const [dailyQuota , totalamount] = await Promise.all([
await this.commonConfigRepository.getCommonConfig(),
await this.eventLogRepository.sumAmountBridgeOfUserInDay(address)
])

if(totalamount && totalamount.totalamount > dailyQuota.dailyQuota) {
return false
}
return true
}

}

17 changes: 8 additions & 9 deletions src/modules/crawler/sender.minabridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CommonConfigRepository } from 'database/repositories/common-configurati
import { Mina, PublicKey, Experimental, fetchAccount, PrivateKey, UInt64, AccountUpdate } from 'o1js';
import { Token } from './erc20.js';
import { Bridge } from './bridgeSC.js';
import { calculateFee } from '@shared/utils/bignumber.js';
import { addDecimal, calculateFee } from '@shared/utils/bignumber.js';
import { ConfigService } from '@nestjs/config';
import { EEnvKey } from '@constants/env.constant';
import BigNumber from 'bignumber.js';
Expand All @@ -28,13 +28,13 @@ export class SenderMinaBridge {
])

const { tokenReceivedAddress, id, receiveAddress, amountFrom } = dataLock
// const protocolFee = calculateFee(amountFrom, 0 , this.configService.get(EEnvKey.GASFEEMINA), configTip.tip)
// const protocolFee = calculateFee(amountFrom, 0 , addDecimal(this.configService.get(EEnvKey.GASFEEMINA), 18), configTip.tip)

// const isPassDailyQuota = await this.isPassDailyQuota(receiveAddress);
// if(!isPassDailyQuota) {
// await this.eventLogRepository.updateStatusAndRetryEvenLog(dataLock.id, dataLock.retry, EEventStatus.FAILED, EError.OVER_DAILY_QUOTA);
// return ;
// }
const isPassDailyQuota = await this.isPassDailyQuota(receiveAddress);
if(!isPassDailyQuota) {
await this.eventLogRepository.updateStatusAndRetryEvenLog(dataLock.id, dataLock.retry, EEventStatus.FAILED, EError.OVER_DAILY_QUOTA);
return ;
}

const result = await this.callUnlockFunction(amountFrom, id, receiveAddress)

Expand Down Expand Up @@ -103,10 +103,9 @@ export class SenderMinaBridge {
await this.eventLogRepository.sumAmountBridgeOfUserInDay(address)
])

if(totalamount && totalamount > dailyQuota.dailyQuota) {
if(totalamount && totalamount.totalamount > dailyQuota.dailyQuota) {
return false
}

return true
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/modules/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { TokenPair } from '@modules/users/entities/tokenpair.entity';
import { ENetworkName } from '@constants/blockchain.constant';
import { addDecimal, calculateFee } from '@shared/utils/bignumber';
import { ETHBridgeContract } from '@shared/modules/web3/web3.service';
import { ConfigService } from '@nestjs/config';
import { EEnvKey } from '@constants/env.constant';

@Injectable()
export class UsersService {
Expand All @@ -21,6 +23,7 @@ export class UsersService {
private readonly commonConfigRepository: CommonConfigRepository,
private readonly dataSource: DataSource,
private readonly ethBridgeContract: ETHBridgeContract,
private readonly configService: ConfigService,

) {}
async getProfile(userId: number) {
Expand Down Expand Up @@ -91,7 +94,7 @@ export class UsersService {
])

if(tokenPair.toChain == ENetworkName.MINA) {
gasFee = addDecimal(10, 18);
gasFee = addDecimal(this.configService.get(EEnvKey.GASFEEMINA), 18);
} else {
gasFee = await this.ethBridgeContract.getEstimateGas(tokenPair.fromAddress, addDecimal(0, tokenPair.toDecimal), 1 , tokenPair.toScAddress, 0);
}
Expand Down
2 changes: 1 addition & 1 deletion src/shared/utils/bignumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const addDecimal = (value: string | number, decimal: number) => {
};

export const calculateFee = (amount: string | number, decimal: number, gasFee: string | number, tipPercent: number) => {
const tip = BigNumber(addDecimal(amount, decimal)).minus(BigNumber(gasFee)).multipliedBy(tipPercent * 100).dividedBy(100);
const tip = BigNumber(addDecimal(amount, 0)).minus(BigNumber(gasFee)).multipliedBy(tipPercent * 100).dividedBy(100);

return BigNumber(gasFee).plus(tip).toFixed(0, BigNumber.ROUND_DOWN);
}

0 comments on commit 657229b

Please sign in to comment.