Skip to content

Commit

Permalink
feat(api): voucher call for declining voucher (#1486)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Feb 12, 2024
1 parent 742419b commit f6f83ae
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
8 changes: 8 additions & 0 deletions api/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## 0.36.5

_02/12/2024_

### Changes
https://github.com/gear-tech/gear-js/pull/1486
- Add ability to decline voucher using voucher funds

## 0.36.4

_02/12/2024_
Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gear-js/api",
"version": "0.36.4",
"version": "0.36.5",
"description": "A JavaScript library that provides functionality to connect GEAR Component APIs.",
"main": "cjs/index.js",
"module": "index.js",
Expand Down
9 changes: 7 additions & 2 deletions api/src/Voucher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,17 @@ export class GearVoucher extends GearTransaction {
return this._api.tx.gearVoucher.call(voucherId, {
SendMessage: { destination, payload, gasLimit, value, keepAlive },
});
} else if ('SendReply' in params) {
}
if ('SendReply' in params) {
if (params.SendReply.method.method !== 'sendReply') {
throw new Error(`Invalid method name. Expected 'SendReply' but actual is ${params.SendReply.method.method}`);
}
const [replyToId, payload, gasLimit, value, keepAlive] = params.SendReply.args;
return this._api.tx.gearVoucher.call(voucherId, {
SendReply: { replyToId, payload, gasLimit, value, keepAlive },
});
} else if ('UploadCode' in params) {
}
if ('UploadCode' in params) {
if (params.UploadCode.method.method !== 'uploadCode') {
throw new Error(`Invalid method name. Expected 'UploadCode' but actual is ${params.UploadCode.method.method}`);
}
Expand All @@ -125,6 +127,9 @@ export class GearVoucher extends GearTransaction {
UploadCode: { code },
});
}
if ('DeclineVoucher' in params) {
return this._api.tx.gearVoucher.call(voucherId, { DeclineVoucher: null });
}

throw new Error('Invalid call params');
}
Expand Down
1 change: 1 addition & 0 deletions api/src/types/augment/augment-api-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ declare module '@polkadot/api-base/types/submittable' {
code: string | Uint8Array | Codec;
};
}
| { DeclineVoucher: null }
| string
| Uint8Array,
) => SubmittableExtrinsic<ApiType>,
Expand Down
3 changes: 2 additions & 1 deletion api/src/types/interfaces/voucher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { SubmittableExtrinsic } from '@polkadot/api/types';
export type ICallOptions =
| { SendMessage: SubmittableExtrinsic<'promise', ISubmittableResult> }
| { SendReply: SubmittableExtrinsic<'promise', ISubmittableResult> }
| { UploadCode: SubmittableExtrinsic<'promise', ISubmittableResult> };
| { UploadCode: SubmittableExtrinsic<'promise', ISubmittableResult> }
| { DeclineVoucher: null };

export interface IUpdateVoucherParams {
/**
Expand Down
3 changes: 2 additions & 1 deletion api/src/types/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,8 @@ export interface PalletGearVoucherInternalPrepaidCall extends Enum {
readonly asUploadCode: {
readonly code: Bytes;
} & Struct;
readonly type: 'SendMessage' | 'SendReply' | 'UploadCode';
readonly isDeclineVoucher: boolean;
readonly type: 'SendMessage' | 'SendReply' | 'UploadCode' | 'DeclineVoucher';
}

export interface PalletGearVoucherEvent extends Enum {
Expand Down
19 changes: 19 additions & 0 deletions api/test/Voucher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ describe('Voucher', () => {
expect(txData.spender.toHuman()).toBe(charlie.address);
});

test('Decline voucher with no funds', async () => {
const { extrinsic } = await api.voucher.issue(charlieRaw, 100e12, 1000, [programId], true);

const [txData] = await sendTransaction(extrinsic, alice, ['VoucherIssued']);

const voucherId = txData.voucherId.toHex();

const tx = api.voucher.call(voucherId, { DeclineVoucher: null });

const [txData2] = await sendTransaction(tx, charlie, ['VoucherDeclined']);

expect(txData2).toBeDefined();
expect(txData2).toHaveProperty('voucherId');
expect(txData2).toHaveProperty('spender');
expect(Object.keys(txData2.toJSON())).toHaveLength(2);
expect(txData2.voucherId.toHex()).toBe(voucherId);
expect(txData2.spender.toHuman()).toBe(charlie.address);
});

test('Revoke voucher', async () => {
expect(voucher).toBeDefined();
expect(validUpTo).toBeDefined();
Expand Down

0 comments on commit f6f83ae

Please sign in to comment.