본문 바로가기
IT/이더리움 Ethereum

이더리움 geth 테스트넷 Ether 송금하기

by SidePower 2022. 9. 20.

 

블록체인에서 송금은 주소에서 주소로 가상화폐를 보내는것입니다.

이더리움의 송금 단위는 wei입니다.

1 ether = 10^18 wei

 채굴하기
> miner.start()
null
> miner.stop()
null
> eth.getBalance(eth.accounts[0])
35000000000000000000
>
> eth.blockNumber
7
▶ 송금 테스트를 위해 미리 채굴해서 7개 블록과 35ether를 보상받았습니다.


 wei 단위를 ether 단위로 변환하기
> web3.fromWei(eth.getBalance(eth.accounts[0]))
35
▶ wei로는 보기 불편해서 ether단위로 변환했지만 
블록체인 전송 단위는 wei입니다.

> web3.fromWei(12000000000)
"0.000000012"
> web3.fromWei(12000000000000)
"0.000012"
> web3.fromWei(12000000000000000)
"0.012"
> web3.fromWei(1200000000000000000)
"1.2"


 ether를 wei로 변환하기
> web3.toWei(5)
"5000000000000000000"
▶ 5ether는 5000000000000000000wei (5×10^18)로 변환되네요.


 계정 주소 확인
> eth.accounts
["0x39de79a7f186f84f03f3e832a3d80e7bbdd2a7f9", 
 "0xc6725761e6d790ef42f0bce2cec9b95577ef9cf8",
"0xcee6a5b6f45f7a0de0ec246783213a80e9428965"]


 계정 주소별로 잔고 확인하기
> eth.getBalance(eth.accounts[0])
35000000000000000000
> eth.getBalance(eth.accounts[1])
0
> eth.getBalance(eth.accounts[2])
0

 

 Ether 송금하기

eth.sendTransaction({from:송신자 주소, to:수신자 주소, value:송금할 wei})
 eth.accounts[0]의 5ether를 eth.accounts[2]로 송금할게요.

> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[2], value:web3.toWei(5)})
Error: authentication needed: password or unlock
        at web3.js:6365:37(47)
        at send (web3.js:5099:62(35))
        at <eval>:1:20(20)
▶ 블록체인에서 트랜젝션 발생은 유료이며 수수료가 발생합니다.
그래서 실수나 잘못된 송금을 막기위해 기본적으로 계정(주소)이 생기면 계정이 잠겨 있고
잠금 해제 후에 가능합니다.

 

 계정(주소) 잠금 해제

personal.unlockAccount(계정주소,비밀번호,유효시간)
 계정주소만 입력해서 잠금 해제
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x39de79a7f186f84f03f3e832a3d80e7bbdd2a7f9
Passphrase: 계정 암호 입력 + (키보드 Enter키)
true
▶ 계정 비밀번호 eth999-1을 입력하니 true로 잠금 해제 처리가 되었네요.


 계정주소와 암호로 잠금 해제
> personal.unlockAccount(eth.accounts[0],'eth999-1')
true


 유효시간까지 입력해서 잠금 해제
> personal.unlockAccount(eth.accounts[0],'eth999-1',0)
true
▶ 잠금 해제는 기본적으로 300초의 유효시간이 주어집니다.
0으로 지정하면 geth 테스트넷이 종료될때까지 유효합니다.

 

 다시 송금하기

> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[2], value:web3.toWei(5)})
"0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927"
▶ 오류없이 정상 송금 수행되었습니다.
결과로 나오는 0xba....................는 트랜젝션ID입니다.

> eth.getBalance(eth.accounts[2])
0
▶ eth.accounts[2] 잔고를 확인해 보니 헐~~~ 0으로 나옵니다.
분명히 5ether를 보냈고 sendTransaction 정상이었는데 말이죠.ㅋ

 원인은 바로 블록체인이기 때문인데요.
블록체인에서 모든 처리는 블록에 의해서 실행되며
블록은 miner(채굴)에 의해서만 생성 되죠.
sendTransaction으로 트랜젝션을 발행하는 시점부터 대기하다가
이후로 블록이 생기면 자동으로 포함되어 실행됩니다.
결국 송금도 채굴을 통해서 처리되는거에요.


 트랜젝션 상태 확인
