Lottery DApp 개발 Lottery React웹 프론트[3]
web3js - filter
call - 스마트 컨트랙트에서 상태를 변화시키지 않는, 값만 읽어오는 액션 send - 스마트 컨트랙트의 값을 변화시키는, 상호작용하는 액션
lotery의 컨트랙트 주소를 복사해서 App.js에 넣어준다. lotery.json에서 abi를 가져와 변수에 넣어준다.
componentDidMount = async () => {
let loteryAddress = '0x764CD7AE528bcC7Ac2673A8AA5891320f57C87Ac';
let loteryABI = [ { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "index", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "bettor", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" }, { "indexed": false, "internalType": "bytes1", "name": "challenges", "type": "bytes1" }, { "indexed": false, "internalType": "uint256", "name": "answerBlockNumber", "type": "uint256" } ], "name": "BET", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "index", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "bettor", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" }, { "indexed": false, "internalType": "bytes1", "name": "challenges", "type": "bytes1" }, { "indexed": false, "internalType": "bytes1", "name": "answer", "type": "bytes1" }, { "indexed": false, "internalType": "uint256", "name": "answerBlockNumber", "type": "uint256" } ], "name": "DRAW", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "index", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "bettor", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" }, { "indexed": false, "internalType": "bytes1", "name": "challenges", "type": "bytes1" }, { "indexed": false, "internalType": "bytes1", "name": "answer", "type": "bytes1" }, { "indexed": false, "internalType": "uint256", "name": "answerBlockNumber", "type": "uint256" } ], "name": "FAIL", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "index", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "bettor", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" }, { "indexed": false, "internalType": "bytes1", "name": "challenges", "type": "bytes1" }, { "indexed": false, "internalType": "uint256", "name": "answerBlockNumber", "type": "uint256" } ], "name": "REFUND", "type": "event" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "index", "type": "uint256" }, { "indexed": false, "internalType": "address", "name": "bettor", "type": "address" }, { "indexed": false, "internalType": "uint256", "name": "amount", "type": "uint256" }, { "indexed": false, "internalType": "bytes1", "name": "challenges", "type": "bytes1" }, { "indexed": false, "internalType": "bytes1", "name": "answer", "type": "bytes1" }, { "indexed": false, "internalType": "uint256", "name": "answerBlockNumber", "type": "uint256" } ], "name": "WIN", "type": "event" }, { "constant": true, "inputs": [], "name": "answerForTest", "outputs": [ { "internalType": "bytes32", "name": "", "type": "bytes32" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "owner", "outputs": [ { "internalType": "address payable", "name": "", "type": "address" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "getPot", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [ { "internalType": "uint256", "name": "index", "type": "uint256" } ], "name": "getBetInfo", "outputs": [ { "internalType": "uint256", "name": "answerBlockNumber", "type": "uint256" }, { "internalType": "address", "name": "bettor", "type": "address" }, { "internalType": "bytes1", "name": "challenges", "type": "bytes1" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [ { "internalType": "bytes1", "name": "challenges", "type": "bytes1" } ], "name": "betAndDistribute", "outputs": [ { "internalType": "bool", "name": "result", "type": "bool" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [ { "internalType": "bytes1", "name": "challenges", "type": "bytes1" } ], "name": "bet", "outputs": [ { "internalType": "bool", "name": "result", "type": "bool" } ], "payable": true, "stateMutability": "payable", "type": "function" }, { "constant": false, "inputs": [], "name": "distribute", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "internalType": "bytes32", "name": "answer", "type": "bytes32" } ], "name": "setAnswerForTest", "outputs": [ { "internalType": "bool", "name": "result", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [ { "internalType": "bytes1", "name": "challenges", "type": "bytes1" }, { "internalType": "bytes32", "name": "answer", "type": "bytes32" } ], "name": "isMatch", "outputs": [ { "internalType": "enum Lotery.BettingResult", "name": "", "type": "uint8" } ], "payable": false, "stateMutability": "pure", "type": "function" } ];
initWeb3 안에
let accounts = await this.web3.eth.getAccounts();
this.accounts = accounts[0];
this.loteryContract = new this.web3.eth.Contract(loteryABI, loteryAddress);
let pot = this.loteryContract.methods.getPot().call();
console.log(pot);
let owner = this.loteryContract.methods.owner().call();
console.log(owner);
Contract는 꼭 대문자!!
현재 pot머니 값과 owner 주소를 출력해주면
현재 pot머니는 0이고 주소는 가나슈의 0번째 주소로 맞게 잘 나온것을 확인할 수 있다.
껐다가 다시 시작해서 새로운 lotery 컨트랙트 주소를 넣어준다. 새롭게 다시 시작했을 때 새로운 것을 넣어주지 않으며 연결오류가 난다.
let loteryAddress = ‘0x249B91F4b74846790b1F301EB73BA8482162A85f’;
Lotery.deployed().then(function(instance){lt = instance})
web3.eth.getAccounts()
인덱스 0 번째 주소를 넣어주고 새롭게 10 eth를 메타마스크 주소에 보내준다.
let ac1=’0x7901d3c08a66315ADB075B36DE2f111E77A5fC78’
web3.eth.sendTransaction({from:ac1,to:’0x5A2200731664fd6d5521C776832F576E61781C02’,value:10000000000000000000})
새로운 변수에 0번째 인덱스 주소를 넣어주고
let bettor = ‘0x7901d3c08a66315ADB075B36DE2f111E77A5fC78’
lt.betAndDistribute(‘0xab’,{from:bettor, value:5000000000000000,gas:300000})
이것을 계속반복 하다보면 어느순간 fail이 뜨게된다.
총 3번 성공하고 9번 fail하였다. 그러므로 pot에는 0.005eth * 9번의 이더가 들어가있어야 한다.
send함수를 사용해 직접 베팅을 해보자!
bet = async() => {
//nonce = 특정 어드레스가 그동안 몇개의 트랜잭션을 만들었는지에 대한 것 , 트랜잭션 리플레이방지와 마음대로 사용 금지시킴
//메타마스크가 자체로 nonce를 가져와져서 안써도 되지만 원래는 꼭 써야함
let nonce = await this.web3.eth.getTransactionCount(this.accounts);
this.loteryContract.methods.betAndDistribute('0xcd').send({from:this.accounts, value:5000000000000000, gas:300000});
}
getBetInfo 함수를 사용하여 어디에 베팅한 정보다 들어있는지 확인한다. 10 - 12 까지 ‘0xab’베팅 정보가 들어있고 13에 ‘0xcd’가 들어있다.
web3js - filter
web3js - send & call
React js 환경 설정
Distribute 함수설계 & 테스트
Distribute 함수설계 & isMath함수 구현 및 테스트
Bet 함수 구현&테스트
Bet 함수 구현&테스트
Lotery domain과 queue설계
Lotery 를 테스트할 테스트 파일을 test폴더 안에 만들어준다.
왜 제목이 안보이지; 너무어렵다 블로긍 ㅠㅡㅜ 다들 맥으로 설명해오앵애ㅐㅐㅐㅐㅐㅐㅐㅐㅐㅐ