token 송금

이더리움 2019. 10. 28. 17:43

출처 ㅣ https://codeday.me/ko/qa/20190930/1507276.html

 

 

금요일 사용 했던 소스

const Web3 = require("web3");
const web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/f76f49a440bf4303ab6d2c1aa21c7537"));
const toAddress = "0x7a0e3AB3c9626018850c06A099981F0B1f183D95";
const privateKey = "개인키";
web3.eth.accounts.signTransaction({
      to: toAddress,
      value: "300000000000000000",
      chainId: 4,
      gas: 210000
  }, privateKey, function (err, result) {
      if (err) {
          console.error(err);
          return;
      }
      console.log('rawTransaction ${result.rawTransaction}');
      web3.eth.sendSignedTransaction(result.rawTransaction).on('receipt', console.log);
  });

 

오늘 사용한 소스

var Web3 = require("web3")
var web3 = new Web3("https://ropsten.infura.io/v3/f76f49a440bf4303ab6d2c1aa21c7537")
var privateKey = "개인키"
const toAddress = "0x7a0e3AB3c9626018850c06A099981F0B1f183D95";

var tx = {
 to: toAddress,
 value: "300000000000000000",
 chainId: 3,
 gas: 21000
}


const app = async () => {
  try {
   var signed = await web3.eth.accounts.signTransaction(tx, privateKey);
   console.log('SIGN', web3.eth.sendSignedTransaction(signed.rawTransaction));

  }catch (error) { console.log('Task Failure',error);
 }
};
app();

 

 

 

금요일까지 잘되던 소스가 안되어서 오후 내 종일 삽질했네

# chainId
    0: Olympic, Ethereum public pre-release testnet
    1: Frontier, Homestead, Metropolis, the Ethereum public main network
    1: Classic, the (un)forked public Ethereum Classic main network, chain ID 61
    1: Expanse, an alternative Ethereum implementation, chain ID 2
    2: Morden, the public Ethereum testnet, now Ethereum Classic testnet
    3: Ropsten, the public cross-client Ethereum testnet
    4: Rinkeby, the public Geth PoA testnet
    42: Kovan, the public Parity PoA testnet
    77: Sokol, the public POA Network testnet
    99: Core, the public POA Network main network
    7762959: Musicoin, the music blockchain

 

바꿔서 실행해보고 안되면 또 삽질

 

 

 

 

 

어제 까지 잘 되었던 윗 소스가 오늘은 롭스톤에서 동작 안함

 

 

 

var Web3 = require("web3")
var web3 = new Web3("https://rinkeby.infura.io/v3/f76f49a440bf4303ab6d2c1aa21c7537")
var privateKey = "0xA209084D071D63F598EB5C3A1B315CE8BDFED7906FF6D071CDADAB34244AAA69"
const toAddress = "0x7a0e3AB3c9626018850c06A099981F0B1f183D95";

var tx = {
 to: toAddress,
 value: "300000000000000000",
 chainId: 4,
 gas: 21000,
 gasLimit : 9000
}


const app = async () => {
  try {
   var signed = await web3.eth.accounts.signTransaction(tx, privateKey);
   console.log('SIGN', web3.eth.sendSignedTransaction(signed.rawTransaction));

  }catch (error) { console.log('Task Failure',error);
 }
};
app();

 

린케비에서 개발 다하고 나중에 메인넷에 한번 붙어 볼까 함

생각 보다 POS 전환 테스트 이더리움이 테스트넷에서 진행하면 조금 불안하다는

생각이 듬

 

 

 

 

 

const Tx = require('ethereumjs-tx').Transaction
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/f76f49a440bf4303ab6d2c1aa21c7537"));

// set token source, destination and amount
var myAddress = "0x908C1B696B38B2647D93280d6b9deB951D61fA50"
var toAddress = "0xFf0797D06e8F9897B1D5066C10D9497Ed7054A47"
var amount = web3.utils.toHex(1e16)

// get transaction count, later will used as nonce
web3.eth.getTransactionCount(myAddress).then(function(v){console.log(v); count = v}) 

// set your private key here, we'll sign the transaction below
var privateKey = new Buffer('개인키', 'hex') 

// Get abi array here https://etherscan.io/address/0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0#code
var abiArray = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},
{"constant":false,"inputs":[],"name":"stop","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},
{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},
{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},
{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},
{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],
"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint128"}],"name":"push","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"name_","type":"bytes32"}],"name":"setName","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"stopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"wad","type":"uint128"}],"name":"pull","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"burn","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"start","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"},{"name":"guy","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"symbol_","type":"bytes32"}],"payable":false,"type":"constructor"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}]
// Here you may get the abicode from a string or a file, here is a string case
// var abiArray = JSON.parse('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"stop","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint128"}],"name":"push","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"name_","type":"bytes32"}],"name":"setName","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"stopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"wad","type":"uint128"}],"name":"pull","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"burn","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"start","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"},{"name":"guy","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"symbol_","type":"bytes32"}],"payable":false,"type":"constructor"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}]', 'utf-8')

