-
Notifications
You must be signed in to change notification settings - Fork 1
3 Web3 컨트랙트 연동
MoSangIl edited this page Sep 8, 2021
·
5 revisions
npm install web3
"dependencies": {
"web3": "^1.5.2"
},
<head>
<script src="node_modules/web3/dist/web3.min.js"></script>
</head>
경로주의! (frontend 프로젝트 폴더 안)
C:\Users\admin\gitFolder\frontend> mkdir contracts
- chainserver/build/contracts 안에 있는 Demo.json 파일 복사
- frontend/contracts 에 붙여넣기
function getContractWithFetch() {
fetch("./contracts/Demo.json")
.then((response) => response.json())
.then((data) => {
// change to contract address (Demo)
const address = "0x9b0C0f91f9148D2bCd60075b146551900f00f423"; // Demo contract address
const ABI = data.abi;
contracts.Demo = new web3.eth.Contract(ABI, address);
console.log(contracts.Demo);
});
}
const address = "0x3eA2073DE1aaAA3A03D189eC5114F15e5f555021";
"0x3eA2073DE1aaAA3A03D189eC5114F15e5f555021" 부분 자신이 배포한 Demo contract 주소로 변경
function testing() public returns (string memory) {
return "hello world";
}
function onCallMethodBtn(event) {
event.preventDefault();
contracts.Demo.methods
.testing()
.call()
.then((data) => {
alert(`Succed! call the contracts methods! => ${data}`);
});
}
- localhost:9000 (프로젝트 실행 후)
npm run dev
- '컨트랙트 함수 테스트' 버튼 클릭
- Success 알람 확인.