## Gear 智能合约 -- Vara Network ### 使用 Gear JS 预估交易 Gas 费 1. Init (for upload_program extrinsic) 2. Init (for create_program extrinsic) 3. Handle 4. Reply to a message --- ## Calculate gas Gear nodes charge gas fees for all network operations, whether that be executing a program’s code or processing a message. This gas is paid for by the initiator of these actions. They guarantee successful message processing and to avoid errors like `Gaslimit exceeded`, you can simulate the execution in advance to calculate the exact amount of gas that will be consumed. --- ## Calculate gas for messages To find out the minimum gas amount required to send extrinsic, use `api.program.calculateGas.[method]`. Depending on the conditions, you can calculate gas for initializing a program or processing a message in `handle()` or `reply()`. :::info Gas calculation returns the GasInfo object, which contains 5 parameters: - `min_limit` - minimum gas limit required for the execution - `reserved` - gas amount that will be reserved for other on-chain interactions - `burned` - number of gas burned during message processing - `may_be_returned` - value that can be returned in some cases - `waited` - notifies that the message will be added to the waitlist --- ### Init (for upload_program extrinsic) ```javascript const code = fs.readFileSync('demo_ping.opt.wasm'); const gas = await api.program.calculateGas.initUpload( '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', // source id code, '0x00', // payload 0, // value true, // allow other panics ); console.log(gas.toHuman()); ``` --- ### Init (for create_program extrinsic) ```javascript const codeId = '0x…'; const gas = await api.program.calculateGas.initCreate( '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', // source id codeId, '0x00', // payload 0, // value true, // allow other panics ); console.log(gas.toHuman()); ``` --- ### Handle ```javascript import { getProgramMetadata } from '@gear-js/api'; const metadata = await getProgramMetadata('0x' + fs.readFileSync('demo_new_meta.meta.txt')); const gas = await api.program.calculateGas.handle( '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', // source id '0xa178362715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', // program id { id: { decimal: 64, hex: '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', }, }, // payload 0, // value false, // allow other panics metadata, ); console.log(gas.toHuman()); ``` --- ### Reply to a message ```javascript import { getProgramMetadata } from '@gear-js/api'; const metadata = await getProgramMetadata('0x' + fs.readFileSync('demo_async.meta.txt')); const gas = await api.program.calculateGas.reply( '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', // source id '0x518e6bc03d274aadb3454f566f634bc2b6aef9ae6faeb832c18ae8300fd72635', // message id 'PONG', // payload 0, // value true, // allow other panics metadata, ); console.log(gas.toHuman()); ``` --- ## References https://github.com/gear-tech/gear-js/blob/563148c8bd20360f95af71b24943449bb2e5b6f7/api/src/default/types-program.json#L44 https://github.com/gear-tech/gear-js/blob/563148c8bd20360f95af71b24943449bb2e5b6f7/idea/frontend/src/hooks/useGasCalculate/helpers.ts#L11 https://github.com/gear-tech/gear/tree/master/node/authorship https://wiki.gear-tech.io/docs/api/calculate-gas/ --- # Questions <img src="https://hackmd.io/_uploads/SyK8P72d2.jpg" width="240" alt="GEAR"> <img src="https://hackmd.io/_uploads/By_kgE3_2.png" width="240" alt="VARA"> 课后答疑频道: https://t.me/Gear_CN
{"title":"使用 Gear JS 预估交易 Gas 费","slideOptions":"{\"theme\":\"dark\",\"spotlight\":{\"enabled\":false},\"width\":1600,\"height\":900}","description":"- Init (for upload_program extrinsic)\n- Init (for create_program extrinsic)\n- Handle\n- Reply to a message","contributors":"[{\"id\":\"94262fbf-81ae-4ed7-933c-561a41bd977a\",\"add\":4212,\"del\":8}]"}
    163 views