var contractAddress = '0xfcc242afa8d4f14c32d14798efc7110beadb1659'
var contract = new web3.eth.Contract(abiArray, contractAddress, {from: myAddress})

 


web3.eth.getTransactionCount(myAddress, (err, txCount) => {
    const app = async () => {
    try {
        var rawTransaction = {"from":myAddress,
         "gasPrice":web3.utils.toHex(2 * 1e9),
        "gasLimit":web3.utils.toHex(210000),
        "to":contractAddress,"value":"0x0",
        "data":contract.methods.transfer(toAddress, amount).encodeABI(),
        "nonce":web3.utils.toHex(txCount)}
        var transaction = new Tx(rawTransaction,{'chain':'rinkeby'})
        transaction.sign(privateKey)

        web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))

        }catch (error) { console.log('Task Failure',error);
        }
    }
    app();
})


// check the balance
contract.methods.balanceOf(myAddress).call().then(function(balance){console.log(balance)})

 

- const Tx = require('ethereumjs-tx').Transaction

- var transaction = new Tx(rawTransaction,{'chain':'rinkeby'})

promise 뜨는 부분 함수로 바꾸었다.

예전 소스 안되어서 다시 구글링해서 만들었다.

 

 

 

 

 

https://rinkeby.etherscan.io/address/0xfcc242afa8d4f14c32d14798efc7110beadb1659

 

 

 

송금 전

 

 

 

 

송금 후

 

 

 

자바스크립트가 큰수를 처리 못해

amount 양이 적은경우가 있는데

아래 메세지가 나오는 경우

 

 Task Failure { Error: invalid number value (arg="wad", coderType="uint256", value="19e16")

 

var tokens = 3
var amount = web3.utils.toWei(tokens.toString(), 'ether')

변경 가능 10^18까지 스트링으로 처리해서 위에 메타마스크 모습 처럼 송금 완료.

 

 

 

 const Tx = require('ethereumjs-tx').Transaction
var Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/f76f49a440bf4303ab6d2c1aa21c7537"));

// set token source, destination and amount
var myAddress = "0x908C1B696B38B2647D93280d6b9deB951D61fA50"
var toAddress = "0xFf0797D06e8F9897B1D5066C10D9497Ed7054A47"
//var amount = web3.utils.toWei(1, 'ether');

var tokens = 3
var amount = web3.utils.toWei(tokens.toString(), 'ether')

 

// get transaction count, later will used as nonce
web3.eth.getTransactionCount(myAddress).then(function(v){console.log(v); count = v}) 

// set your private key here, we'll sign the transaction below
var privateKey = new Buffer('개윈퀴', 'hex') 

// Get abi array here https://etherscan.io/address/0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0#code
var abiArray = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},
{"constant":false,"inputs":[],"name":"stop","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},
{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},
{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},
{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},
{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],
"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint128"}],"name":"push","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"name_","type":"bytes32"}],"name":"setName","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"stopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"wad","type":"uint128"}],"name":"pull","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"burn","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"start","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"},{"name":"guy","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"symbol_","type":"bytes32"}],"payable":false,"type":"constructor"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}]
// Here you may get the abicode from a string or a file, here is a string case
// var abiArray = JSON.parse('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"stop","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"owner_","type":"address"}],"name":"setOwner","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint128"}],"name":"push","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"name_","type":"bytes32"}],"name":"setName","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"mint","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"stopped","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"authority_","type":"address"}],"name":"setAuthority","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"wad","type":"uint128"}],"name":"pull","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint128"}],"name":"burn","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"bytes32"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"start","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"authority","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"src","type":"address"},{"name":"guy","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[{"name":"symbol_","type":"bytes32"}],"payable":false,"type":"constructor"},{"anonymous":true,"inputs":[{"indexed":true,"name":"sig","type":"bytes4"},{"indexed":true,"name":"guy","type":"address"},{"indexed":true,"name":"foo","type":"bytes32"},{"indexed":true,"name":"bar","type":"bytes32"},{"indexed":false,"name":"wad","type":"uint256"},{"indexed":false,"name":"fax","type":"bytes"}],"name":"LogNote","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"authority","type":"address"}],"name":"LogSetAuthority","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"}],"name":"LogSetOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"}]', 'utf-8')

var contractAddress = '0xfcc242afa8d4f14c32d14798efc7110beadb1659'
var contract = new web3.eth.Contract(abiArray, contractAddress, {from: myAddress})

 


