@totems/evm 1.0.2 → 1.0.5
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/package.json +21 -7
- package/test/constants.d.ts +11 -0
- package/test/constants.js +17 -0
- package/test/helpers.d.ts +73 -0
- package/test/helpers.js +300 -0
- package/test/helpers.ts +0 -466
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totems/evm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Totems EVM smart contracts for building modular token systems",
|
|
5
5
|
"author": "nsjames",
|
|
6
6
|
"license": "MIT & AGPL-3.0",
|
|
@@ -21,15 +21,29 @@
|
|
|
21
21
|
],
|
|
22
22
|
"files": [
|
|
23
23
|
"contracts/**/*.sol",
|
|
24
|
-
"constants/**/*.sol",
|
|
25
24
|
"interfaces/**/*.sol",
|
|
26
25
|
"mods/**/*.sol",
|
|
27
|
-
"test/**/*"
|
|
28
|
-
"LICENSE",
|
|
29
|
-
"package.json",
|
|
30
|
-
"README.md"
|
|
26
|
+
"test/**/*"
|
|
31
27
|
],
|
|
28
|
+
"exports": {
|
|
29
|
+
"./test/constants": {
|
|
30
|
+
"types": "./test/constants.d.ts",
|
|
31
|
+
"import": "./test/constants.js",
|
|
32
|
+
"require": "./test/constants.js",
|
|
33
|
+
"default": "./test/constants.js"
|
|
34
|
+
},
|
|
35
|
+
"./test/helpers": {
|
|
36
|
+
"types": "./test/helpers.d.ts",
|
|
37
|
+
"import": "./test/helpers.js",
|
|
38
|
+
"require": "./test/helpers.js",
|
|
39
|
+
"default": "./test/helpers.js"
|
|
40
|
+
},
|
|
41
|
+
"./contracts/*": "./contracts/*",
|
|
42
|
+
"./interfaces/*": "./interfaces/*",
|
|
43
|
+
"./mods/*": "./mods/*"
|
|
44
|
+
},
|
|
32
45
|
"peerDependencies": {
|
|
33
|
-
"
|
|
46
|
+
"hardhat": "^3.1.4",
|
|
47
|
+
"viem": "^2.44.4"
|
|
34
48
|
}
|
|
35
49
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
2
|
+
export declare const MIN_BASE_FEE = 500000000000000n;
|
|
3
|
+
export declare const BURNED_FEE = 100000000000000n;
|
|
4
|
+
export declare const Hook: {
|
|
5
|
+
readonly Created: 0;
|
|
6
|
+
readonly Mint: 1;
|
|
7
|
+
readonly Burn: 2;
|
|
8
|
+
readonly Transfer: 3;
|
|
9
|
+
readonly TransferOwnership: 4;
|
|
10
|
+
};
|
|
11
|
+
export type HookType = typeof Hook[keyof typeof Hook];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// test/constants.ts
|
|
2
|
+
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
3
|
+
var MIN_BASE_FEE = 500000000000000n;
|
|
4
|
+
var BURNED_FEE = 100000000000000n;
|
|
5
|
+
var Hook = {
|
|
6
|
+
Created: 0,
|
|
7
|
+
Mint: 1,
|
|
8
|
+
Burn: 2,
|
|
9
|
+
Transfer: 3,
|
|
10
|
+
TransferOwnership: 4
|
|
11
|
+
};
|
|
12
|
+
export {
|
|
13
|
+
ZERO_ADDRESS,
|
|
14
|
+
MIN_BASE_FEE,
|
|
15
|
+
Hook,
|
|
16
|
+
BURNED_FEE
|
|
17
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
export { ZERO_ADDRESS, MIN_BASE_FEE, BURNED_FEE, Hook } from "./constants.js";
|
|
2
|
+
export type { HookType } from "./constants.js";
|
|
3
|
+
/**
|
|
4
|
+
* Computes the 4-byte selector for a custom error signature
|
|
5
|
+
* @param signature Error signature like "NotLicensed()" or "InsufficientBalance(uint256,uint256)"
|
|
6
|
+
*/
|
|
7
|
+
export declare function errorSelector(signature: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Asserts that a promise rejects with a specific custom error
|
|
10
|
+
* @param promise The promise to test
|
|
11
|
+
* @param expectedSelector The expected error selector (use errorSelector() to compute)
|
|
12
|
+
* @param errorName Human-readable error name for assertion messages
|
|
13
|
+
*/
|
|
14
|
+
export declare function expectCustomError(promise: Promise<any>, expectedSelector: string, errorName: string): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Asserts that a promise rejects with a string revert message
|
|
17
|
+
* Error(string) selector is 0x08c379a0
|
|
18
|
+
*/
|
|
19
|
+
export declare function expectRevertMessage(promise: Promise<any>, expectedMessage: string | RegExp): Promise<void>;
|
|
20
|
+
export declare const ErrorSelectors: {
|
|
21
|
+
InvalidModEventOrigin: string;
|
|
22
|
+
NotLicensed: string;
|
|
23
|
+
Unauthorized: string;
|
|
24
|
+
TotemNotFound: string;
|
|
25
|
+
TotemNotActive: string;
|
|
26
|
+
InsufficientBalance: string;
|
|
27
|
+
CantSetLicense: string;
|
|
28
|
+
};
|
|
29
|
+
export declare const setupTotemsTest: (minBaseFee?: bigint, burnedFee?: bigint) => Promise<{
|
|
30
|
+
viem: any;
|
|
31
|
+
publicClient: any;
|
|
32
|
+
market: any;
|
|
33
|
+
totems: any;
|
|
34
|
+
accounts: any;
|
|
35
|
+
proxyModSeller: any;
|
|
36
|
+
proxyMod: any;
|
|
37
|
+
}>;
|
|
38
|
+
export declare const modDetails: (details?: any) => any;
|
|
39
|
+
export declare const publishMod: (market: any, seller: string, contract: string, hooks?: number[], details?: any, requiredActions?: any[], referrer?: string, price?: bigint, fee?: undefined) => Promise<any>;
|
|
40
|
+
export declare const totemDetails: (ticker: string, decimals: number) => {
|
|
41
|
+
ticker: string;
|
|
42
|
+
decimals: number;
|
|
43
|
+
name: string;
|
|
44
|
+
description: string;
|
|
45
|
+
image: string;
|
|
46
|
+
website: string;
|
|
47
|
+
seed: string;
|
|
48
|
+
};
|
|
49
|
+
export declare const createTotem: (totems: any, market: any, creator: string, ticker: string, decimals: number, allocations: any[], mods?: {
|
|
50
|
+
transfer?: string[];
|
|
51
|
+
mint?: string[];
|
|
52
|
+
burn?: string[];
|
|
53
|
+
created?: string[];
|
|
54
|
+
transferOwnership?: string[];
|
|
55
|
+
}, referrer?: string, details?: any) => Promise<any>;
|
|
56
|
+
export declare const transfer: (totems: any, ticker: string, from: string, to: string, amount: number | bigint, memo?: string) => Promise<any>;
|
|
57
|
+
export declare const mint: (totems: any, mod: string, minter: string, ticker: string, amount: number | bigint, memo?: string, payment?: number | bigint) => Promise<any>;
|
|
58
|
+
export declare const burn: (totems: any, ticker: string, owner: string, amount: number | bigint, memo?: string) => Promise<any>;
|
|
59
|
+
export declare const getBalance: (totems: any, ticker: string, account: string) => Promise<any>;
|
|
60
|
+
export declare const getTotem: (totems: any, ticker: string) => Promise<any>;
|
|
61
|
+
export declare const getTotems: (totems: any, tickers: string[]) => Promise<any>;
|
|
62
|
+
export declare const getStats: (totems: any, ticker: string) => Promise<any>;
|
|
63
|
+
export declare const transferOwnership: (totems: any, ticker: string, currentOwner: string, newOwner: string) => Promise<any>;
|
|
64
|
+
export declare const getMod: (market: any, mod: string) => Promise<any>;
|
|
65
|
+
export declare const getMods: (market: any, mods: string[]) => Promise<any>;
|
|
66
|
+
export declare const getModFee: (market: any, mod: string) => Promise<any>;
|
|
67
|
+
export declare const getModsFee: (market: any, mods: string[]) => Promise<any>;
|
|
68
|
+
export declare const isLicensed: (totems: any, ticker: string, mod: string) => Promise<any>;
|
|
69
|
+
export declare const getRelays: (totems: any, ticker: string) => Promise<any>;
|
|
70
|
+
export declare const getSupportedHooks: (market: any, mod: string) => Promise<any>;
|
|
71
|
+
export declare const isUnlimitedMinter: (market: any, mod: string) => Promise<any>;
|
|
72
|
+
export declare const addMod: (proxyMod: any, totems: any, market: any, ticker: string, hooks: number[], mod: string, caller: string, referrer?: string) => Promise<any>;
|
|
73
|
+
export declare const removeMod: (proxyMod: any, ticker: string, mod: string, caller: string) => Promise<any>;
|
package/test/helpers.js
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
// test/helpers.ts
|
|
2
|
+
import {network} from "hardhat";
|
|
3
|
+
import {keccak256, toBytes, decodeErrorResult} from "viem";
|
|
4
|
+
|
|
5
|
+
// test/constants.ts
|
|
6
|
+
var ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
7
|
+
var MIN_BASE_FEE = 500000000000000n;
|
|
8
|
+
var BURNED_FEE = 100000000000000n;
|
|
9
|
+
var Hook = {
|
|
10
|
+
Created: 0,
|
|
11
|
+
Mint: 1,
|
|
12
|
+
Burn: 2,
|
|
13
|
+
Transfer: 3,
|
|
14
|
+
TransferOwnership: 4
|
|
15
|
+
};
|
|
16
|
+
// test/helpers.ts
|
|
17
|
+
function errorSelector(signature) {
|
|
18
|
+
return keccak256(toBytes(signature)).slice(0, 10);
|
|
19
|
+
}
|
|
20
|
+
var getErrorData = function(error) {
|
|
21
|
+
const data = error?.cause?.cause?.data || error?.cause?.data || error?.data || error?.message?.match(/return data: (0x[a-fA-F0-9]+)/)?.[1] || error?.message?.match(/data: (0x[a-fA-F0-9]+)/)?.[1];
|
|
22
|
+
return data || null;
|
|
23
|
+
};
|
|
24
|
+
async function expectCustomError(promise, expectedSelector, errorName) {
|
|
25
|
+
try {
|
|
26
|
+
await promise;
|
|
27
|
+
throw new Error(`Expected ${errorName} but transaction succeeded`);
|
|
28
|
+
} catch (e) {
|
|
29
|
+
if (e.message?.startsWith(`Expected ${errorName}`))
|
|
30
|
+
throw e;
|
|
31
|
+
const data = getErrorData(e);
|
|
32
|
+
if (!data) {
|
|
33
|
+
throw new Error(`Expected ${errorName} but got error without revert data: ${e.message}`);
|
|
34
|
+
}
|
|
35
|
+
const actualSelector = data.slice(0, 10).toLowerCase();
|
|
36
|
+
const expected = expectedSelector.toLowerCase();
|
|
37
|
+
if (actualSelector !== expected) {
|
|
38
|
+
throw new Error(`Expected ${errorName} (${expected}) but got selector ${actualSelector}\nFull data: ${data}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
async function expectRevertMessage(promise, expectedMessage) {
|
|
43
|
+
const ERROR_STRING_SELECTOR = "0x08c379a0";
|
|
44
|
+
try {
|
|
45
|
+
await promise;
|
|
46
|
+
throw new Error(`Expected revert with "${expectedMessage}" but transaction succeeded`);
|
|
47
|
+
} catch (e) {
|
|
48
|
+
if (e.message?.startsWith("Expected revert"))
|
|
49
|
+
throw e;
|
|
50
|
+
const data = getErrorData(e);
|
|
51
|
+
if (!data) {
|
|
52
|
+
const matches = typeof expectedMessage === "string" ? e.message?.includes(expectedMessage) : expectedMessage.test(e.message);
|
|
53
|
+
if (!matches) {
|
|
54
|
+
throw new Error(`Expected revert with "${expectedMessage}" but got: ${e.message}`);
|
|
55
|
+
}
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const selector = data.slice(0, 10).toLowerCase();
|
|
59
|
+
if (selector !== ERROR_STRING_SELECTOR) {
|
|
60
|
+
const matches = typeof expectedMessage === "string" ? e.message?.includes(expectedMessage) : expectedMessage.test(e.message);
|
|
61
|
+
if (!matches) {
|
|
62
|
+
throw new Error(`Expected string revert but got custom error with selector ${selector}`);
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const abi = [{
|
|
68
|
+
type: "error",
|
|
69
|
+
name: "Error",
|
|
70
|
+
inputs: [{ name: "message", type: "string" }]
|
|
71
|
+
}];
|
|
72
|
+
const decoded = decodeErrorResult({ abi, data });
|
|
73
|
+
const message = decoded.args[0];
|
|
74
|
+
const matches = typeof expectedMessage === "string" ? message.includes(expectedMessage) : expectedMessage.test(message);
|
|
75
|
+
if (!matches) {
|
|
76
|
+
throw new Error(`Expected revert with "${expectedMessage}" but got "${message}"`);
|
|
77
|
+
}
|
|
78
|
+
} catch (decodeError) {
|
|
79
|
+
const matches = typeof expectedMessage === "string" ? e.message?.includes(expectedMessage) : expectedMessage.test(e.message);
|
|
80
|
+
if (!matches) {
|
|
81
|
+
throw new Error(`Expected revert with "${expectedMessage}" but decoding failed: ${e.message}`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
var ErrorSelectors = {
|
|
87
|
+
InvalidModEventOrigin: errorSelector("InvalidModEventOrigin()"),
|
|
88
|
+
NotLicensed: errorSelector("NotLicensed()"),
|
|
89
|
+
Unauthorized: errorSelector("Unauthorized()"),
|
|
90
|
+
TotemNotFound: errorSelector("TotemNotFound(string)"),
|
|
91
|
+
TotemNotActive: errorSelector("TotemNotActive()"),
|
|
92
|
+
InsufficientBalance: errorSelector("InsufficientBalance(uint256,uint256)"),
|
|
93
|
+
CantSetLicense: errorSelector("CantSetLicense()")
|
|
94
|
+
};
|
|
95
|
+
var setupTotemsTest = async (minBaseFee = MIN_BASE_FEE, burnedFee = BURNED_FEE) => {
|
|
96
|
+
const { viem } = await network.connect();
|
|
97
|
+
const publicClient = await viem.getPublicClient();
|
|
98
|
+
const walletClient = await viem.getWalletClient();
|
|
99
|
+
const addresses = await walletClient.getAddresses();
|
|
100
|
+
const proxyModInitializer = addresses[0];
|
|
101
|
+
const proxyMod = await viem.deployContract("ProxyMod", [
|
|
102
|
+
proxyModInitializer
|
|
103
|
+
]);
|
|
104
|
+
let market = await viem.deployContract("ModMarket", [minBaseFee, burnedFee]);
|
|
105
|
+
let totems = await viem.deployContract("Totems", [
|
|
106
|
+
market.address,
|
|
107
|
+
proxyMod.address,
|
|
108
|
+
minBaseFee,
|
|
109
|
+
burnedFee
|
|
110
|
+
]);
|
|
111
|
+
totems = await viem.getContractAt("ITotems", totems.address);
|
|
112
|
+
market = await viem.getContractAt("IMarket", market.address);
|
|
113
|
+
await proxyMod.write.initialize([totems.address, market.address], { account: proxyModInitializer });
|
|
114
|
+
return {
|
|
115
|
+
viem,
|
|
116
|
+
publicClient,
|
|
117
|
+
market,
|
|
118
|
+
totems,
|
|
119
|
+
accounts: addresses.slice(0, addresses.length),
|
|
120
|
+
proxyModSeller: addresses[0],
|
|
121
|
+
proxyMod
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
var modDetails = (details) => Object.assign({
|
|
125
|
+
name: "Test Mod",
|
|
126
|
+
summary: "A test mod",
|
|
127
|
+
markdown: "## Test Mod\nThis is a test mod.",
|
|
128
|
+
image: "https://example.com/image.png",
|
|
129
|
+
website: "https://example.com",
|
|
130
|
+
websiteTickerPath: "/path/to/{ticker}",
|
|
131
|
+
isMinter: false,
|
|
132
|
+
needsUnlimited: false
|
|
133
|
+
}, details || {});
|
|
134
|
+
var publishMod = async (market, seller, contract, hooks = [], details = modDetails(), requiredActions = [], referrer = ZERO_ADDRESS, price = 1000000n, fee = undefined) => {
|
|
135
|
+
fee = fee ?? await market.read.getFee([referrer]);
|
|
136
|
+
return market.write.publish([
|
|
137
|
+
contract,
|
|
138
|
+
hooks,
|
|
139
|
+
price,
|
|
140
|
+
details,
|
|
141
|
+
requiredActions,
|
|
142
|
+
referrer
|
|
143
|
+
], { value: fee, account: seller });
|
|
144
|
+
};
|
|
145
|
+
var totemDetails = (ticker, decimals) => {
|
|
146
|
+
return {
|
|
147
|
+
ticker,
|
|
148
|
+
decimals,
|
|
149
|
+
name: `${ticker} Totem`,
|
|
150
|
+
description: `This is the ${ticker} totem.`,
|
|
151
|
+
image: `https://example.com/${ticker.toLowerCase()}.png`,
|
|
152
|
+
website: `https://example.com/${ticker.toLowerCase()}`,
|
|
153
|
+
seed: "0x1110762033e7a10db4502359a19a61eb81312834769b8419047a2c9ae03ee847"
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
var createTotem = async (totems, market, creator, ticker, decimals, allocations, mods, referrer = ZERO_ADDRESS, details = undefined) => {
|
|
157
|
+
const baseFee = await totems.read.getFee([referrer]);
|
|
158
|
+
const _mods = Object.assign({
|
|
159
|
+
transfer: [],
|
|
160
|
+
mint: [],
|
|
161
|
+
burn: [],
|
|
162
|
+
created: [],
|
|
163
|
+
transferOwnership: []
|
|
164
|
+
}, mods || {});
|
|
165
|
+
const uniqueMods = new Set;
|
|
166
|
+
Object.values(_mods).forEach((modList) => {
|
|
167
|
+
modList.forEach((m) => uniqueMods.add(m));
|
|
168
|
+
});
|
|
169
|
+
const modsFee = await market.read.getModsFee([[...uniqueMods]]);
|
|
170
|
+
return await totems.write.create([
|
|
171
|
+
details ? Object.assign({
|
|
172
|
+
ticker,
|
|
173
|
+
decimals
|
|
174
|
+
}, details) : totemDetails(ticker, decimals),
|
|
175
|
+
allocations.map((a) => ({
|
|
176
|
+
...a,
|
|
177
|
+
label: a.label || "",
|
|
178
|
+
isMinter: a.hasOwnProperty("isMinter") ? a.isMinter : false
|
|
179
|
+
})),
|
|
180
|
+
_mods,
|
|
181
|
+
referrer
|
|
182
|
+
], { account: creator, value: baseFee + modsFee });
|
|
183
|
+
};
|
|
184
|
+
var transfer = async (totems, ticker, from, to, amount, memo = "") => {
|
|
185
|
+
return await totems.write.transfer([
|
|
186
|
+
ticker,
|
|
187
|
+
from,
|
|
188
|
+
to,
|
|
189
|
+
amount,
|
|
190
|
+
memo
|
|
191
|
+
], { account: from });
|
|
192
|
+
};
|
|
193
|
+
var mint = async (totems, mod, minter, ticker, amount, memo = "", payment = 0n) => {
|
|
194
|
+
return await totems.write.mint([
|
|
195
|
+
mod,
|
|
196
|
+
minter,
|
|
197
|
+
ticker,
|
|
198
|
+
amount,
|
|
199
|
+
memo
|
|
200
|
+
], { account: minter, value: payment });
|
|
201
|
+
};
|
|
202
|
+
var burn = async (totems, ticker, owner, amount, memo = "") => {
|
|
203
|
+
return await totems.write.burn([
|
|
204
|
+
ticker,
|
|
205
|
+
owner,
|
|
206
|
+
amount,
|
|
207
|
+
memo
|
|
208
|
+
], { account: owner });
|
|
209
|
+
};
|
|
210
|
+
var getBalance = async (totems, ticker, account) => {
|
|
211
|
+
return await totems.read.getBalance([ticker, account]);
|
|
212
|
+
};
|
|
213
|
+
var getTotem = async (totems, ticker) => {
|
|
214
|
+
return await totems.read.getTotem([ticker]);
|
|
215
|
+
};
|
|
216
|
+
var getTotems = async (totems, tickers) => {
|
|
217
|
+
return await totems.read.getTotems([tickers]);
|
|
218
|
+
};
|
|
219
|
+
var getStats = async (totems, ticker) => {
|
|
220
|
+
return await totems.read.getStats([ticker]);
|
|
221
|
+
};
|
|
222
|
+
var transferOwnership = async (totems, ticker, currentOwner, newOwner) => {
|
|
223
|
+
return await totems.write.transferOwnership([
|
|
224
|
+
ticker,
|
|
225
|
+
newOwner
|
|
226
|
+
], { account: currentOwner });
|
|
227
|
+
};
|
|
228
|
+
var getMod = async (market, mod) => {
|
|
229
|
+
return await market.read.getMod([mod]);
|
|
230
|
+
};
|
|
231
|
+
var getMods = async (market, mods) => {
|
|
232
|
+
return await market.read.getMods([mods]);
|
|
233
|
+
};
|
|
234
|
+
var getModFee = async (market, mod) => {
|
|
235
|
+
return await market.read.getModFee([mod]);
|
|
236
|
+
};
|
|
237
|
+
var getModsFee = async (market, mods) => {
|
|
238
|
+
return await market.read.getModsFee([mods]);
|
|
239
|
+
};
|
|
240
|
+
var isLicensed = async (totems, ticker, mod) => {
|
|
241
|
+
return await totems.read.isLicensed([ticker, mod]);
|
|
242
|
+
};
|
|
243
|
+
var getRelays = async (totems, ticker) => {
|
|
244
|
+
return await totems.read.getRelays([ticker]);
|
|
245
|
+
};
|
|
246
|
+
var getSupportedHooks = async (market, mod) => {
|
|
247
|
+
return await market.read.getSupportedHooks([mod]);
|
|
248
|
+
};
|
|
249
|
+
var isUnlimitedMinter = async (market, mod) => {
|
|
250
|
+
return await market.read.isUnlimitedMinter([mod]);
|
|
251
|
+
};
|
|
252
|
+
var addMod = async (proxyMod, totems, market, ticker, hooks, mod, caller, referrer = ZERO_ADDRESS) => {
|
|
253
|
+
const modFee = await market.read.getModFee([mod]);
|
|
254
|
+
const referrerFee = await totems.read.getFee([referrer]);
|
|
255
|
+
return await proxyMod.write.addMod([
|
|
256
|
+
ticker,
|
|
257
|
+
hooks,
|
|
258
|
+
mod,
|
|
259
|
+
referrer
|
|
260
|
+
], { account: caller, value: modFee + referrerFee });
|
|
261
|
+
};
|
|
262
|
+
var removeMod = async (proxyMod, ticker, mod, caller) => {
|
|
263
|
+
return await proxyMod.write.removeMod([
|
|
264
|
+
ticker,
|
|
265
|
+
mod
|
|
266
|
+
], { account: caller });
|
|
267
|
+
};
|
|
268
|
+
export {
|
|
269
|
+
transferOwnership,
|
|
270
|
+
transfer,
|
|
271
|
+
totemDetails,
|
|
272
|
+
setupTotemsTest,
|
|
273
|
+
removeMod,
|
|
274
|
+
publishMod,
|
|
275
|
+
modDetails,
|
|
276
|
+
mint,
|
|
277
|
+
isUnlimitedMinter,
|
|
278
|
+
isLicensed,
|
|
279
|
+
getTotems,
|
|
280
|
+
getTotem,
|
|
281
|
+
getSupportedHooks,
|
|
282
|
+
getStats,
|
|
283
|
+
getRelays,
|
|
284
|
+
getModsFee,
|
|
285
|
+
getMods,
|
|
286
|
+
getModFee,
|
|
287
|
+
getMod,
|
|
288
|
+
getBalance,
|
|
289
|
+
expectRevertMessage,
|
|
290
|
+
expectCustomError,
|
|
291
|
+
errorSelector,
|
|
292
|
+
createTotem,
|
|
293
|
+
burn,
|
|
294
|
+
addMod,
|
|
295
|
+
ZERO_ADDRESS,
|
|
296
|
+
MIN_BASE_FEE,
|
|
297
|
+
Hook,
|
|
298
|
+
ErrorSelectors,
|
|
299
|
+
BURNED_FEE
|
|
300
|
+
};
|
package/test/helpers.ts
DELETED
|
@@ -1,466 +0,0 @@
|
|
|
1
|
-
import {network} from "hardhat";
|
|
2
|
-
import { keccak256, toBytes, decodeErrorResult, Abi } from "viem";
|
|
3
|
-
|
|
4
|
-
export const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Computes the 4-byte selector for a custom error signature
|
|
8
|
-
* @param signature Error signature like "NotLicensed()" or "InsufficientBalance(uint256,uint256)"
|
|
9
|
-
*/
|
|
10
|
-
export function errorSelector(signature: string): string {
|
|
11
|
-
return keccak256(toBytes(signature)).slice(0, 10); // 0x + 8 hex chars = 4 bytes
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Extracts the error selector from a caught error's revert data
|
|
16
|
-
*/
|
|
17
|
-
function getErrorData(error: any): string | null {
|
|
18
|
-
// Try common paths where revert data might be
|
|
19
|
-
const data = error?.cause?.cause?.data
|
|
20
|
-
|| error?.cause?.data
|
|
21
|
-
|| error?.data
|
|
22
|
-
|| error?.message?.match(/return data: (0x[a-fA-F0-9]+)/)?.[1]
|
|
23
|
-
|| error?.message?.match(/data: (0x[a-fA-F0-9]+)/)?.[1];
|
|
24
|
-
return data || null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Asserts that a promise rejects with a specific custom error
|
|
29
|
-
* @param promise The promise to test
|
|
30
|
-
* @param expectedSelector The expected error selector (use errorSelector() to compute)
|
|
31
|
-
* @param errorName Human-readable error name for assertion messages
|
|
32
|
-
*/
|
|
33
|
-
export async function expectCustomError(
|
|
34
|
-
promise: Promise<any>,
|
|
35
|
-
expectedSelector: string,
|
|
36
|
-
errorName: string
|
|
37
|
-
): Promise<void> {
|
|
38
|
-
try {
|
|
39
|
-
await promise;
|
|
40
|
-
throw new Error(`Expected ${errorName} but transaction succeeded`);
|
|
41
|
-
} catch (e: any) {
|
|
42
|
-
if (e.message?.startsWith(`Expected ${errorName}`)) throw e;
|
|
43
|
-
|
|
44
|
-
const data = getErrorData(e);
|
|
45
|
-
if (!data) {
|
|
46
|
-
throw new Error(`Expected ${errorName} but got error without revert data: ${e.message}`);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const actualSelector = data.slice(0, 10).toLowerCase();
|
|
50
|
-
const expected = expectedSelector.toLowerCase();
|
|
51
|
-
|
|
52
|
-
if (actualSelector !== expected) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
`Expected ${errorName} (${expected}) but got selector ${actualSelector}\nFull data: ${data}`
|
|
55
|
-
);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Asserts that a promise rejects with a string revert message
|
|
62
|
-
* Error(string) selector is 0x08c379a0
|
|
63
|
-
*/
|
|
64
|
-
export async function expectRevertMessage(
|
|
65
|
-
promise: Promise<any>,
|
|
66
|
-
expectedMessage: string | RegExp
|
|
67
|
-
): Promise<void> {
|
|
68
|
-
const ERROR_STRING_SELECTOR = "0x08c379a0";
|
|
69
|
-
|
|
70
|
-
try {
|
|
71
|
-
await promise;
|
|
72
|
-
throw new Error(`Expected revert with "${expectedMessage}" but transaction succeeded`);
|
|
73
|
-
} catch (e: any) {
|
|
74
|
-
if (e.message?.startsWith("Expected revert")) throw e;
|
|
75
|
-
|
|
76
|
-
const data = getErrorData(e);
|
|
77
|
-
if (!data) {
|
|
78
|
-
// Fallback to checking error message directly
|
|
79
|
-
const matches = typeof expectedMessage === 'string'
|
|
80
|
-
? e.message?.includes(expectedMessage)
|
|
81
|
-
: expectedMessage.test(e.message);
|
|
82
|
-
if (!matches) {
|
|
83
|
-
throw new Error(`Expected revert with "${expectedMessage}" but got: ${e.message}`);
|
|
84
|
-
}
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const selector = data.slice(0, 10).toLowerCase();
|
|
89
|
-
if (selector !== ERROR_STRING_SELECTOR) {
|
|
90
|
-
// Not a string error, check if message is in the raw error
|
|
91
|
-
const matches = typeof expectedMessage === 'string'
|
|
92
|
-
? e.message?.includes(expectedMessage)
|
|
93
|
-
: expectedMessage.test(e.message);
|
|
94
|
-
if (!matches) {
|
|
95
|
-
throw new Error(`Expected string revert but got custom error with selector ${selector}`);
|
|
96
|
-
}
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Decode the string from the ABI-encoded data
|
|
101
|
-
// Format: selector (4 bytes) + offset (32 bytes) + length (32 bytes) + string data
|
|
102
|
-
try {
|
|
103
|
-
const abi: Abi = [{
|
|
104
|
-
type: 'error',
|
|
105
|
-
name: 'Error',
|
|
106
|
-
inputs: [{ name: 'message', type: 'string' }]
|
|
107
|
-
}];
|
|
108
|
-
const decoded = decodeErrorResult({ abi, data: data as `0x${string}` });
|
|
109
|
-
const message = (decoded.args as string[])[0];
|
|
110
|
-
|
|
111
|
-
const matches = typeof expectedMessage === 'string'
|
|
112
|
-
? message.includes(expectedMessage)
|
|
113
|
-
: expectedMessage.test(message);
|
|
114
|
-
|
|
115
|
-
if (!matches) {
|
|
116
|
-
throw new Error(`Expected revert with "${expectedMessage}" but got "${message}"`);
|
|
117
|
-
}
|
|
118
|
-
} catch (decodeError) {
|
|
119
|
-
// If decoding fails, fall back to checking error message
|
|
120
|
-
const matches = typeof expectedMessage === 'string'
|
|
121
|
-
? e.message?.includes(expectedMessage)
|
|
122
|
-
: expectedMessage.test(e.message);
|
|
123
|
-
if (!matches) {
|
|
124
|
-
throw new Error(`Expected revert with "${expectedMessage}" but decoding failed: ${e.message}`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// Pre-computed selectors for common errors
|
|
131
|
-
export const ErrorSelectors = {
|
|
132
|
-
// TotemMod errors
|
|
133
|
-
InvalidModEventOrigin: errorSelector("InvalidModEventOrigin()"),
|
|
134
|
-
NotLicensed: errorSelector("NotLicensed()"),
|
|
135
|
-
|
|
136
|
-
// Totems errors
|
|
137
|
-
Unauthorized: errorSelector("Unauthorized()"),
|
|
138
|
-
TotemNotFound: errorSelector("TotemNotFound(string)"),
|
|
139
|
-
TotemNotActive: errorSelector("TotemNotActive()"),
|
|
140
|
-
InsufficientBalance: errorSelector("InsufficientBalance(uint256,uint256)"),
|
|
141
|
-
CantSetLicense: errorSelector("CantSetLicense()"),
|
|
142
|
-
};
|
|
143
|
-
export const MIN_BASE_FEE = 500000000000000n; // 0.0005 ether
|
|
144
|
-
export const BURNED_FEE = 100000000000000n; // 0.0001 ether
|
|
145
|
-
|
|
146
|
-
export enum Hook {
|
|
147
|
-
Created = 0,
|
|
148
|
-
Mint = 1,
|
|
149
|
-
Burn = 2,
|
|
150
|
-
Transfer = 3,
|
|
151
|
-
TransferOwnership = 4,
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export const setupTotemsTest = async (minBaseFee: bigint = MIN_BASE_FEE, burnedFee: bigint = BURNED_FEE) => {
|
|
155
|
-
const { viem } = await network.connect();
|
|
156
|
-
const publicClient = await viem.getPublicClient();
|
|
157
|
-
// @ts-ignore
|
|
158
|
-
const walletClient = await viem.getWalletClient();
|
|
159
|
-
|
|
160
|
-
const addresses = await walletClient.getAddresses();
|
|
161
|
-
const proxyModInitializer = addresses[0];
|
|
162
|
-
const proxyMod = await viem.deployContract("ProxyMod", [
|
|
163
|
-
proxyModInitializer
|
|
164
|
-
]);
|
|
165
|
-
|
|
166
|
-
let market = await viem.deployContract("ModMarket", [minBaseFee, burnedFee]);
|
|
167
|
-
let totems:any = await viem.deployContract("Totems", [
|
|
168
|
-
market.address,
|
|
169
|
-
proxyMod.address,
|
|
170
|
-
minBaseFee,
|
|
171
|
-
burnedFee,
|
|
172
|
-
]);
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
// using these to validate the interfaces
|
|
176
|
-
totems = await viem.getContractAt("ITotems", totems.address);
|
|
177
|
-
// @ts-ignore
|
|
178
|
-
market = await viem.getContractAt("IMarket", market.address);
|
|
179
|
-
// initialize proxy mod
|
|
180
|
-
await proxyMod.write.initialize([totems.address, market.address], { account: proxyModInitializer });
|
|
181
|
-
|
|
182
|
-
return {
|
|
183
|
-
viem,
|
|
184
|
-
publicClient,
|
|
185
|
-
market,
|
|
186
|
-
totems,
|
|
187
|
-
accounts: addresses.slice(0, addresses.length),
|
|
188
|
-
proxyModSeller: addresses[0],
|
|
189
|
-
proxyMod,
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
export const modDetails = (details?:any) => Object.assign({
|
|
196
|
-
name: "Test Mod",
|
|
197
|
-
summary: "A test mod",
|
|
198
|
-
markdown: "## Test Mod\nThis is a test mod.",
|
|
199
|
-
image: "https://example.com/image.png",
|
|
200
|
-
website: "https://example.com",
|
|
201
|
-
websiteTickerPath: "/path/to/{ticker}",
|
|
202
|
-
isMinter: false,
|
|
203
|
-
needsUnlimited: false,
|
|
204
|
-
}, details || {});
|
|
205
|
-
|
|
206
|
-
export const publishMod = async (
|
|
207
|
-
market:any,
|
|
208
|
-
seller:string,
|
|
209
|
-
contract:string,
|
|
210
|
-
hooks:number[] = [],
|
|
211
|
-
details = modDetails(),
|
|
212
|
-
requiredActions:any[] = [],
|
|
213
|
-
referrer = ZERO_ADDRESS,
|
|
214
|
-
price = 1_000_000n,
|
|
215
|
-
fee = undefined
|
|
216
|
-
) => {
|
|
217
|
-
fee = fee ?? await market.read.getFee([referrer]);
|
|
218
|
-
|
|
219
|
-
return market.write.publish([
|
|
220
|
-
contract,
|
|
221
|
-
hooks,
|
|
222
|
-
price,
|
|
223
|
-
details,
|
|
224
|
-
requiredActions,
|
|
225
|
-
referrer,
|
|
226
|
-
], { value: fee, account: seller });
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export const totemDetails = (ticker:string, decimals:number) => {
|
|
230
|
-
return {
|
|
231
|
-
ticker: ticker,
|
|
232
|
-
decimals: decimals,
|
|
233
|
-
name: `${ticker} Totem`,
|
|
234
|
-
description: `This is the ${ticker} totem.`,
|
|
235
|
-
image: `https://example.com/${ticker.toLowerCase()}.png`,
|
|
236
|
-
website: `https://example.com/${ticker.toLowerCase()}`,
|
|
237
|
-
seed: '0x1110762033e7a10db4502359a19a61eb81312834769b8419047a2c9ae03ee847',
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
export const createTotem = async (
|
|
242
|
-
totems:any,
|
|
243
|
-
market:any,
|
|
244
|
-
creator:string,
|
|
245
|
-
ticker:string,
|
|
246
|
-
decimals:number,
|
|
247
|
-
allocations:any[],
|
|
248
|
-
mods?:{
|
|
249
|
-
transfer?:string[],
|
|
250
|
-
mint?:string[],
|
|
251
|
-
burn?:string[],
|
|
252
|
-
created?:string[],
|
|
253
|
-
transferOwnership?:string[]
|
|
254
|
-
},
|
|
255
|
-
referrer:string = ZERO_ADDRESS,
|
|
256
|
-
details:any = undefined,
|
|
257
|
-
) => {
|
|
258
|
-
const baseFee = await totems.read.getFee([referrer]);
|
|
259
|
-
|
|
260
|
-
const _mods = Object.assign({
|
|
261
|
-
transfer: [],
|
|
262
|
-
mint: [],
|
|
263
|
-
burn: [],
|
|
264
|
-
created: [],
|
|
265
|
-
transferOwnership: [],
|
|
266
|
-
}, mods || {});
|
|
267
|
-
const uniqueMods = new Set<string>();
|
|
268
|
-
Object.values(_mods).forEach((modList:any[]) => {
|
|
269
|
-
modList.forEach(m => uniqueMods.add(m));
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
const modsFee = await market.read.getModsFee([[...uniqueMods]]);
|
|
273
|
-
return await totems.write.create([
|
|
274
|
-
details ? Object.assign({
|
|
275
|
-
ticker,
|
|
276
|
-
decimals,
|
|
277
|
-
}, details) : totemDetails(ticker, decimals),
|
|
278
|
-
allocations.map(a => ({
|
|
279
|
-
...a,
|
|
280
|
-
label: a.label || "",
|
|
281
|
-
isMinter: a.hasOwnProperty('isMinter') ? a.isMinter : false,
|
|
282
|
-
})),
|
|
283
|
-
_mods,
|
|
284
|
-
referrer,
|
|
285
|
-
], { account: creator, value: baseFee + modsFee });
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
export const transfer = async (
|
|
289
|
-
totems:any,
|
|
290
|
-
ticker:string,
|
|
291
|
-
from:string,
|
|
292
|
-
to:string,
|
|
293
|
-
amount:number|bigint,
|
|
294
|
-
memo:string = "",
|
|
295
|
-
) => {
|
|
296
|
-
return await totems.write.transfer([
|
|
297
|
-
ticker,
|
|
298
|
-
from,
|
|
299
|
-
to,
|
|
300
|
-
amount,
|
|
301
|
-
memo,
|
|
302
|
-
], { account: from });
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
export const mint = async (
|
|
306
|
-
totems:any,
|
|
307
|
-
mod:string,
|
|
308
|
-
minter:string,
|
|
309
|
-
ticker:string,
|
|
310
|
-
amount:number|bigint,
|
|
311
|
-
memo:string = "",
|
|
312
|
-
payment:number|bigint = 0n,
|
|
313
|
-
) => {
|
|
314
|
-
return await totems.write.mint([
|
|
315
|
-
mod,
|
|
316
|
-
minter,
|
|
317
|
-
ticker,
|
|
318
|
-
amount,
|
|
319
|
-
memo,
|
|
320
|
-
], { account: minter, value: payment });
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
export const burn = async (
|
|
324
|
-
totems:any,
|
|
325
|
-
ticker:string,
|
|
326
|
-
owner:string,
|
|
327
|
-
amount:number|bigint,
|
|
328
|
-
memo:string = "",
|
|
329
|
-
) => {
|
|
330
|
-
return await totems.write.burn([
|
|
331
|
-
ticker,
|
|
332
|
-
owner,
|
|
333
|
-
amount,
|
|
334
|
-
memo,
|
|
335
|
-
], { account: owner });
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
export const getBalance = async (
|
|
339
|
-
totems:any,
|
|
340
|
-
ticker:string,
|
|
341
|
-
account:string,
|
|
342
|
-
) => {
|
|
343
|
-
return await totems.read.getBalance([ticker, account]);
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
export const getTotem = async (
|
|
347
|
-
totems:any,
|
|
348
|
-
ticker:string,
|
|
349
|
-
) => {
|
|
350
|
-
return await totems.read.getTotem([ticker]);
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
export const getTotems = async (
|
|
354
|
-
totems:any,
|
|
355
|
-
tickers:string[],
|
|
356
|
-
) => {
|
|
357
|
-
return await totems.read.getTotems([tickers]);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
export const getStats = async (
|
|
361
|
-
totems:any,
|
|
362
|
-
ticker:string,
|
|
363
|
-
) => {
|
|
364
|
-
return await totems.read.getStats([ticker]);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
export const transferOwnership = async (
|
|
368
|
-
totems:any,
|
|
369
|
-
ticker:string,
|
|
370
|
-
currentOwner:string,
|
|
371
|
-
newOwner:string,
|
|
372
|
-
) => {
|
|
373
|
-
return await totems.write.transferOwnership([
|
|
374
|
-
ticker,
|
|
375
|
-
newOwner,
|
|
376
|
-
], { account: currentOwner });
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
export const getMod = async (
|
|
380
|
-
market:any,
|
|
381
|
-
mod:string,
|
|
382
|
-
) => {
|
|
383
|
-
return await market.read.getMod([mod]);
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
export const getMods = async (
|
|
387
|
-
market:any,
|
|
388
|
-
mods:string[],
|
|
389
|
-
) => {
|
|
390
|
-
return await market.read.getMods([mods]);
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
export const getModFee = async (
|
|
394
|
-
market:any,
|
|
395
|
-
mod:string,
|
|
396
|
-
) => {
|
|
397
|
-
return await market.read.getModFee([mod]);
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
export const getModsFee = async (
|
|
401
|
-
market:any,
|
|
402
|
-
mods:string[],
|
|
403
|
-
) => {
|
|
404
|
-
return await market.read.getModsFee([mods]);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
export const isLicensed = async (
|
|
408
|
-
totems:any,
|
|
409
|
-
ticker:string,
|
|
410
|
-
mod:string,
|
|
411
|
-
) => {
|
|
412
|
-
return await totems.read.isLicensed([ticker, mod]);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
export const getRelays = async (
|
|
416
|
-
totems:any,
|
|
417
|
-
ticker:string,
|
|
418
|
-
) => {
|
|
419
|
-
return await totems.read.getRelays([ticker]);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
export const getSupportedHooks = async (
|
|
423
|
-
market:any,
|
|
424
|
-
mod:string,
|
|
425
|
-
) => {
|
|
426
|
-
return await market.read.getSupportedHooks([mod]);
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
export const isUnlimitedMinter = async (
|
|
430
|
-
market:any,
|
|
431
|
-
mod:string,
|
|
432
|
-
) => {
|
|
433
|
-
return await market.read.isUnlimitedMinter([mod]);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
export const addMod = async (
|
|
437
|
-
proxyMod:any,
|
|
438
|
-
totems:any,
|
|
439
|
-
market:any,
|
|
440
|
-
ticker:string,
|
|
441
|
-
hooks:number[],
|
|
442
|
-
mod:string,
|
|
443
|
-
caller:string,
|
|
444
|
-
referrer:string = ZERO_ADDRESS,
|
|
445
|
-
) => {
|
|
446
|
-
const modFee = await market.read.getModFee([mod]);
|
|
447
|
-
const referrerFee = await totems.read.getFee([referrer]);
|
|
448
|
-
return await proxyMod.write.addMod([
|
|
449
|
-
ticker,
|
|
450
|
-
hooks,
|
|
451
|
-
mod,
|
|
452
|
-
referrer,
|
|
453
|
-
], { account: caller, value: modFee + referrerFee });
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
export const removeMod = async (
|
|
457
|
-
proxyMod:any,
|
|
458
|
-
ticker:string,
|
|
459
|
-
mod:string,
|
|
460
|
-
caller:string,
|
|
461
|
-
) => {
|
|
462
|
-
return await proxyMod.write.removeMod([
|
|
463
|
-
ticker,
|
|
464
|
-
mod,
|
|
465
|
-
], { account: caller });
|
|
466
|
-
}
|