Show subscription price and token address on subscription page

This commit is contained in:
silverpill 2022-05-20 18:49:37 +00:00
parent 89ccba3201
commit f48cb67299
2 changed files with 65 additions and 30 deletions

View file

@ -1,4 +1,4 @@
import { Signer } from "ethers" import { BigNumber, FixedNumber, Signer } from "ethers"
import { TransactionResponse } from "@ethersproject/abstract-provider" import { TransactionResponse } from "@ethersproject/abstract-provider"
import { BACKEND_URL } from "@/constants" import { BACKEND_URL } from "@/constants"
@ -38,14 +38,34 @@ export async function configureSubscription(
return transaction return transaction
} }
export async function isSubscriptionConfigured( export interface Subscription {
recipientAddress: string;
token: string;
price: FixedNumber;
}
const SECONDS_IN_MONTH = 3600 * 24 * 30
export async function getSubscriptionInfo(
contractAddress: string, contractAddress: string,
signer: Signer, signer: Signer,
recipientAddress: string, recipientAddress: string,
): Promise<boolean> { ): Promise<Subscription | null> {
const adapter = await getContract(Contracts.Adapter, contractAddress, signer) const adapter = await getContract(Contracts.Adapter, contractAddress, signer)
const result = await adapter.isSubscriptionConfigured(recipientAddress) const result = await adapter.isSubscriptionConfigured(recipientAddress)
return result if (result === true) {
const tokenAddress = await adapter.subscriptionToken()
const price = await adapter.getSubscriptionPrice(recipientAddress)
const pricePerMonth = price.mul(SECONDS_IN_MONTH)
const priceDec = FixedNumber.fromValue(pricePerMonth, 18).round(2)
return {
recipientAddress,
token: tokenAddress,
price: priceDec,
}
} else {
return null
}
} }
export async function makeSubscriptionPayment( export async function makeSubscriptionPayment(
@ -59,8 +79,7 @@ export async function makeSubscriptionPayment(
const tokenAddress = await adapter.subscriptionToken() const tokenAddress = await adapter.subscriptionToken()
const token = await getContract(Contracts.ERC20, tokenAddress, signer) const token = await getContract(Contracts.ERC20, tokenAddress, signer)
const subscriptionPrice = await adapter.getSubscriptionPrice(recipientAddress) const subscriptionPrice = await adapter.getSubscriptionPrice(recipientAddress)
const duration = 3600 * 24 * 31 // 1 month const amount = subscriptionPrice.mul(SECONDS_IN_MONTH)
const amount = subscriptionPrice.mul(duration)
const allowance = await token.allowance( const allowance = await token.allowance(
signer.getAddress(), signer.getAddress(),
subscription.address, subscription.address,

View file

@ -7,19 +7,26 @@
<div class="connect-wallet" v-if="canConnectWallet()"> <div class="connect-wallet" v-if="canConnectWallet()">
<button class="btn" @click="connectWallet()">Connect wallet</button> <button class="btn" @click="connectWallet()">Connect wallet</button>
</div> </div>
<div class="setup" v-if="canConfigureSubscription()"> <div class="info" v-if="subscriptionConfigured !== null">
<template v-if="subscriptionConfigured"> <template v-if="subscription">
Subscription is configured. <div>Recipient address: {{ subscription.recipientAddress }}</div>
<div>Token address: {{ subscription.token }}</div>
<div>Price of one month: {{ subscription.price }}</div>
</template> </template>
<button v-else class="btn" @click="onConfigureSubscription()"> <template v-else-if="isCurrentUser()">
Subscription is not configured.
</template>
<template v-else>
Subscription is not available.
</template>
</div>
<div class="setup" v-if="canConfigureSubscription()">
<button class="btn" @click="onConfigureSubscription()">
Set up subscription Set up subscription
</button> </button>
</div> </div>
<div class="payment" v-if="canSubscribe()"> <div class="payment" v-if="canSubscribe()">
<template v-if="!subscriptionConfigured"> <button class="btn" @click="onMakeSubscriptionPayment()">
Subscription is not available.
</template>
<button v-else class="btn" @click="onMakeSubscriptionPayment()">
Pay for subscription Pay for subscription
</button> </button>
</div> </div>
@ -40,10 +47,11 @@ import {
Profile, Profile,
} from "@/api/users" } from "@/api/users"
import { import {
getSubscriptionAuthorization,
configureSubscription, configureSubscription,
isSubscriptionConfigured, getSubscriptionAuthorization,
getSubscriptionInfo,
makeSubscriptionPayment, makeSubscriptionPayment,
Subscription,
} from "@/api/subscriptions" } from "@/api/subscriptions"
import Sidebar from "@/components/Sidebar.vue" import Sidebar from "@/components/Sidebar.vue"
import { useInstanceInfo } from "@/store/instance" import { useInstanceInfo } from "@/store/instance"
@ -56,6 +64,7 @@ const { instance, getActorAddress } = $(useInstanceInfo())
let profile = $ref<Profile | null>(null) let profile = $ref<Profile | null>(null)
let walletConnected = $ref(false) let walletConnected = $ref(false)
let subscriptionConfigured = $ref<boolean | null>(null) let subscriptionConfigured = $ref<boolean | null>(null)
let subscription = $ref<Subscription | null>(null)
onMounted(async () => { onMounted(async () => {
const { authToken } = useCurrentUser() const { authToken } = useCurrentUser()
@ -88,10 +97,10 @@ async function connectWallet() {
return return
} }
walletConnected = true walletConnected = true
checkSubscriptionConfigured() checkSubscription()
} }
async function checkSubscriptionConfigured() { async function checkSubscription() {
if ( if (
!profile || !profile ||
!instance || !instance ||
@ -107,16 +116,21 @@ async function checkSubscriptionConfigured() {
if (!signer) { if (!signer) {
return return
} }
subscriptionConfigured = await isSubscriptionConfigured( subscription = await getSubscriptionInfo(
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer, signer,
profileEthereumAddress, profileEthereumAddress,
) )
if (subscription !== null) {
subscriptionConfigured = true
} else {
subscriptionConfigured = false
}
} }
function canConfigureSubscription(): boolean { function canConfigureSubscription(): boolean {
// If wallet is not connected, subscriptionConfigured is set to null // If wallet is not connected, subscriptionConfigured is set to null
return isCurrentUser() && subscriptionConfigured !== null return isCurrentUser() && subscriptionConfigured === false
} }
async function onConfigureSubscription() { async function onConfigureSubscription() {
@ -135,17 +149,23 @@ async function onConfigureSubscription() {
} }
const authToken = ensureAuthToken() const authToken = ensureAuthToken()
const signature = await getSubscriptionAuthorization(authToken) const signature = await getSubscriptionAuthorization(authToken)
await configureSubscription( const transaction = await configureSubscription(
instance.blockchain_contract_address, instance.blockchain_contract_address,
signer, signer,
currentUser.wallet_address, currentUser.wallet_address,
signature, signature,
) )
await transaction.wait()
subscriptionConfigured = true subscriptionConfigured = true
subscription = await getSubscriptionInfo(
instance.blockchain_contract_address,
signer,
currentUser.wallet_address,
)
} }
function canSubscribe(): boolean { function canSubscribe(): boolean {
return !isCurrentUser() && subscriptionConfigured !== null return !isCurrentUser() && subscriptionConfigured === true
} }
async function onMakeSubscriptionPayment() { async function onMakeSubscriptionPayment() {
@ -182,19 +202,15 @@ async function onMakeSubscriptionPayment() {
background-color: $block-background-color; background-color: $block-background-color;
border-radius: $block-border-radius; border-radius: $block-border-radius;
display: flex;
flex-direction: column;
gap: $block-inner-padding;
margin-bottom: $block-outer-padding; margin-bottom: $block-outer-padding;
padding: $block-inner-padding; padding: $block-inner-padding;
h1 { h1 {
font-size: 20px; font-size: 20px;
text-align: center; margin: 0;
} }
} }
.connect-wallet,
.setup,
.payment {
display: flex;
justify-content: center;
}
</style> </style>