web3.service.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { Inject, Injectable, Logger } from '@nestjs/common'
  2. import { ethers } from 'ethers'
  3. import { Wallet, Provider, utils } from 'zksync-web3'
  4. import web3Config from './web3.config'
  5. import { ConfigType } from '@nestjs/config'
  6. import { AccountsService } from 'src/accounts/accounts.service'
  7. @Injectable()
  8. export class Web3Service {
  9. constructor(
  10. @Inject(web3Config.KEY)
  11. private readonly web3Configuration: ConfigType<typeof web3Config>,
  12. private readonly accountService: AccountsService
  13. ) {}
  14. async zkDeposite(accountId, amount) {
  15. const account = await this.accountService.findById(accountId)
  16. const provider = new Provider(this.web3Configuration.zksyncRpcUrl)
  17. const ethProvider = new ethers.providers.InfuraProvider(
  18. this.web3Configuration.ethereumNetwork,
  19. this.web3Configuration.infuraApiKey
  20. )
  21. const wallet = new Wallet(account.privateKey, provider, ethProvider)
  22. const deposit = await wallet.deposit({
  23. token: utils.ETH_ADDRESS,
  24. amount: ethers.utils.parseEther(amount)
  25. })
  26. Logger.log('Deposit sent. Awaiting confirmation...')
  27. // // Await processing of the deposit on L1
  28. // const ethereumTxReceipt = await deposit.waitL1Commit()
  29. // Logger.log('L1 deposit confirmed! Waiting for zkSync...')
  30. // // Await processing the deposit on zkSync
  31. // const depositReceipt = await deposit.wait()
  32. // Logger.log('zkSync deposit committed.')
  33. // Logger.log('Checking zkSync account balance')
  34. // // Retrieving the current (committed) zkSync ETH balance of an account
  35. // const committedEthBalance = await wallet.getBalance()
  36. // Logger.log('committedEthBalance', ethers.utils.formatEther(committedEthBalance))
  37. // // Retrieving the ETH balance of an account in the last finalized zkSync block.
  38. // const finalizedEthBalance = await wallet.getBalance(utils.ETH_ADDRESS, 'finalized')
  39. // Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
  40. }
  41. async zkWidthdraw() {}
  42. }