Skip to content

Commit

Permalink
✨ server: support multiple collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Jan 17, 2025
1 parent 8b49475 commit de1e7a7
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
2 changes: 0 additions & 2 deletions .do/app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ services:
- key: AUTH_SECRET
scope: RUN_TIME
type: SECRET
- key: COLLECTOR_ADDRESS
scope: RUN_TIME
- key: CRYPTOMATE_API_KEY
scope: RUN_TIME
type: SECRET
Expand Down
6 changes: 3 additions & 3 deletions server/api/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ import { withRetry, zeroAddress } from "viem";
import database, { credentials } from "../database";
import { previewerAbi, marketAbi } from "../generated/contracts";
import auth from "../middleware/auth";
import COLLECTOR from "../utils/COLLECTOR";
import collectors from "../utils/collectors";
import publicClient from "../utils/publicClient";

const app = new Hono();
app.use(auth);

const ActivityTypes = picklist(["card", "received", "repay", "sent"]);

const collector = COLLECTOR.toLowerCase();
const collectorSet = new Set(collectors.map((address) => address.toLowerCase()));

export default app.get(
"/",
Expand Down Expand Up @@ -134,7 +134,7 @@ export default app.get(
})
.then((logs) =>
logs
.filter(({ args }) => args.receiver.toLowerCase() !== collector)
.filter(({ args }) => !collectorSet.has(args.receiver.toLowerCase()))
.map((log) =>
parse(WithdrawActivity, { ...log, market: market(log.address) } satisfies InferInput<
typeof WithdrawActivity
Expand Down
15 changes: 9 additions & 6 deletions server/hooks/cryptomate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { privateKeyToAccount } from "viem/accounts";

import database, { cards, transactions } from "../database/index";
import { auditorAbi, issuerCheckerAbi, issuerCheckerAddress, marketAbi } from "../generated/contracts";
import COLLECTOR from "../utils/COLLECTOR";
import collectors from "../utils/collectors";
import keeper from "../utils/keeper";
import { sendPushNotification } from "../utils/onesignal";
import publicClient from "../utils/publicClient";
Expand Down Expand Up @@ -146,7 +146,7 @@ export default new Hono().post(
return c.json({ response_code: "69" });
}
if (
usdcTransfersToCollector(trace).reduce(
usdcTransfersToCollectors(trace).reduce(
(total, { topics, data }) =>
total + decodeEventLog({ abi: erc20Abi, eventName: "Transfer", topics, data }).args.value,
0n,
Expand Down Expand Up @@ -306,16 +306,19 @@ async function prepareCollection(payload: v.InferOutput<typeof Payload>) {
};
}

const collectorTopic = padHex(COLLECTOR);
const collectorTopics = new Set(collectors.map((address) => padHex(address.toLowerCase() as Hex)));
const [transferTopic] = encodeEventTopics({ abi: erc20Abi, eventName: "Transfer" });
const usdcLowercase = usdcAddress.toLowerCase() as Hex;
function usdcTransfersToCollector({ calls, logs }: CallFrame): TransferLog[] {
function usdcTransfersToCollectors({ calls, logs }: CallFrame): TransferLog[] {
return [
...(logs?.filter(
(log): log is TransferLog =>
log.address === usdcLowercase && log.topics?.[0] === transferTopic && log.topics[2] === collectorTopic,
log.address === usdcLowercase &&
log.topics?.[0] === transferTopic &&
log.topics[2] !== undefined &&
collectorTopics.has(log.topics[2]),
) ?? []),
...(calls?.flatMap(usdcTransfersToCollector) ?? []),
...(calls?.flatMap(usdcTransfersToCollectors) ?? []),
];
}

Expand Down
1 change: 0 additions & 1 deletion server/test/anvil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export default async function setup({ provide }: GlobalSetupContext) {
cwd: "node_modules/@exactly/plugin",
env: {
OPTIMISM_ETHERSCAN_KEY: "",
COLLECTOR_ADDRESS: privateKeyToAddress(padHex("0x666")),
ISSUER_ADDRESS: privateKeyToAddress(padHex("0x420")),
KEEPER_ADDRESS: keeper.address,
DEPLOYER_ADDRESS: deployer,
Expand Down
2 changes: 1 addition & 1 deletion server/test/hooks/cryptomate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ async function collectorBalance() {
},
],
functionName: "balanceOf",
args: [process.env.COLLECTOR_ADDRESS],
args: ["0xDb90CDB64CfF03f254e4015C4F705C3F3C834400"],
}),
})
.then(({ data }) => {
Expand Down
6 changes: 0 additions & 6 deletions server/utils/COLLECTOR.ts

This file was deleted.

12 changes: 12 additions & 0 deletions server/utils/collectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import chain from "@exactly/common/generated/chain";
import { Address } from "@exactly/common/validation";
import { parse } from "valibot";
import { optimism } from "viem/chains";

const collectors: Address[] = (
{
[optimism.id]: ["0x0f25bA5b8B0BA4Ff4dF645fDE030652da60BabA6", "0x471e5F3428D5C50543072c817a9D0CcBa8ed7D5F"],
}[chain.id] ?? ["0xDb90CDB64CfF03f254e4015C4F705C3F3C834400"]
).map((address) => parse(Address, address));

export default collectors;
2 changes: 0 additions & 2 deletions server/vitest.config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { padHex } from "viem";
import { privateKeyToAddress } from "viem/accounts";
import { defineConfig } from "vitest/config";

export default defineConfig({
Expand All @@ -10,7 +9,6 @@ export default defineConfig({
ALCHEMY_BLOCK_KEY: "block",
ALCHEMY_WEBHOOKS_KEY: "webhooks",
AUTH_SECRET: "auth",
COLLECTOR_ADDRESS: privateKeyToAddress(padHex("0x666")),
CRYPTOMATE_WEBHOOK_KEY: "cryptomate",
EXPO_PUBLIC_ALCHEMY_API_KEY: " ",
ISSUER_PRIVATE_KEY: padHex("0x420"),
Expand Down

0 comments on commit de1e7a7

Please sign in to comment.