Enable subscription payments

This commit is contained in:
silverpill 2022-02-05 17:16:42 +00:00
parent 16b32ac00c
commit 67cd088ffa
3 changed files with 64 additions and 1 deletions

View file

@ -5,6 +5,8 @@ import { http } from "./common"
export enum Contracts { export enum Contracts {
Adapter = "IAdapter", Adapter = "IAdapter",
Subscription = "ISubscription",
ERC20 = "IERC20",
} }
async function getContractAbi(contractName: string): Promise<any> { async function getContractAbi(contractName: string): Promise<any> {

View file

@ -47,3 +47,30 @@ export async function isSubscriptionConfigured(
const result = await adapter.isSubscriptionConfigured(recipientAddress) const result = await adapter.isSubscriptionConfigured(recipientAddress)
return result return result
} }
export async function makeSubscriptionPayment(
contractAddress: string,
signer: Signer,
recipientAddress: string,
): Promise<TransactionResponse> {
const adapter = await getContract(Contracts.Adapter, contractAddress, signer)
const subscriptionAddress = await adapter.subscription()
const subscription = await getContract(Contracts.Subscription, subscriptionAddress, signer)
const tokenAddress = await adapter.subscriptionToken()
const token = await getContract(Contracts.ERC20, tokenAddress, signer)
const subscriptionPrice = await adapter.getSubscriptionPrice(recipientAddress)
const duration = 3600 * 24 * 31 // 1 month
const amount = subscriptionPrice.mul(duration)
const allowance = await token.allowance(
signer.getAddress(),
subscription.address,
)
if (allowance.lt(amount)) {
await token.approve(subscription.address, amount)
}
const transaction = await subscription.send(
recipientAddress,
amount,
)
return transaction
}

View file

@ -15,7 +15,7 @@
</div> </div>
</div> </div>
<div <div
v-if="!isLocalUser() || canConnectWallet() || canConfigureSubscription()" v-if="!isLocalUser() || canConnectWallet() || canConfigureSubscription() || canSubscribe()"
class="dropdown-menu-wrapper" class="dropdown-menu-wrapper"
v-click-away="hideProfileMenu" v-click-away="hideProfileMenu"
> >
@ -50,6 +50,14 @@
Set up subscription Set up subscription
</a> </a>
</li> </li>
<li v-if="canSubscribe()">
<a
title="Pay for subscription"
@click="hideProfileMenu(); makeSubscriptionPayment()"
>
Pay for subscription
</a>
</li>
</ul> </ul>
</div> </div>
</div> </div>
@ -134,6 +142,7 @@ import {
getSubscriptionAuthorization, getSubscriptionAuthorization,
configureSubscription, configureSubscription,
isSubscriptionConfigured, isSubscriptionConfigured,
makeSubscriptionPayment,
} from "@/api/subscriptions" } from "@/api/subscriptions"
import Avatar from "@/components/Avatar.vue" import Avatar from "@/components/Avatar.vue"
import PostList from "@/components/PostList.vue" import PostList from "@/components/PostList.vue"
@ -356,6 +365,31 @@ export default class ProfileView extends Vue {
this.subscriptionConfigured = true this.subscriptionConfigured = true
} }
canSubscribe(): boolean {
return this.subscriptionConfigured === true && !this.isCurrentUser()
}
async makeSubscriptionPayment() {
const { instance } = this.store
if (
!this.profile ||
!this.profile.wallet_address ||
!instance ||
!instance.blockchain_contract_address
) {
return
}
const signer = await getSigner()
if (!signer) {
return
}
await makeSubscriptionPayment(
instance.blockchain_contract_address,
signer,
this.profile.wallet_address,
)
}
async loadNextPage(maxId: string) { async loadNextPage(maxId: string) {
if (!this.profile) { if (!this.profile) {
return return