BaseOperationTest.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { web3 } from "hardhat"
  2. import { ethers } from "hardhat"
  3. import deployer from '../.secret'
  4. import contracts from '../config/contracts'
  5. import fee from '../config/fee'
  6. describe('Base operation test', () => {
  7. it('Parse ether test', async () => {
  8. console.log(web3.utils.toWei('1', 'gwei'))
  9. })
  10. it('Get block number test', async () => {
  11. console.log(await web3.eth.getBlockNumber())
  12. })
  13. it('Get balance test', async () => {
  14. console.log(await web3.eth.getBalance(deployer.address))
  15. })
  16. it('Transfer test', async () => {
  17. let rawTx = {
  18. from: deployer.address,
  19. nonce: await web3.eth.getTransactionCount(deployer.address),
  20. gasPrice: web3.utils.toWei('2', 'gwei'),
  21. gasLimit: 1_000_000,
  22. to: contracts.ZERO,
  23. value: 1e9
  24. }
  25. await web3.eth.sendTransaction(rawTx).then(console.log)
  26. })
  27. it('Weth withdraw test', async () => {
  28. const WETH_ABI = require('../abi/WETH_ABI').default
  29. const WETH_ADDR = contracts.WETH
  30. let weth_contract = new web3.eth.Contract(WETH_ABI, WETH_ADDR)
  31. // await weth_contract.methods.balanceOf(deployer.address).call().then(console.log)
  32. let rawTx = {
  33. from: deployer.address,
  34. nonce: await web3.eth.getTransactionCount(deployer.address),
  35. gasPrice: web3.utils.toWei('2', 'gwei'),
  36. gasLimit: 1_000_000
  37. }
  38. await weth_contract.methods.withdraw(1e15).send(rawTx).then(console.log)
  39. })
  40. it ('Weth approve test', async () => {
  41. const WETH_ABI = require('../abi/WETH_ABI').default
  42. const WETH_ADDR = contracts.WETH
  43. let weth_contract = new web3.eth.Contract(WETH_ABI, WETH_ADDR)
  44. // await weth_contract.methods.balanceOf(deployer.address).call().then(console.log)
  45. let rawTx = {
  46. from: deployer.address,
  47. nonce: await web3.eth.getTransactionCount(deployer.address),
  48. gasPrice: web3.utils.toWei('2', 'gwei'),
  49. gasLimit: 1_000_000
  50. }
  51. await weth_contract.methods.approve(contracts.UNIV3, 1e18.toString()).send(rawTx).then(console.log)
  52. })
  53. it('Univ3 swap test', async () => {
  54. const UNIV3_ABI = require('../abi/UNIV3_ABI').default
  55. let univ3_contract = new web3.eth.Contract(UNIV3_ABI, contracts.UNIV3)
  56. let rawTx = {
  57. from: deployer.address,
  58. nonce: await web3.eth.getTransactionCount(deployer.address),
  59. gasPrice: web3.utils.toWei('2', 'gwei'),
  60. gasLimit: 1_000_000
  61. }
  62. let amountIn = 1_000_000
  63. let amountOutMin = 1
  64. let path = [contracts.WETH, contracts.HEX]
  65. let to = deployer.address
  66. await univ3_contract.methods
  67. .swapExactTokensForTokens(amountIn, amountOutMin, path, to)
  68. .send(rawTx).then(console.log)
  69. })
  70. it('Univ3 `exactInput` test', async () => {
  71. const UNIV3_ABI = require('../abi/UNIV3_ABI').default
  72. let univ3_contract = new web3.eth.Contract(UNIV3_ABI, contracts.UNIV3)
  73. let rawTx = {
  74. from: deployer.address,
  75. nonce: await web3.eth.getTransactionCount(deployer.address),
  76. gasPrice: web3.utils.toWei('2', 'gwei'),
  77. gasLimit: 1e6
  78. }
  79. let amountIn = 1e9
  80. let amountOutMin = 1
  81. let deadline = parseInt(String(new Date().getTime() / 1e3))
  82. let path = [contracts.WETH, fee._5_per_10000, contracts.USDT]
  83. let to = deployer.address
  84. let params = {
  85. path: ethers.utils.solidityPack(['address', 'uint24', 'address'], path),
  86. recipient: to,
  87. deadline: deadline,
  88. amountIn: amountIn,
  89. amountOutMinimum: amountOutMin
  90. }
  91. await univ3_contract.methods
  92. .exactInput(params)
  93. .send(rawTx).then(console.log)
  94. })
  95. })