caver.contract
caver.contract
オブジェクトは、kaiaブロックチェーンプラットフォーム上のスマートコントラクトとのやり取りを容易にする。 新しいコントラクト・オブジェクトを作成する際、そのスマート・コントラクトのJSONインターフェースを提供する必要がある。caver-jsは、javascriptでコントラクト・オブジェクトを使用するすべての呼び出しを、RPCを介した低レベルABI呼び出しに自動的に変換する。
これにより、スマート・コントラクトをJavaScriptのオブジェクトのように扱うことができる。
caver.contract.create
caver.contract.create(jsonInterface [, address] [, options])
JSON インターフェース・オブジェクトに定義されたすべてのメソッドとイベントを持つ、新しいコントラクト・インスタンスを作成します。 この関数は、new caver.contract と同じ働きをする。
NOTE caver.contract.create
は、caver-js v1.6.1からサポートされています。
パラメーター
new caver.contract](#new-contract) を参照。
リターン・バリュー
new caver.contract](#new-contract) を参照。
例
const contract = caver.contract.create([ { constant: true, inputs: [{ name: 'interfaceId', type: 'bytes4' }], name: 'supportsInterface', outputs: [{ name: '', type: 'bool' }], payable: false, stateMutability: 'view', type: 'function', }, ... ], '0x{address in hex}')
caver.contract
new caver.contract(jsonInterface [, address] [, options])
JSON インターフェース・オブジェクトに定義されたすべてのメソッドとイベントを持つ、新しいコントラクト・インスタンスを作成します。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
jsonInterface | オブジェクト | インスタンス化するコントラクトのJSONインターフェース |
住所 | ストリング | (オプション) 呼び出すスマート・コントラクトのアドレス。 myContract.options.address='0x1234...'`を使って後で追加することができる。 |
オプション | オブジェクト | (オプション)契約のオプション。 詳細は以下の表を参照のこと。 |
オプション・オブジェクトには以下のものが含まれる:
名称 | タイプ | 説明 |
---|---|---|
より | ストリング | (オプション) 取引を行うアドレス。 |
ガス価格 | ストリング | (オプション)取引に使用するガス価格(peb)。 |
ガス | 番号 | (オプション) 取引に提供されるガスの最大値(ガスリミット)。 |
データ | ストリング | (オプション)契約のバイトコード。 契約が配備されるときに使用される。 |
フィーデレゲーション | ブーリアン | (オプション) 手数料委任取引を使用するかどうか。 |
料金支払者 | ストリング | (オプション)トランザクションFeeを支払方のアドレス。 feeDelegation が true のとき、その値はトランザクションの feePayer フィールドに設定される。 |
手数料率 | ストリング | (任意)手数料支払者が負担する取引手数料の比率。 feeDelegation が true で、feeRatio に有効な値が設定されている場合、部分的な料金委譲トランザクショ ンが使用される。 有効範囲は1~99。 0や100以上の比率は許されない。 |
リターン・バリュー
タイプ | 説明 |
---|---|
オブジェクト | すべてのメソッドとイベントを持つ契約インスタンス。 |
例
const myContract = new caver.contract([...], '0x{address in hex}', { gasPrice: '25000000000' })
myContract.options
myContract.options
契約インスタンスの options
オブジェクト。 from
、gas
、gasPrice
、feePayer
および feeRatio
は、トランザクションを送信する際のフォールバック値として使用される。
プロパティ
名称 | タイプ | 説明 |
---|---|---|
住所 | ストリング | 契約が展開されている住所。 |
jsonInterface | 配列 | 契約の JSON インターフェース。 |
より | ストリング | 契約展開/実行トランザクションが送信されるデフォルトのアドレス。 トランザクションの作成時に from アドレスが定義されていない場合、この myContract.options.from がトランザクションの作成に常に使用される。 |
ガス価格 | ストリング | 取引に使用するペブでのガス料金。 |
ガス | 番号 | 取引に提供されるガスの上限(ガスリミット)。 |
データ | ストリング | 契約のバイトコード。 契約が配備されるときに使用される。 |
フィーデレゲーション | ブーリアン | (オプション) 手数料委任取引を使用するかどうか。 |
料金支払者 | ストリング | (オプション)取引手数料を支払う手数料支払人の住所。 feeDelegation が true のとき、その値はトランザクションの feePayer フィールドに設定される。 |
手数料率 | ストリング | (任意)手数料支払者が負担する取引手数料の比率。 feeDelegation が true で、feeRatio に有効な値が設定されている場合、部分的な料金委譲トランザクショ ンが使用される。 有効範囲は1~99。 0や100以上の比率は許されない。 |
NOTE feeDelegation
、feePayer
、feeRatio
は caver-js v1.6.1 からサポートされています。
例
> myContract.options{ address: [Getter/Setter], jsonInterface: [Getter/Setter], from: [Getter/Setter], feePayer: [Getter/Setter], feeDelegation: [Getter/Setter], feeRatio: [Getter/Setter], gasPrice: [Getter/Setter], gas: [Getter/Setter], data: [Getter/Setter]}> myContract.options.from = '0x1234567890123456789012345678901234567891' // default from address> myContract.options.gasPrice = '25000000000000' // default gas price in peb> myContract.options.gas = 5000000 // provide as fallback always 5M gas> myContract.options.feeDelegation = true // use fee delegation transaction> myContract.options.feePayer = '0x1234567890123456789012345678901234567891' // default fee payer address> myContract.options.feeRatio = 20 // default fee ratio when send partial fee delegation transaction
myContract.options.address
myContract.options.アドレス
この契約インスタンス myContract
に使用されるアドレス。 この契約からcaver-jsによって生成されるすべてのトランザクションは、このアドレスをトランザクションのto
として含む。
プロパティ
名称 | タイプ | 説明 |
---|---|---|
住所 | string \ | このコントラクトのアドレス。まだ設定されていない場合は null 。 |
例
> myContract.options.address'0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'// 契約アドレスを設定> myContract.options.address = '0x1234FFDD...'
myContract.options.jsonInterface
myContract.options.jsonInterface
この契約の ABI myContract
から派生した JSON インターフェースオブジェクト。
プロパティ
名称 | タイプ | 説明 |
---|---|---|
jsonInterface | 配列 | この契約の JSON インターフェース。 これを再設定すると、コントラクト・インスタンスのメソッドとイベントが再生成される。 |
例
> myContract.options.jsonInterface[ { constant: true, inputs: [ { name: 'interfaceId', type: 'bytes4' } ], name: 'supportsInterface', outputs: [ { name: '', type: 'bool' } ], payable: false, stateMutability: 'view', type: 'function', signature: '0x01ffc9a7', }, ... { anonymous: false, inputs: [ { indexed: true, name: 'owner', type: 'address' }, { indexed: true, name: 'spender', type: 'address' }, { indexed: false, name: 'value', type: 'uint256' } ], name: 'Approval', type: 'event', signature: '0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925', },]// set a new jsonInterface> myContract.options.jsonInterface = [...]
myContract.clone
myContract.clone([contractAddress])
現在の契約インスタンスをクローンします。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
契 約住所 | ストリング | (オプション)新しい契約の住所。 省略された場合、元のインスタンスのアドレスが設定される(例:myContract.options.address )。 |
リターン・バリュー
タイプ | 説明 |
---|---|
オブジェクト | クローン化された新しい契約インスタンス。 |
例
> myContract.clone()Contract { currentProvider:[Getter/Setter], ... _keyrings:KeyringContainer { ... }}.
myContract.deploy
myContract.deploy(options, byteCode [, param1 [, param2 [, ...]])
契約をkaiaネットワークに展開する。 デプロイに成功すると、プロミス は新しいコントラクト・インスタンスで解決される。 既存のmyContract.deploy関数の使い勝手とは異なり、この関数はkaiaネットワークに直接トランザクションを送信する。 返されたオブジェクトで send()
を呼び出す必要はない。
NOTE caver.wallet
に署名を行うには、options
またはmyContract.options
のfrom
とfeePayer
に対応するキーリングのインスタンスが含まれていなければならない。
NOTE myContract.deploy
は caver-js v1.6.1 からサポートされています。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | 送信に使用するオプション。 詳細はmethods.methodName.sendの表を参照のこと。 |
バイトコード | ストリング | 契約のバイトコード。 |
パラメーター | ミックス | (オプション) デプロイ時にコンストラクタに渡されるパラメータ。 |
リターン・バリュー
Promise
は PromiEvent
を返す:プロミスは新しいコントラクトのインスタンスで解決される。
タイプ | 説明 |
---|---|
プロミイベント | プロミスを組み合わせたイベント・エミッター。 取引レシートが利用可能になれば解決する。 もし send() が myContract.deploy() から呼ばれた場合、プロミスは新しいコントラクトのイ ンスタンスで解決されます。 |
PromiEventでは、以下のイベントが利用可能です:
- トランザクションハッシュ
transactionHash
: トランザクションが送信され、トランザクションハッシュが利用可能になった直後に発生する。 型はstring
である。 receipt
:トランザクションのレシートが利用可能になったときに発生する。 詳細については、caver.rpc.klay.getTransactionReceipt を参照してください。 型はobject
である。error
:送信中にエラーが発生した場合に発生する。 ガス欠エラーの場合、2番目のパラメータはレシートとなる。 その型はError
である。
例
// Deploy a smart contract without constructor arguments> myContract.deploy({ from: '0x{address in hex}', gas: 1500000, }, '0x{byte code}') .on('error', function(error) { ... }) .on('transactionHash', function(transactionHash) { ... }) .on('receipt', function(receipt) { console.log(receipt.contractAddress) // contains the new contract address }) .then(function(newContractInstance) { console.log(newContractInstance.options.address) // instance with the new contract address })// Deploy a smart contract with constructor arguments> myContract.deploy({ from: '0x{address in hex}', gas: 1500000, }, '0x{byte code}', 'keyString', ...) .on('error', function(error) { ... }) .on('transactionHash', function(transactionHash) { ... }) .on('receipt', function(receipt) { console.log(receipt.contractAddress) }) .then(function(newContractInstance) { console.log(newContractInstance.options.address) })// Deploy a smart contract with fee delegation transaction (TxTypeFeeDelegatedSmartContractDeploy)> myContract.deploy({ from: '0x{address in hex}', feeDelegation: true, feePayer: '0x{address in hex}', gas: 1500000, }, '0x{byte code}') .on('error', function(error) { ... }) .on('transactionHash', function(transactionHash) { ... }) .on('receipt', function(receipt) { console.log(receipt.contractAddress) }) .then(function(newContractInstance) { console.log(newContractInstance.options.address) })// Deploy a smart contract with partial fee delegation transaction (TxTypeFeeDelegatedSmartContractDeployWithRatio)> myContract.deploy({ from: '0x{address in hex}', feeDelegation: true, feePayer: '0x{address in hex}', feeRatio: 30, gas: 1500000, }, '0x{byte code}') .on('error', function(error) { ... }) .on('transactionHash', function(transactionHash) { ... }) .on('receipt', function(receipt) { console.log(receipt.contractAddress) }) .then(function(newContractInstance) { console.log(newContractInstance.options.address) })
myContract.deploy
myContract.deploy(options)
スマートコントラクトをkaiaにデプロイする際に使用したオブジェクトを返します。 スマートコントラクトのデプロイ・トランザクションは、myContract.deploy({ data, arguments }).send(options)
を呼び出すことで送信できる。 デプロイに成功すると、プロミスは新しいコントラクト・インスタンスで解決される。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | 展開に使用するオプションオブジェクト。 以下の表を参照してください。 |
オプション・オブジェクトには、以下のものを含めることができる:
名称 | タイプ | 説明 |
---|---|---|
データ | ストリング | 契約のバイトコード。 |
引数 | 配列 | (オプション) デプロイ時にコンストラクタに渡される引数。 |
リターン・バリュー
タイプ | 説明 |
---|---|
オブ ジェクト | 契約配布のための引数や関数が定義されたオブジェクト。 以下の表を参照してください。 |
このオブジェクトには以下が含まれる:
名称 | タイプ | 説明 |
---|---|---|
引数 | 配列 | options.arguments に渡された引数。 |
送信 | 機能 | カイアにコントラクトをデプロイする関数。 この関数の結果としてのプロミスは、新しいコントラクト・インスタンスで解決される。 |
サイン | 機能 | 送信者としてスマートコントラクトのデプロイ取引に署名する関数。 sign関数は署名されたトランザクションを返す。 |
SignAsFeePayer | 機能 | スマートコントラクトのデプロイ取引に手数料支払者として署名する機能。 signAsFeePayer 関数は署名されたトランザクションを返す。 |
推定ガス | 機能 | 配備に使用されるガスを見積もる関数。 この機能を実行しても、契約は破棄されない。 |
encodeABI | 機能 | デプロイのABIをエンコードする関数で、コントラクトデータ+コンストラクタのパラメータとなる。 この機能を実行しても、契約は破棄されない。 |
注意 myContract.deploy({ data, arguments }).sign(options)
と myContract.deploy({ data, arguments }).signAsFeePayer(options)
は caver-js v1.6.1 以 降でサポートされています。
例
> myContract.deploy({ data: '0x12345...', arguments: [123, 'My string'] }) .send({ from: '0x1234567890123456789012345678901234567891', gas: 1500000, value: 0, }, function(error, transactionHash) { ... }) .on('error', function(error) { ... }) .on('transactionHash', function(transactionHash) { ... }) .on('receipt', function(receipt) { console.log(receipt.contractAddress) // contains the new contract address }) .then(function(newContractInstance) { console.log(newContractInstance.options.address) // instance with the new contract address })// When the data is already set as an option to the contract itself> myContract.options.data = '0x12345...'> myContract.deploy({ arguments: [123, 'My string'] }) .send({ from: '0x1234567890123456789012345678901234567891', gas: 1500000, value: 0, }) .then(function(newContractInstance) { console.log(newContractInstance.options.address) // instance with the new contract address })// Simply encoding> myContract.deploy({ data: '0x12345...', arguments: [123, 'My string'] }) .encodeABI()'0x12345...0000012345678765432'// Gas estimation> myContract.deploy({ data: '0x12345...', arguments: [123, 'My string'] }) .estimateGas(function(err, gas) { console.log(gas) })
myContract.send
myContract.send(options, methodName [, param1 [, param2 [, ...]])
スマートコントラクトの機能を実行するためにトランザクションを提出する。 これにより、スマート・コントラクトの状態を変更することができる。
この関数で使用されるトランザクションタイプは、options
または myContract.options
で定義された値に依存する。 myContract.send
で手数料を委譲したトランザクションを使用したい場合は、feeDelegation
とfeePayer
を適切に設定する必要がある。
feeDelegation
が定義されていないか、false
に定義されています:SmartContractExecutionfeeDelegation
はtrue
に定義されているが、feePayer
は定義されていない : エラーをスローする。feeDelegation
はtrue
に定義され、feePayer
は定義されているが、feeRatio
は定義されていない:FeeDelegatedSmartContractExecutionfeeDelegation
をtrue
に定義し、feePayer
とfeeRatio
を定義する:FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
に署名を行うには、options
またはmyContract.options
のfrom
とfeePayer
に対応するキーリングのインスタンスが含まれていなければならない。
NOTE myContract.send
は caver-js v1.6.1 以降でサポートされています。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | 送信に使用するオプション。 詳細はmethods.methodName.sendの表を参照のこと。 |
メソッド名 | ストリング | 実行するコントラクト関数のメソッド名。 |
パラメーター | ミックス | (オプション) スマートコントラクト関数に渡されるパラメータ。 |
リターン・バリュー
Promise
は PromiEvent
を返す。
タイプ | 説明 |
---|---|
プロミイベント | プロミスを組み合わせたイベント・エミッター。 取引レシートが利用可能になれば解決する。 約束は新しい契約インスタンスで解決される。 |
PromiEventでは、以下のイベントが利用可能です:
transactionHash
:トランザクションが送信され、トランザクションハッシュが利用可能になった直後に発生する。 型はstring
である。receipt
:トランザクションのレシートが利用可能になったときに発生する。 詳細については、caver.rpc.klay.getTransactionReceipt を参照してください。 その型はobject
である。error
:送信中にエラーが発生 した場合に発生する。 ガス欠エラーの場合、2番目のパラメータはレシートとなる。 その型はError
である。
例
// Send a SmartContractExecution and use the promise> myContract.send({ from: '0x{address in hex}', gas: 1000000 }, 'methodName', 123).then(console.log){ blockHash: '0x294202dcd1d3c422880e2a209b9cd70ce7036300536c78ab74130c5717cb90da', blockNumber: 16342, contractAddress: null, from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', gas: '0xf4240', gasPrice: '0x5d21dba00', gasUsed: 47411, input: '0x983b2...', logsBloom: '0x00800...', nonce: '0x1cd', senderTxHash: '0xe3f50d2bab2c462ef99379860d2b634d85a0c9fba4e2b189daf1d96bd4bbf8ff', signatures: [ { V: '0x4e43', R: '0x2ba27...', S: '0x50d37...' } ], status: true, to: '0x361870b50834a6afc3358e81a3f7f1b1eb9c7e55', transactionHash: '0xe3f50d2bab2c462ef99379860d2b634d85a0c9fba4e2b189daf1d96bd4bbf8ff', transactionIndex: 0, type: 'TxTypeSmartContractExecution', typeInt: 48, value: '0x0', events: {...}}// Send a SmartContractExecution and use the event emitter> myContract.send({ from: '0x{address in hex}', gas: 1000000 }, 'methodName', 123) .on('transactionHash', function(hash) { ... }) .on('receipt', function(receipt) { console.log(receipt) }) .on('error', console.error)// Send a FeeDelegatedSmartContractExecution> myContract.send({ from: '0x{address in hex}', gas: 1000000, feeDelegation: true, feePayer: '0x{address in hex}', }, 'methodName', 123).then(console.log){ blockHash: '0x149e36f279577c306fccb9779a0274e802501c32f7054c951f592778bd5c168a', blockNumber: 16458, contractAddress: null, feePayer: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', feePayerSignatures: [ { V: '0x4e43', R: '0x48c28...', S: '0x18413...' } ], from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', gas: '0xf4240', gasPrice: '0x5d21dba00', gasUsed: 57411, input: '0x983b2d5600000000000000000000000022bb89bd35e7b12bd25bea4165cf0f9330032f8c', logsBloom: '0x00800...', nonce: '0x1f5', senderTxHash: '0x5b06ca5046229e066c11dfc0c74fcbc98509294370981f9b142378a8f2bd5fe8', signatures: [ { V: '0x4e44', R: '0xfb707...', S: '0x641c6...' } ], status: true, to: '0x361870b50834a6afc3358e81a3f7f1b1eb9c7e55', transactionHash: '0x0e04be479ad06ec87acbf49abd44f16a56390c736f0a7354860ebc7fc0f92e13', transactionIndex: 1, type: 'TxTypeFeeDelegatedSmartContractExecution', typeInt: 49, value: '0x0', events: {...}}// Send a FeeDelegatedSmartContractExecutionWithRatio> myContract.send({ from: '0x{address in hex}', gas: 1000000, feeDelegation: true, feePayer: '0x{address in hex}', feeRatio: 30, }, 'methodName', 123).then(console.log){ blockHash: '0x8f0a0137cf7e0fea503c818910140246437db36121871bc54b2ebc688873b3f3', blockNumber: 16539, contractAddress: null, feePayer: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', feePayerSignatures: [ { V: '0x4e43', R: '0x80db0...', S: '0xf8c7c...' } ], feeRatio: '0x1e', from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', gas: '0xf4240', gasPrice: '0x5d21dba00', gasUsed: 62411, input: '0x983b2d560000000000000000000000007ad1a538041fa3ba1a721f87203cb1a3822b8eaa', logsBloom: '0x00800...', nonce: '0x219', senderTxHash: '0x14c7b674a0e253b31c85c7be8cbfe4bf9d86e66e940fcae34b854e25eab1ce15', signatures: [ { V: '0x4e43', R: '0xd57ef...', S: '0xe14f3...' } ], status: true, to: '0x361870b50834a6afc3358e81a3f7f1b1eb9c7e55', transactionHash: '0xfbf00ec189aeb0941d554384f1660ffdac7768b3af2bb1526bcb3983215c1183', transactionIndex: 0, type: 'TxTypeFeeDelegatedSmartContractExecutionWithRatio', typeInt: 50, value: '0x0', events: {...}}
myContract.sign
myContract.sign(options, methodName [, param1 [, param2 [, ...]])
スマートコントラクトを展開したり、スマートコントラクトの機能を実行したりするために、送信者としてスマートコントラクト取引に署名する。
}, 'constructor', byteCode, ...)`.
この関数で使用されるトランザクションタイプは、options
または myContract.options
で定義された値に依存する。 myContract.sign
で手数料を委譲したトランザクションを使用したい場合は、feeDelegation
を true
として定義する必要がある。
feeDelegation
が定義されていないか、false
に定義されています:SmartContractDeploy / SmartContractExecutionFeeDelegation
はtrue
に定義されているが、feeRatio
は定義されていない:FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionFeeDelegation
をtrue
に定義し、feeRatio
を定義する:FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio です。
NOTE caver.wallet
は、署名を行うために、options
またはmyContract.options
のfrom
に対応するキーリングのインスタンスを含んでいなければならない。
NOTE myContract.sign
はcaver-js v1.6.1からサポートされています。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | 送信に使用するオプション。 詳細はmethods.methodName.sendの表を参照のこと。 |
メソッド名 | ストリング | 実行するコントラクト関数のメソッド名。 スマート・コントラクトをデプロイするためのトランザクションに署名したい場合は、メソッド名の代わりに「コンストラクタ」文字列を使用する。 |
パラメーター | ミックス | (オプション) スマートコントラクト関数に渡されるパラメータ。 スマート・コントラクトをデプロイしたトランザクションに署名したい場合は、byteCodeとコンストラクタのパラメータを渡す。 |
リターン・バリュー
Promise
return Transaction - 署名されたスマートコントラクトのトランザクション。
例
// Sign a SmartContractDeploy> myContract.sign({ from: '0x{address in hex}', gas: 1000000 }, 'constructor', byteCode, 123).then(console.log)SmartContractDeploy { _type: 'TxTypeSmartContractDeploy', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x4e43', _r: '0xeb6b5...', _s: '0x5e4f9...' } ], _to: '0x', _value: '0x0', _input: '0x60806...', _humanReadable: false, _codeFormat: '0x0', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x2a5'}// Sign a FeeDelegatedSmartContractDeploy> myContract.sign({ from: '0x{address in hex}', feeDelegation: true, gas: 1000000 }, 'constructor', byteCode, 123).then(console.log)FeeDelegatedSmartContractDeploy { _type: 'TxTypeFeeDelegatedSmartContractDeploy', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x4e43', _r: '0xee0f5...', _s: '0x31cbf...' } ], _feePayer: '0x0000000000000000000000000000000000000000', _feePayerSignatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _to: '0x', _value: '0x0', _input: '0x60806...', _humanReadable: false, _codeFormat: '0x0', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x320'}// Sign a FeeDelegatedSmartContractDeployWithRatio> myContract.sign({ from: keyring.address, feeDelegation: true, feeRatio: 30, gas: 1000000 }, 'constructor', byteCode, 123).then(console.log)FeeDelegatedSmartContractDeployWithRatio { _type: 'TxTypeFeeDelegatedSmartContractDeployWithRatio', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x4e44', _r: '0x4c2b0...', _s: '0x47df8...' } ], _feePayer: '0x0000000000000000000000000000000000000000', _feePayerSignatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _feeRatio: '0x1e', _to: '0x', _value: '0x0', _input: '0x60806...', _humanReadable: false, _codeFormat: '0x0', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x306'}// Sign a SmartContractExecution> myContract.sign({ from: '0x{address in hex}', gas: 1000000 }, 'methodName', 123).then(console.log)SmartContractExecution { _type: 'TxTypeSmartContractExecution', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x4e44', _r: '0xb2846...', _s: '0x422c1...' } ], _to: '0x361870b50834a6afc3358e81a3f7f1b1eb9c7e55', _value: '0x0', _input: '0x983b2...', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x23b'}// Sign a FeeDelegatedSmartContractExecution> myContract.sign({ from: '0x{address in hex}', gas: 1000000, feeDelegation: true, }, 'methodName', 123).then(console.log)FeeDelegatedSmartContractExecution { _type: 'TxTypeFeeDelegatedSmartContractExecution', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x4e43', _r: '0xf7676...', _s: '0x42673...' } ], _feePayer: '0x0000000000000000000000000000000000000000', _feePayerSignatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _to: '0x361870b50834a6afc3358e81a3f7f1b1eb9c7e55', _value: '0x0', _input: '0x983b2...', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x254'}// Sign a FeeDelegatedSmartContractExecutionWithRatio> myContract.sign({ from: '0x{address in hex}', gas: 1000000, feeDelegation: true, feeRatio: 30, }, 'methodName', 123).then(console.log)FeeDelegatedSmartContractExecutionWithRatio { _type: 'TxTypeFeeDelegatedSmartContractExecutionWithRatio', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x4e44', _r: '0x58b06...', _s: '0x637ff...' } ], _feePayer: '0x0000000000000000000000000000000000000000', _feePayerSignatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _feeRatio: '0x1e', _to: '0x361870b50834a6afc3358e81a3f7f1b1eb9c7e55', _value: '0x0', _input: '0x983b2...', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x262'}
myContract.signAsFeePayer
myContract.signAsFeePayer(options, methodName [, param1 [, param2 [, ...]])
スマートコントラクトを展開する、またはスマートコントラクトの機能を実行するために、手数料の支払い者としてスマートコントラクト取引に署名する。
スマート・コントラクトがデプロイされている場合、'constructor' を methodName に入力することができる。例えば、myContract.signAsFeePayer({ from, feeDelegation: true, feePayer, .... }, 'constructor', byteCode, ...)
.
この関数で使用されるトランザクションタイプは、options
または myContract.options
で定義された値に依存する。 signAsFeePayer
は取引手数料の支払者として署名する関数なので、feeDelegation
フィールドは true
として定義しなければならない。 また、feePayer
フィールドに料金支払者のアドレスを定義しなければならない。
feeDelegation
が定義されていません : エラーをスローします。feeDelegation
は定義されているが、feePayer
は定義されていない : エラーをスローする。FeeDelegation
はtrue
に定義され、feePayer
は定義されているが、feeRatio
は定義されていない:FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionFeeDelegation
をtrue
に定義し、feePayer
とfeeRatio
を定義する:FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio.
NOTE caver.wallet
に署名を行うには、options
またはmyContract.options
のfeePayer
に対応するkeyringインスタンスが含まれていなければならない。
NOTE myContract.signAsFeePayer
は caver-js v1.6.1 以降でサポートされてい ます。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | 送信に使用するオプション。 詳細はmethods.methodName.sendの表を参照のこと。 |
メソッド名 | ストリング | 実行するコントラクト関数のメソッド名。 スマート・コントラクトをデプロイするためのトランザクションに署名したい場合は、メソッド名の代わりに「コンストラクタ」文字列を使用する。 |
パラメーター | ミックス | (オプション) スマートコントラクト関数に渡されるパラメータ。 スマート・コントラクトをデプロイしたトランザクションに署名したい場合は、byteCodeとコンストラクタのパラメータを渡す。 |
リターン・バリュー
Promise
return Transaction - 署名されたスマートコントラクトのトランザクション。
例
// Sign a FeeDelegatedSmartContractDeploy> myContract.signAsFeePayer({ from: '0x{address in hex}', feeDelegation: true, feePayer: '0x{address in hex}', gas: 1000000 }, 'constructor', byteCode, 123).then(console.log)FeeDelegatedSmartContractDeploy { _type: 'TxTypeFeeDelegatedSmartContractDeploy', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _feePayer: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _feePayerSignatures: [ SignatureData { _v: '0x4e43', _r: '0xe0641...', _s: '0x1d21e...' } ], _to: '0x', _value: '0x0', _input: '0x60806...', _humanReadable: false, _codeFormat: '0x0', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x32a'}// Sign a FeeDelegatedSmartContractDeployWithRatio> myContract.signAsFeePayer({ from: keyring.address, feeDelegation: true, feePayer: '0x{address in hex}', feeRatio: 30, gas: 1000000 }, 'constructor', byteCode, 123).then(console.log)FeeDelegatedSmartContractDeployWithRatio { _type: 'TxTypeFeeDelegatedSmartContractDeployWithRatio', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _feePayer: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _feePayerSignatures: [ SignatureData { _v: '0x4e44', _r: '0x307bd...', _s: '0x75110...' } ], _feeRatio: '0x1e', _to: '0x', _value: '0x0', _input: '0x60806...', _humanReadable: false, _codeFormat: '0x0', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x359'}// Sign a FeeDelegatedSmartContractExecution> myContract.signAsFeePayer({ from: '0x{address in hex}', gas: 1000000, feeDelegation: true, feePayer: '0x{address in hex}', }, 'methodName', 123).then(console.log)FeeDelegatedSmartContractExecution { _type: 'TxTypeFeeDelegatedSmartContractExecution', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _feePayer: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _feePayerSignatures: [ SignatureData { _v: '0x4e43', _r: '0xc58ba...', _s: '0x76fdb...' } ], _to: '0x4a9d979707aede18fa674711f3b2fe110fac4e7e', _value: '0x0', _input: '0x983b2...', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x36c'}// Sign a FeeDelegatedSmartContractExecutionWithRatio> myContract.signAsFeePayer({ from: '0x{address in hex}', gas: 1000000, feeDelegation: true, feePayer: '0x{address in hex}', feeRatio: 30, }, 'methodName', 123).then(console.log)FeeDelegatedSmartContractExecutionWithRatio { _type: 'TxTypeFeeDelegatedSmartContractExecutionWithRatio', _from: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _gas: '0xf4240', _signatures: [ SignatureData { _v: '0x01', _r: '0x', _s: '0x' } ], _feePayer: '0x69c3a6e3485446118d8081063dcef2e65b69ae91', _feePayerSignatures: [ SignatureData { _v: '0x4e44', _r: '0xeb78d...', _s: '0x2864d...' } ], _feeRatio: '0x1e', _to: '0x4a9d979707aede18fa674711f3b2fe110fac4e7e', _value: '0x0', _input: '0x983b2...', _chainId: '0x2710', _gasPrice: '0x5d21dba00', _nonce: '0x37b'}
myContract.call
myContract.call('methodName', [param1 [, param2 [, ...]])myContract.call(options, 'methodName', [param1 [, param2 [, ...]])
定数メソッドを呼び出し、そのスマートコントラクトメソッドをkaia仮想マシン内でトランザクションを送信せずに実行する。 呼び出しによってスマート・コントラクトの状態を変更することはできない。
NOTE myContract.call
はcaver-js v1.6.1からサポートされています。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | (オプション) 呼び出しに使用するオプション。 詳しくはmethods.methodName.callの表を参照のこと。 |
メソッド名 | ストリング | 呼び出す契約関数のメソッド名。 |
パラメーター | ミックス | (オプション) スマートコントラクト関数に渡されるパラメータ。 |
リターン・バリュー
Mixed
を返す Promise
- スマートコントラクトメソッドの戻り値。 単一の値を返す場合 は、そのまま返される。 複数の戻り値を持つ場合は、プロパティとインデックスを持つオブジェクトを返す。
例
> myContract.call('methodName').then(console.log)Jasmine> myContract.call({ from: '0x{address in hex}' }, 'methodName', 123).then(console.log)Test Result
myContract.decodeFunctionCall
myContract.decodeFunctionCall(functionCall)
関数コールをデコードし、パラメータを返す。
NOTE myContract.decodeFunctionCall
は caver-js v1.6.3 以降でサポートされています。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
ファンクションコール | ストリング | エンコードされた関数呼び出し文字列。 |
リターン・バリュー
タイプ | 説明 |
---|---|
オブジェクト | プレーンパラメータを含むオブジェクト。 result[0] は、パラメーターの順番で配列のようにアクセスできるように用意されているので、それを使うことができる。 |
例
// The myContract variable is instantiated with the below abi.// [// {// constant: true,// inputs: [{ name: 'key', type: 'string' }],// name: 'get',// outputs: [{ name: '', type: 'string' }],// payable: false,// stateMutability: 'view',// type: 'function',// },// {// constant: false,// inputs: [{ name: 'key', type: 'string' }, { name: 'value', type: 'string' }],// name: 'set',// outputs: [],// payable: false,// stateMutability: 'nonpayable',// type: 'function',// },// ]> myContract.decodeFunctionCall('0xe942b5160000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000036b65790000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000576616c7565000000000000000000000000000000000000000000000000000000')Result { '0': '2345675643', '1': 'Hello!%', __length__: 2, myNumber: '2345675643', mystring: 'Hello!%'}
myContract.methods
myContract.methods.methodName([param1 [, param2 [, ...]])myContract.methods['methodName']([param1 [, param2 [, ...]])
そのメソッドのトランザクションオブジェクトを作成し、そのオブジェクトを呼び出したり、送信したり、推定したり、ABIエンコードしたりすることができる。
このスマート・コントラクトのメソッドは、以下の方法で利用できる:
- メソッド名:
myContract.methods.methodName(123)
またはmyContract.methods[methodName](123)
- メソッドのプロトタイプ:myContract.methods'methodName(uint256)'`。
- メソッドのシグネチャ:
myContract.methods['0x58cf5f10'](123)
。
これにより、JavaScriptコントラクト・オブジェクトから、同じ名前で異なるパラメータを持つ関数を呼び 出すことができる。
cf)機能シグネチャ(機能セレクタ)
関数のシグネチャのKeccak-256(SHA-3)ハッシュの最初の(ビッグエンディアンの左 上位)4バイト。
関数のシグネチャは2つの異なる方法で指定できます。 caver.abi.encodefunctionSignature('funcName(paramType1,paramType2,...)')
2. caver.utils.sha3('funcName(paramType1,paramType2,...').substr(0, 10)`.
元)
caver.abi.encodefunctionSignature('メソッド名(uint256)')> 0x58cf5f10caver.utils.sha3('methodName(uint256)').substr(0, 10)> 0x58cf5f10
パラメーター
JSONインターフェイスで定義された、このスマート・コントラクトに属するメソッドのパラメータ。
リターン・バリュー
契約実行のための引数と関数が定義されたオブジェクト:
名称 | タイプ | 説明 |
---|---|---|
引数 | 配列 | このメソッドに渡される引数。 |
コール | 機能 | トランザクションを送信することなく(スマートコントラクトの状態を変更することはできません)、kaia仮想マシン上でスマートコントラクト内の定数メソッドを呼び出し、実行する関数。 |
送信 | 機能 | kaiaにトランザクションを送信し、そのメソッドを実行する関数(スマートコントラクトの状態を変更できる)。 |
サイン | 機能 | 送信者としてトランザクションに署名する関数。 sign関数は署名されたトランザクションを返す。 |
SignAsFeePayer | 機能 | 手数料支払い者として取引に署名する関数。 signAsFeePayer 関数は署名されたトランザクションを返す。 |
推定ガス | 機能 | その関数は、実行に使用されるガスを推定する。 |
encodeABI | 機能 | このメソッドのABIをエンコードする関数。 これは、トランザクションを使って送信したり、メソッドを呼び出したり、別のスマート・コントラクトのメソッドに引数として渡したりすることができる。 |
NOTE sign
と signAsFeePayer
は caver-js v1.6.1 からサポートされています。
例
// Calling a method> myContract.methods.methodName(123).call({ ... }, function(error, result) { ... })> myContract.methods.methodName(123).call({ ... }).then((result) => { ... })// Sending basic transaction and using the promise> myContract.methods.methodName(123).send({ from: '0x{address in hex}', ... }).then(function(receipt) { // receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()" })// Sending basic transaction and using the eventEmitter> myContract.methods.methodName(123).send({ from: '0x{address in hex}', ... }).on('transactionHash', function(hash) { ... }) .on('receipt', function(receipt) { ... }) .on('error', console.error)// Sending fee delegation transaction and using the promise> myContract.methods.methodName(123).send({ from: '0x{address in hex}', feePayer: '0x{fee-payer address}', feeDelegation: true,f ... }).then(function(receipt) { // receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()" })// Sending partial fee delegation transaction and using the promise> myContract.methods.methodName(123).send({ from: '0x{address in hex}', feePayer: '0x{fee-payer address}', feeDelegation: true, feeRatio: 30, ... }).then(function(receipt) { // receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()" })// sign the basic transaction> myContract.methods.methodName(123).sign({ from: '0x{address in hex}', feeDelegation: true, ... }).then(function(signedTx) { ... })// sign the fee delegation transaction> myContract.methods.methodName(123).sign({ from: '0x{address in hex}', feeDelegation: true, ... }).then(function(signedTx) { ... })// sign the partial fee delegation transaction> myContract.methods.methodName(123).sign({ from: '0x{address in hex}', feeDelegation: true, feeRatio: 30, ... }).then(function(signedTx) { ... })// sign the fee delegation transaction as a fee payer> myContract.methods.methodName(123).signAsFeePayer({ from: '0x{address in hex}', feePayer: '0x{fee-payer address}', feeDelegation: true, ... }).then(function(signedTx) { ... })// sign the partial fee delegation transaction as a fee payer> myContract.methods.methodName(123).signAsFeePayer({ from: '0x{address in hex}', feePayer: '0x{fee-payer address}', feeDelegation: true, feeRatio: 30, ... }).then(function(signedTx) { ... })
methods.methodName.call
myContract.methods.methodName([param1 [, param2 [, ...]]).call(options [, callback])myContract.methods['methodName']([param1 [, param2 [, ...]]).call(options [, callback])
定数メソッドを呼び出し、そのスマートコントラクトメソッドをkaia仮想マシン内でトランザクションを送信せずに実行する。 呼び出しによってスマート・コントラクトの状態を変更することはできない。 ショートカット関数として提供されているmyContract.callを使用することを推奨する。
パラメーター
名称 | タイプ | 説明 |
---|---|---|
オプション | オブジェクト | (オプション) 呼び出しに使用するオプション。 詳細は以下の表を参照のこと。 |
コールバック | 機能 | (オプション) このコールバックは、スマート・コントラクト・メソッドの実行結果を第2引数として、またはエラー・オブジェクトを第1引数として発行されます。 |
オプション・オブジェクトには、以下のものを含めることができる:
名称 | タイプ | 説明 |
---|---|---|
より | ストリング | (オプション) 契約メソッドを呼び出す際のアドレス。 |
ガス価格 | ストリング | (オプション)この呼び出しに使用するガス価格(peb)。 |
ガス | 番号 | (オプション) この呼に供給されるガスの最大値(ガスリミット)。 |
リターン・バリュー
Mixed
を返す Promise
- スマートコントラクトメソッドの戻り値。 単一の値を返す場合は、そのまま返される。 複数の戻り値を持つ場合は、プロパティとインデックスを持つオブジェクトを返す。
例
// using the promise> myContract.methods.methodName(123).call({from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'}) .then(function(result) { ... })
// Solidity:MULTIPLE RETURN VALUEScontract MyContract { function myFunction( public returns(uint256 myNumber, string memory myString) { return (23456, "Hello!%"); }.}
> var MyContract = new caver.contract(abi, address)> MyContract.methods.myfunction().call().then(console.log)Result { mynumber: '23456', mystring: 'Hello!%', 0: '23456', 1: 'Hello!%'}
// Solidity:SINGLE RETURN VALUEcontract MyContract { function myfunction( public returns(string memory mystring) { return "Hello!%"; }.}
> var MyContract = new caver.contract(abi, address)> MyContract.methods.myfunction().call().then(console.log)"Hello!%"
methods.methodName.send
myContract.methods.methodName([param1 [, param2 [, ...]]).send(options [, callback])myContract.methods['methodName']([param1 [, param2 [, ...]]).send(options [, callback])
スマートコントラクトをデプロイしたり、スマートコントラクトの機能を実行したりするためのトランザクションを送信する。 これにより、スマート・コントラクトの状態を変更することができる。 ショートカット関数として提供されているmyContract.sendを使用することを推奨する。
スマートコントラクトをデプロイする場合、methodName に 'constructor' を入力することができる。例えば myContract.methods.constructor
や myContract.methods['constructor']
などだが、myContract.deploy 関数を使用することを推奨する。
この関数で使用されるトランザクションタイプは、options
または myContract.options
で定義された値に依存する。 methods.methodName.send
を使用して手数料を委譲したトランザクションを使用したい場合は、feeDelegation
とfeePayer
を適切に設定する必要があります。
feeDelegation
が定義されていないか、またはfalse
に定義されています:SmartContractDeploy / SmartContractExecutionfeeDelegation
はtrue
に定義されているが、feePayer
は定義されていない : エラーをスローする。FeeDelegation
はtrue
に定義され、feePayer
は定義されているが、feeRatio
は定義されていない:FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionFeeDelegation
をtrue
に定義し、feePayer
とfeeRatio
を定義する:FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio.
NOTE caver.wallet
に署名を行うには、options
またはmyContract.options
のfrom
とfeePayer
に対応するキーリングのインスタンスが含まれていなければならない。
パラメーター
名称 |
---|