You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import * as dotevnv from "dotenv"
  2. import {toUtf8String,parseUnits,ContractFactory, parseEther,JsonRpcProvider, Contract, JsonRpcSigner, Wallet,ContractTransactionResponse, MaxInt256
  3. ,Transaction,formatEther,
  4. ZeroHash,ethers,keccak256
  5. } from 'ethers'
  6. import chalk from "chalk";
  7. import {Calulator} from "../inc/calc";
  8. import { TokenIssuer } from "../inc/TokenIssuer";
  9. import {
  10. SecurityToken,
  11. SecurityToken__factory,
  12. } from '../typechain'
  13. // import { HardhatEthersSigner } from "@nomicfoundation/hardhat-ethers/signers";
  14. import {checkReceipt,checkReceipt2, setProvider,getDeploymentAddresses,
  15. getElapsed, sleep,getBlockInfo} from '../inc/util'
  16. dotevnv.config();
  17. if (!process.env.RPCURL) {
  18. console.log(`No rpcur value specified...`)
  19. }
  20. console.log(`목적 서버 : ${process.env.RPCURL}`);
  21. async function getNonce(addr: string): Promise<number> {
  22. const nonce = await provider.getTransactionCount(addr);
  23. return nonce;
  24. }
  25. async function info(symbol: string) {
  26. const proxy = getDeploymentAddresses(symbol).address;
  27. console.log(`토큰 주소 : ${proxy}`);
  28. const token = new SecurityToken__factory(issuer).attach(proxy) as SecurityToken;
  29. await token.connect(admin);
  30. const ops = await token.operators();
  31. console.log(ops);
  32. // token.grantRole(keccak256('OPERATOR_ROLE'),'0xaaa')
  33. }
  34. async function transfer(symbol:string,from: Wallet, to: Wallet, amount: number = 1, count: number = 10) {
  35. const proxy = getDeploymentAddresses(symbol).address;
  36. console.log(`토큰 주소 : ${proxy}`);
  37. const token = new SecurityToken__factory(issuer).attach(proxy) as SecurityToken;
  38. await token.connect(admin);
  39. let balance1= await token.balanceOf(from);
  40. let balance2 = await token.balanceOf(to);
  41. const b = await token.isTokenHolderKYC(from);
  42. if(!b) {
  43. console.log('holder kyc error');
  44. return;
  45. }
  46. const partitions = await token.getDefaultPartitions();
  47. console.log(chalk.yellow('###################### 전송전 잔고 ###################################'))
  48. console.log(`balance of sender(${from.address}) = ${balance1}\r\nbalance of receiver(${to.address}) = ${balance2}`);
  49. let nonce = await getNonce(signer.address);
  50. let txids = [];
  51. let proms = [];
  52. let firstTx:string = null;
  53. let lastTx:string = null;
  54. const start = Date.now();
  55. console.log(chalk.cyan(`전송시작 시간 : ${new Date(start).toUTCString()}`));
  56. console.log(chalk.yellow(`sending ${count} transactions`));
  57. for(let i = 0; i < count; i++) {
  58. const tx = await token.operatorTransferByPartition.populateTransaction(
  59. partitions[0],
  60. from.address,
  61. to.address,
  62. amount,
  63. ZeroHash,
  64. ZeroHash,
  65. {
  66. // gasPrice: 0,
  67. gasLimit: 2100000,
  68. nonce: nonce,
  69. type: 2,
  70. chainId: 1337
  71. }
  72. );
  73. ++nonce;
  74. const signed = await signer.signTransaction(tx);
  75. const pm = provider.send('eth_sendRawTransaction',[signed]);
  76. if(i == 0) { firstTx = keccak256(signed); }
  77. if (i == count -1) { lastTx = keccak256(signed); }
  78. proms.push(pm);
  79. }
  80. let txs = await Promise.all(proms);
  81. const elapsed = getElapsed(start,1000);
  82. console.log(`all tx sent in ${elapsed} seconds`);
  83. console.log(`first Tx = ${firstTx}`);
  84. console.log(`last Tx = ${lastTx}`);
  85. let txMap = new Map<string,boolean>();
  86. txs.forEach(txid => {
  87. txMap.set(txid,false);
  88. });
  89. const blockHashes = await checkReceipt2(txMap,[firstTx as string, lastTx as string], 'wait for receipt...');
  90. const firstBlockHash = blockHashes.get(firstTx);
  91. const lastBlockHash = blockHashes.get(lastTx);
  92. console.log(`firstBlockHash = ${firstBlockHash} \nlastBlockHash = ${lastBlockHash}`);
  93. balance1= await token.balanceOf(from);
  94. balance2 = await token.balanceOf(to);
  95. console.log(chalk.yellow('###################### 전송후 잔고 ###################################'))
  96. console.log(`balance of sender(${from.address}) = ${balance1}\r\nbalance of receiver(${to.address}) = ${balance2}`);
  97. const lastB = await getBlockInfo(lastBlockHash);
  98. const lastTimeStamp = parseInt(lastB.timestamp,16);
  99. console.log(`eth_getBlockByHash(${lastBlockHash}).timestamp = ${lastTimeStamp}`)
  100. const d = new Date(lastTimeStamp * 1000);
  101. console.log(chalk.cyan(`전송종료 시간(블록완결시간) : ${d.toUTCString()}`));
  102. console.log(chalk.yellow(`start timestamp = ${start}`));
  103. console.log(chalk.yellow(`end timestamp = ${lastTimeStamp * 1000}`));
  104. const tps = calc.CTPS(count,start,lastTimeStamp * 1000);
  105. console.log(`CTPS = ${tps}`);
  106. }
  107. const calc = new Calulator();
  108. const rpcUrl = process.env.RPCURL;
  109. const provider = new JsonRpcProvider(rpcUrl);
  110. setProvider(provider);
  111. const admin = new Wallet('0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',provider);
  112. const issuer = new Wallet('0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63',provider);
  113. // const signer = new Wallet('0x55d1000aa2057a878b5f7902158615f889cb1e9b811f7e1e9c0b4518399ba84a',provider);
  114. const signer = issuer;
  115. const holder1 = new Wallet('0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f',provider);
  116. const holder2 = new Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',provider);
  117. async function main() {
  118. transfer('test001',holder2, holder1,1, 1);
  119. }
  120. main();