Skip to content

Commit

Permalink
Generate & Upload (near,mpc,safe) Address Tuples To Dune (#124)
Browse files Browse the repository at this point in the history
This script is a small data pipeline querying all [sign
requests](https://nearblocks.io/address/v1.signer) made to `v1.signer`
from [Dune](https://dune.com/queries/4611467?sidebar=none) along with
their `derivation_path` and `key_version`. It then filters for
`(key_version, derivation_path) = (0, ethereum,1)` (i.e. those used by
[bitte.ai](https://wallet.bitte.ai/)) and generates the tuple
`(nearAddress,mpcAddress,safeAddress)` and uploads the CSV back to Dune
which can be queried as demonstrated
[here](https://dune.com/queries/4611928).

This data can be joined with all the safe activity data to gain insight
on Near Accounts Interacting with EVMs via Bitte Protocol.
  • Loading branch information
bh2smith authored Jan 24, 2025
1 parent 8fa5ff1 commit 6637da0
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 190 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
"dependencies": {
"@safe-global/safe-gateway-typescript-sdk": "^3.22.6",
"near-api-js": "^5.0.1",
"near-ca": "^0.8.3",
"near-ca": "^0.8.4",
"semver": "^7.6.3",
"viem": "^2.22.8"
"viem": "^2.22.12"
},
"devDependencies": {
"@duneanalytics/client-sdk": "^0.2.5",
"@safe-global/safe-deployments": "^1.37.23",
"@safe-global/safe-modules-deployments": "^2.2.4",
"@types/jest": "^29.5.14",
Expand Down
100 changes: 100 additions & 0 deletions scripts/data-link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { DuneClient } from "@duneanalytics/client-sdk";
import dotenv from "dotenv";

import { NearSafe } from "../src";

interface Pairing {
near: string;
mpc: string;
safe: string;
}

export async function accountPairings(data: {
nearAddress: string;
keyVersion: number;
derivationPath: string;
safeSaltNonce: string;
}): Promise<Pairing> {
const { nearAddress, keyVersion, derivationPath, safeSaltNonce } = data;
if (keyVersion !== 0) {
throw new Error("Only key version 0 is supported");
}
const adapter = await NearSafe.create({
mpc: {
accountId: nearAddress,
mpcContractId: "v1.signer",
derivationPath,
},
pimlicoKey: "",
safeSaltNonce,
});
return {
near: nearAddress,
mpc: adapter.mpcAddress,
safe: adapter.address,
};
}

interface SignerUser {
sign_for: string;
key_version: number;
derivation_path: string;
num_relayed: number;
total: number;
}

// Example usage
export async function getAllSignerUsers(
dune: DuneClient
): Promise<SignerUser[]> {
// This Dune Query is better than near blocks:
// https://dune.com/queries/4611467
const queryId = 4611467;
const { result } = await dune.getLatestResult({ queryId });
const data: SignerUser[] = (result?.rows ?? []).map((row) => ({
sign_for: String(row.sign_for),
key_version: Number(row.key_version),
derivation_path: String(row.derivation_path),
num_relayed: Number(row.num_relayed),
total: Number(row.total),
}));
return data;
}

export async function generateCSV(dune: DuneClient): Promise<string> {
const data = await getAllSignerUsers(dune);
const BITTE_WALLET_SALT_NONCE = "130811896738364156958237239906781888512";
const pairings = await Promise.all(
data
.filter((user) => user.derivation_path === "ethereum,1")
.map((user) =>
accountPairings({
nearAddress: user.sign_for,
keyVersion: user.key_version,
derivationPath: user.derivation_path,
safeSaltNonce: BITTE_WALLET_SALT_NONCE,
})
)
);

const csvContent = pairings
.map((pairing) => `${pairing.near},${pairing.mpc},${pairing.safe}`)
.join("\n");

// fs.writeFileSync("wallet-pairings.csv", "near,mpc,safe\n" + csvContent);
return csvContent;
}

export async function duneUpload(): Promise<void> {
dotenv.config();
const dune = new DuneClient(process.env.DUNE_API_KEY ?? "");
const csv = await generateCSV(dune);
// fs.writeFileSync("wallet-pairings.csv", "near,mpc,safe\n" + csv);
const result = await dune.table.uploadCsv({
table_name: "neareth_pairings",
data: "near,mpc,safe\n" + csv,
description: "Near -> MPC -> Safe Tuples",
is_private: false,
});
console.log(result);
}
6 changes: 6 additions & 0 deletions tests/unit/constants.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { duneUpload } from "../../scripts/data-link";
import { DEFAULT_SAFE_SALT_NONCE, USER_OP_IDENTIFIER } from "../../src";

// DO NOT MODIFY
Expand All @@ -10,4 +11,9 @@ describe("Protocol Domain Separator", () => {
"130811896738364114529934864114944206080"
);
});

// Requires DUNE_API_KEY
it.skip("datalink", async () => {
await duneUpload();
});
});
Loading

0 comments on commit 6637da0

Please sign in to comment.