## Gear 智能合约 -- Vara Network ### 使用 Gas Vouchers API 实现 Payless Transactions 本 PPT 链接: https://hackmd.io/@btwiuse/vouchers 参考文档: https://wiki.gear-tech.io/docs/api/vouchers --- # Gas vouchers Vouchers, issued by any actor empower users with gas-free interactions, enabling them to send messages to specific programs seamlessly. An example of using vouchers is shown in the [Battleship](/examples/Gaming/battleship.md) game. Users without tokens on their balance can make moves by sending messages to a program using a voucher. --- ### Issue a voucher Use `api.voucher.issue` method to issue a new voucher for a user to be used to pay for sending messages to `program_id` program. ```javascript import { VoucherIssued } from '@gear-js/api'; const programId = '0x..'; const account = '0x...'; const tx = api.voucher.issue(account, programId, 10000); tx.signAndSend(account, (events) => { const voucherIssuedEvent = events.events.filter(({event: {method}}) => method === 'VoucherIssued') as VoucherIssued; console.log(voucherIssuedEvent.toJSON()); }) ``` --- ### Check voucher Use `api.voucher.exists` method to check that the voucher exists for a particular user and program: ```javascript const voucherExists = await api.voucher.exists(programId, accountId); ``` --- ### Send a message using voucher To send message with voucher you can use `api.voucher.call` method: ```javascript const messageTx = api.message.send({ destination: destination, payload: somePayload, gasLimit: 10000000, value: 1000 }, meta); const voucherTx = api.voucher.call({ SendMessage: messageTx }); await voucherTx.signAndSend(account, (events) => { console.log(events.toHuman()); }); ``` --- ### Send a reply using voucher Sending a reply with issued voucher works similar to sending message with voucher: ```javascript const messageTx = api.message.sendReply(...); const voucherTx = api.voucher.call({ SendReply: messageTx }); await voucherTx.signAndSend(account, (events) => { console.log(events.toHuman()); }); ``` --- 总结 - check voucher - send a message using voucher - send a reply using voucher 课后作业:使用 Gas Voucher API 向自定义用户地址发放免 Gas 券,并进行一次交易 https://github.com/gear-tech/gear/issues/3399 https://github.com/gear-tech/gear/pull/3606 --- # 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":"使用 Gas Vouchers API 实现 Payless Transactions","slideOptions":"{\"theme\":\"dark\",\"spotlight\":{\"enabled\":false},\"width\":1600,\"height\":900}","description":"本 PPT 链接: https://hackmd.io/@btwiuse/mailbox","contributors":"[{\"id\":\"94262fbf-81ae-4ed7-933c-561a41bd977a\",\"add\":6921,\"del\":4213}]"}
    332 views