Refactor Subscription class
This commit is contained in:
parent
c5cf53a1be
commit
a0593b6a01
3 changed files with 16 additions and 11 deletions
|
@ -48,7 +48,7 @@ export class Subscription {
|
||||||
tokenAddress: string;
|
tokenAddress: string;
|
||||||
tokenSymbol: string;
|
tokenSymbol: string;
|
||||||
private tokenDecimals: number;
|
private tokenDecimals: number;
|
||||||
private _price: BigNumber; // per second
|
private price: BigNumber; // per second
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
recipientAddress: string,
|
recipientAddress: string,
|
||||||
|
@ -61,16 +61,19 @@ export class Subscription {
|
||||||
this.tokenAddress = tokenAddress
|
this.tokenAddress = tokenAddress
|
||||||
this.tokenSymbol = tokenSymbol
|
this.tokenSymbol = tokenSymbol
|
||||||
this.tokenDecimals = tokenDecimals
|
this.tokenDecimals = tokenDecimals
|
||||||
this._price = price
|
this.price = price
|
||||||
}
|
}
|
||||||
|
|
||||||
get price(): FixedNumber {
|
get pricePerMonthInt(): BigNumber {
|
||||||
const pricePerMonth = this._price.mul(SECONDS_IN_MONTH)
|
return this.price.mul(SECONDS_IN_MONTH)
|
||||||
return FixedNumber.fromValue(pricePerMonth, this.tokenDecimals).round(2)
|
}
|
||||||
|
|
||||||
|
get pricePerMonth(): FixedNumber {
|
||||||
|
return FixedNumber.fromValue(this.pricePerMonthInt, this.tokenDecimals)
|
||||||
}
|
}
|
||||||
|
|
||||||
getExpirationDate(balance: FixedNumber): DateTime {
|
getExpirationDate(balance: FixedNumber): DateTime {
|
||||||
const price = FixedNumber.fromValue(this._price, this.tokenDecimals)
|
const price = FixedNumber.fromValue(this.price, this.tokenDecimals)
|
||||||
const seconds = balance.divUnsafe(price).toUnsafeFloat()
|
const seconds = balance.divUnsafe(price).toUnsafeFloat()
|
||||||
const now = DateTime.now()
|
const now = DateTime.now()
|
||||||
return now.plus({ seconds })
|
return now.plus({ seconds })
|
||||||
|
@ -129,14 +132,13 @@ export async function makeSubscriptionPayment(
|
||||||
contractAddress: string,
|
contractAddress: string,
|
||||||
signer: Signer,
|
signer: Signer,
|
||||||
recipientAddress: string,
|
recipientAddress: string,
|
||||||
|
amount: BigNumber,
|
||||||
): Promise<TransactionResponse> {
|
): Promise<TransactionResponse> {
|
||||||
const adapter = await getContract(Contracts.Adapter, contractAddress, signer)
|
const adapter = await getContract(Contracts.Adapter, contractAddress, signer)
|
||||||
const subscriptionAddress = await adapter.subscription()
|
const subscriptionAddress = await adapter.subscription()
|
||||||
const subscription = await getContract(Contracts.Subscription, subscriptionAddress, signer)
|
const subscription = await getContract(Contracts.Subscription, subscriptionAddress, signer)
|
||||||
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 amount = subscriptionPrice.mul(SECONDS_IN_MONTH)
|
|
||||||
const allowance = await token.allowance(
|
const allowance = await token.allowance(
|
||||||
signer.getAddress(),
|
signer.getAddress(),
|
||||||
subscription.address,
|
subscription.address,
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<div>Recipient address: {{ subscription.recipientAddress }}</div>
|
<div>Recipient address: {{ subscription.recipientAddress }}</div>
|
||||||
<div>Token address: {{ subscription.tokenAddress }}</div>
|
<div>Token address: {{ subscription.tokenAddress }}</div>
|
||||||
<div>Token symbol: {{ subscription.tokenSymbol }}</div>
|
<div>Token symbol: {{ subscription.tokenSymbol }}</div>
|
||||||
<div>Price of one month: {{ subscription.price }}</div>
|
<div>Price of one month: {{ subscription.pricePerMonth.round(2) }}</div>
|
||||||
<template v-if="subscriptionState">
|
<template v-if="subscriptionState">
|
||||||
<div>Your address: {{ subscriptionState.senderAddress }}</div>
|
<div>Your address: {{ subscriptionState.senderAddress }}</div>
|
||||||
<div>Your balance: {{ subscriptionState.senderBalance }}</div>
|
<div>Your balance: {{ subscriptionState.senderBalance }}</div>
|
||||||
|
@ -128,15 +128,18 @@ async function onMakeSubscriptionPayment() {
|
||||||
if (
|
if (
|
||||||
!instance?.blockchain_contract_address ||
|
!instance?.blockchain_contract_address ||
|
||||||
!recipientEthereumAddress ||
|
!recipientEthereumAddress ||
|
||||||
!walletAddress
|
!walletAddress ||
|
||||||
|
!subscription
|
||||||
) {
|
) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const signer = getWeb3Provider().getSigner()
|
const signer = getWeb3Provider().getSigner()
|
||||||
|
const amount = subscription.pricePerMonthInt
|
||||||
const transaction = await makeSubscriptionPayment(
|
const transaction = await makeSubscriptionPayment(
|
||||||
instance.blockchain_contract_address,
|
instance.blockchain_contract_address,
|
||||||
signer,
|
signer,
|
||||||
recipientEthereumAddress,
|
recipientEthereumAddress,
|
||||||
|
amount,
|
||||||
)
|
)
|
||||||
await transaction.wait()
|
await transaction.wait()
|
||||||
subscriptionState = await getSubscriptionState(
|
subscriptionState = await getSubscriptionState(
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
<div>Recipient address: {{ subscription.recipientAddress }}</div>
|
<div>Recipient address: {{ subscription.recipientAddress }}</div>
|
||||||
<div>Token address: {{ subscription.tokenAddress }}</div>
|
<div>Token address: {{ subscription.tokenAddress }}</div>
|
||||||
<div>Token symbol: {{ subscription.tokenSymbol }}</div>
|
<div>Token symbol: {{ subscription.tokenSymbol }}</div>
|
||||||
<div>Price of one month: {{ subscription.price }}</div>
|
<div>Price of one month: {{ subscription.pricePerMonth.round(2) }}</div>
|
||||||
</template>
|
</template>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
Subscription is not configured.
|
Subscription is not configured.
|
||||||
|
|
Loading…
Reference in a new issue