{"id":2469,"date":"2022-05-22T14:55:45","date_gmt":"2022-05-22T05:55:45","guid":{"rendered":"https:\/\/blog.wsd.sh\/?p=2469"},"modified":"2022-05-23T13:56:41","modified_gmt":"2022-05-23T04:56:41","slug":"ropsten-%e3%81%a7%e3%81%ae%e9%80%81%e9%87%91%e3%83%86%e3%82%b9%e3%83%88-code","status":"publish","type":"post","link":"https:\/\/blog.wsd.sh\/?p=2469","title":{"rendered":"<small>ropsten \u3067\u306e\u9001\u91d1\u30c6\u30b9\u30c8 code<\/small>"},"content":{"rendered":"<p>\uff11\uff0eropsten \u3067\u306e\u9001\u91d1\u30c6\u30b9\u30c8 code<\/p>\n<p>\uff12\uff0ecode<\/p>\n<pre>\r\n\"use strict\";\r\n\r\nconst Web3 = require('web3')\r\nconst web3 = new Web3('https:\/\/ropsten.infura.io\/v3\/xxxxxxxxxxxxxxxxxxxxxxxxx')\r\nconst { eth } = web3;\r\nconst Tx      = require('ethereumjs-tx').Transaction;\r\n\r\n(async() => {\r\n\r\n    const sellerAddr = 'seller adder'\r\n    const buyerAddr = 'buyer adder'\r\n\r\n    const sellerPrivateKey = 'seller privatekey'\r\n    const buyerPrivateKey = 'buyer privatekey'\r\n\r\n\r\n    const count = await eth.getTransactionCount(buyerAddr);\r\n    console.log(\"count=\"+count);\r\n    const nonce = \"\"+web3.utils.toHex(count);\r\n\r\n    const gasPrice = await eth.getGasPrice()\r\n    console.log(\"gasPrice\"+gasPrice)\r\n\r\n    const sellerBalanceStart = await eth.getBalance(sellerAddr);\r\n    const buyerBalanceStart = await eth.getBalance(buyerAddr);\r\n\r\n    console.log(\"Before Balance\");\r\n    console.log(\"seller = \"+sellerBalanceStart);\r\n    console.log(\"buyer  = \"+buyerBalanceStart);\r\n\r\n    const transactionObj = {\r\n        nonce   : nonce,\r\n        chainId : 3,\r\n        gasPrice: web3.utils.toHex(gasPrice),\r\n        gasLimit: '0x27100',\r\n        to: sellerAddr,\r\n        value: web3.utils.toHex(1*1000000000),\r\n        timeout : web3.utils.toHex(5000)\r\n    }\r\n\r\n    const privateKeyBUF = Buffer.from(buyerPrivateKey, 'hex');\r\n\r\n    const  tx = new Tx ( transactionObj , { chain: 'ropsten' });\r\n    tx.sign(privateKeyBUF);\r\n\r\n    const serializedTx = tx.serialize();\r\n    const serializedTx_hex = '0x' + serializedTx.toString('hex');\r\n\r\n    const transaction = await eth.sendSignedTransaction(serializedTx_hex);\r\n    console.log(transaction);\r\n\r\n    const sellerBalanceEnd = await eth.getBalance(sellerAddr);\r\n    const buyerBalanceEnd = await eth.getBalance(buyerAddr);\r\n\r\n    console.log(\"After Balance\");\r\n    console.log(\"seller = \"+sellerBalanceEnd);\r\n    console.log(\"buyer  = \"+buyerBalanceEnd);\r\n\r\n    \/\/await eth.getPendingTransactions().then(console.log);\r\n    var subscription = web3.eth.subscribe('pendingTransactions', function(error, result){\r\n    if (!error)\r\n        console.log(result);\r\n    }) .on(\"data\", function(transaction){\r\n    console.log(transaction);\r\n    });\r\n\r\n})();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uff11\uff0eropsten \u3067\u306e\u9001\u91d1\u30c6\u30b9\u30c8 code \uff12\uff0ecode &#8220;use strict&#8221;; const Web3 = require(&#8216;web3&#8217;) const web3 = new Web3(&#8216;https:\/\/ropste&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/2469"}],"collection":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2469"}],"version-history":[{"count":4,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/2469\/revisions"}],"predecessor-version":[{"id":2559,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=\/wp\/v2\/posts\/2469\/revisions\/2559"}],"wp:attachment":[{"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wsd.sh\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}