Skip to content

Commit

Permalink
Adjust script to new queries (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
fleupold authored Sep 2, 2024
1 parent ba23bef commit 3d035a1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
6 changes: 3 additions & 3 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ ZODIAC_ROLES_MOD=0xa2f93c12E697ABC34770CFAB2def5532043E26e9
# ZODIAC_ROLES_MOD=

# Billing Config
BILLING_QUERY=3630322
FEE_QUERY=3605385
BILLING_QUERY=4005800
FEE_QUERY=4002039

# Drafting Config
ZODIAC_ROLE_KEY=
## This is $3.50 with ETH at $3500
FINE_MIN=0.001
BOND_THRESHOLD=10.0
PAYMENT_QUERY=3742749
PAYMENT_QUERY=4016152

# Secrets: Required for both Billing & Drafting
DUNE_API_KEY=
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ Requires two additional environment variables:

```sh
# Dune QueryId of Payments Due every week
BILLING_QUERY=3630322
BILLING_QUERY=4005800
# Dune QueryId for computing the monthly fee value.
FEE_QUERY=3605385
FEE_QUERY=4002039
```

### Drafting
Expand All @@ -67,7 +67,7 @@ Assuming the roles are appropriately confugured this program requires the follow
# A 32-byte hex string associated to the configured zodiac roles.
ZODIAC_ROLE_KEY
# Dune Query ID for detecting unpaid bills.
PAYMENT_QUERY=3742749
PAYMENT_QUERY=4016152
# Minimum fine to charge for drafting (in ETH)
FINE_MIN=0.001
# Minimum account balance required to stay connected to the network. (in ETH)
Expand Down
2 changes: 1 addition & 1 deletion src/accountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class AccountManager {
for (let paymentStatus of paymentStatuses) {
if (paymentStatus.status !== PaymentStatus.PAID) {
messages.push(
`${paymentStatus.account} was supposed to pay ${paymentStatus.billedAmount} but paid ${paymentStatus.paidAmount}`,
`${paymentStatus.account} was supposed to pay ${ethers.formatEther(paymentStatus.billedAmount)} ETH but paid ${ethers.formatEther(paymentStatus.paidAmount)} ETH`,
);
}
}
Expand Down
36 changes: 27 additions & 9 deletions src/dune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,26 @@ export class QueryRunner {
);
}

private async getAmountsDue(date: string): Promise<AmountDue[]> {
private async getAmountsDue(
billingDate: string,
feeComputationStart: string,
feeComputationEnd: string,
): Promise<AmountDue[]> {
try {
const billingResponse = await this.dune.runQuery({
query_parameters: [QueryParameter.date("bill_date", date)],
query_parameters: [
QueryParameter.date("billing_date", billingDate),
QueryParameter.date("fee_computation_start", feeComputationStart),
QueryParameter.date("fee_computation_end", feeComputationEnd),
],
queryId: this.billingQuery,
...this.options,
});
const results = billingResponse.result!.rows;
console.log("Got Billing Results:", results);
return results.map((row: any) => ({
billingAddress: row.miner_biller_address!,
builder: row.miner_label,
billingAddress: row.billing_address!,
builder: row.label,
dueAmountWei: BigInt(row.amount_due_wei!),
}));
} catch (error) {
Expand All @@ -77,10 +85,13 @@ export class QueryRunner {
}
}

private async getPeriodFee(date: string): Promise<bigint> {
private async getPeriodFee(start: string, end: string): Promise<bigint> {
try {
const feeResponse = await this.dune.runQuery({
query_parameters: [QueryParameter.date("bill_date", date)],
query_parameters: [
QueryParameter.date("start", start),
QueryParameter.date("end", end),
],
queryId: this.feeQuery,
...this.options,
});
Expand All @@ -99,11 +110,18 @@ export class QueryRunner {

async getBillingData(date: Date): Promise<BillingData> {
try {
const dateString = moment(date).format("YYYY-MM-DD HH:mm:ss");
const billingDate = moment(date).format("YYYY-MM-DD 00:00:00");
const feeComputationStart = moment(date)
.subtract(1, "month")
.startOf("month")
.format("YYYY-MM-DD 00:00:00");
const feeComputationEnd = moment(date)
.startOf("month")
.format("YYYY-MM-DD 00:00:00");
console.log(`Executing fee and payment queries this may take a while...`);
const [dueAmounts, periodFee] = await Promise.all([
this.getAmountsDue(dateString),
this.getPeriodFee(dateString),
this.getAmountsDue(billingDate, feeComputationStart, feeComputationEnd),
this.getPeriodFee(feeComputationStart, feeComputationEnd),
]);
return { dueAmounts, periodFee };
} catch (error) {
Expand Down

0 comments on commit 3d035a1

Please sign in to comment.