7-1. Connect Contract to Frontend
src/klaytn
caver.js
KlaystagramContract.js
src/redux
1) src/klaytn
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
caver.js
After making the connection, you can call methods on smart contract with caver.
KlaystagramContract.js
KlaystagramContract.js
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 AddressDEPLOYED_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
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.
Import contract instance
By using
KlaystagramContract
instance, we can call contract's methods when components need to interact with contract.Call contract method
Store data from contract
If transaction is successful, we will call redux action to save information from contract to redux store.
Redux store controls all data flow in front-end
Last updated