Skip to content

Commit 8c38ea1

Browse files
feat(sendTelegram): add allowDeposit parameter to enable automatic deposit
- Add allowDeposit optional parameter to SendTelegramParams type - Add allowDeposit parameter to sendTelegram function with default value false - Pass allowDeposit to matchOrders call - Add e2e tests for allowDeposit feature This feature allows users to automatically deposit funds when their balance is insufficient to cover the task cost.
1 parent b1354b9 commit 8c38ea1

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

src/web3telegram/sendTelegram.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const sendTelegram = async ({
5050
workerpoolMaxPrice = MAX_DESIRED_WORKERPOOL_ORDER_PRICE,
5151
protectedData,
5252
useVoucher = false,
53+
allowDeposit = false,
5354
}: IExecConsumer &
5455
SubgraphConsumer &
5556
DappAddressConsumer &
@@ -94,6 +95,9 @@ export const sendTelegram = async ({
9495
const vUseVoucher = booleanSchema()
9596
.label('useVoucher')
9697
.validateSync(useVoucher);
98+
const vAllowDeposit = booleanSchema()
99+
.label('allowDeposit')
100+
.validateSync(allowDeposit);
97101

98102
// Check protected data validity through subgraph
99103
const isValidProtectedData = await checkProtectedDataValidity(
@@ -286,7 +290,10 @@ export const sendTelegram = async ({
286290
workerpoolorder: workerpoolorder,
287291
requestorder: requestorder,
288292
},
289-
{ useVoucher: vUseVoucher }
293+
// TODO: Remove @ts-ignore once iexec SDK is updated to a version that includes allowDeposit in matchOrders types
294+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
295+
// @ts-ignore - allowDeposit is supported at runtime but not yet in TypeScript types
296+
{ useVoucher: vUseVoucher, allowDeposit: vAllowDeposit }
290297
);
291298
const taskId = await iexec.deal.computeTaskId(dealId, 0);
292299
return {

src/web3telegram/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export type SendTelegramParams = {
7777
appMaxPrice?: number;
7878
workerpoolMaxPrice?: number;
7979
useVoucher?: boolean;
80+
allowDeposit?: boolean;
8081
};
8182

8283
export type SendTelegramResponse = {

tests/e2e/sendTelegram.test.ts

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ describe('web3telegram.sendTelegram()', () => {
4646
const iexecOptions = getTestIExecOption();
4747
const prodWorkerpoolPublicPrice = 1000;
4848
const defaultConfig = getChainDefaultConfig(DEFAULT_CHAIN_ID);
49-
49+
const workerpoolprice = 1_000;
5050
beforeAll(async () => {
5151
// (default) prod workerpool (not free) always available
5252
await createAndPublishWorkerpoolOrder(
5353
TEST_CHAIN.prodWorkerpool,
5454
TEST_CHAIN.prodWorkerpoolOwnerWallet,
5555
NULL_ADDRESS,
56-
1_000,
56+
workerpoolprice,
5757
prodWorkerpoolPublicPrice
5858
);
5959
// learn prod pool (free) assumed always available
@@ -613,4 +613,75 @@ describe('web3telegram.sendTelegram()', () => {
613613
});
614614
});
615615
});
616+
617+
describe('allowDeposit', () => {
618+
let protectData: ProtectedDataWithSecretProps;
619+
consumerWallet = getRandomWallet();
620+
const dataPricePerAccess = 1000;
621+
let web3telegramConsumerInstance: IExecWeb3telegram;
622+
beforeAll(async () => {
623+
protectData = await dataProtector.protectData({
624+
data: { telegram_chatId: '12345' },
625+
name: 'test do not use',
626+
});
627+
await dataProtector.grantAccess({
628+
authorizedApp: getChainDefaultConfig(DEFAULT_CHAIN_ID).dappAddress,
629+
protectedData: protectData.address,
630+
authorizedUser: consumerWallet.address, // consumer wallet
631+
numberOfAccess: 1000,
632+
pricePerAccess: dataPricePerAccess,
633+
});
634+
await waitSubgraphIndexing();
635+
web3telegramConsumerInstance = new IExecWeb3telegram(
636+
...getTestConfig(consumerWallet.privateKey)
637+
);
638+
}, 2 * MAX_EXPECTED_BLOCKTIME);
639+
it(
640+
'should throw error if insufficient total balance to cover task cost and allowDeposit is false',
641+
async () => {
642+
let error;
643+
try {
644+
await web3telegramConsumerInstance.sendTelegram({
645+
telegramContent: 'e2e telegram content for test',
646+
protectedData: protectData.address,
647+
dataMaxPrice: dataPricePerAccess,
648+
workerpoolMaxPrice: workerpoolprice,
649+
allowDeposit: false,
650+
});
651+
} catch (err) {
652+
error = err;
653+
}
654+
expect(error).toBeInstanceOf(Web3TelegramWorkflowError);
655+
expect(error).toBeInstanceOf(Error);
656+
expect(error.message).toBe('Failed to sendTelegram');
657+
const causeMsg =
658+
error.errorCause?.message ||
659+
error.cause?.message ||
660+
error.cause ||
661+
error.errorCause;
662+
expect(causeMsg).toBe(
663+
`Cost per task (${
664+
dataPricePerAccess + workerpoolprice
665+
}) is greater than requester account stake (0). Orders can't be matched. If you are the requester, you should deposit to top up your account`
666+
);
667+
},
668+
3 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
669+
);
670+
it(
671+
'should send telegram after depositing sufficient funds to cover task cost when allowDeposit is true',
672+
async () => {
673+
const result = await web3telegramConsumerInstance.sendTelegram({
674+
telegramContent: 'e2e telegram content for test',
675+
protectedData: protectData.address,
676+
dataMaxPrice: dataPricePerAccess,
677+
workerpoolMaxPrice: workerpoolprice,
678+
allowDeposit: true,
679+
});
680+
expect(result).toBeDefined();
681+
expect(result).toHaveProperty('taskId');
682+
expect(result).toHaveProperty('dealId');
683+
},
684+
3 * MAX_EXPECTED_BLOCKTIME + MAX_EXPECTED_WEB2_SERVICES_TIME
685+
);
686+
});
616687
});

0 commit comments

Comments
 (0)