5-3. Count Component
Count 컴포넌트
Count 컴포넌트1) Full code
import React, { Component } from 'react'
import cx from 'classnames'
import { cav } from 'klaytn/caver'
import './Count.scss'
class Count extends Component {
constructor() {
super()
// ** 1. 컨트랙트 인스턴스 생성 **
// 예시: new cav.klay.Contract(DEPLOYED_ABI, DEPLOYED_ADDRESS)
// 이 인스턴스를 통해 컨트랙트 메서드를 호출할 수 있습니다.
// 이제 `this.countContract` 변수로 이 인스턴스에 접근할 수 있습니다.
this.countContract = DEPLOYED_ABI
&& DEPLOYED_ADDRESS
&& new cav.klay.Contract(DEPLOYED_ABI, DEPLOYED_ADDRESS)
this.state = {
count: '',
lastParticipant: '',
isSetting: false,
}
}
intervalId = null
getCount = async () => {
// ** 2. 컨트랙트 메서드 호출(CALL) **
// 예시: this.countContract.methods.methodName(arguments).call()
// 위와 같이 컨트랙트 메서드(CALL)를 호출할 수 있습니다.
// 예를 들어 컨트랙트에 `count`라는 메서드가 있을 때,
// 해당 메서드를 아래와 같이 호출할 수 있습니다.
// 예시: this.countContract.methods.count().call()
// 이는 프로미스를 반환하므로 .then() 또는 async-await으로 접근할 수 있습니다.
const count = await this.countContract.methods.count().call()
const lastParticipant = await this.countContract.methods.lastParticipant().call()
this.setState({
count,
lastParticipant,
})
}
setPlus = () => {
const walletInstance = cav.klay.accounts.wallet && cav.klay.accounts.wallet[0]
// 컨트랙트 메서드 호출을 위해 지갑을 연동해야 합니다.
if (!walletInstance) return
this.setState({ settingDirection: 'plus' })
// 3. ** 컨트랙트 메서드 호출 (SEND) **
// 예시: this.countContract.methods.methodName(arguments).send(txObject)
// 위와 같이 컨트랙트 메서드(SEND)를 호출할 수 있습니다.
// 예를 들어 컨트랙트에 `plus`라는 메서드가 있을 때,
// You can call it like below:
// ex:) this.countContract.methods.plus().send({
// from: '0x952A8dD075fdc0876d48fC26a389b53331C34585', // PUT YOUR ADDRESS
// gas: '200000',
// })
try{
this.countContract.send({
from: walletInstance.address,
gas: '200000',
}, 'plus')
.then((receipt) => {
console.log(`
Received receipt! It means your transaction(calling plus function)
is in klaytn block(#${receipt.blockNumber})
`, receipt)
this.setState({
settingDirection: null,
txHash: receipt.transactionHash,
})
})
} catch (error) {
alert(err.message)
this.setState({ settingDirection: null })
}
}
setMinus = () => {
const walletInstance = cav.klay.accounts.wallet && cav.klay.accounts.wallet[0]
// Need to integrate wallet for calling contract method.
if (!walletInstance) return
this.setState({ settingDirection: 'minus' })
// 3. ** Call contract method (SEND) **
// ex:) this.countContract.methods.methodName(arguments).send(txObject)
// You can call contract method (SEND) like above.
// 예를 들어 컨트랙트에 `minus`라는 메서드가 있을 때,
// 해당 메서드를 다음과 같이 호출할 수 있습니다.
// 예시: this.countContract.methods.minus().send({
// from: '0x952A8dD075fdc0876d48fC26a389b53331C34585', // 본인의 주소를 적으세요.
// gas: '200000',
// })
// 이는 이벤트 이미터를 반환하므로 전송 후에 이벤트로 결과를 받아올 수 있습니다.
// .on('transactionHash') 이벤트를 사용하세요.
// : 트랜잭션을 전송한 후 로직을 처리하려는 경우
// .once('receipt') 이벤트를 사용하세요.
// : 트랜잭션이 블록에 포함된 후 로직을 처리하려는 경우
// ex:) .once('receipt', (data) => {
// console.log(data)
// })
try{
this.countContract.send({
from: walletInstance.address,
gas: '200000',
}, 'minus')
.then((receipt) => {
console.log(`
Received receipt! It means your transaction(calling minus function)
is in klaytn block(#${receipt.blockNumber})
`, receipt)
this.setState({
settingDirection: null,
txHash: receipt.transactionHash,
})
})
} catch (error) {
alert(err.message)
this.setState({ settingDirection: null })
}
}
componentDidMount() {
this.intervalId = setInterval(this.getCount, 1000)
}
componentWillUnmount() {
clearInterval(this.intervalId)
}
render() {
const { lastParticipant, count, settingDirection, txHash } = this.state
return (
<div className="Count">
{lastParticipant && (
<div className="Count__lastParticipant">
last participant: {lastParticipant}
</div>
)}
<div className="Count__count">COUNT: {count}</div>
<button
onClick={this.setPlus}
className={cx('Count__button', {
'Count__button--setting': settingDirection === 'plus',
})}
>
+
</button>
<button
onClick={this.setMinus}
className={cx('Count__button', {
'Count__button--setting': settingDirection === 'minus',
})}
>
-
</button>
{txHash && (
<div className="Count__lastTransaction">
<p className="Count__lastTransactionMessage">
You can check your last transaction in klaytn scope:
</p>
<a
target="_blank"
href={`https://baobab.klaytnfinder.io/tx/${txHash}`}
className="Count__lastTransactionLink"
>
{txHash}
</a>
</div>
)}
</div>
)
}
}
export default Count2) Count component's role
Count component's role3) How to interact with contract?
4) Interact with contract: getCount method
getCount method5) Interact with contract: setPlus method
setPlus method6) Transaction life cycle
블록체인에서 어떻게 트랜잭션을 확인할 수 있나요?

Last updated
Was this helpful?