Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
Merge pull request #27 from spheronFdn/arome/sph-2339
Browse files Browse the repository at this point in the history
Bundle Token Approval and User Deposit
  • Loading branch information
arome3 authored Jun 29, 2023
2 parents 075c023 + 2d70fb7 commit 06d0ff0
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<p align="center">

<img src="https://img.shields.io/static/v1?label=npm&message=v2.3.0&color=green" />
<img src="https://img.shields.io/static/v1?label=npm&message=v2.4.0&color=green" />

<img src="https://img.shields.io/static/v1?label=license&message=Apache-2&color=green" />

Expand Down
1 change: 1 addition & 0 deletions build/errors/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
3 changes: 2 additions & 1 deletion build/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
1 change: 1 addition & 0 deletions build/subscription.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default class SubscriptionContract extends Deployed {
setDataContract(dataContractAddress: string): Promise<TxResponse>;
setManagers(newOwners: string[]): Promise<TxResponse>;
setNewApprovals(approvalAmount: string): Promise<TxResponse>;
approveAndDeposit(approvalAmount: string): Promise<TxResponse>;
gasslessApproval(approvalAmount: string, chainId: number): Promise<TxResponse>;
gaslessUserAction(a: string, contract: ethers.Contract, erc20: ethers.Contract, biconomy: any): Promise<TxResponse>;
gaslessUserDeposit(a: string, contract: ethers.Contract, erc20: ethers.Contract, biconomy: any): Promise<TxResponse>;
Expand Down
18 changes: 18 additions & 0 deletions build/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
21 changes: 20 additions & 1 deletion src/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<TxResponse> {
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.
Expand Down

0 comments on commit 06d0ff0

Please sign in to comment.