Kaikas Docs
Search…
Interacting with Contracts

Basic Concepts

To interact with contracts, you need to know a few basic concepts.

The Contract Network

If you aren’t connected to the right network, you aren’t going to have any luck sending transactions to your contract, so make sure you have this right!
Many clever BApps will recognize the user’s current network, and actually adapt to it! For example, if you detect a test network, you could deliberately connect to a test-network version of your smart contract, which makes it easy for users to “try out” your system without using real money!

The Contract Address

Every account in Klaytn has an address, whether it’s an external key-pair account, or a smart contract. In order for any smart contract library to communicate with your contracts, they’ll need to know its exact address.

The Contract ABI

In Klaytn, The ABI Specification is a way to encode the interface of a smart contract in a way that your user interface can make sense of. It is an array of method-describing objects, and when you feed this and the address into a contract-abstraction library like Caver or truffle, this ABI tells those libraries about what methods to provide, and how to compose transactions to call those methods.

The Contract Bytecode

If your web app is going to publish a new smart contract that is pre-compiled, it may need to include some bytecode. In this case, you will not know the contract address in advance, but instead will have to publish, watch for the transaction to be processed, and then extract the final contract’s address from the completed transaction.
If publishing a contract from bytecode, you will still want an ABI if you want to interact with it! The bytecode does not describe how to interact with the final contract.

The Contract Source Code

If your website is going to allow users to edit smart contract source code and compile it, like Klaytn IDE, you may import a whole compiler, in which case you’re going to derive your bytecode and ABI from that source code, and eventually you will derive the contract’s address from the completed transaction publishing that bytecode.

Examples

Smart Contract Deployment

Before deployment, you need to prepare contract's bytecode and ABI. You can either use Truffle framework or Klaytn IDE to easily get them.
1
const caver = new Caver(klaytn)
2
3
caver.klay.sendTransaction({
4
type: 'SMART_CONTRACT_DEPLOY',
5
data: '0x608060405260006008553480156...' //compiled bytecode
6
from: klaytn.selectedAddress,
7
to: '0x0000000000000000000000000000000000000000',
8
value: caver.utils.toPeb('1', 'KLAY'),
9
gas: 8000000,
10
})
11
.once('transactionHash', transactionHash => {
12
console.log('txHash', transactionHash)
13
})
14
.once('receipt', receipt => {
15
console.log('receipt', receipt)
16
})
17
.once('error', error => {
18
console.log('error', error)
19
})
Copied!
You can also use caver.klay.Contract object.
1
const caver = new Caver(klaytn)
2
3
const myContract = new caver.klay.Contract(contractABI)
4
5
myContract.deploy({
6
data: '0x12345...',
7
arguments: [123, 'My String']
8
})
9
.send({
10
from: klaytn.selectedAddress,
11
gas: 1500000,
12
value: 0,
13
}, function(error, transactionHash) { ... })
14
.on('error', function(error) { ... })
15
.on('transactionHash', function(transactionHash) { ... })
16
.on('receipt', function(receipt) {
17
console.log(receipt.contractAddress) // contains the new contract address
18
})
19
.then(function(newContractInstance) {
20
console.log(newContractInstance.options.address) // instance with the new contract address
21
});
Copied!

Calling Contract's Methods

function transfer(address recipient, uint256 amount) public returns (bool) Let's assume that we are calling token transfer function above. There are two different ways to call this contract method.
1
const caver = new Caver(klaytn)
2
const data = caver.klay.abi.encodeFunctionCall(
3
{
4
name: 'transfer',
5
type: 'function',
6
inputs: [
7
{
8
type: 'address',
9
name: 'recipient'
10
},
11
{
12
type: 'uint256',
13
name: 'amount'
14
}
15
]
16
},
17
[to, caver.utils.toPeb(amount, 'KLAY')]
18
)
19
20
caver.klay.sendTransaction({
21
type: 'SMART_CONTRACT_EXECUTION',
22
from: klaytn.selectedAddress,
23
to: contractAddress,
24
gas: '8000000',
25
data
26
})
Copied!
You can also use caver.klay.Contract object here too.
1
const caver = new Caver(klaytn)
2
3
const myContract = new caver.klay.Contract(contractABI, contractAddress)
4
5
myContract.methods.transfer(
6
to, caver.utils.toPeb(amount, 'KLAY')
7
).send({from: klaytn.selectedAddress},
8
function(error, transactionHash) {
9
...
10
});
Copied!
When triggered, the following pop-up will be shown, asking for the user's confirmation.
Contract Execution (token transfer)
If you want to know more about caver.klay.Contract object, please refer to here.
Last modified 1yr ago