명령어 : eth.getTransaction(트랜젝션ID)
> eth.getTransaction("0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927")
{
  blockHash: null,
  blockNumber: null,
  from: "0x39de79a7f186f84f03f3e832a3d80e7bbdd2a7f9",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927",
  input: "0x",
  nonce: 0,
  r: "0xefc5f11e4a4fb4d6de00878c185d23745969c6aa7f7352ae506300b7a501fb04",
  s: "0x5ba8e7caed266a0f79ed7d73c474b4722df9b7bf5d4e713a3eb46a07af30d732",
  to: "0xcee6a5b6f45f7a0de0ec246783213a80e9428965",
  transactionIndex: null,
  type: "0x0",
  v: "0xd3",
  value: 5000000000000000000
}
▶ blockNumber가 null로 지정된게 보이시죠.
블록에 포함되지 않았으므로 미처리 상태라고 판단하시면 됩니다.


 미처리된 트랜젝션 확인
> eth.pendingTransactions
[{
    blockHash: null,
    blockNumber: null,
    from: "0x39de79a7f186f84f03f3e832a3d80e7bbdd2a7f9",
    gas: 21000,
    gasPrice: 1000000000,
    hash: "0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927",
    input: "0x",
    nonce: 0,
    r: "0xefc5f11e4a4fb4d6de00878c185d23745969c6aa7f7352ae506300b7a501fb04",
    s: "0x5ba8e7caed266a0f79ed7d73c474b4722df9b7bf5d4e713a3eb46a07af30d732",
    to: "0xcee6a5b6f45f7a0de0ec246783213a80e9428965",
    transactionIndex: null,
    type: "0x0",
    v: "0xd3",
    value: 5000000000000000000
}]

 

 미처리 트랜젝션 실행을 위해 채굴하기

 채굴하기
> miner.start()
null
> miner.stop()
null

 기존 7개에서 12개로 블록늘었네요.
> eth.blockNumber
12


 미처리된 트랜젝션 확인
> eth.pendingTransactions
[]
▶ [] 값은 없다는 의미입니다.


 트랜젝션 상태 확인
> eth.getTransaction("0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927")
{
  blockHash: "0xc00c6cdd46b94bfe5f9050aaaf6cb598723f0d2fe782a69352889081720ea53d",
  blockNumber: 8,
  from: "0x39de79a7f186f84f03f3e832a3d80e7bbdd2a7f9",
  gas: 21000,
  gasPrice: 1000000000,
  hash: "0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927",
  input: "0x",
  nonce: 0,
  r: "0xefc5f11e4a4fb4d6de00878c185d23745969c6aa7f7352ae506300b7a501fb04",
  s: "0x5ba8e7caed266a0f79ed7d73c474b4722df9b7bf5d4e713a3eb46a07af30d732",
  to: "0xcee6a5b6f45f7a0de0ec246783213a80e9428965",
  transactionIndex: 0,
  type: "0x0",
  v: "0xd3",
  value: 5000000000000000000
}
▶ blockNumber: 8로 지정되었어요.


 블록 상태 확인
> eth.getBlock(8)
{
  difficulty: 131072,
  extraData: "0xd883010a0e846765746888676f312e31372e33856c696e7578",
  gasLimit: 2130763676,
  gasUsed: 21000,
  hash: "0xc00c6cdd46b94bfe5f9050aaaf6cb598723f0d2fe782a69352889081720ea53d",
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  miner: "0x39de79a7f186f84f03f3e832a3d80e7bbdd2a7f9",
  mixHash: "0xabe54df868f74cbe034ebace859727314a679729bed6b9d3c4f50cd380c5e870",
  nonce: "0x16167a11c93d9482",
  number: 8,
  parentHash: "0xa56e2890b2aba841180205826d51ac5e376b7e1bafe012e07161dc9f4814f4dc",
  receiptsRoot: "0x148ac3e6210c50a7d9b0181119476a63f4b093b69ad98b4c4f393b4215ac1cab",
  sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
  size: 650,
  stateRoot: "0xdc5e10237a6552a7306b44164f22ceb9c3f4dce043400a33f0066f6efde70391",
  timestamp: 1638955688,
  totalDifficulty: 1065792,
  transactions: ["0xba44d2216d51a6de9718eb2da8a5ddd68860ff2ea76fd0471e589ad10bff8927"],
  transactionsRoot: "0x9171dff06c052106f09967dee1ec4b385325a94ad4692244047b92ebb5b620df",
  uncles: []
}


 송금되었는지 eth.accounts[2] 계정 잔고 확인
> eth.getBalance(eth.accounts[2])
5000000000000000000
> web3.fromWei(eth.getBalance(eth.accounts[2]))
5
▶ 5ether가 무사히 송금된게 확인되네요.

 

감사합니다.

반응형

댓글