var web3 = new Web3(new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws"));
사용법 - 컨트랙트 인스턴스화하기
var myContract = new web3js.eth.Contract(myABI, myContractAddress);
myContract.methods.myMethod(123).call()
myContract.methods.myMethod(123).send()
var userAccount = web3.eth.accounts[0]; //0번째 사용자 계정을 가져옴
web3js.utils.toWei("1"); //1 ether를 wei로 변경
//solidity
event NewZombie(uint zombieId, string name, uint dna);
//web3.js
cryptoZombies.events.NewZombie()
.on("data", function(event) { //NewZombie 이벤트가 실행될 때마다 이 함수가 실행됨.
let zombie = event.returnValues;
console.log("새로운 좀비가 태어났습니다!", zombie.zombieId, zombie.name, zombie.dna);
}).on("error", console.error);
cryptoZombies.getPastEvents("NewZombie", { fromBlock: 0, toBlock: "latest" })
.then(function(events) {
//처음부터 가장 최신의 이벤트를 받는다.
});
//solidity
event Transfer(address indexed _from, address indexed _to, uint256 _tokenId);
//web3.js
// `filter`를 사용해 `_to`가 `userAccount`와 같을 때만 코드를 실행
cryptoZombies.events.Transfer({ filter: { _to: userAccount } })
.on("data", function(event) {
...
}).on("error", console.error);