caver.klay.Contract
스마트 컨트랙트와 상호작용하는 데 사용되는 caver-js 객체입니다.
The caver.klay.Contract
object makes it easy to interact with smart contracts on the Klaytn blockchain. When you create a new contract object, you give it the JSON interface of the respective smart contract and caver will auto convert all calls into low level ABI calls over RPC for you.
This allows you to interact with smart contracts as if they were JavaScript objects.
new contract
Creates a new contract instance with all its methods and events defined in its JSON interface object.
Parameters
jsonInterface
Object
The JSON interface for the contract to instantiate
address
String
(optional) The address of the smart contract to call. Can be added later using myContract.options.address = '0x1234..'
options
Object
(optional) The options of the contract. See the table below for the details.
The options object contains the following:
from
String
(optional) The address from which transactions should be made.
gasPrice
String
(optional) The gas price in peb to use for transactions.
gas
Number
(optional) The maximum gas provided for a transaction (gas limit).
data
String
(optional) The byte code of the contract. Used when the contract gets deployed.
Return Value
Object
The contract instance with all its methods and events.
Example
options
The options
object for the contract instance. from
, gas
and gasPrice
are used as fallback values when sending transactions.
Properties
address
String
jsonInterface
Array
data
String
The byte code of the contract. Used when the contract gets deployed.
from
String
트랜잭션이 만들어진 송신자 주소.
gasPrice
String
The gas price in peb to use for transactions.
gas
Number
The maximum gas provided for a transaction (gas limit).
Example
options.address
The address used for this contract instance myContract
. All transactions generated by caver-js from this contract will contain this address as the "to". 주소는 소문자로 저장됩니다.
Property
address
String \
null
Example
options.jsonInterface
The JSON interface object derived from the ABI of this contract myContract
.
Property
jsonInterface
Array
The JSON interface for this contract. Re-setting this will regenerate the methods and events of the contract instance.
Example
clone
Clones the current contract instance.
Parameters
None
Return Value
Object
The new cloned contract instance.
Example
deploy
컨트랙트를 Klaytn 블록체인에 배포합니다. After successful deployment, the promise will be resolved with a new contract instance.
Parameters
options
: 배포에 사용되는 옵션 객체:
data
String
The byte code of the contract.
arguments
Array
(optional) The arguments that get passed to the constructor on deployment.
Return Value
Object
: 트랜잭션 객체:
Array
arguments: 이전에 메소드에 전달되었던 인자. 이들은 변경될 수 있습니다.
Function
Function
Function
Example
methods
Creates a transaction object for that method, which then can be called, sent, estimated or ABI encoded.
이 스마트 컨트랙트의 메소드는 다음을 통해 이용할 수 있습니다:
이름:
myContract.methods.myMethod(123)
매개변수가 있는 이름:
myContract.methods['myMethod(uint256)'](123)
The signature*:
myContract.methods['0x58cf5f10'](123)
This allows calling functions with the same name but different parameters from the JavaScript contract object.
cf) *Function signature (Function selector)
The first four bytes of the call data for a function call specifies the function to be called. It is the first (left, high-order in big-endian) four bytes of the Keccak-256 (SHA-3) hash of the signature of the function.
The function signature can be made by 2 different methods.
1. caver.klay.abi.encodeFunctionSignature('funcName(paramType1,paramType2,...)')
2. caver.utils.sha3('funcName(paramType1,paramType2,...)').substr(0, 10)
ex)
Parameters
모든 메소드의 매개변수는 JSON 인터페이스에 정의된 스마트 컨트랙트 메소드에 의존합니다.
Return Value
Object
: The transaction object:
Array
arguments: The arguments passed to the method before. They can be changed.
Function
Function
Function
Function
Example
methods.myMethod.call
Will call a "constant" method and execute its smart contract method in the Klaytn Virtual Machine without sending any transaction. Note that calling cannot alter the smart contract state.
Parameters
options
Object
(optional) The options used for calling. See the table below for the details.
callback
Function
(optional) This callback will be fired with the result of the smart contract method execution as the second argument, or with an error object as the first argument.
The options object can contain the following:
from
String
(optional) The address the call “transaction” should be made from.
gasPrice
String
(optional) The gas price in peb to use for this call "transaction".
gas
Number
(optional) The maximum gas provided for this call "transaction" (gas limit).
Return Value
Promise
returns Mixed
: The return value(s) of the smart contract method. If it returns a single value, it is returned as it is. If it has multiple return values, they are returned as an object with properties and indices.
Example
methods.myMethod.send
스마트 컨트랙트에 트랜잭션을 보내고 그 메소드를 실행합니다. Note that this can alter the smart contract state.
Parameters
options
Object
The options used for sending. See the table below for the details.
callback
Function
(optional) This callback will be fired first with the "transactionHash", or with an error object as the first argument.
The options object can contain the following:
from
String
The address from which the transaction should be sent.
gasPrice
String
(optional) The gas price in peb to use for this transaction.
gas
Number
The maximum gas provided for this transaction (gas limit).
value
Number \
String | BN | BigNumber
Return Value
callback
은 32바이트 트랜잭션 해시를 반환합니다.
PromiEvent
: 프로미스(promise)가 조합된 이벤트 이미터(event emitter). Will be resolved when the transaction receipt is available, or if this send()
is called from a someContract.deploy()
, then the promise will be resolved with the new contract instance. 추가로 다음과 같은 이벤트를 사용할 수 있습니다:
transactionHash
String
트랜잭션이 전송된 직후 및 트랜잭션 해시를 사용할 수 있을 때 발생합니다.
receipt
Object
error
Error
전송 중 오류가 발생하면 발생됩니다. On an out-of-gas error, the second parameter is the receipt.
Example
methods.myMethod.estimateGas
Will estimate the gas that a method execution will take when executed in the Klaytn Virtual Machine. The estimation can differ from the actual gas used when later sending a transaction, as the state of the smart contract can be different at that time.
Parameters
options
Object
(optional) The options used for calling. See the table below for the details.
callback
Function
(optional) This callback will be fired with the result of the gas estimation as the second argument, or with an error object as the first argument.
The options object can contain the following:
from
String
(optional) The address from which the call "transaction" should be made.
gas
Number
(optional) The maximum gas provided for this call "transaction" (gas limit). Setting a specific value helps to detect out of gas errors. If all gas is used, it will return the same number.
value
Number \
String | BN | BigNumber
Return Value
Promise
는 Number
를 반환합니다 - 모의 호출/트랜잭션에 사용된 가스.
Example
methods.myMethod.encodeABI
Encodes the ABI for this method. This can be used to send a transaction, call a method, or pass it into another smart contract method as arguments.
Parameters
None
Return Value
String
The encoded ABI byte code to send via a transaction or call.
Example
once
Subscribes to an event and unsubscribes immediately after the first event or error. Will only fire for a single event.
Parameters
event
String
컨트랙트, 또는 모든 이벤트를 받기 위한 "allEvents"
에서의 이벤트 이름.
options
Object
(optional) The options used for deployment. See the table below for the details.
callback
Function
The options object can contain the following:
filter
Object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {myNumber: [12,13]}}
means all events where "myNumber" is 12 or 13.
topics
Array
(optional) This allows you to manually set the topics for the event filter. 필터 특성 및 이벤트 서명이 제공되면, topic[0]
가 자동으로 설정되지 않습니다.
Return Value
undefined
Example
events
이벤트를 구독합니다.
Parameters
options
Object
(optional) The options used for deployment. See the table below for the details.
callback
Function
(optional) This callback will be fired for each event as the second argument, or an error as the first argument.
The options object can contain the following:
filter
Object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {myNumber: [12,13]}}
means all events where "myNumber" is 12 or 13.
fromBlock
Number
(optional) The block number from which to get events on.
topics
Array
(optional) This allows to manually set the topics for the event filter. If given the filter property and event signature, topic[0]
will not be set automatically.
Return Value
EventEmitter
: 이벤트 이미터는 다음 이벤트를 가집니다:
data
Object
이벤트 객체를 인수로 각 수신 이벤트를 발생합니다.
error
Object
구독 오류가 발생하면 발생합니다.
반환된 이벤트 Object
의 구조는 다음과 같습니다:
event
String
이벤트 이름.
signature
String \
null
address
String
이 이벤트가 발생한 주소.
returnValues
Object
The return values coming from the event, e.g., {myVar: 1, myVar2: '0x234...'}
.
logIndex
Number
블록에서 이벤트 인덱스 위치의 정수값.
transactionIndex
Number
이벤트가 생성된 트랜잭션의 인덱스 위치의 정수값.
transactionHash
32-byte String
이 이벤트가 생성된 블록의 해시. 아직 보류 중인 경우 null
.
blockHash
32-byte String
Hash of the block this event was created in. null
when it is still pending.
blockNumber
Number
이 로그가 생성된 블록 번호. 아직 보류 중인 경우 null
.
raw.data
String
색인화되지 않은 로그 매개변수를 포함하는 데이터.
raw.topics
Array
최대 4개의 32바이트 주제를 가진 배열, 주제 1-3은 이벤트의 색인화된 매개변수가 포함됩니다.
id
String
로그 식별자. It is made through concatenating "log_" string with keccak256(blockHash + transactionHash + logIndex).substr(0, 8)
Example
events.allEvents
Same as events but receives all events from this smart contract. 선택적으로 filter 속성은 해당 이벤트를 필터링할 수 있습니다.
getPastEvents
이 컨트랙트의 이전 이벤트를 가져옵니다.
Parameters
event
String
The name of the event in the contract, or "allEvents"
to get all events.
options
Object
(optional) The options used for deployment. See the table below for the details.
callback
Function
(optional) This callback will be fired with an array of event logs as the second argument, or an error as the first argument.
옵션 개체에는 다음이 포함됩니다:
filter
Object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {myNumber: [12,13]}}
means all events where "myNumber" is 12 or 13.
fromBlock
Number
(optional) The block number from which to get events on.
toBlock
Number
(optional) The block number to get events up to (defaults to "latest"
).
topics
Array
(optional) This allows manually setting the topics for the event filter. If given the filter property and event signature, topic[0]
will not be set automatically.
Return Value
Promise
returns Array
: An array with the past event objects, matching the given event name and filter.
Example
Last updated