@latticexyz/common 2.0.0-next.11 → 2.0.0-next.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-7WIPV3R3.js +2 -0
- package/dist/chunk-7WIPV3R3.js.map +1 -0
- package/dist/codegen.js +13 -13
- package/dist/codegen.js.map +1 -1
- package/dist/foundry.js +2 -2
- package/dist/foundry.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/utils.js +1 -1
- package/dist/utils.js.map +1 -1
- package/package.json +3 -7
- package/src/codegen/debug.ts +3 -0
- package/src/codegen/utils/format.ts +1 -2
- package/src/codegen/utils/formatAndWrite.ts +8 -7
- package/src/codegen/utils/loadUserTypesFile.ts +2 -1
- package/src/common.ts +4 -2
- package/src/createNonceManager.ts +7 -2
- package/src/{createContract.ts → deprecated/createContract.ts} +1 -1
- package/src/deprecated/hexToResourceId.ts +4 -0
- package/src/deprecated/resourceIdToHex.ts +4 -0
- package/src/foundry/index.ts +7 -5
- package/src/getContract.ts +18 -4
- package/src/getNonceManager.ts +2 -2
- package/src/getNonceManagerId.ts +1 -0
- package/src/hexToResource.test.ts +11 -0
- package/src/{hexToResourceId.ts → hexToResource.ts} +5 -5
- package/src/index.ts +8 -3
- package/src/{resourceIdToHex.test.ts → resourceToHex.test.ts} +15 -11
- package/src/{resourceIdToHex.ts → resourceToHex.ts} +5 -5
- package/src/sendTransaction.ts +89 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/mapObject.test.ts +23 -0
- package/src/utils/mapObject.ts +11 -0
- package/src/utils/uniqueBy.ts +7 -0
- package/src/writeContract.ts +34 -62
- package/dist/deprecated.js +0 -2
- package/dist/deprecated.js.map +0 -1
- package/src/deprecated/TableId.test.ts +0 -33
- package/src/deprecated/TableId.ts +0 -41
- package/src/deprecated/getTableIds.ts +0 -6
- package/src/deprecated/index.ts +0 -2
- package/src/hexToResourceId.test.ts +0 -11
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Hex, stringToHex, concatHex } from "viem";
|
|
2
|
-
import {
|
|
2
|
+
import { Resource } from "./common";
|
|
3
3
|
import { ResourceType } from "./resourceTypes";
|
|
4
4
|
|
|
5
5
|
/** @internal */
|
|
@@ -13,11 +13,11 @@ export const resourceTypeIds = {
|
|
|
13
13
|
system: "sy",
|
|
14
14
|
} as const satisfies Record<ResourceType, string>;
|
|
15
15
|
|
|
16
|
-
export function
|
|
17
|
-
const typeId = resourceTypeIds[
|
|
16
|
+
export function resourceToHex(resource: Omit<Resource, "resourceId">): Hex {
|
|
17
|
+
const typeId = resourceTypeIds[resource.type];
|
|
18
18
|
return concatHex([
|
|
19
19
|
stringToHex(typeId, { size: 2 }),
|
|
20
|
-
stringToHex(
|
|
21
|
-
stringToHex(
|
|
20
|
+
stringToHex(resource.namespace.slice(0, 14), { size: 14 }),
|
|
21
|
+
stringToHex(resource.name.slice(0, 16), { size: 16 }),
|
|
22
22
|
]);
|
|
23
23
|
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Account,
|
|
3
|
+
CallParameters,
|
|
4
|
+
Chain,
|
|
5
|
+
Client,
|
|
6
|
+
SendTransactionParameters,
|
|
7
|
+
Transport,
|
|
8
|
+
WriteContractReturnType,
|
|
9
|
+
} from "viem";
|
|
10
|
+
import { call, sendTransaction as viem_sendTransaction } from "viem/actions";
|
|
11
|
+
import pRetry from "p-retry";
|
|
12
|
+
import { debug as parentDebug } from "./debug";
|
|
13
|
+
import { getNonceManager } from "./getNonceManager";
|
|
14
|
+
import { parseAccount } from "viem/accounts";
|
|
15
|
+
|
|
16
|
+
const debug = parentDebug.extend("sendTransaction");
|
|
17
|
+
|
|
18
|
+
// TODO: migrate away from this approach once we can hook into viem's nonce management: https://github.com/wagmi-dev/viem/discussions/1230
|
|
19
|
+
|
|
20
|
+
export async function sendTransaction<
|
|
21
|
+
TChain extends Chain | undefined,
|
|
22
|
+
TAccount extends Account | undefined,
|
|
23
|
+
TChainOverride extends Chain | undefined
|
|
24
|
+
>(
|
|
25
|
+
client: Client<Transport, TChain, TAccount>,
|
|
26
|
+
request: SendTransactionParameters<TChain, TAccount, TChainOverride>
|
|
27
|
+
): Promise<WriteContractReturnType> {
|
|
28
|
+
const rawAccount = request.account ?? client.account;
|
|
29
|
+
if (!rawAccount) {
|
|
30
|
+
// TODO: replace with viem AccountNotFoundError once its exported
|
|
31
|
+
throw new Error("No account provided");
|
|
32
|
+
}
|
|
33
|
+
const account = parseAccount(rawAccount);
|
|
34
|
+
|
|
35
|
+
const nonceManager = await getNonceManager({
|
|
36
|
+
client,
|
|
37
|
+
address: account.address,
|
|
38
|
+
blockTag: "pending",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
async function prepare(): Promise<SendTransactionParameters<TChain, TAccount, TChainOverride>> {
|
|
42
|
+
if (request.gas) {
|
|
43
|
+
debug("gas provided, skipping simulate", request.to);
|
|
44
|
+
return request;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
debug("simulating tx to", request.to);
|
|
48
|
+
await call(client, {
|
|
49
|
+
...request,
|
|
50
|
+
blockTag: "pending",
|
|
51
|
+
account,
|
|
52
|
+
} as CallParameters<TChain>);
|
|
53
|
+
|
|
54
|
+
// TODO: estimate gas
|
|
55
|
+
|
|
56
|
+
return request;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const preparedRequest = await prepare();
|
|
60
|
+
|
|
61
|
+
return await nonceManager.mempoolQueue.add(
|
|
62
|
+
() =>
|
|
63
|
+
pRetry(
|
|
64
|
+
async () => {
|
|
65
|
+
if (!nonceManager.hasNonce()) {
|
|
66
|
+
await nonceManager.resetNonce();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const nonce = nonceManager.nextNonce();
|
|
70
|
+
debug("sending tx with nonce", nonce, "to", preparedRequest.to);
|
|
71
|
+
return await viem_sendTransaction(client, { nonce, ...preparedRequest });
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
retries: 3,
|
|
75
|
+
onFailedAttempt: async (error) => {
|
|
76
|
+
// On nonce errors, reset the nonce and retry
|
|
77
|
+
if (nonceManager.shouldResetNonce(error)) {
|
|
78
|
+
debug("got nonce error, retrying", error.message);
|
|
79
|
+
await nonceManager.resetNonce();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// TODO: prepare again if there are gas errors?
|
|
83
|
+
throw error;
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
),
|
|
87
|
+
{ throwOnTimeout: true }
|
|
88
|
+
);
|
|
89
|
+
}
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, expectTypeOf, it } from "vitest";
|
|
2
|
+
import { mapObject } from "./mapObject";
|
|
3
|
+
import { assertExhaustive } from "./assertExhaustive";
|
|
4
|
+
|
|
5
|
+
describe("mapObject", () => {
|
|
6
|
+
it("should map the source to the target", () => {
|
|
7
|
+
const source = {
|
|
8
|
+
hello: "world",
|
|
9
|
+
foo: "bar",
|
|
10
|
+
} as const;
|
|
11
|
+
|
|
12
|
+
type Mapped<T extends Record<string, string>> = { [key in keyof T]: `mapped-${T[key]}` };
|
|
13
|
+
|
|
14
|
+
const target = mapObject<typeof source, Mapped<typeof source>>(source, (value, key) => {
|
|
15
|
+
if (key === "hello") return `mapped-${value}`;
|
|
16
|
+
if (key === "foo") return `mapped-${value}`;
|
|
17
|
+
assertExhaustive(key);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
expect(target).toEqual({ hello: `mapped-world`, foo: `mapped-bar` });
|
|
21
|
+
expectTypeOf<typeof target>().toEqualTypeOf<Mapped<typeof source>>();
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map each key of a source object via a given valueMap function
|
|
3
|
+
*/
|
|
4
|
+
export function mapObject<
|
|
5
|
+
Source extends Record<string | number | symbol, unknown>,
|
|
6
|
+
Target extends { [key in keyof Source]: unknown }
|
|
7
|
+
>(source: Source, valueMap: (value: Source[typeof key], key: keyof Source) => Target[typeof key]): Target {
|
|
8
|
+
return Object.fromEntries(
|
|
9
|
+
Object.entries(source).map(([key, value]) => [key, valueMap(value as Source[keyof Source], key)])
|
|
10
|
+
) as Target;
|
|
11
|
+
}
|
package/src/writeContract.ts
CHANGED
|
@@ -3,7 +3,6 @@ import {
|
|
|
3
3
|
Account,
|
|
4
4
|
Chain,
|
|
5
5
|
Client,
|
|
6
|
-
Hex,
|
|
7
6
|
SimulateContractParameters,
|
|
8
7
|
Transport,
|
|
9
8
|
WriteContractParameters,
|
|
@@ -16,29 +15,6 @@ import { getNonceManager } from "./getNonceManager";
|
|
|
16
15
|
import { parseAccount } from "viem/accounts";
|
|
17
16
|
|
|
18
17
|
const debug = parentDebug.extend("writeContract");
|
|
19
|
-
let nextWriteId = 0;
|
|
20
|
-
|
|
21
|
-
export type ContractWrite<
|
|
22
|
-
TAbi extends Abi | readonly unknown[] = Abi,
|
|
23
|
-
TFunctionName extends string = string,
|
|
24
|
-
TChain extends Chain | undefined = Chain,
|
|
25
|
-
TAccount extends Account | undefined = Account | undefined,
|
|
26
|
-
TChainOverride extends Chain | undefined = Chain | undefined
|
|
27
|
-
> = {
|
|
28
|
-
id: string;
|
|
29
|
-
request: WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>;
|
|
30
|
-
result: Promise<Hex>;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
export type WriteContractOptions<
|
|
34
|
-
TAbi extends Abi | readonly unknown[] = Abi,
|
|
35
|
-
TFunctionName extends string = string,
|
|
36
|
-
TChain extends Chain | undefined = Chain,
|
|
37
|
-
TAccount extends Account | undefined = Account | undefined,
|
|
38
|
-
TChainOverride extends Chain | undefined = Chain | undefined
|
|
39
|
-
> = WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride> & {
|
|
40
|
-
onWrite?: (write: ContractWrite<TAbi, TFunctionName, TChain, TAccount, TChainOverride>) => void;
|
|
41
|
-
};
|
|
42
18
|
|
|
43
19
|
// TODO: migrate away from this approach once we can hook into viem's nonce management: https://github.com/wagmi-dev/viem/discussions/1230
|
|
44
20
|
|
|
@@ -50,71 +26,67 @@ export async function writeContract<
|
|
|
50
26
|
TChainOverride extends Chain | undefined
|
|
51
27
|
>(
|
|
52
28
|
client: Client<Transport, TChain, TAccount>,
|
|
53
|
-
|
|
29
|
+
request: WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>
|
|
54
30
|
): Promise<WriteContractReturnType> {
|
|
55
|
-
const
|
|
56
|
-
|
|
57
|
-
const account_ = request.account ?? client.account;
|
|
58
|
-
if (!account_) {
|
|
31
|
+
const rawAccount = request.account ?? client.account;
|
|
32
|
+
if (!rawAccount) {
|
|
59
33
|
// TODO: replace with viem AccountNotFoundError once its exported
|
|
60
34
|
throw new Error("No account provided");
|
|
61
35
|
}
|
|
62
|
-
const account = parseAccount(
|
|
36
|
+
const account = parseAccount(rawAccount);
|
|
63
37
|
|
|
64
38
|
const nonceManager = await getNonceManager({
|
|
65
39
|
client,
|
|
66
40
|
address: account.address,
|
|
41
|
+
blockTag: "pending",
|
|
67
42
|
});
|
|
68
43
|
|
|
69
44
|
async function prepareWrite(): Promise<
|
|
70
45
|
WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>
|
|
71
46
|
> {
|
|
72
47
|
if (request.gas) {
|
|
73
|
-
debug("gas provided, skipping simulate", request);
|
|
48
|
+
debug("gas provided, skipping simulate", request.functionName, request.address);
|
|
74
49
|
return request;
|
|
75
50
|
}
|
|
76
51
|
|
|
77
|
-
debug("simulating
|
|
52
|
+
debug("simulating", request.functionName, "at", request.address);
|
|
78
53
|
const result = await simulateContract<TChain, TAbi, TFunctionName, TChainOverride>(client, {
|
|
79
54
|
...request,
|
|
55
|
+
blockTag: "pending",
|
|
80
56
|
account,
|
|
81
57
|
} as unknown as SimulateContractParameters<TAbi, TFunctionName, TChain, TChainOverride>);
|
|
82
58
|
|
|
83
59
|
return result.request as unknown as WriteContractParameters<TAbi, TFunctionName, TChain, TAccount, TChainOverride>;
|
|
84
60
|
}
|
|
85
61
|
|
|
86
|
-
|
|
87
|
-
const preparedWrite = await prepareWrite();
|
|
62
|
+
const preparedWrite = await prepareWrite();
|
|
88
63
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
const nonce = nonceManager.nextNonce();
|
|
96
|
-
debug("calling write function with nonce", nonce, preparedWrite);
|
|
97
|
-
return await viem_writeContract(client, { nonce, ...preparedWrite } as typeof preparedWrite);
|
|
98
|
-
},
|
|
99
|
-
{
|
|
100
|
-
retries: 3,
|
|
101
|
-
onFailedAttempt: async (error) => {
|
|
102
|
-
// On nonce errors, reset the nonce and retry
|
|
103
|
-
if (nonceManager.shouldResetNonce(error)) {
|
|
104
|
-
debug("got nonce error, retrying", error);
|
|
64
|
+
return nonceManager.mempoolQueue.add(
|
|
65
|
+
() =>
|
|
66
|
+
pRetry(
|
|
67
|
+
async () => {
|
|
68
|
+
if (!nonceManager.hasNonce()) {
|
|
105
69
|
await nonceManager.resetNonce();
|
|
106
|
-
return;
|
|
107
70
|
}
|
|
108
|
-
// TODO: prepareWrite again if there are gas errors?
|
|
109
|
-
throw error;
|
|
110
|
-
},
|
|
111
|
-
}
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const result = write();
|
|
116
|
-
|
|
117
|
-
onWrite?.({ id: `${nextWriteId++}`, request, result });
|
|
118
71
|
|
|
119
|
-
|
|
72
|
+
const nonce = nonceManager.nextNonce();
|
|
73
|
+
debug("calling", preparedWrite.functionName, "with nonce", nonce, "at", preparedWrite.address);
|
|
74
|
+
return await viem_writeContract(client, { nonce, ...preparedWrite } as typeof preparedWrite);
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
retries: 3,
|
|
78
|
+
onFailedAttempt: async (error) => {
|
|
79
|
+
// On nonce errors, reset the nonce and retry
|
|
80
|
+
if (nonceManager.shouldResetNonce(error)) {
|
|
81
|
+
debug("got nonce error, retrying", error.message);
|
|
82
|
+
await nonceManager.resetNonce();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
// TODO: prepareWrite again if there are gas errors?
|
|
86
|
+
throw error;
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
),
|
|
90
|
+
{ throwOnTimeout: true }
|
|
91
|
+
);
|
|
120
92
|
}
|
package/dist/deprecated.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{stringToHex as i,hexToString as m,sliceHex as o,concatHex as c}from"viem";var n=class{namespace;name;constructor(e,t){this.namespace=e.substring(0,16),this.name=t.substring(0,16)}toString(){return`TableId<${this.namespace||"[empty]"}:${this.name||"[empty]"}>`}toHex(){return n.toHex(this.namespace,this.name)}static toHex(e,t){return c([i(e.substring(0,16),{size:16}),i(t.substring(0,16),{size:16})])}static fromHex(e){let t=m(o(e,0,16)).replace(/\0+$/,""),s=m(o(e,16,32)).replace(/\0+$/,"");return new n(t,s)}static parse(e){let t=e.match(/^TableId<(.+?):(.+?)>$/);if(!t)return null;let[,s,a]=t;return new n(s==="[empty]"?"":s,a==="[empty]"?"":a)}};function x(r){return Object.keys(r.tables).map(e=>new n(r.namespace,e))}export{n as TableId,x as getTableIds};
|
|
2
|
-
//# sourceMappingURL=deprecated.js.map
|
package/dist/deprecated.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/deprecated/TableId.ts","../src/deprecated/getTableIds.ts"],"sourcesContent":["import { Hex, stringToHex, hexToString, sliceHex, concatHex } from \"viem\";\n\n/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */\nexport class TableId {\n readonly namespace: string;\n readonly name: string;\n\n constructor(namespace: string, name: string) {\n this.namespace = namespace.substring(0, 16);\n this.name = name.substring(0, 16);\n }\n\n toString(): string {\n return `TableId<${this.namespace || \"[empty]\"}:${this.name || \"[empty]\"}>`;\n }\n\n toHex(): Hex {\n return TableId.toHex(this.namespace, this.name);\n }\n\n static toHex(namespace: string, name: string): Hex {\n return concatHex([\n stringToHex(namespace.substring(0, 16), { size: 16 }),\n stringToHex(name.substring(0, 16), { size: 16 }),\n ]);\n }\n\n static fromHex(hex: Hex): TableId {\n const namespace = hexToString(sliceHex(hex, 0, 16)).replace(/\\0+$/, \"\");\n const name = hexToString(sliceHex(hex, 16, 32)).replace(/\\0+$/, \"\");\n return new TableId(namespace, name);\n }\n\n /** @deprecated Don't use this! This is a temporary hack for v2<>v1 compatibility until we can write v2 client libraries. This is here so it stays close to the formatting of `toString()` above. */\n static parse(tableIdString: string): TableId | null {\n const match = tableIdString.match(/^TableId<(.+?):(.+?)>$/);\n if (!match) return null;\n const [, namespace, name] = match;\n return new TableId(namespace === \"[empty]\" ? \"\" : namespace, name === \"[empty]\" ? \"\" : name);\n }\n}\n","import { TableId } from \"./TableId\";\n\n/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */\nexport function getTableIds(config: { namespace: string; tables: { [key: string]: unknown } }): TableId[] {\n return Object.keys(config.tables).map((table) => new TableId(config.namespace, table));\n}\n"],"mappings":"AAAA,OAAc,eAAAA,EAAa,eAAAC,EAAa,YAAAC,EAAU,aAAAC,MAAiB,OAG5D,IAAMC,EAAN,KAAc,CACV,UACA,KAET,YAAYC,EAAmBC,EAAc,CAC3C,KAAK,UAAYD,EAAU,UAAU,EAAG,EAAE,EAC1C,KAAK,KAAOC,EAAK,UAAU,EAAG,EAAE,CAClC,CAEA,UAAmB,CACjB,MAAO,WAAW,KAAK,WAAa,aAAa,KAAK,MAAQ,YAChE,CAEA,OAAa,CACX,OAAOF,EAAQ,MAAM,KAAK,UAAW,KAAK,IAAI,CAChD,CAEA,OAAO,MAAMC,EAAmBC,EAAmB,CACjD,OAAOH,EAAU,CACfH,EAAYK,EAAU,UAAU,EAAG,EAAE,EAAG,CAAE,KAAM,EAAG,CAAC,EACpDL,EAAYM,EAAK,UAAU,EAAG,EAAE,EAAG,CAAE,KAAM,EAAG,CAAC,CACjD,CAAC,CACH,CAEA,OAAO,QAAQC,EAAmB,CAChC,IAAMF,EAAYJ,EAAYC,EAASK,EAAK,EAAG,EAAE,CAAC,EAAE,QAAQ,OAAQ,EAAE,EAChED,EAAOL,EAAYC,EAASK,EAAK,GAAI,EAAE,CAAC,EAAE,QAAQ,OAAQ,EAAE,EAClE,OAAO,IAAIH,EAAQC,EAAWC,CAAI,CACpC,CAGA,OAAO,MAAME,EAAuC,CAClD,IAAMC,EAAQD,EAAc,MAAM,wBAAwB,EAC1D,GAAI,CAACC,EAAO,OAAO,KACnB,GAAM,CAAC,CAAEJ,EAAWC,CAAI,EAAIG,EAC5B,OAAO,IAAIL,EAAQC,IAAc,UAAY,GAAKA,EAAWC,IAAS,UAAY,GAAKA,CAAI,CAC7F,CACF,ECrCO,SAASI,EAAYC,EAA8E,CACxG,OAAO,OAAO,KAAKA,EAAO,MAAM,EAAE,IAAKC,GAAU,IAAIC,EAAQF,EAAO,UAAWC,CAAK,CAAC,CACvF","names":["stringToHex","hexToString","sliceHex","concatHex","TableId","namespace","name","hex","tableIdString","match","getTableIds","config","table","TableId"]}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { TableId } from "./TableId";
|
|
3
|
-
|
|
4
|
-
describe("TableId", () => {
|
|
5
|
-
it("can convert to hex string", () => {
|
|
6
|
-
const tableId = new TableId("namespace", "name");
|
|
7
|
-
expect(tableId.toHex()).toMatchInlineSnapshot(
|
|
8
|
-
'"0x6e616d657370616365000000000000006e616d65000000000000000000000000"'
|
|
9
|
-
);
|
|
10
|
-
});
|
|
11
|
-
|
|
12
|
-
it("can convert from hex string", () => {
|
|
13
|
-
const tableId = TableId.fromHex("0x6e616d657370616365000000000000006e616d65000000000000000000000000");
|
|
14
|
-
expect(tableId.namespace).toMatchInlineSnapshot('"namespace"');
|
|
15
|
-
expect(tableId.name).toMatchInlineSnapshot('"name"');
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("truncates namespaces >16 bytes", () => {
|
|
19
|
-
const hex = "0x41566572794c6f6e674e616d657370616e616d65000000000000000000000000";
|
|
20
|
-
expect(TableId.toHex("AVeryLongNamespace", "name")).toEqual(hex);
|
|
21
|
-
const tableId = new TableId("AVeryLongNamespace", "name");
|
|
22
|
-
expect(tableId.toHex()).toEqual(hex);
|
|
23
|
-
expect(TableId.fromHex(tableId.toHex()).namespace).toMatchInlineSnapshot('"AVeryLongNamespa"');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it("truncates names >16 bytes", () => {
|
|
27
|
-
const hex = "0x6e616d65737061636500000000000000416e556e6e65636573736172696c794c";
|
|
28
|
-
expect(TableId.toHex("namespace", "AnUnnecessarilyLongName")).toEqual(hex);
|
|
29
|
-
const tableId = new TableId("namespace", "AnUnnecessarilyLongName");
|
|
30
|
-
expect(tableId.toHex()).toEqual(hex);
|
|
31
|
-
expect(TableId.fromHex(tableId.toHex()).name).toMatchInlineSnapshot('"AnUnnecessarilyL"');
|
|
32
|
-
});
|
|
33
|
-
});
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { Hex, stringToHex, hexToString, sliceHex, concatHex } from "viem";
|
|
2
|
-
|
|
3
|
-
/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */
|
|
4
|
-
export class TableId {
|
|
5
|
-
readonly namespace: string;
|
|
6
|
-
readonly name: string;
|
|
7
|
-
|
|
8
|
-
constructor(namespace: string, name: string) {
|
|
9
|
-
this.namespace = namespace.substring(0, 16);
|
|
10
|
-
this.name = name.substring(0, 16);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
toString(): string {
|
|
14
|
-
return `TableId<${this.namespace || "[empty]"}:${this.name || "[empty]"}>`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
toHex(): Hex {
|
|
18
|
-
return TableId.toHex(this.namespace, this.name);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
static toHex(namespace: string, name: string): Hex {
|
|
22
|
-
return concatHex([
|
|
23
|
-
stringToHex(namespace.substring(0, 16), { size: 16 }),
|
|
24
|
-
stringToHex(name.substring(0, 16), { size: 16 }),
|
|
25
|
-
]);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
static fromHex(hex: Hex): TableId {
|
|
29
|
-
const namespace = hexToString(sliceHex(hex, 0, 16)).replace(/\0+$/, "");
|
|
30
|
-
const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, "");
|
|
31
|
-
return new TableId(namespace, name);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** @deprecated Don't use this! This is a temporary hack for v2<>v1 compatibility until we can write v2 client libraries. This is here so it stays close to the formatting of `toString()` above. */
|
|
35
|
-
static parse(tableIdString: string): TableId | null {
|
|
36
|
-
const match = tableIdString.match(/^TableId<(.+?):(.+?)>$/);
|
|
37
|
-
if (!match) return null;
|
|
38
|
-
const [, namespace, name] = match;
|
|
39
|
-
return new TableId(namespace === "[empty]" ? "" : namespace, name === "[empty]" ? "" : name);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { TableId } from "./TableId";
|
|
2
|
-
|
|
3
|
-
/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */
|
|
4
|
-
export function getTableIds(config: { namespace: string; tables: { [key: string]: unknown } }): TableId[] {
|
|
5
|
-
return Object.keys(config.tables).map((table) => new TableId(config.namespace, table));
|
|
6
|
-
}
|
package/src/deprecated/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import { hexToResourceId } from "./hexToResourceId";
|
|
3
|
-
|
|
4
|
-
describe("hexToResourceId", () => {
|
|
5
|
-
it("can convert from hex string", () => {
|
|
6
|
-
const resourceId = hexToResourceId("0x74626e616d65737061636500000000006e616d65000000000000000000000000");
|
|
7
|
-
expect(resourceId.type).toMatchInlineSnapshot('"table"');
|
|
8
|
-
expect(resourceId.namespace).toMatchInlineSnapshot('"namespace"');
|
|
9
|
-
expect(resourceId.name).toMatchInlineSnapshot('"name"');
|
|
10
|
-
});
|
|
11
|
-
});
|