web3.eth.getTransactionCount(myAddress, (err, txCount) => {
    const app = async () => {
    try {
        var rawTransaction = {"from":myAddress,
         "gasPrice":web3.utils.toHex(2 * 1e9),
        "gasLimit":web3.utils.toHex(210000),
        "to":contractAddress,"value":"0x0",
        "data":contract.methods.transfer(toAddress, amount).encodeABI(),
        "nonce":web3.utils.toHex(txCount)}
        var transaction = new Tx(rawTransaction,{'chain':'rinkeby'})
        transaction.sign(privateKey)

        web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))

        }catch (error) { console.log('Task Failure',error);
        }
    }
    app();
})


// check the balance
contract.methods.balanceOf(myAddress).call().then(function(balance){console.log(balance)})

 

 

 

'이더리움' 카테고리의 다른 글

graphsq + mongodb  (0) 2021.06.08
스마트컨트렉 함수 접근 방법  (0) 2020.12.08
rinkeby.io facebook으로 테스트 이더 받기  (0) 2019.10.25
erc20 token balance 조회  (0) 2019.10.24
이더스캔 제작4 (개인별 잔액조회)  (0) 2019.10.22
블로그 이미지

iesay

,

https://www.rinkeby.io/#faucet

 

린케비에서 테스트 이더를 받기 위해서는 위 URL로 접속 한다.

롭스톤이나 다른 테스트넷은

 

 

 

게시물을 작성 후 퍼가기 클릭

 

이런 URL이 나온다 이중에 파란색 부분만 복사 해서,,,

 <iframe src="https://www.facebook.com/plugins/post.php?href=https%3A%2F%2Fwww.facebook.com%2Fgodam21321duc%2Fposts%2F2662232697177879&width=500" width="500" height="191" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allow="encrypted-media"></iframe>

 

아래와 같이 URL인코딩된거://     /  이런 문자열 재조합 한뒤에

이더리움 주소가 제대로 보이는지 확인 한다.

해당 게시물이 전체 공개로 되어 있어야 한다.

 

 

 

 

린케비에 등록하면  완료

 

 

 

 

5분이 지나면 메타마스크에서 테스트 이더 등록 완료.

 

테스트 이더도 많이 발급  못받게 할려구 SNS 기능을 넣은거 같다.

 

 

 

 

 

'이더리움' 카테고리의 다른 글

스마트컨트렉 함수 접근 방법  (0) 2020.12.08
token 송금  (0) 2019.10.28
erc20 token balance 조회  (0) 2019.10.24
이더스캔 제작4 (개인별 잔액조회)  (0) 2019.10.22
이더스캔 크롤링 json 저장  (0) 2018.12.06
블로그 이미지

iesay

,


<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <script src="../js/web3.min.js"></script>
</head>
<body>

  <h1>Get ERC20-Token Balance</h1>

  <h2>Contract Address</h2>
  <input type="text" id="contract-address" size="80" oninput="onAddressChange()"></input>
  <p>ex> 0xcd45a142d109bbc8b22ff6028614027d1db4e32f</p>

  <h2>Wallet Address</h2>
  <input type="text" id="wallet-address" size="80" oninput="onAddressChange()"></input>
  <p>ex> 0x528018c88448f67d2b618a43546cf7e0b026857e</p>

  <h2>Result</h2>
  <span id="result"></span>

  <script>

    function getERC20TokenBalance(tokenAddress, walletAddress, callback) {

   
      let minABI = [
     
        {
          "constant":true,
          "inputs":[{"name":"_owner","type":"address"}],
          "name":"balanceOf",
          "outputs":[{"name":"balance","type":"uint256"}],
          "type":"function"
        },
        // decimals
        {
          "constant":true,
          "inputs":[],
          "name":"decimals",
          "outputs":[{"name":"","type":"uint8"}],
          "type":"function"
        }
      ];

    
      let contract = web3.eth.contract(minABI).at(tokenAddress);
 
      contract.balanceOf(walletAddress, (error, balance) => {
    
        contract.decimals((error, decimals) => {
       
          balance = balance.div(10**decimals);
          console.log(balance.toString());
          callback(balance);
        });
      });
    }

    function onAddressChange(e) {
      let tokenAddress = document.getElementById('contract-address').value;
      let walletAddress = document.getElementById('wallet-address').value;
      if(tokenAddress != "" && walletAddress != "") {
        getERC20TokenBalance(tokenAddress, walletAddress, (balance) => {
          document.getElementById('result').innerText = balance.toString();
        });       
      }
    }

    window.onload = function() {
      if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
      } else {
        web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/f76f49a440bf4303ab6d2c1aa21c7537"));
      }
      console.log(web3.version);
    }

  </script>
  <p>ex> https://mainnet.infura.io</p>
</body>
</html> 

 

 

출처 :https://medium.com/@piyopiyo/how-to-get-erc20-token-balance-with-web3-js-206df52f2561

 

web3.js는 최신 버전으로 잘 구해 넣어야 동작함 ..

 

블로그 이미지

iesay

,