Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

start.ts 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 {Calulator} from "../inc/calc";
  7. import axios from "axios";
  8. import {
  9. SecurityToken,
  10. SecurityToken__factory,
  11. } from '../typechain'
  12. import chalk from "chalk";
  13. import {checkReceipt,checkReceipt2, setProvider,getDeploymentAddresses,
  14. getElapsed, sleep,getBlockInfo} from '../inc/util'
  15. import { sign } from "crypto";
  16. dotevnv.config();
  17. if (!process.env.RPCURL) {
  18. console.log(`No rpcur value specified...`)
  19. }
  20. async function getNonce(addr: string): Promise<number> {
  21. const nonce = await provider.getTransactionCount(addr);
  22. return nonce;
  23. }
  24. async function info(symbol: string) {
  25. const proxy = getDeploymentAddresses(symbol).address;
  26. console.log(`토큰 주소 : ${proxy}`);
  27. const token = new SecurityToken__factory(issuer).attach(proxy) as SecurityToken;
  28. await token.connect(admin);
  29. const ops = await token.operators();
  30. console.log(ops);
  31. const balance = await token.balanceOf(firstHolder);
  32. console.log(`initial balance = ${balance}`);
  33. let b = await token.isTokenHolderKYC('0x2071ec931b2567a1418e9bc34b786d654a079b43');
  34. if(!b) {
  35. console.log('kyc check first!');
  36. return;
  37. }
  38. await token.KYCtokenHolders([holder1.address,holder2.address]);
  39. b = await token.isTokenHolderKYC(holder1.address);
  40. if(!b) {
  41. console.log('kyc check first!');
  42. return;
  43. }
  44. // token.grantRole(keccak256('OPERATOR_ROLE'),'0xaaa')
  45. }
  46. function getAuthHeader(token: string) : any{
  47. return {
  48. headers: {
  49. 'Content-Type': 'application/json;charset=UTF-8',
  50. 'Access-Control-Allow-Origin': '*',
  51. 'Authorization': `Bearer ${token}`
  52. }
  53. };
  54. }
  55. async function getBalance(symbol: string, address: string ): Promise<number> {
  56. const path = `holders/${address}/tokens/${symbol}/balance`;
  57. const ret = await axios.get(gateWay + path, getAuthHeader(authToken));
  58. return ret.data;
  59. }
  60. async function getTx(signerAddress:string, tokenSymbol: string, from: string, to: string, value: number) : Promise<any> {
  61. const path = 'transfer/tx/data';
  62. const ret = await axios.post(gateWay + path, {
  63. signerAddress,
  64. tokenSymbol,
  65. transfers : [{
  66. partitionName:'초기',
  67. from,
  68. to,
  69. value,
  70. data:'0x0000000000000000000000000000000000000000000000000000000000000000',
  71. operatorData:'0x0000000000000000000000000000000000000000000000000000000000000000'
  72. }]
  73. },getAuthHeader(authToken));
  74. return ret;
  75. }
  76. async function send(signedTransaction: string) {
  77. const path = 'transaction/send';
  78. const ret = await axios.post(gateWay + path, {
  79. signedTransaction
  80. },getAuthHeader(authToken));
  81. return ret;
  82. }
  83. async function transfer(signer: Wallet,symbol:string,from: Wallet, to: Wallet, amount: number = 1, count: number = 10) {
  84. const proxy = getDeploymentAddresses(symbol).address;
  85. console.log(`토큰 주소 : ${proxy}`);
  86. const token = new SecurityToken__factory(issuer).attach(proxy) as SecurityToken;
  87. const decimals = await token.decimals();
  88. const digit = 10 ** Number(decimals);
  89. //console.log(`자릿수 = ${digit}`);
  90. let fb = await getBalance(symbol,from.address);
  91. let tb = await getBalance(symbol,to.address);
  92. fb = (1/digit) * fb;
  93. tb = (1/digit) * tb;
  94. console.log(chalk.yellow('###################### 전송전 잔고 ###################################'))
  95. console.log(`balance of sender(${from.address}) = ${fb.toFixed(Number(decimals))}\r\nbalance of receiver(${to.address}) = ${tb.toFixed(Number(decimals))}`);
  96. let firstTx:string = null;
  97. let lastTx:string = null;
  98. const start = Date.now();
  99. console.log(chalk.cyan(`전송시작 시간 : ${new Date(start).toUTCString()}`));
  100. console.log(chalk.yellow(`sending ${count} transactions`));
  101. let signedData = [];
  102. for(let i = 0; i < count; i++) {
  103. const rawTx = await getTx(signer.address,symbol,from.address,to.address, amount * digit);
  104. const tx = Transaction.from(rawTx.data);
  105. const signed = await signer.signTransaction(tx);
  106. signedData.push(signed);
  107. if(i == 0) {
  108. firstTx = keccak256(signed);
  109. } else if(i == count -1) {
  110. lastTx = keccak256(signed);
  111. }
  112. }
  113. // const start = Date.now();
  114. // console.log(chalk.cyan(`전송시작 시간 : ${new Date(start).toUTCString()}`));
  115. let proms = [];
  116. for(let i = 0; i < count; i++) {
  117. proms.push(send(signedData[i]));
  118. }
  119. let resolved = await Promise.all(proms);
  120. let txMap = new Map<string,boolean>();
  121. resolved.forEach(tx => {
  122. txMap.set(tx.data.transactionHash,false)
  123. //console.log(tx.data.transactionHash);
  124. });
  125. const blockHashes = await checkReceipt2(txMap,[firstTx as string, lastTx as string], 'wait for receipt...');
  126. const firstBlockHash = blockHashes.get(firstTx);
  127. const lastBlockHash = blockHashes.get(lastTx);
  128. console.log(`firstBlockHash = ${firstBlockHash} \nlastBlockHash = ${lastBlockHash}`);
  129. fb = await getBalance(symbol,from.address);
  130. tb = await getBalance(symbol,to.address);
  131. fb = (1/digit) * fb;
  132. tb = (1/digit) * tb;
  133. console.log(chalk.yellow('###################### 전송후 잔고 ###################################'))
  134. console.log(`balance of sender(${from.address}) = ${fb.toFixed(Number(decimals))}\r\nbalance of receiver(${to.address}) = ${tb.toFixed(Number(decimals))}`);
  135. console.log(firstTx);
  136. // const lastB = await getBlockInfo(lastBlockHash);
  137. // const lastTimeStamp = parseInt(lastB.timestamp,16);
  138. // console.log(`eth_getBlockByHash(${lastBlockHash}).timestamp = ${lastTimeStamp}`)
  139. // const d = new Date(lastTimeStamp * 1000);
  140. // console.log(chalk.cyan(`전송종료 시간(블록완결시간) : ${d.toUTCString()}`));
  141. // console.log(chalk.yellow(`start timestamp = ${start}`));
  142. // console.log(chalk.yellow(`end timestamp = ${lastTimeStamp * 1000}`));
  143. // const tps = calc.CTPS(count,start,lastTimeStamp * 1000);
  144. // console.log(`CTPS = ${tps}`);
  145. }
  146. const firstHolder = '0x2071ec931b2567a1418e9bc34b786d654a079b43';
  147. const calc = new Calulator();
  148. const rpcUrl = process.env.RPCURL;
  149. const gateWay = process.env.GATE_WAY;
  150. const authToken=process.env.AUTHTOKEN;
  151. const provider = new JsonRpcProvider(rpcUrl);
  152. setProvider(provider);
  153. const admin = new Wallet('0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',provider);
  154. const issuer = new Wallet('0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63',provider);
  155. const holder1 = new Wallet('0xae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f',provider);
  156. const holder2 = new Wallet('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',provider);
  157. // const signer = holder1;
  158. console.log(`목적 서버 : ${gateWay}`);
  159. async function main() {
  160. transfer(holder1,'BCG_TEST',holder1, holder2,1, 1);
  161. }
  162. main();