Skip to content

Commit

Permalink
Add effectiveTimestamp support for legacy network
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytrotkk committed Dec 20, 2023
1 parent aac4be6 commit 84d0672
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ body::-webkit-scrollbar {
.br__tile {
height: 150px;
border-radius: 25px !important;
border: 1px solid #171616 !important
border: 1px solid #171616 !important;
}

.pageCard {
Expand Down Expand Up @@ -369,7 +369,7 @@ body::-webkit-scrollbar {
text-transform: none !important;
color: #ffffffe6 !important;
padding: 6px 10px 6px 12px !important;
font-size: 0.75rem !important;
font-size: 0.7rem !important;
min-width: 55px;

width: 100%;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Paymaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function Paymaster(props: { mpc: MetaportCore; name: string }) {
}, [sklToken, address])

async function loadPaymasterInfo() {
const info = await getPaymasterInfo(paymaster, props.name)
const info = await getPaymasterInfo(paymaster, props.name, network)
let skl = sklToken
if (skl === undefined) {
skl = new Contract(info.skaleToken, ERC_ABIS.erc20.abi, paymaster.runner)
Expand Down
10 changes: 8 additions & 2 deletions src/components/PricingInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ export default function PricingInfo(props: { info: PaymasterInfo }) {
const chainPriceUsd = fromWei(props.info.schainPricePerMonth, DEFAULT_ERC20_DECIMALS)
const chainPriceSkl = divideBigInts(props.info.schainPricePerMonth, props.info.oneSklPrice)

const untilDueDateDays = daysBetweenNowAndTimestamp(props.info.schain.paidUntil)
const untilDueDateMonths = monthsBetweenNowAndTimestamp(props.info.schain.paidUntil)
const untilDueDateDays = daysBetweenNowAndTimestamp(
props.info.schain.paidUntil,
props.info.effectiveTimestamp
)
const untilDueDateMonths = monthsBetweenNowAndTimestamp(
props.info.schain.paidUntil,
props.info.effectiveTimestamp
)
const dueDateStatus = getDueDateStatus(untilDueDateDays)
const dueDateText = untilDueDateDays < 0 ? 'Payment overdue' : 'Paid for'

Expand Down
5 changes: 4 additions & 1 deletion src/components/Topup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ export default function Topup(props: {
const balanceOk = props.tokenBalance >= totalPriceWei
const topupBtnText = balanceOk ? 'Top-up chain' : 'Insufficient funds'

const untilDueDateMonths = monthsBetweenNowAndTimestamp(props.info.schain.paidUntil)
const untilDueDateMonths = monthsBetweenNowAndTimestamp(
props.info.schain.paidUntil,
props.info.effectiveTimestamp
)
const maxTopupPeriod = Number(props.info.maxReplenishmentPeriod) - untilDueDateMonths

return (
Expand Down
13 changes: 11 additions & 2 deletions src/core/paymaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface PaymasterInfo {
name: string
paidUntil: bigint
}
effectiveTimestamp?: bigint
}

export type DueDateStatus = 'primary' | 'warning' | 'error' | 'success'
Expand Down Expand Up @@ -74,7 +75,8 @@ export function initPaymaster(mpc: MetaportCore): Contract {

export async function getPaymasterInfo(
paymaster: Contract,
targetChainName: string
targetChainName: string,
skaleNetwork: interfaces.SkaleNetwork
): Promise<PaymasterInfo> {
const rawData = await Promise.all([
paymaster.maxReplenishmentPeriod(),
Expand All @@ -83,6 +85,12 @@ export async function getPaymasterInfo(
paymaster.skaleToken(),
paymaster.schains(id(targetChainName))
])

let effectiveTimestamp
if (skaleNetwork === 'legacy') {
effectiveTimestamp = await paymaster.effectiveTimestamp()
}

return {
maxReplenishmentPeriod: rawData[0],
oneSklPrice: rawData[1],
Expand All @@ -91,7 +99,8 @@ export async function getPaymasterInfo(
schain: {
name: rawData[4][1],
paidUntil: rawData[4][2]
}
},
effectiveTimestamp
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/core/timeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,17 @@ export function monthsBetweenTimestamps(from: bigint, to: bigint): number {
return Math.round(diffInDays / AVG_MONTH_LENGTH)
}

export function getCurrentTsBigInt(): bigint {
export function getCurrentTsBigInt(customCurrentTs?: bigint): bigint {
if (customCurrentTs !== undefined && customCurrentTs !== null) return customCurrentTs
return BigInt(Math.round(new Date().getTime() / 1000))
}

export function daysBetweenNowAndTimestamp(timestamp: bigint): number {
return daysBetweenTimestamps(getCurrentTsBigInt(), timestamp)
export function daysBetweenNowAndTimestamp(timestamp: bigint, customCurrentTs?: bigint): number {
return daysBetweenTimestamps(getCurrentTsBigInt(customCurrentTs), timestamp)
}

export function monthsBetweenNowAndTimestamp(timestamp: bigint): number {
return monthsBetweenTimestamps(getCurrentTsBigInt(), timestamp)
export function monthsBetweenNowAndTimestamp(timestamp: bigint, customCurrentTs?: bigint): number {
return monthsBetweenTimestamps(getCurrentTsBigInt(customCurrentTs), timestamp)
}

export function calculateElapsedPercentage(
Expand Down
Loading

0 comments on commit 84d0672

Please sign in to comment.