|
|
@@ -47,12 +47,12 @@ import { Cron } from '@nestjs/schedule'
|
|
|
export class Web3Service {
|
|
|
constructor(private readonly accountService: AccountsService) {}
|
|
|
|
|
|
- async zkDeposit(accountId, amount, network: Network) {
|
|
|
+ async zkDeposit(accountId, amount) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const ethProvider = new ethers.providers.InfuraProvider(
|
|
|
- web3Config[network].ethereumNetwork,
|
|
|
- web3Config[network].infuraApiKey
|
|
|
+ web3Config[account.network].ethereumNetwork,
|
|
|
+ web3Config[account.network].infuraApiKey
|
|
|
)
|
|
|
const wallet = new Wallet(account.privateKey, provider, ethProvider)
|
|
|
const deposit = await wallet.deposit({
|
|
|
@@ -78,19 +78,19 @@ export class Web3Service {
|
|
|
// Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
|
|
|
}
|
|
|
|
|
|
- async zkWithdraw(accountId, amount, network: Network) {
|
|
|
+ async zkWithdraw(accountId, amount) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const ethProvider = new ethers.providers.InfuraProvider(
|
|
|
- web3Config[network].ethereumNetwork,
|
|
|
- web3Config[network].infuraApiKey
|
|
|
+ web3Config[account.network].ethereumNetwork,
|
|
|
+ web3Config[account.network].infuraApiKey
|
|
|
)
|
|
|
const wallet = new Wallet(account.privateKey, provider, ethProvider)
|
|
|
const withdrawL2 = await wallet.withdraw({
|
|
|
token: utils.ETH_ADDRESS,
|
|
|
amount: ethers.utils.parseEther(amount)
|
|
|
})
|
|
|
- Logger.log(`Widthdraw sent. ${web3Config[network].zksyncExplorer}/tx/${withdrawL2.hash}`)
|
|
|
+ Logger.log(`Widthdraw sent. ${web3Config[account.network].zksyncExplorer}/tx/${withdrawL2.hash}`)
|
|
|
// // Await processing of the deposit on L1
|
|
|
// const ethereumTxReceipt = await deposit.waitL1Commit()
|
|
|
|
|
|
@@ -109,17 +109,17 @@ export class Web3Service {
|
|
|
// Logger.log('finalizedEthBalance', ethers.utils.formatEther(finalizedEthBalance))
|
|
|
}
|
|
|
|
|
|
- async orbiterDeposit(accountId, amount, network: Network) {
|
|
|
+ async orbiterDeposit(accountId, amount) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
const provider = new ethers.providers.InfuraProvider(
|
|
|
- web3Config[network].ethereumNetwork,
|
|
|
- web3Config[network].infuraApiKey
|
|
|
+ web3Config[account.network].ethereumNetwork,
|
|
|
+ web3Config[account.network].infuraApiKey
|
|
|
)
|
|
|
const wallet = new ethers.Wallet(account.privateKey, provider)
|
|
|
const tx = await wallet.sendTransaction({
|
|
|
from: wallet.address,
|
|
|
- to: web3Config[network].orbiterEthAddress,
|
|
|
- value: ethers.utils.parseEther(amount).add(web3Config[network].orbiterIdCodeZk)
|
|
|
+ to: web3Config[account.network].orbiterEthAddress,
|
|
|
+ value: ethers.utils.parseEther(amount).add(web3Config[account.network].orbiterIdCodeZk)
|
|
|
})
|
|
|
console.log('Mining transaction...')
|
|
|
// Waiting for the transaction to be mined
|
|
|
@@ -127,31 +127,31 @@ export class Web3Service {
|
|
|
console.log('Transaction mined!', receipt)
|
|
|
}
|
|
|
|
|
|
- async orbiterWithdraw(accountId, amount, network: Network) {
|
|
|
+ async orbiterWithdraw(accountId, amount) {
|
|
|
throw new Error('Method not implemented.')
|
|
|
}
|
|
|
|
|
|
- async addLiquidity(accountId, amount, network: Network) {
|
|
|
- const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ async addLiquidity(accountId, amount) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const zkWallet = new Wallet(account.privateKey).connect(provider)
|
|
|
const chain = initialChainTable[chainId]
|
|
|
- const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
|
|
|
+ const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
|
|
|
console.log('address: ', zkWallet.address)
|
|
|
|
|
|
const liquidityManagerContract = getLiquidityManagerContract(
|
|
|
- web3Config[network].liquidityManagerAddress,
|
|
|
+ web3Config[account.network].liquidityManagerAddress,
|
|
|
web3 as any
|
|
|
)
|
|
|
- console.log('liquidity manager address: ', web3Config[network].liquidityManagerAddress)
|
|
|
+ console.log('liquidity manager address: ', web3Config[account.network].liquidityManagerAddress)
|
|
|
|
|
|
const tokenA = getGasToken(chainId)
|
|
|
console.log('tokenA: ', tokenA)
|
|
|
- const tokenB = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
|
|
|
+ const tokenB = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
|
|
|
console.log('tokenB: ', tokenB)
|
|
|
|
|
|
- await this.approve(tokenB.address, web3Config[network].liquidityManagerAddress, zkWallet, network)
|
|
|
+ await this.approve(tokenB.address, web3Config[account.network].liquidityManagerAddress, zkWallet, account.network)
|
|
|
|
|
|
const fee = 2000 // 2000 means 0.2%
|
|
|
const poolAddress = await getPoolAddress(liquidityManagerContract, tokenA, tokenB, fee)
|
|
|
@@ -216,29 +216,29 @@ export class Web3Service {
|
|
|
const tx = await zkWallet.sendTransaction({
|
|
|
from: options.from,
|
|
|
value: Web3.utils.numberToHex(options.value),
|
|
|
- to: web3Config[network].liquidityManagerAddress,
|
|
|
+ to: web3Config[account.network].liquidityManagerAddress,
|
|
|
data: calling.encodeABI(),
|
|
|
gasLimit
|
|
|
})
|
|
|
console.log('tx: ', tx)
|
|
|
}
|
|
|
|
|
|
- async removeLiquidity(accountId, network: Network) {
|
|
|
- const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ async removeLiquidity(accountId) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const wallet = new Wallet(account.privateKey).connect(provider)
|
|
|
const chain = initialChainTable[chainId]
|
|
|
- const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
|
|
|
+ const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
|
|
|
|
|
|
const liquidityManagerContract = getLiquidityManagerContract(
|
|
|
- web3Config[network].liquidityManagerAddress,
|
|
|
+ web3Config[account.network].liquidityManagerAddress,
|
|
|
web3 as any
|
|
|
)
|
|
|
- Logger.log('liquidity manager address: ', web3Config[network].liquidityManagerAddress)
|
|
|
+ Logger.log('liquidity manager address: ', web3Config[account.network].liquidityManagerAddress)
|
|
|
|
|
|
const tokenA = getGasToken(chainId)
|
|
|
- const tokenB = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
|
|
|
+ const tokenB = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
|
|
|
|
|
|
Logger.log(`tokenA: ${tokenA.symbol} tokenB: ${tokenB.symbol}`, 'FetchLiquidities')
|
|
|
const liquidities = await fetchLiquiditiesOfAccount(
|
|
|
@@ -283,7 +283,7 @@ export class Web3Service {
|
|
|
console.log('gas limit: ', gasLimit)
|
|
|
const tx0 = await wallet.sendTransaction({
|
|
|
from: wallet.address,
|
|
|
- to: web3Config[network].liquidityManagerAddress,
|
|
|
+ to: web3Config[account.network].liquidityManagerAddress,
|
|
|
data: decLiquidityCalling.encodeABI(),
|
|
|
gasLimit
|
|
|
})
|
|
|
@@ -321,13 +321,13 @@ export class Web3Service {
|
|
|
console.log('approve tx: ', JSON.stringify(tx0, null, 4))
|
|
|
}
|
|
|
|
|
|
- async swapWithExactOutput(accountId, amount, network: Network) {
|
|
|
+ async swapWithExactOutput(accountId, amount) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
const chain = initialChainTable[chainId]
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const wallet = new Wallet(account.privateKey).connect(provider)
|
|
|
- const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
|
|
|
+ const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
|
|
|
console.log('address: ', wallet.address)
|
|
|
|
|
|
const balance = await web3.eth.getBalance(wallet.address)
|
|
|
@@ -335,7 +335,7 @@ export class Web3Service {
|
|
|
|
|
|
const fromToken = getGasToken(chainId)
|
|
|
console.log('fromToken: ', fromToken)
|
|
|
- const toToken = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
|
|
|
+ const toToken = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
|
|
|
console.log('toToken: ', toToken)
|
|
|
const fee = 2000 // 2000 means 0.2%
|
|
|
|
|
|
@@ -344,7 +344,7 @@ export class Web3Service {
|
|
|
const toTokenBalance = await toTokenContract.methods.balanceOf(wallet.address).call()
|
|
|
console.log(toToken.symbol + ' balance: ' + amount2Decimal(new BigNumber(toTokenBalance), toToken))
|
|
|
|
|
|
- const quoterContract = getQuoterContract(web3Config[network].quoterAddress, web3 as any)
|
|
|
+ const quoterContract = getQuoterContract(web3Config[account.network].quoterAddress, web3 as any)
|
|
|
|
|
|
const receiveAmount = new BigNumber(amount).times(10 ** toToken.decimal)
|
|
|
|
|
|
@@ -363,7 +363,7 @@ export class Web3Service {
|
|
|
console.log(toToken.symbol + ' to desired: ', amount)
|
|
|
console.log(fromToken.symbol + ' to pay: ', payAmountDecimal)
|
|
|
|
|
|
- const swapContract = getSwapContract(web3Config[network].swapAddress, web3 as any)
|
|
|
+ const swapContract = getSwapContract(web3Config[account.network].swapAddress, web3 as any)
|
|
|
|
|
|
const swapParams = {
|
|
|
...params,
|
|
|
@@ -387,29 +387,29 @@ export class Web3Service {
|
|
|
const tx = await wallet.sendTransaction({
|
|
|
value: Web3.utils.numberToHex(options.value),
|
|
|
data: swapCalling.encodeABI(),
|
|
|
- to: web3Config[network].swapAddress,
|
|
|
+ to: web3Config[account.network].swapAddress,
|
|
|
gasLimit
|
|
|
})
|
|
|
console.log(tx)
|
|
|
}
|
|
|
|
|
|
- async swapWithExactInput(accountId, amount, network: Network) {
|
|
|
+ async swapWithExactInput(accountId, amount) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
const chain = initialChainTable[chainId]
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const wallet = new Wallet(account.privateKey).connect(provider)
|
|
|
- const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
|
|
|
+ const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
|
|
|
console.log('address: ', wallet.address)
|
|
|
|
|
|
- const fromToken = await fetchToken(web3Config[network].zkUsdcAddress, chain, web3 as any)
|
|
|
+ const fromToken = await fetchToken(web3Config[account.network].zkUsdcAddress, chain, web3 as any)
|
|
|
console.log('fromToken: ', fromToken)
|
|
|
const toToken = getGasToken(chainId)
|
|
|
console.log('toToken: ', toToken)
|
|
|
|
|
|
- await this.approve(fromToken.address, web3Config[network].swapAddress, wallet, network)
|
|
|
+ await this.approve(fromToken.address, web3Config[account.network].swapAddress, wallet, account.network)
|
|
|
|
|
|
- const quoterContract = getQuoterContract(web3Config[network].quoterAddress, web3 as any)
|
|
|
+ const quoterContract = getQuoterContract(web3Config[account.network].quoterAddress, web3 as any)
|
|
|
|
|
|
const payAmount = new BigNumber(amount).times(10 ** fromToken.decimal)
|
|
|
const fee = 2000 // 2000 means 0.2%
|
|
|
@@ -428,7 +428,7 @@ export class Web3Service {
|
|
|
console.log(fromToken.symbol + ' to pay: ', amount)
|
|
|
console.log(toToken.symbol + ' to acquire: ', receiveAmountDecimal)
|
|
|
|
|
|
- const swapContract = getSwapContract(web3Config[network].swapAddress, web3 as any)
|
|
|
+ const swapContract = getSwapContract(web3Config[account.network].swapAddress, web3 as any)
|
|
|
|
|
|
const swapParams = {
|
|
|
...params,
|
|
|
@@ -450,29 +450,29 @@ export class Web3Service {
|
|
|
const tx = await wallet.sendTransaction({
|
|
|
value: Web3.utils.numberToHex(options.value),
|
|
|
data: swapCalling.encodeABI(),
|
|
|
- to: web3Config[network].swapAddress,
|
|
|
+ to: web3Config[account.network].swapAddress,
|
|
|
gasLimit
|
|
|
})
|
|
|
console.log(JSON.stringify(tx, null, 4))
|
|
|
}
|
|
|
|
|
|
- async mint(accountId, network: Network) {
|
|
|
+ async mint(accountId) {
|
|
|
const account = await this.accountService.findById(accountId)
|
|
|
- const chainId = web3Config[network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
+ const chainId = web3Config[account.network].ethereumNetwork == 'goerli' ? ChainId.ZkSyncAlphaTest : ChainId.ZkSyncEra
|
|
|
const chain = initialChainTable[chainId]
|
|
|
- const provider = new Provider(web3Config[network].zksyncRpcUrl)
|
|
|
+ const provider = new Provider(web3Config[account.network].zksyncRpcUrl)
|
|
|
const wallet = new Wallet(account.privateKey).connect(provider)
|
|
|
- const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[network].zksyncRpcUrl))
|
|
|
+ const web3 = new Web3(new Web3.providers.HttpProvider(web3Config[account.network].zksyncRpcUrl))
|
|
|
console.log('address: ', wallet.address)
|
|
|
|
|
|
- const contract = new web3.eth.Contract(mintSquareAbi, web3Config[network].mintAddress, web3 as any)
|
|
|
+ const contract = new web3.eth.Contract(mintSquareAbi, web3Config[account.network].mintAddress, web3 as any)
|
|
|
// @ts-ignore
|
|
|
const mintCall = await contract.methods.mint('ipfs://QmPxcRPvjMDrv7d4WjQK5KNXkuXudGhKjPTDnzuFkRQ6Fs')
|
|
|
const gasLimit = await mintCall.estimateGas({ from: wallet.address })
|
|
|
console.log('gas limit: ', gasLimit)
|
|
|
const tx = await wallet.sendTransaction({
|
|
|
from: wallet.address,
|
|
|
- to: web3Config[network].mintAddress,
|
|
|
+ to: web3Config[account.network].mintAddress,
|
|
|
data: mintCall.encodeABI(),
|
|
|
gasLimit
|
|
|
})
|