選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

start.ts 6.0KB

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