Skip to content

Commit

Permalink
Remove redundant withVoucher extrinsics (#1353)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Aug 21, 2023
1 parent 5bed6c3 commit ef4aca6
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 277 deletions.
10 changes: 10 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## master

_08/21/2023_

### Changes

https://github.com/gear-tech/gear-js/pull/1353
- Remove `sendMessageWithVoucher` and `sendReplyWithVoucher` methods according to https://github.com/gear-tech/gear/pull/3083.
- Add optional `prepaid` and `account` fields to `api.message.send` and `api.message.sendReply` method arguments. They are used to send messages with the issued voucher.

## 0.32.4

_07/25/2023_
Expand Down
44 changes: 41 additions & 3 deletions api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ try {
value: 1000,
};
// In that case payload will be encoded using meta.handle_input type
let extrinsic = gearApi.message.send(message, meta);
let extrinsic = await gearApi.message.send(message, meta);
// So if you want to use another type you can specify it
extrinsic = gearApi.message.send(message, meta, meta.async_handle_input);
extrinsic = await gearApi.message.send(message, meta, meta.async_handle_input);
} catch (error) {
console.error(`${error.name}: ${error.message}`);
}
Expand All @@ -240,7 +240,7 @@ const reply = {
gasLimit: 10000000,
value: 1000,
};
const extrinsic = gearApi.message.sendReply(reply, meta);
const extrinsic = await gearApi.message.sendReply(reply, meta);
await extrinsic(keyring, (events) => {
console.log(event.toHuman());
});
Expand Down Expand Up @@ -332,6 +332,44 @@ const tx = api.program.resumeSession.commit({ sessionId, blockCount: 20_000 });
tx.signAndSend(account);
```


### 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 that the voucher exists for a particular user and program
The `api.voucher.exists` method returns a boolean value indicates whether the voucher exists or not.
```javascript
const voucherExists = await api.voucher.exists(programId, accountId)
```

#### Send message and reply with the issued voucher
To send message with voucher all you need to do is to set `prepaid` flag to `true` in the first argument of `api.message.send` and `api.message.sendReply` methods. Also it's good to specify account ID that is used to send the extrinsic to check whether the voucher exists or not.
```javascript
let extrinsic = await api.message.send({
destination: destination,
payload: somePayload,
gasLimit: 10000000,
value: 1000,
prepaid: true,
account: accountId,
}, meta);
```


#### Send message and reply with issued voucher

## Work with programs and blockchain state

### Check that the address belongs to some program
Expand Down
Loading

0 comments on commit ef4aca6

Please sign in to comment.