load_test_entropy.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import yargs from "yargs";
  2. import { hideBin } from "yargs/helpers";
  3. import { DefaultStore, EvmChain, toPrivateKey } from "../src";
  4. import { COMMON_DEPLOY_OPTIONS, findEntropyContract } from "./common";
  5. const parser = yargs(hideBin(process.argv))
  6. .usage(
  7. "Load tests the entropy contract using the EntropyTester contract with many requests in a single transaction\n" +
  8. "it does not monitor whether the callbacks are actually submitted or not.\n" +
  9. "Usage: $0 --private-key <private-key> --chain <chain-id> --tester-address <tester-address>",
  10. )
  11. .options({
  12. chain: {
  13. type: "string",
  14. demandOption: true,
  15. desc: "Chain to load test the entropy contract on",
  16. },
  17. "tester-address": {
  18. type: "string",
  19. demandOption: true,
  20. desc: "Address of the EntropyTester contract",
  21. },
  22. "success-count": {
  23. type: "number",
  24. default: 100,
  25. desc: "How many successful requests to make",
  26. },
  27. "revert-count": {
  28. type: "number",
  29. default: 0,
  30. desc: "How many requests to make where the callback should revert",
  31. },
  32. "private-key": COMMON_DEPLOY_OPTIONS["private-key"],
  33. });
  34. const ABI = [
  35. {
  36. inputs: [
  37. {
  38. internalType: "address",
  39. name: "provider",
  40. type: "address",
  41. },
  42. {
  43. internalType: "uint64",
  44. name: "success",
  45. type: "uint64",
  46. },
  47. {
  48. internalType: "uint64",
  49. name: "fail",
  50. type: "uint64",
  51. },
  52. ],
  53. name: "batchRequests",
  54. outputs: [],
  55. stateMutability: "nonpayable",
  56. type: "function",
  57. },
  58. ] as any; // eslint-disable-line @typescript-eslint/no-explicit-any
  59. async function main() {
  60. const argv = await parser.argv;
  61. const privateKey = toPrivateKey(argv.privateKey);
  62. const chain = DefaultStore.getChainOrThrow(argv.chain, EvmChain);
  63. const contract = findEntropyContract(chain);
  64. const provider = await contract.getDefaultProvider();
  65. const fee = await contract.getFee(provider);
  66. const web3 = contract.chain.getWeb3();
  67. const testerContract = new web3.eth.Contract(ABI, argv.testerAddress);
  68. const { address } = web3.eth.accounts.wallet.add(privateKey);
  69. const transactionObject = testerContract.methods.batchRequests(
  70. provider,
  71. argv.successCount,
  72. argv.revertCount,
  73. );
  74. const totalCount = argv.successCount + argv.revertCount;
  75. const result = await contract.chain.estiamteAndSendTransaction(
  76. transactionObject,
  77. {
  78. from: address,
  79. value: (fee * totalCount).toString(),
  80. },
  81. );
  82. console.log("Submitted transaction ", result.transactionHash);
  83. }
  84. main();