Skip to content

Commit 3e1ba80

Browse files
authored
add encode entity key plugin (#970)
* add encode entity key plugin * add bs58 types * version fix
1 parent 0a6a98c commit 3e1ba80

File tree

8 files changed

+98
-11
lines changed

8 files changed

+98
-11
lines changed

packages/account-postgres-sink-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"devDependencies": {
7575
"@types/async-retry": "^1.4.8",
7676
"@types/bn.js": "^5.1.1",
77+
"@types/bs58": "^4.0.1",
7778
"@types/cron": "^2.4.0",
7879
"@types/lodash": "^4.14.195",
7980
"@types/node": "^18.11.11",
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { DataTypes } from "sequelize";
2+
import { IPlugin } from "../types";
3+
import bs58 from "bs58";
4+
import { camelize } from "inflection";
5+
6+
export const EncodeEntityKeyPlugin = ((): IPlugin => {
7+
const name = "EncodeEntityKey";
8+
const init = async (config: { [key: string]: any }) => {
9+
const updateOnDuplicateFields = ["encoded_entity_key"];
10+
const addFields = (schema: { [key: string]: any }, accountName: string) => {
11+
schema[accountName] = {
12+
...schema[accountName],
13+
encoded_entity_key: DataTypes.TEXT,
14+
};
15+
};
16+
17+
const addIndexes = (
18+
schema: { [key: string]: any },
19+
accountName: string
20+
) => {
21+
schema[accountName] = {
22+
...schema[accountName],
23+
indexes: [
24+
{
25+
fields: ["encoded_entity_key"],
26+
name: `idx_encoded_entity_key`,
27+
unique: true,
28+
},
29+
],
30+
};
31+
};
32+
33+
const processAccount = async (account: { [key: string]: any }) => {
34+
const entityKey = account[camelize(config.field || "entity_key", true)];
35+
const keySerializationRaw = account[camelize("key_serialization", true)];
36+
const keySerialization =
37+
typeof keySerializationRaw === "string"
38+
? keySerializationRaw.trim().toLowerCase()
39+
: String(keySerializationRaw).trim().toLowerCase();
40+
let encodedEntityKey: string | null = null;
41+
if (entityKey && keySerialization) {
42+
if (keySerialization === "utf8") {
43+
encodedEntityKey = Buffer.from(entityKey, "utf8").toString("utf8");
44+
} else if (keySerialization === "b58" || keySerialization === "bs58") {
45+
encodedEntityKey = bs58.encode(entityKey);
46+
}
47+
}
48+
49+
return {
50+
...account,
51+
encoded_entity_key: encodedEntityKey,
52+
};
53+
};
54+
55+
return {
56+
updateOnDuplicateFields,
57+
addFields,
58+
addIndexes,
59+
processAccount,
60+
};
61+
};
62+
63+
return {
64+
name,
65+
init,
66+
};
67+
})();

packages/account-postgres-sink-service/src/plugins/extractHexLocation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export const ExtractHexLocationPlugin = ((): IPlugin => {
9494
const mapbox = MapboxService.getInstance();
9595
const processAccount = async (account: { [key: string]: any }) => {
9696
let reverseGeod: ReverseGeoCache | null = null;
97-
const location = account[config.field || "location"];
97+
const location = account[camelize(config.field || "location", true)];
9898
if (location) {
9999
reverseGeod = await ReverseGeoCache.findByPk(location.toString(), {
100100
attributes: updateOnDuplicateFields,

packages/account-postgres-sink-service/src/plugins/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { IConfig, IInitedPlugin, IPluginConfig } from "../types";
22
import { truthy } from "../utils/truthy";
33
import { ExtractHexLocationPlugin } from "./extractHexLocation";
4+
import { EncodeEntityKeyPlugin } from "./encodeEntityKey";
45

5-
export const Plugins = [ExtractHexLocationPlugin];
6+
export const Plugins = [ExtractHexLocationPlugin, EncodeEntityKeyPlugin];
67

78
export const initPlugins = async (pluginConfigs: IPluginConfig[] = []) =>
89
(

packages/account-postgres-sink-service/src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Transaction } from "sequelize";
22

3-
type Plugins = "ExtractHexLocation";
3+
type Plugins = "ExtractHexLocation" | "EncodeEntityKey";
44
type Crons = "refresh-accounts" | "integrity-check";
55

66
export interface IPluginConfig {
@@ -34,6 +34,7 @@ export interface IConfig {
3434
export interface IInitedPlugin {
3535
updateOnDuplicateFields?: string[];
3636
addFields?: (schema: { [key: string]: any }, accountName: string) => void;
37+
addIndexes?: (schema: { [key: string]: any }, accountName: string) => void;
3738
processAccount: (account: any, t?: Transaction) => Promise<any>;
3839
}
3940

packages/account-postgres-sink-service/src/utils/defineIdlModels.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
IdlTypeDef,
1111
IdlTypeDefTyStruct,
1212
} from "@coral-xyz/anchor/dist/cjs/idl";
13+
import { omit, pick } from "lodash";
1314

1415
const TypeMap = new Map<string, any>([
1516
["string", DataTypes.STRING],
@@ -57,6 +58,11 @@ const determineType = (type: string | object): any => {
5758
return DataTypes.JSONB;
5859
};
5960

61+
const shouldEnableCreatedAt = (schema: any, accName: string) => {
62+
const accSchema = schema[accName];
63+
return !accSchema || (!accSchema.createdAt && !accSchema.created_at);
64+
};
65+
6066
export const defineIdlModels = async ({
6167
idl,
6268
accounts,
@@ -84,9 +90,10 @@ export const defineIdlModels = async ({
8490
}
8591
}
8692

87-
(await initPlugins(accConfig?.plugins)).map(
88-
(plugin) => plugin?.addFields && plugin.addFields(schema, acc.name)
89-
);
93+
(await initPlugins(accConfig?.plugins)).map((plugin) => {
94+
if (plugin?.addFields) plugin.addFields(schema, acc.name);
95+
if (plugin?.addIndexes) plugin.addIndexes(schema, acc.name);
96+
});
9097

9198
if (accConfig.schema) {
9299
await sequelize.createSchema(accConfig.schema, {});
@@ -99,7 +106,7 @@ export const defineIdlModels = async ({
99106
type: DataTypes.STRING,
100107
primaryKey: true,
101108
},
102-
...schema[acc.name],
109+
...omit(schema[acc.name] || {}, ["indexes"]),
103110
refreshed_at: {
104111
type: DataTypes.DATE,
105112
},
@@ -109,9 +116,8 @@ export const defineIdlModels = async ({
109116
updatedAt: false,
110117
schema: underscore(accConfig.schema || "public"),
111118
tableName: underscore(accConfig.table || acc.name),
112-
createdAt:
113-
!schema[acc.name] ||
114-
(!schema[acc.name].createdAt && !schema[acc.name].created_at),
119+
createdAt: shouldEnableCreatedAt(schema, acc.name),
120+
...pick(schema[acc.name], "indexes"),
115121
}
116122
);
117123

packages/account-postgres-sink-service/yarn.deploy.lock

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ __metadata:
222222
"@triton-one/yellowstone-grpc": ^0.4.0
223223
"@types/async-retry": ^1.4.8
224224
"@types/bn.js": ^5.1.1
225+
"@types/bs58": ^4.0.1
225226
"@types/cron": ^2.4.0
226227
"@types/deep-equal": ^1.0.4
227228
"@types/lodash": ^4.14.195
@@ -715,6 +716,15 @@ __metadata:
715716
languageName: node
716717
linkType: hard
717718

719+
"@types/bs58@npm:^4.0.1":
720+
version: 4.0.1
721+
resolution: "@types/bs58@npm:4.0.1"
722+
dependencies:
723+
base-x: "npm:^3.0.6"
724+
checksum: 5063fed6bb3816f28eef3e689a05f8f85d03ce7c2e3ceefe77c349ed147023999eb024e863bf3fa81ba2bded690e5296efe4d34864c875097bd261614e37f786
725+
languageName: node
726+
linkType: hard
727+
718728
"@types/connect@npm:^3.4.33":
719729
version: 3.4.35
720730
resolution: "@types/connect@npm:3.4.35"
@@ -1176,7 +1186,7 @@ __metadata:
11761186
languageName: node
11771187
linkType: hard
11781188

1179-
"base-x@npm:^3.0.2":
1189+
"base-x@npm:^3.0.2, base-x@npm:^3.0.6":
11801190
version: 3.0.9
11811191
resolution: "base-x@npm:3.0.9"
11821192
dependencies:

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,7 @@ __metadata:
749749
"@triton-one/yellowstone-grpc": ^0.4.0
750750
"@types/async-retry": ^1.4.8
751751
"@types/bn.js": ^5.1.1
752+
"@types/bs58": ^4.0.1
752753
"@types/cron": ^2.4.0
753754
"@types/deep-equal": ^1.0.4
754755
"@types/lodash": ^4.14.195

0 commit comments

Comments
 (0)