From fe6ef494e30c010ab14d4bdbead48ec83d47d496 Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 23 May 2022 22:40:18 +0000 Subject: [PATCH] Add controls for cancelling subscription and for withdrawing recieived funds --- src/api/subscriptions.ts | 58 +++++++++++---- src/views/Subscription.vue | 140 +++++++++++++++++++++++++++++++++---- 2 files changed, 172 insertions(+), 26 deletions(-) diff --git a/src/api/subscriptions.ts b/src/api/subscriptions.ts index 62abed9..bc37142 100644 --- a/src/api/subscriptions.ts +++ b/src/api/subscriptions.ts @@ -43,8 +43,6 @@ export interface Subscription { tokenAddress: string; tokenSymbol: string; price: FixedNumber; - senderAddress: string | null; - senderBalance: FixedNumber | null; } const SECONDS_IN_MONTH = 3600 * 24 * 30 @@ -64,27 +62,39 @@ export async function getSubscriptionInfo( const priceInt = await adapter.getSubscriptionPrice(recipientAddress) const pricePerMonth = priceInt.mul(SECONDS_IN_MONTH) const price = FixedNumber.fromValue(pricePerMonth, tokenDecimals).round(2) - const signerAddress = await signer.getAddress() - let senderAddress = null - let senderBalance = null - if (!ethereumAddressMatch(signerAddress, recipientAddress)) { - senderAddress = signerAddress - const [senderBalanceInt] = await adapter.getSubscriptionState(senderAddress, recipientAddress) - senderBalance = FixedNumber.fromValue(senderBalanceInt, tokenDecimals).round(2) - } return { recipientAddress, tokenAddress, tokenSymbol, price, - senderAddress, - senderBalance, } } else { return null } } +export interface SubscriptionState { + senderAddress: string; + senderBalance: FixedNumber; + recipientBalance: FixedNumber; +} + +export async function getSubscriptionState( + contractAddress: string, + signer: Signer, + senderAddress: string, + recipientAddress: string, +): Promise { + const adapter = await getContract(Contracts.Adapter, contractAddress, signer) + const tokenAddress = await adapter.subscriptionToken() + const token = await getContract(Contracts.ERC20, tokenAddress, signer) + const tokenDecimals = await token.decimals() + const [senderBalanceInt, recipientBalanceInt] = await adapter.getSubscriptionState(senderAddress, recipientAddress) + const senderBalance = FixedNumber.fromValue(senderBalanceInt, tokenDecimals) + const recipientBalance = FixedNumber.fromValue(recipientBalanceInt, tokenDecimals) + return { senderAddress, senderBalance, recipientBalance } +} + export async function makeSubscriptionPayment( contractAddress: string, signer: Signer, @@ -112,3 +122,27 @@ export async function makeSubscriptionPayment( ) return transaction } + +export async function cancelSubscription( + contractAddress: string, + signer: Signer, + recipientAddress: string, +): Promise { + const adapter = await getContract(Contracts.Adapter, contractAddress, signer) + const subscriptionAddress = await adapter.subscription() + const subscription = await getContract(Contracts.Subscription, subscriptionAddress, signer) + const transaction = await subscription.cancel(recipientAddress) + return transaction +} + +export async function withdrawReceived( + contractAddress: string, + signer: Signer, + senderAddress: string, +): Promise { + const adapter = await getContract(Contracts.Adapter, contractAddress, signer) + const subscriptionAddress = await adapter.subscription() + const subscription = await getContract(Contracts.Subscription, subscriptionAddress, signer) + const transaction = await subscription.withdrawReceived(senderAddress) + return transaction +} diff --git a/src/views/Subscription.vue b/src/views/Subscription.vue index bcbb080..36451f6 100644 --- a/src/views/Subscription.vue +++ b/src/views/Subscription.vue @@ -13,9 +13,9 @@
Token address: {{ subscription.tokenAddress }}
Token symbol: {{ subscription.tokenSymbol }}
Price of one month: {{ subscription.price }}
-