diff --git a/README.md b/README.md index e01eceb..ee95dcb 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@

- + diff --git a/build/errors/index.d.ts b/build/errors/index.d.ts index 33d5184..0d78722 100644 --- a/build/errors/index.d.ts +++ b/build/errors/index.d.ts @@ -4,3 +4,4 @@ export declare const PROVIDER_REQUIRED = "provider required"; export declare const API_KEY_REQUIRED = "Api key required"; export declare const INVALID_API_KEY = "Api key is invalid"; export declare const INVALID_BICONOMY_KEY = "Biconomy key is invalid"; +export declare const TRANSACTION_FAILED = "Transaction failed"; diff --git a/build/errors/index.js b/build/errors/index.js index 11a1886..bad91af 100644 --- a/build/errors/index.js +++ b/build/errors/index.js @@ -14,10 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.INVALID_BICONOMY_KEY = exports.INVALID_API_KEY = exports.API_KEY_REQUIRED = exports.PROVIDER_REQUIRED = exports.OWNER_REQUIRED = void 0; +exports.TRANSACTION_FAILED = exports.INVALID_BICONOMY_KEY = exports.INVALID_API_KEY = exports.API_KEY_REQUIRED = exports.PROVIDER_REQUIRED = exports.OWNER_REQUIRED = void 0; __exportStar(require("./vendor"), exports); exports.OWNER_REQUIRED = 'owner required'; exports.PROVIDER_REQUIRED = 'provider required'; exports.API_KEY_REQUIRED = 'Api key required'; exports.INVALID_API_KEY = 'Api key is invalid'; exports.INVALID_BICONOMY_KEY = 'Biconomy key is invalid'; +exports.TRANSACTION_FAILED = 'Transaction failed'; diff --git a/build/subscription.d.ts b/build/subscription.d.ts index 5e39add..31d9171 100644 --- a/build/subscription.d.ts +++ b/build/subscription.d.ts @@ -13,6 +13,7 @@ export default class SubscriptionContract extends Deployed { setDataContract(dataContractAddress: string): Promise; setManagers(newOwners: string[]): Promise; setNewApprovals(approvalAmount: string): Promise; + approveAndDeposit(approvalAmount: string): Promise; gasslessApproval(approvalAmount: string, chainId: number): Promise; gaslessUserAction(a: string, contract: ethers.Contract, erc20: ethers.Contract, biconomy: any): Promise; gaslessUserDeposit(a: string, contract: ethers.Contract, erc20: ethers.Contract, biconomy: any): Promise; diff --git a/build/subscription.js b/build/subscription.js index 58f5cc6..2c4bbdb 100644 --- a/build/subscription.js +++ b/build/subscription.js @@ -78,6 +78,24 @@ class SubscriptionContract extends deployed_1.default { return yield ((_a = this.erc20Contract) === null || _a === void 0 ? void 0 : _a.functions.approve((_b = this.subscriptionPaymentContract) === null || _b === void 0 ? void 0 : _b.address, weiAmount)); }); } + approveAndDeposit(approvalAmount) { + var _a, _b, _c, _d, _e; + return __awaiter(this, void 0, void 0, function* () { + try { + const weiAmount = this.vendor.convertToWei(approvalAmount, this.tokenPrecision || 18); + yield ((_a = this.erc20Contract) === null || _a === void 0 ? void 0 : _a.functions.approve((_b = this.subscriptionPaymentContract) === null || _b === void 0 ? void 0 : _b.address, weiAmount)); + return (_c = this.subscriptionPaymentContract) === null || _c === void 0 ? void 0 : _c.functions.userDeposit((_e = (_d = this.erc20Contract) === null || _d === void 0 ? void 0 : _d.address) !== null && _e !== void 0 ? _e : '', weiAmount); + } + catch (error) { + if (error instanceof Error) { + throw new Error(`Transaction failed: ${error.message}`); + } + else { + throw new Error(errors_1.TRANSACTION_FAILED); + } + } + }); + } gasslessApproval(approvalAmount, chainId) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { diff --git a/package.json b/package.json index cc5103a..eb67360 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spheron/desub-js", - "version": "2.3.0", + "version": "2.4.0", "description": "Typescript library for working with the decentralised subscription contracts in Spheron", "main": "build/index.js", "types": "build/index.d.ts", diff --git a/src/errors/index.ts b/src/errors/index.ts index 884cd91..cb3bc71 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -13,3 +13,4 @@ export const PROVIDER_REQUIRED = 'provider required' export const API_KEY_REQUIRED = 'Api key required' export const INVALID_API_KEY = 'Api key is invalid' export const INVALID_BICONOMY_KEY = 'Biconomy key is invalid' +export const TRANSACTION_FAILED = 'Transaction failed' diff --git a/src/subscription.ts b/src/subscription.ts index 217ab87..6bd1fd3 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -8,7 +8,7 @@ import { SUBSCRIPTION_DATA_ABI, SUBSCRIPTION_NATIVE_PAYMENT_ABI, } from './constants' -import { INVALID_BICONOMY_KEY } from './errors' +import { INVALID_BICONOMY_KEY, TRANSACTION_FAILED } from './errors' import { SubscriptionParameters, TokenData, TxResponse } from './interfaces' export default class SubscriptionContract extends Deployed { @@ -113,6 +113,25 @@ export default class SubscriptionContract extends Deployed { return await this.erc20Contract?.functions.approve(this.subscriptionPaymentContract?.address, weiAmount) } + /** + * Token approval and user deposit in one transaction. + * Do not use this function without frontend. + * @param approvalAmount - Amount of tokens to approve and deposit. + */ + async approveAndDeposit(approvalAmount: string): Promise { + try { + const weiAmount = this.vendor.convertToWei(approvalAmount, this.tokenPrecision || 18) + await this.erc20Contract?.functions.approve(this.subscriptionPaymentContract?.address, weiAmount) + return this.subscriptionPaymentContract?.functions.userDeposit(this.erc20Contract?.address ?? '', weiAmount) + } catch (error) { + if (error instanceof Error) { + throw new Error(`Transaction failed: ${error.message}`) + } else { + throw new Error(TRANSACTION_FAILED) + } + } + } + /** * Update approval for ERC-20 token. * Do not use this function without frontend.