-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chain compatibility #3345
Chain compatibility #3345
Changes from all commits
ea51eac
0f14c2c
20695f9
b91872b
0d35e62
ac7fa2e
487ec98
378b39d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { | |
Contract, | ||
ContractLock, | ||
ContractLockOptions, | ||
ContractPaymentState, | ||
Contracts, | ||
ExtrinsicResult, | ||
GetDedicatedNodePriceOptions, | ||
|
@@ -13,6 +14,8 @@ import { formatErrorMessage } from "../../helpers"; | |
import { ContractStates } from "../../modules"; | ||
import { Graphql } from "../graphql/client"; | ||
|
||
const ONE_HOUR = 1000 * 60 * 60; | ||
|
||
export type DiscountLevel = "None" | "Default" | "Bronze" | "Silver" | "Gold"; | ||
|
||
export interface ListContractByTwinIdOptions { | ||
|
@@ -98,6 +101,11 @@ export interface GetConsumptionOptions { | |
id: number; | ||
} | ||
|
||
export interface CalculateOverdueOptions { | ||
graphqlURL: string; | ||
id: number; | ||
paymentState: ContractPaymentState; | ||
} | ||
export interface CancelMyContractOptions { | ||
graphqlURL: string; | ||
} | ||
|
@@ -109,6 +117,18 @@ export interface LockContracts { | |
rentContracts: LockDetails; | ||
totalAmountLocked: number; | ||
} | ||
export interface ContractOverdueDetails extends ContractPaymentState { | ||
overdueAmount: number; | ||
} | ||
|
||
export type OverdueDetails = { [key: number]: ContractOverdueDetails }; | ||
|
||
export interface ContractsOverdue { | ||
nameContracts: OverdueDetails; | ||
nodeContracts: OverdueDetails; | ||
rentContracts: OverdueDetails; | ||
totalOverdueAmount: number; | ||
} | ||
|
||
class TFContracts extends Contracts { | ||
async listContractsByTwinId(options: ListContractByTwinIdOptions): Promise<GqlContracts> { | ||
|
@@ -308,6 +328,30 @@ class TFContracts extends Contracts { | |
return res; | ||
} | ||
|
||
/** | ||
* Calculates the overdue amount for a contract. | ||
* | ||
* This method calculates the overdue amount by summing the overdraft amounts | ||
* from the contract payment state and multiplying it by the product of: | ||
* 1. The time elapsed since the last billing in seconds (with an additional time allowance). | ||
* 2. The contract cost per second. | ||
* | ||
* The resulting overdue amount represents the amount that needs to be addressed. | ||
* | ||
* @param {CalculateOverdueOptions} options - The options containing the contract ID. | ||
* @returns {Promise<number>} - The calculated overdue amount. | ||
*/ | ||
async calculateContractOverDue(options: CalculateOverdueOptions) { | ||
const { lastUpdatedSeconds, standardOverdraft, additionalOverdraft } = options.paymentState; | ||
const contractCost = await this.getConsumption({ id: options.id, graphqlURL: options.graphqlURL }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just an estimate, as some contracts might have public IP addresses, and the cost will depend on the amount of bandwidth used, which is likely to vary. The cost calculation should simulate the billing process on tfchain (dry run) to provide a more accurate estimate. This involves checking the pricing policy, NU, and other resource consumption to determine the actual cost of the contract. Then, account for the time between the calculation and the submission of the transaction. We still need to estimate the cost of this extra time, but it shouldn't be a problem. Since the cost changes dynamically, we should also update the cost accordingly. It's unlikely that we will receive a new NU report, especially if we refresh the cost at short intervals, such as every 10~20 seconds. |
||
const elapsedSeconds = new Decimal(Date.now()).minus(lastUpdatedSeconds).div(1000); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that ContractPaymentState.lastUpdatedSecond is already a timestamp in seconds, you need to convert the current timestamp from milliseconds to seconds before performing the subtraction, right? The corrected code would be
|
||
const overdue = standardOverdraft | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please explain how the overdue is calculated, add a comment, and possibly simplify it by calculating it progressively using descriptive intermediary variable names? |
||
.plus(additionalOverdraft) | ||
.times(elapsedSeconds.plus(ONE_HOUR).times((contractCost / 60) * 60)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Units are not consistent here. Also, I'm trying to understand why we divide by 60 and then multiply by 60. |
||
|
||
return Number(overdue.div(10 ** 7)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, add a comment to explain the division here, and/or first assign the result to a descriptive variable name like |
||
} | ||
|
||
/** | ||
* WARNING: Please be careful when executing this method, it will delete all your contracts. | ||
* @param {CancelMyContractOptions} options | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what unit exactly? Please change the variable name to a more descriptive one.
An ideal name for a variable that holds the number of milliseconds in one hour could be
MILLISECONDS_PER_HOUR
, which is descriptive and clearly convey the purpose of the variable.