Klaytn Docs
Search…
7-1. Connect Contract to Frontend
  1. 1.
    src/klaytn
    • caver.js
    • KlaystagramContract.js
  2. 2.
    src/redux

1) src/klaytn

src/klaytn: Contains files that help interact with Klaytn blockchain.
  • src/klaytn/caver.js: Instantiates caver within configured setting.
    cf) caver-js is a RPC library which makes a connection to klaytn node, interacting with node or smart contract deployed on Klaytn.
  • src/klaytn/Klaystagram.js: Creates an instance of contract using caver-js API. You can interact with contract through the instance

caver.js

1
/**
2
* caver-js library helps making connection with klaytn node.
3
* You can connect to specific klaytn node by setting 'rpcURL' value.
4
* default rpcURL is 'https://api.baobab.klaytn.net:8651'.
5
*/
6
import Caver from 'caver-js'
7
8
export const config = {
9
rpcURL: 'https://api.baobab.klaytn.net:8651'
10
}
11
12
export const cav = new Caver(config.rpcURL)
13
14
export default cav
Copied!
After making the connection, you can call methods on smart contract with caver.

KlaystagramContract.js

1
// klaytn/KlaystagramContract.js
2
3
import { cav } from 'klaytn/caver'
4
5
/**
6
* 1. Create contract instance
7
* ex:) new cav.klay.Contract(DEPLOYED_ABI, DEPLOYED_ADDRESS)
8
* You can call contract method through this instance.
9
*/
10
11
const KlaystagramContract = DEPLOYED_ABI
12
&& DEPLOYED_ADDRESS
13
&& new cav.klay.Contract(DEPLOYED_ABI, DEPLOYED_ADDRESS)
14
15
export default KlaystagramContract
Copied!
To interact with contract, we need a contract instance.
KlaystagramContract creates a contract instance to interact with Klaystagram contract, by providing DEPLOYED_ABI(Application Binary Interface) and DEPLOYED_ADDRESS to cav.klay.Contract API.
When compiling & deploying Klaystagram.sol contract (5. Deploy Contract), we already created deployedABI and deployedAddress files. They contain ABI of Klaystagram contract and deployed contract address.
And thanks to webpack's configuration, we can access it as variable.(DEPLOYED_ADDRESS, DEPLOYED_ABI)
  • DEPLOYED_ADDRESS returns deployed Address
  • DEPLOYED_ABI returns Klaystagram contract ABI
cf) contract ABI(Application Binary Interface) contract ABI is the interface for calling contract methods. With this interface, we can call contract methods as below
  • contractInstance.methods.methodName().call()
  • contractInstance.methods.methodName().send({ ... })
Now we are ready to interact with contract in the application. cf. For more information, refer to caver.klay.Contract.

2) src/redux

We are going to make API functions with Klaystagram instance. After calling API functions, we use redux store to controls all data flow.
  1. 1.
    Import contract instance
    By using KlaystagramContract instance, we can call contract's methods when components need to interact with contract.
  2. 2.
    Call contract method
  3. 3.
    Store data from contract
    If transaction is successful, we will call redux action to save information from contract to redux store.
1
// src/redux/actions/photos.js
2
3
// 1. Import contract instance
4
import KlaystagramContract from 'klaytn/KlaystagramContract'
5
6
const setFeed = (feed) => ({
7
type: SET_FEED,
8
payload: { feed },
9
})
10
11
const updateFeed = (tokenId) => (dispatch, getState) => {
12
13
// 2. Call contract method (CALL): getPhoto()
14
KlaystagramContract.methods.getPhoto(tokenId).call()
15
.then((newPhoto) => {
16
const { photos: { feed } } = getState()
17
const newFeed = [newPhoto, ...feed]
18
19
// 3. Store data from contract
20
dispatch(setFeed(newFeed))
21
})
22
}
Copied!
Redux store controls all data flow in front-end