Last active
December 31, 2024 09:24
-
-
Save Phathdt/6b3340f158460899a9d401cb27a2827d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export const GetSettlementSignatureSchema = z.object({ | |
| tradeId: z.string(), | |
| committedQuote: z.string(), | |
| solverFee: z.string(), | |
| tradeDeadline: z.string(), | |
| scriptDeadline: z.string(), | |
| }); | |
| export class GetSettlementSignatureDto extends createZodDto( | |
| GetSettlementSignatureSchema | |
| ) {} | |
| export class SettlementService { | |
| private readonly pmmWallet: ethers.Wallet; | |
| private provider: ethers.JsonRpcProvider; | |
| private pmmId: string; | |
| private routerService = routerService; | |
| constructor( | |
| private readonly configService: ConfigService, | |
| private readonly tradeService: TradeService, | |
| @InjectQueue(TRANSFER_SETTLEMENT_QUEUE) | |
| private transferSettlementQueue: Queue | |
| ) { | |
| const rpcUrl = this.configService.getOrThrow<string>('RPC_URL'); | |
| const pmmPrivateKey = | |
| this.configService.getOrThrow<string>('PMM_PRIVATE_KEY'); | |
| this.pmmId = stringToHex(this.configService.getOrThrow<string>('PMM_ID')); | |
| console.log('π ~ SettlementService ~ pmmId:', this.pmmId); | |
| this.provider = new ethers.JsonRpcProvider(rpcUrl); | |
| this.pmmWallet = new ethers.Wallet(pmmPrivateKey, this.provider); | |
| } | |
| async getSettlementSignature( | |
| dto: GetSettlementSignatureDto, | |
| trade: Trade | |
| ): Promise<SettlementSignatureResponseDto> { | |
| try { | |
| const { tradeId } = trade; | |
| const [presigns, tradeData] = await Promise.all([ | |
| this.routerService.getPresigns(tradeId), | |
| this.routerService.getTradeData(tradeId), | |
| ]); | |
| const { toChain } = tradeData.tradeInfo; | |
| const scriptTimeout = BigInt(Math.floor(Date.now() / 1000) + 1800); | |
| const pmmPresign = presigns.find((t) => t.pmmId === this.pmmId); | |
| if (!pmmPresign) { | |
| throw new BadRequestException('pmmPresign not found'); | |
| } | |
| console.log('π ~ pmmPresign.pmmId:', pmmPresign.pmmId); | |
| console.log('π ~ pmmPresign.pmmRecvAddress:', pmmPresign.pmmRecvAddress); | |
| console.log('π ~ pmmPresign.toChain[1]:', toChain[1]); | |
| console.log('π ~ pmmPresign.toChain[2]:', toChain[2]); | |
| console.log('π ~ dto.committedQuote:', dto.committedQuote); | |
| console.log('π ~ scriptTimeout:', scriptTimeout); | |
| const commitInfoHash = getCommitInfoHash( | |
| pmmPresign.pmmId, | |
| pmmPresign.pmmRecvAddress, | |
| toChain[1], | |
| toChain[2], | |
| BigInt(dto.committedQuote), | |
| scriptTimeout | |
| ); | |
| console.log('π ~ SettlementService ~ commitInfoHash:', commitInfoHash); | |
| const signerAddress = await this.routerService.getSigner(); | |
| console.log('π ~ SettlementService ~ signerAddress:', signerAddress); | |
| const domainData = await signerService.getDomain(signerAddress); | |
| const domain = { | |
| name: domainData.name, | |
| version: domainData.version, | |
| chainId: domainData.chainId, | |
| verifyingContract: domainData.verifyingContract, | |
| }; | |
| console.log('π ~ SettlementService ~ domain:', domain); | |
| const signature = await getSignature( | |
| this.pmmWallet, | |
| this.provider, | |
| signerAddress, | |
| tradeId, | |
| commitInfoHash, | |
| SignatureType.VerifyingContract, | |
| domain | |
| ); | |
| console.log('π ~ SettlementService ~ tradeId:', tradeId); | |
| console.log('π ~ SettlementService ~ signature:', signature); | |
| await this.tradeService.updateTradeStatus(tradeId, TradeStatus.COMMITTED); | |
| return { | |
| tradeId: tradeId, | |
| signature, | |
| deadline: Number(scriptTimeout), | |
| error: '', | |
| }; | |
| } catch (error: any) { | |
| if (error instanceof HttpException) { | |
| throw error; | |
| } | |
| throw new BadRequestException(error.message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment