main.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package main
  2. import (
  3. "log"
  4. "math/big"
  5. "sync-block/er"
  6. "sync-block/eth"
  7. "time"
  8. _ "github.com/go-sql-driver/mysql"
  9. )
  10. func main() {
  11. log.SetFlags(log.LstdFlags | log.Lshortfile)
  12. ethclient, err := eth.New("http://3.38.62.211:8545")
  13. er.Check(err)
  14. pkey := "d81d044cbf69701d2f93807fee2979fd1028657e455a27a7fbb24206c89d2ca3"
  15. latestblock, err := ethclient.GetLastBlockNumber()
  16. log.Println("현재 블록", latestblock)
  17. er.Check(err)
  18. //당근토큰 주소
  19. tokenAddr := "0xA7B737851241eB0d8Fa79323e1250c84bCEcc20f"
  20. fromAddr := "0x87bac811E0792F33AB05d16814954C264CAAe82D"
  21. toAddr := "0x703FEA5E56cFaF5a30F73a0b663Fe936406485De"
  22. //toAddr := "0x30482875c734452dee589ce820d9cca59e537f01"
  23. //_, gasLimit, err := ethclient.GetEstimatedGasLimit(pkey, tokenAddr, "10000")
  24. //log.Println(gasLimit)
  25. //return
  26. balance, err := ethclient.GetBalance(fromAddr)
  27. er.Check(err)
  28. log.Println("현재 from 이더 발란스", balance)
  29. balance, err = ethclient.GetTokenBalance(tokenAddr, fromAddr)
  30. er.Check(err)
  31. log.Println("현재 from 토큰 발란스", balance)
  32. balance, err = ethclient.GetTokenBalance(tokenAddr, toAddr)
  33. er.Check(err)
  34. log.Println("현재 to 토큰 발란스", balance)
  35. //return
  36. // _, gasLimit, err := ethclient.GetEstimatedGasLimit(tokenAddr, pkey, toAddr, "10000")
  37. _, gasLimit, err := ethclient.GetEstimatedGasLimit(tokenAddr, pkey, toAddr, big.NewInt(10000))
  38. // er.Check(err)
  39. gasPrice, err := ethclient.GetSuggestedGasPrice()
  40. // er.Check(err)
  41. // log.Println(gasPrice, gasLimit)
  42. // return
  43. txHash, err := ethclient.TransferToken(tokenAddr, pkey, toAddr, big.NewInt(10000), gasPrice, gasLimit)
  44. log.Println(txHash, err)
  45. for {
  46. er.Check(err)
  47. tx, err := ethclient.GetTxByHash(txHash)
  48. er.Check(err)
  49. if tx.BlockNumber != "" {
  50. break
  51. }
  52. time.Sleep(time.Second)
  53. log.Println("wait...")
  54. }
  55. log.Println("토큰 전송 완료")
  56. balance, err = ethclient.GetTokenBalance(tokenAddr, fromAddr)
  57. er.Check(err)
  58. log.Println("현재 from 토큰 발란스", balance)
  59. balance, err = ethclient.GetTokenBalance(tokenAddr, toAddr)
  60. er.Check(err)
  61. log.Println("현재 to 토큰 발란스", balance)
  62. rct, err := ethclient.GetTxReceipt(txHash)
  63. er.Check(err)
  64. log.Println("tx status", rct.Status, rct.GasUsed, rct.CumulativeGasUsed)
  65. return
  66. }