Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. const Web3 = require("web3");
  2. const fs = require("fs")
  3. const contracts = require("./config/contract")
  4.  
  5. const web3 = new Web3(
  6. new Web3.providers.HttpProvider("http://10.50.0.2:22000")
  7. );
  8.  
  9.  
  10. let userAcc1 = "0x444698E7F53463c59C6fD1042F8d6C64ef765381"
  11. let userAcc2 = "0x693543d1Ba1c6E00b1A526C2d04BD83777DacceE"
  12.  
  13.  
  14.  
  15. async function initialize() {
  16. let acc1 = await web3.eth.personal.newAccount("test")
  17. console.log(acc1)
  18. let acc2 = await web3.eth.personal.newAccount("test")
  19. fs.writeFile("./test/acc1.json", (acc1), function (params) {})
  20. fs.writeFile("./test/acc2.json", (acc2), function name(params) {})
  21. }
  22.  
  23. async function createAccount(password) {
  24. let acc = await web3.eth.personal.newAccount(password)
  25. await web3.eth.personal.unlockAccount(acc, password, 600000)
  26. return acc
  27. }
  28.  
  29.  
  30.  
  31. async function initialSupply(contract, addressFrom, addressTo, amount) {
  32. try {
  33. let res1 = await contract.methods.increaseAllowance(addressTo, amount * 3).send({
  34. from: addressFrom
  35. })
  36.  
  37. console.log(res1)
  38.  
  39. let res = await contract.methods.transferFrom(addressFrom, addressTo, amount).send({
  40. from: addressTo
  41. })
  42. } catch (error) {
  43. console.log(error)
  44.  
  45. }
  46. }
  47.  
  48. async function getBalance(contract, account) {
  49. return contract.methods.balanceOf(account).call({
  50. from: account
  51. })
  52. }
  53.  
  54. (async function main() {
  55. //initialize();
  56. let accAddressArr = await web3.eth.getAccounts();
  57. let accAddress = accAddressArr[0]
  58.  
  59. try {
  60. await web3.eth.personal.unlockAccount(accAddress, "laki", 60000)
  61. await web3.eth.personal.unlockAccount(userAcc1, "test", 60000)
  62. await web3.eth.personal.unlockAccount(userAcc2, "test", 60000)
  63. } catch (error) {
  64. console.log(error)
  65. }
  66.  
  67. const usdContract = await new web3.eth.Contract(contracts.contractsConfig.usd.abi, contracts.contractsConfig.usd.address, {
  68. defaultAccount: accAddress,
  69. defaultGasPrice: '0x0'
  70. });
  71.  
  72. const eurContract = await new web3.eth.Contract(contracts.contractsConfig.eur.abi, contracts.contractsConfig.eur.address, {
  73. defaultAccount: accAddress,
  74. defaultGasPrice: '0x0'
  75. });
  76.  
  77.  
  78. // initialSupply(eurContract, accAddress, userAcc1, 133700)
  79. // initialSupply(usdContract, accAddress, userAcc2, 133800)
  80.  
  81. // let tmpUserAcc1 = await createAccount("test")
  82. // let tmpUserAcc2 = await createAccount("test")
  83.  
  84. let amount2 = 4950;
  85. let amount1 = 4400;
  86.  
  87. let tmpUserAcc1 = "0xB5b47c10D1071D7418A57CDCBAc52661E6ad064b";
  88. let tmpUserAcc2 = "0xe98FF5827034E64dB885C7C75D259925259998a6";
  89.  
  90.  
  91. // let res1 = await eurContract.methods.transfer(tmpUserAcc2, amount1).send({
  92. // from: userAcc1
  93. // })
  94.  
  95. // let res2 = await usdContract.methods.transfer(tmpUserAcc1, amount2).send({
  96. // from: userAcc2
  97. // })
  98.  
  99.  
  100.  
  101.  
  102. //check balance and new amounts
  103.  
  104. // let res1 = await eurContract.methods.transfer(userAcc2,amount1).send({
  105. // from: tmpUserAcc2
  106. // })
  107.  
  108. // let res2 = await usdContract.methods.transfer(userAcc1, amount2).send({
  109. // from: tmpUserAcc1
  110. // })
  111.  
  112. await web3.eth.personal.unlockAccount(tmpUserAcc1, "test", 60000)
  113. await web3.eth.personal.unlockAccount(tmpUserAcc2, "test", 60000)
  114.  
  115. let balanceTmpAcc2 = await getBalance(eurContract, tmpUserAcc2);
  116. console.log("tmp acc (eur) 2 ", balanceTmpAcc2)
  117.  
  118. let balanceTmpAcc1 = await getBalance(usdContract, tmpUserAcc1);
  119. console.log("tmp acc (usd) 1", balanceTmpAcc1)
  120.  
  121. try {
  122. res1 = await usdContract.methods.transfer(userAcc2,balanceTmpAcc1).send({
  123. from: tmpUserAcc1
  124. })
  125.  
  126. res2 = await eurContract.methods.transfer(userAcc1, balanceTmpAcc2).send({
  127. from: tmpUserAcc2
  128. })
  129. } catch (error) {
  130. console.log(error)
  131. return
  132. }
  133.  
  134.  
  135.  
  136. console.log("***** final ***** \n")
  137.  
  138. balanceTmpAcc2 = await getBalance(eurContract, tmpUserAcc2);
  139. console.log("tmp acc (eur) 2 ", balanceTmpAcc2)
  140.  
  141. balanceTmpAcc1 = await getBalance(usdContract, tmpUserAcc1);
  142. console.log("tmp acc (usd) 1", balanceTmpAcc1)
  143.  
  144. let b = await getBalance(eurContract, userAcc1);
  145. console.log("eur acc 1", b)
  146. b = await getBalance(usdContract, userAcc1);
  147. console.log("usd acc 1", b)
  148.  
  149. b = await getBalance(usdContract, userAcc2);
  150. console.log("usd acc 2", b)
  151. b = await getBalance(eurContract, userAcc2);
  152. console.log("eur acc 2", b)
  153.  
  154.  
  155.  
  156. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement