diff --git a/score/writing-score.md b/score/writing-score.md index 7a8dd83..cf7bc3d 100644 --- a/score/writing-score.md +++ b/score/writing-score.md @@ -387,279 +387,7 @@ When you handle exceptions in your contract, it is recommended to use `revert` f rather than using an `IconScoreException`. -## Deploying SCOREs -We assume that you already have installed T-Bears. If you have not yet installed it, please read [SCORE Quickstart](doc:score-quickstart) or [T-Bears Installation](doc:tbears-installation) first. - -### package.json - -Before deploying a SCORE, the required files should be packaged into a zip file. In the zip file, SCORE metadata should be included as well as the smart contract source code itself. -The metadata file, `package.json`, contains a version, main module name, and main class. - -Here is an example of `package.json` file. - -```json -{ - "version": "0.0.1", - "main_module": "hello_world", - "main_score": "HelloWorld" -} -``` - -The SCORE execution runtime will find `main_score` class in the `main_module`.py to load and execute it on the ICON network. - -If you want to make `main_module` point to a submodule, you can specify the module name as below. -```json -{ - "version": "0.0.1", - "main_module": "sub_module.hello_world", - "main_score": "HelloWorld" -} -``` - -### Deploying SCOREs with T-Bears - -Here is an example of deploying a SCORE onto the local T-Bears emulator. -If you want to deploy on Mainnet or Testnet, you need to edit `uri` and `keyStore` fields in `tbears_cli_config.json`. - -The [SampleToken](token-crowdsale) is used in this example. - -#### Deploy SampleToken on Local T-Bears Emulator - -1. Generate T-Bears CLI configuration. - ```console - $ tbears genconf - ``` - -2. Start T-Bears emulator on the local environment. - ```console - $ tbears start - ``` - -3. Open `tbears_cli_config.json` and edit it as follows. - Parameters for `on_install()` method (`_initialSupply` and `_decimals`) should be set under `scoreParams` field. - ```json - { - "uri": "http://127.0.0.1:9000/api/v3", - "nid": "0x3", - "keyStore": null, - "from": "hxe7af5fcfd8dfc67530a01a0e403882687528dfcb", - "to": "cx0000000000000000000000000000000000000000", - "deploy": { - "stepLimit": "0x10000000", - "mode": "install", - "scoreParams": { - "_initialSupply": "0x3e8", - "_decimals": "0x12" - } - }, - "txresult": {}, - "transfer": { - "stepLimit": "0xf4240" - } - } - ``` - -4. Deploy the SampleToken - Using `deploy` command in T-Bears, you can deploy the sample_token project with configuration `tbears_cli_config.json`. - T-Bears makes a zip file from the sample_token directory on the fly and deploys it to the local server. - ```console - $ tbears deploy sample_token -c tbears_cli_config.json - Send deploy request successfully. - If you want to check SCORE deployed successfully, execute txresult command - transaction hash: 0xea834af48150189b4021b9a161d4c0aff3d983ccc47ddc189bac50f55bf580b7 - ``` - -5. Get transaction result by transaction hash. - You can check the result of deployment by querying the transaction hash. - ```console - $ tbears txresult 0xea834af48150189b4021b9a161d4c0aff3d983ccc47ddc189bac50f55bf580b7 - ``` - Here's the result of the transaction. You can find the `scoreAddress`, which is the address of this deployed SCORE, and that address will be used for invoking external methods of SampleToken later. - ```consolie - Transaction result: { - "jsonrpc": "2.0", - "result": { - "txHash": "0xea834af48150189b4021b9a161d4c0aff3d983ccc47ddc189bac50f55bf580b7", - "blockHeight": "0xbe", - "blockHash": "0xdc85bccb32109da6cfe5bb5308121ce68a1494c499d9dd8c724a0bd7397d0729", - "txIndex": "0x0", - "to": "cx0000000000000000000000000000000000000000", - "scoreAddress": "cx0841205d73b93aec1062877d8d4d5ea54c6665bb", - "stepUsed": "0x2cc8f10", - "stepPrice": "0x0", - "cumulativeStepUsed": "0x2cc8f10", - "eventLogs": [], - "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - "status": "0x1" - }, - "id": 1 - } - ``` - -## Invoking SCORE methods - -### Invoking read-only methods - -The SCORE methods can be executed by calling [JSON RPC APIs](icon-json-rpc-v3) to ICON nodes. -You need to generate a JSON file which contains information about the calling method and its parameters. - -`icx_call` JSON-RPC API will be used to invoke a read-only method of SCORE. - -#### Getting total supply - -Make a JSON file for calling `totalSupply` in SampleToken as follows. - -```json -# totalsupply.json -{ - "jsonrpc": "2.0", - "method": "icx_call", - "id": 1, - "params": { - "to": "cx0841205d73b93aec1062877d8d4d5ea54c6665bb", - "dataType": "call", - "data": { - "method": "totalSupply" - } - } -} -``` - -You can use T-Bears `call` command to send the request. `tbears call` commands implements the `icx_call` JSON-RPC protocol. - -```console -$ tbears call totalsupply.json -response : { - "jsonrpc": "2.0", - "result": "0x3635c9adc5dea00000", - "id": 1 -} -``` - -#### Getting balance of specific address - -Make another JSON file for calling `balanceOf` in SampleToken. -In this case, you need to specify `_owner` parameter for the `balanceOf` method. - -```json -# balanceof.json -{ - "jsonrpc": "2.0", - "method": "icx_call", - "id": 1, - "params": { - "to": "cx0841205d73b93aec1062877d8d4d5ea54c6665bb", - "dataType": "call", - "data": { - "method": "balanceOf", - "params": { - "_owner": "hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6" - } - } - } -} -``` - -Again, you can use T-Bears `call` command to send the request. - -```console -$ tbears call balanceof.json -response : { - "jsonrpc": "2.0", - "result": "0x0", - "id": 1 -} -``` - -The result of calling `balanceOf` is 0, that means the balance of address `hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6` is 0. - - -### Invoking writable methods - -The JSON RPC method, `icx_sendTransaction`, should be used for invoking writable SCORE methods. - -The writable methods can change the state of the smart contract. So the signature should be presented in the JSON RPC request to prove the transaction was originated by the `from` account. The `to` is the address of the smart contract. - -The following example request is transferring 1 token to `hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6` from `hxe7af5fcfd8dfc67530a01a0e403882687528dfcb`. - -```json -# sendtoken.json -{ - "jsonrpc": "2.0", - "method": "icx_sendTransaction", - "params": { - "version": "0x3", - "from": "hxe7af5fcfd8dfc67530a01a0e403882687528dfcb", - "value": "0x0", - "stepLimit": "0x3000000", - "timestamp": "0x573117f1d6568", - "nid": "0x3", - "nonce": "0x1", - "to": "cx0841205d73b93aec1062877d8d4d5ea54c6665bb", - "signature": "", - "dataType": "call", - "data": { - "method": "transfer", - "params": { - "_to": "hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6", - "_value": "0xde0b6b3a7640000" - } - } - }, - "id": 1 -} -``` - -You can use T-Bears `sendtx` command for calling writable SCORE methods. -You need to input an appropriate password of the keystore file, `keystore_test1`. - -```console -$ tbears sendtx sendtoken.json -k keystore_test1 -Input your keystore password: -Send transaction request successfully. -transaction hash: 0x41bf7b9ada89eb938ee4e36fce02ec86b3e68c1ceefd61decfe1e3dcc7df43a5 -``` - -Getting the transaction hash itself in the response of sending transaction does not mean the successful execution of the transaction. -You need to check the actual result of the transaction by calling `icx_getTransactionResult` JSON RPC API. -With T-Bears, you can use `txresult` command. - -```console -$ tbears txresult 0x41bf7b9ada89eb938ee4e36fce02ec86b3e68c1ceefd61decfe1e3dcc7df43a5 -Transaction result: { - "jsonrpc": "2.0", - "result": { - "txHash": "0x41bf7b9ada89eb938ee4e36fce02ec86b3e68c1ceefd61decfe1e3dcc7df43a5", - "blockHeight": "0x4c", - "blockHash": "0x7f53737f9ead2a04afe72d90bef072d49fa74c1f37b4029801e787aa15d37895", - "txIndex": "0x0", - "to": "cx0841205d73b93aec1062877d8d4d5ea54c6665bb", - "stepUsed": "0xfdb2e", - "stepPrice": "0x0", - "cumulativeStepUsed": "0xfdb2e", - "eventLogs": [ - { - "scoreAddress": "cx0841205d73b93aec1062877d8d4d5ea54c6665bb", - "indexed": [ - "Transfer(Address,Address,int,bytes)", - "hxe7af5fcfd8dfc67530a01a0e403882687528dfcb", - "hxef73db5d0ad02eb1fadb37d0041be96bfa56d4e6", - "0xde0b6b3a7640000" - ], - "data": [ - "0x4e6f6e65" - ] - } - ], - "logsBloom": "0x00000000000000100000002000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000100000040000000000000000200000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000100000002000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000080000000000000000000000000000000000000000", - "status": "0x1" - }, - "id": 1 -} -``` - -The `status` field in the result indicates whether the transaction was succeeded or not (0x1 on success, 0x0 on failure). - - -## API Reference -[iconservice API references](doc:iconservice-api-references) +## Reference +- [iconservice API references](doc:iconservice-api-references) +- [Token & Crowdsale](doc:token-crowdsale) +- [T-Bears CLI Reference](doc:t-bears-cli-reference)