Show subscriber's balance on subscription page
This commit is contained in:
parent
2314254101
commit
044dcf2473
4 changed files with 42 additions and 10 deletions
|
@ -6,7 +6,7 @@ import { http } from "./common"
|
||||||
export enum Contracts {
|
export enum Contracts {
|
||||||
Adapter = "IAdapter",
|
Adapter = "IAdapter",
|
||||||
Subscription = "ISubscription",
|
Subscription = "ISubscription",
|
||||||
ERC20 = "IERC20",
|
ERC20 = "IERC20Metadata",
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getContractAbi(contractName: string): Promise<any> {
|
async function getContractAbi(contractName: string): Promise<any> {
|
||||||
|
|
|
@ -2,7 +2,7 @@ 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"
|
||||||
import { EthereumSignature } from "@/utils/ethereum"
|
import { ethereumAddressMatch, EthereumSignature } from "@/utils/ethereum"
|
||||||
import { http } from "./common"
|
import { http } from "./common"
|
||||||
import { Contracts, getContract } from "./contracts"
|
import { Contracts, getContract } from "./contracts"
|
||||||
|
|
||||||
|
@ -40,8 +40,11 @@ export async function configureSubscription(
|
||||||
|
|
||||||
export interface Subscription {
|
export interface Subscription {
|
||||||
recipientAddress: string;
|
recipientAddress: string;
|
||||||
token: string;
|
tokenAddress: string;
|
||||||
|
tokenSymbol: string;
|
||||||
price: FixedNumber;
|
price: FixedNumber;
|
||||||
|
senderAddress: string | null;
|
||||||
|
senderBalance: FixedNumber | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const SECONDS_IN_MONTH = 3600 * 24 * 30
|
const SECONDS_IN_MONTH = 3600 * 24 * 30
|
||||||
|
@ -55,13 +58,27 @@ export async function getSubscriptionInfo(
|
||||||
const result = await adapter.isSubscriptionConfigured(recipientAddress)
|
const result = await adapter.isSubscriptionConfigured(recipientAddress)
|
||||||
if (result === true) {
|
if (result === true) {
|
||||||
const tokenAddress = await adapter.subscriptionToken()
|
const tokenAddress = await adapter.subscriptionToken()
|
||||||
const price = await adapter.getSubscriptionPrice(recipientAddress)
|
const token = await getContract(Contracts.ERC20, tokenAddress, signer)
|
||||||
const pricePerMonth = price.mul(SECONDS_IN_MONTH)
|
const tokenSymbol = await token.symbol()
|
||||||
const priceDec = FixedNumber.fromValue(pricePerMonth, 18).round(2)
|
const tokenDecimals = await token.decimals()
|
||||||
|
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 {
|
return {
|
||||||
recipientAddress,
|
recipientAddress,
|
||||||
token: tokenAddress,
|
tokenAddress,
|
||||||
price: priceDec,
|
tokenSymbol,
|
||||||
|
price,
|
||||||
|
senderAddress,
|
||||||
|
senderBalance,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return null
|
return null
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
import { Signer } from "ethers"
|
import { Signer } from "ethers"
|
||||||
import { Web3Provider } from "@ethersproject/providers"
|
import { Web3Provider } from "@ethersproject/providers"
|
||||||
|
|
||||||
|
export function ethereumAddressMatch(address1: string, address2: string): boolean {
|
||||||
|
return address1.toLowerCase() === address2.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
export function getWeb3Provider(): Web3Provider {
|
export function getWeb3Provider(): Web3Provider {
|
||||||
const provider = (window as any).ethereum
|
const provider = (window as any).ethereum
|
||||||
return new Web3Provider(provider)
|
return new Web3Provider(provider)
|
||||||
|
|
|
@ -10,8 +10,13 @@
|
||||||
<div class="info" v-if="subscriptionConfigured !== null">
|
<div class="info" v-if="subscriptionConfigured !== null">
|
||||||
<template v-if="subscription">
|
<template v-if="subscription">
|
||||||
<div>Recipient address: {{ subscription.recipientAddress }}</div>
|
<div>Recipient address: {{ subscription.recipientAddress }}</div>
|
||||||
<div>Token address: {{ subscription.token }}</div>
|
<div>Token address: {{ subscription.tokenAddress }}</div>
|
||||||
|
<div>Token symbol: {{ subscription.tokenSymbol }}</div>
|
||||||
<div>Price of one month: {{ subscription.price }}</div>
|
<div>Price of one month: {{ subscription.price }}</div>
|
||||||
|
<template v-if="subscription.senderAddress">
|
||||||
|
<div>Your address: {{ subscription.senderAddress }}</div>
|
||||||
|
<div>Your balance: {{ subscription.senderBalance }}</div>
|
||||||
|
</template>
|
||||||
</template>
|
</template>
|
||||||
<template v-else-if="isCurrentUser()">
|
<template v-else-if="isCurrentUser()">
|
||||||
Subscription is not configured.
|
Subscription is not configured.
|
||||||
|
@ -184,7 +189,13 @@ async function onMakeSubscriptionPayment() {
|
||||||
if (!signer) {
|
if (!signer) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
await makeSubscriptionPayment(
|
const transaction = await makeSubscriptionPayment(
|
||||||
|
instance.blockchain_contract_address,
|
||||||
|
signer,
|
||||||
|
profileEthereumAddress,
|
||||||
|
)
|
||||||
|
await transaction.wait()
|
||||||
|
subscription = await getSubscriptionInfo(
|
||||||
instance.blockchain_contract_address,
|
instance.blockchain_contract_address,
|
||||||
signer,
|
signer,
|
||||||
profileEthereumAddress,
|
profileEthereumAddress,
|
||||||
|
|
Loading…
Reference in a new issue