Skip to content

Instantly share code, notes, and snippets.

@ppillip
Created June 4, 2019 03:05
Show Gist options
  • Save ppillip/8afca10c126349438d01cf981b801535 to your computer and use it in GitHub Desktop.
Save ppillip/8afca10c126349438d01cf981b801535 to your computer and use it in GitHub Desktop.
web3 셈플
import Web3 from 'web3';
Template.wallMain.helpers({
});
Template.wallMain.events({
});
Template.wallMain.onCreated(function () {
alice = {
privateKey:"70fb2482c1d3e3fa0cd4ae22e39151a1fb98d09156ba98721d105d2b57104acd"
,address:"0x3B598576755664a4b6f73B6f16BAc4d425715Ca9"
,name : "TestAccount1"
};
bob = {
privateKey:"19900d46f119d062c25c5c0aad266baa47d3a7c49a8e68eb0cefab511fc13491"
,address:"0xa1bBf60B7149a2fd3bb4D32B46a1c3037B4686Ea"
,name : "Account2"
};
//web3.eth.accounts.wallet.add(alice.privateKey);
async function sendEther(){
let web3 = new Web3(new Web3.providers.WebsocketProvider('wss://ropsten.infura.io/ws'));
//let web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/7adb5edd47cf4c379dad4f9f3b7640ce"));
balance1 = await web3.eth.getBalance("0x3B598576755664a4b6f73B6f16BAc4d425715Ca9");
balance2 = await web3.eth.getBalance("0xa1bBf60B7149a2fd3bb4D32B46a1c3037B4686Ea");
console.log(web3.utils.fromWei(balance1));
console.log(web3.utils.fromWei(balance2));
let gasLimit = 21000; //await web3.eth.estimateGas({from:alice.address,to:bob.address,value:1});
let gasPrice = 1000000000;//await web3.eth.getGasPrice();
let value = web3.utils.toWei("0.1");
let nonce = await web3.eth.getTransactionCount('0x3B598576755664a4b6f73B6f16BAc4d425715Ca9');
let transaction = {
from:'0x3B598576755664a4b6f73B6f16BAc4d425715Ca9'
,to:'0xa1bBf60B7149a2fd3bb4D32B46a1c3037B4686Ea'
,gasPrice:gasPrice
,gasLimit:gasLimit
,value:value
,nonce:nonce
};
let tx = await web3.eth.accounts.signTransaction(transaction, "70fb2482c1d3e3fa0cd4ae22e39151a1fb98d09156ba98721d105d2b57104acd");
console.log("트렌젝션 서명 성공.. 다음단계 대기중",tx);
web3.eth.sendSignedTransaction(tx.rawTransaction.toString('hex'))
.once('transactionHash', (hash) => {
console.info('transactionHash', 'https://ropsten.etherscan.io/tx/' + hash);
}).once('receipt', (receipt) => {
console.info('receipt', receipt);
}).on('error', console.error);
};
window.sendEther = sendEther;
});
Template.wallMain.onRendered(function () {
});
Template.wallMain.onDestroyed(function () {
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment