@totems/evm 1.0.3 → 1.0.7
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/contracts/ProxyMod.sol +331 -0
- package/package.json +6 -4
- package/test/helpers.d.ts +7 -7
- package/test/helpers.js +7 -8
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
2
|
+
pragma solidity ^0.8.28;
|
|
3
|
+
|
|
4
|
+
import "../mods/TotemMod.sol";
|
|
5
|
+
import "../interfaces/ITotemTypes.sol";
|
|
6
|
+
import {TotemsLibrary} from "../mods/TotemsLibrary.sol";
|
|
7
|
+
import {IMarket} from "../interfaces/IMarket.sol";
|
|
8
|
+
import {ITotems} from "../interfaces/ITotems.sol";
|
|
9
|
+
import {ReentrancyGuard} from "./ReentrancyGuard.sol";
|
|
10
|
+
import {Shared} from "./Shared.sol";
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
contract ProxyMod is IModTransfer, IModMint, IModMinter, IModBurn, ReentrancyGuard {
|
|
14
|
+
|
|
15
|
+
// Can't use ModBase here because it's deployed BEFORE the totems contract,
|
|
16
|
+
// so need to re-roll some of that here.
|
|
17
|
+
receive() external payable {}
|
|
18
|
+
fallback() external payable {}
|
|
19
|
+
address payable private seller;
|
|
20
|
+
address public marketContract;
|
|
21
|
+
address public totemsContract;
|
|
22
|
+
|
|
23
|
+
mapping(bytes32 => mapping(ITotemTypes.Hook => mapping(address => bool))) internal isModEnabled;
|
|
24
|
+
mapping(bytes32 => ITotemTypes.TotemMods) internal totemMods;
|
|
25
|
+
bytes32[] public totemsWithMods;
|
|
26
|
+
|
|
27
|
+
function getSeller() external view returns (address payable) {
|
|
28
|
+
return seller != address(0) ? payable(address(seller)) : payable(address(this));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
modifier onlyTotems() {
|
|
32
|
+
require(msg.sender == totemsContract, "Only Totems contract");
|
|
33
|
+
_;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
constructor(address payable _seller) {
|
|
37
|
+
require(_seller != address(0), "Invalid seller address");
|
|
38
|
+
seller = _seller;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function initialize(
|
|
42
|
+
address _totemsContract,
|
|
43
|
+
address _marketContract
|
|
44
|
+
) external {
|
|
45
|
+
require(msg.sender == seller, "Only seller can initialize");
|
|
46
|
+
require(totemsContract == address(0), "Already initialized");
|
|
47
|
+
require(_totemsContract != address(0), "Invalid totems contract");
|
|
48
|
+
require(_marketContract != address(0), "Invalid market contract");
|
|
49
|
+
|
|
50
|
+
totemsContract = _totemsContract;
|
|
51
|
+
marketContract = _marketContract;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// ========== Errors ==========
|
|
55
|
+
error CantUseCreatedHook();
|
|
56
|
+
error UnknownHook();
|
|
57
|
+
error ModNotEnabledForMint();
|
|
58
|
+
error InvalidAddressLength();
|
|
59
|
+
error InvalidHexCharacter();
|
|
60
|
+
error NoFeeRequired();
|
|
61
|
+
|
|
62
|
+
// ========== Managerial Functions ==========
|
|
63
|
+
|
|
64
|
+
function addMod(
|
|
65
|
+
string calldata ticker,
|
|
66
|
+
ITotemTypes.Hook[] calldata hooks,
|
|
67
|
+
address mod,
|
|
68
|
+
address payable referrer
|
|
69
|
+
) external payable nonReentrant {
|
|
70
|
+
ITotems totems = ITotems(totemsContract);
|
|
71
|
+
IMarket market = IMarket(marketContract);
|
|
72
|
+
|
|
73
|
+
require(
|
|
74
|
+
totems.getTotem(ticker).creator == msg.sender,
|
|
75
|
+
"Only the totem creator can add mods"
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
bytes32 tickerBytes = TotemsLibrary.tickerToBytes(ticker);
|
|
79
|
+
|
|
80
|
+
if(!totems.isLicensed(ticker, mod)) {
|
|
81
|
+
uint256 modFee = market.getModFee(mod);
|
|
82
|
+
uint256 referrerFee = totems.getFee(referrer);
|
|
83
|
+
uint256 totalFee = modFee + referrerFee;
|
|
84
|
+
require(msg.value >= totalFee, "Insufficient fee");
|
|
85
|
+
totems.setLicenseFromProxy(tickerBytes, mod);
|
|
86
|
+
|
|
87
|
+
// Pay mod seller
|
|
88
|
+
ITotemTypes.Mod memory totemMod = market.getMod(mod);
|
|
89
|
+
if(modFee > 0) {
|
|
90
|
+
Shared.safeTransfer(totemMod.seller, modFee);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Pay referrer (or burn if no referrer)
|
|
94
|
+
if(referrerFee > 0) {
|
|
95
|
+
address recipient = referrer != address(0) ? referrer : address(0);
|
|
96
|
+
Shared.safeTransfer(recipient, referrerFee);
|
|
97
|
+
}
|
|
98
|
+
} else {
|
|
99
|
+
if(msg.value > 0) revert NoFeeRequired();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (uint256 i = 0; i < hooks.length; i++) {
|
|
103
|
+
ITotemTypes.Hook hook = hooks[i];
|
|
104
|
+
|
|
105
|
+
if (isModEnabled[tickerBytes][hook][mod]) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
isModEnabled[tickerBytes][hook][mod] = true;
|
|
110
|
+
|
|
111
|
+
_pushModToHook(tickerBytes, hook, mod);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function removeMod(
|
|
116
|
+
string calldata ticker,
|
|
117
|
+
address mod
|
|
118
|
+
) external {
|
|
119
|
+
require(
|
|
120
|
+
TotemsLibrary.getCreator(totemsContract, ticker) == msg.sender,
|
|
121
|
+
"Only totem creator"
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
bytes32 tickerBytes = TotemsLibrary.tickerToBytes(ticker);
|
|
125
|
+
|
|
126
|
+
_removeFromHook(tickerBytes, ITotemTypes.Hook.Transfer, mod);
|
|
127
|
+
_removeFromHook(tickerBytes, ITotemTypes.Hook.Mint, mod);
|
|
128
|
+
_removeFromHook(tickerBytes, ITotemTypes.Hook.Burn, mod);
|
|
129
|
+
_removeFromHook(tickerBytes, ITotemTypes.Hook.Created, mod);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
// ========== Mod Hook Functions ==========
|
|
134
|
+
|
|
135
|
+
function onTransfer(
|
|
136
|
+
string calldata ticker,
|
|
137
|
+
address from,
|
|
138
|
+
address to,
|
|
139
|
+
uint256 amount,
|
|
140
|
+
string calldata memo
|
|
141
|
+
) external override onlyTotems {
|
|
142
|
+
TotemsLibrary.checkLicense(totemsContract, ticker);
|
|
143
|
+
|
|
144
|
+
bytes32 tickerBytes = TotemsLibrary.tickerToBytes(ticker);
|
|
145
|
+
address[] memory mods = totemMods[tickerBytes].transfer;
|
|
146
|
+
uint256 length = mods.length;
|
|
147
|
+
|
|
148
|
+
for (uint256 i = 0; i < length; i++) {
|
|
149
|
+
IModTransfer(mods[i]).onTransfer(
|
|
150
|
+
ticker,
|
|
151
|
+
from,
|
|
152
|
+
to,
|
|
153
|
+
amount,
|
|
154
|
+
memo
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function onMint(
|
|
160
|
+
string calldata ticker,
|
|
161
|
+
address minter,
|
|
162
|
+
uint256 amount,
|
|
163
|
+
uint256 payment,
|
|
164
|
+
string calldata memo
|
|
165
|
+
) external override onlyTotems {
|
|
166
|
+
TotemsLibrary.checkLicense(totemsContract, ticker);
|
|
167
|
+
|
|
168
|
+
bytes32 tickerBytes = TotemsLibrary.tickerToBytes(ticker);
|
|
169
|
+
address[] memory mods = totemMods[tickerBytes].mint;
|
|
170
|
+
uint256 length = mods.length;
|
|
171
|
+
|
|
172
|
+
for (uint256 i = 0; i < length; i++) {
|
|
173
|
+
IModMint(mods[i]).onMint(
|
|
174
|
+
ticker,
|
|
175
|
+
minter,
|
|
176
|
+
amount,
|
|
177
|
+
payment,
|
|
178
|
+
memo
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
function mint(
|
|
185
|
+
string calldata ticker,
|
|
186
|
+
address minter,
|
|
187
|
+
uint256 amount,
|
|
188
|
+
string calldata memo
|
|
189
|
+
) external onlyTotems payable {
|
|
190
|
+
TotemsLibrary.checkLicense(totemsContract, ticker);
|
|
191
|
+
|
|
192
|
+
// This likely eliminates some use cases, but it's necessary to avoid
|
|
193
|
+
// ambiguity in which mod to use for minting when multiple are present,
|
|
194
|
+
// especially when payment is involved and to align with the core mint->mod logic.
|
|
195
|
+
address mod = _stringToAddress(memo);
|
|
196
|
+
|
|
197
|
+
bytes32 tickerBytes = TotemsLibrary.tickerToBytes(ticker);
|
|
198
|
+
if(!isModEnabled[tickerBytes][ITotemTypes.Hook.Mint][mod]) {
|
|
199
|
+
revert ModNotEnabledForMint();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
IModMinter(mod).mint{value: msg.value}(
|
|
203
|
+
ticker,
|
|
204
|
+
minter,
|
|
205
|
+
amount,
|
|
206
|
+
memo
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function onBurn(
|
|
211
|
+
string calldata ticker,
|
|
212
|
+
address owner,
|
|
213
|
+
uint256 amount,
|
|
214
|
+
string calldata memo
|
|
215
|
+
) external override onlyTotems {
|
|
216
|
+
TotemsLibrary.checkLicense(totemsContract, ticker);
|
|
217
|
+
|
|
218
|
+
bytes32 tickerBytes = TotemsLibrary.tickerToBytes(ticker);
|
|
219
|
+
address[] memory mods = totemMods[tickerBytes].burn;
|
|
220
|
+
uint256 length = mods.length;
|
|
221
|
+
|
|
222
|
+
for (uint256 i = 0; i < length; i++) {
|
|
223
|
+
IModBurn(mods[i]).onBurn(
|
|
224
|
+
ticker,
|
|
225
|
+
owner,
|
|
226
|
+
amount,
|
|
227
|
+
memo
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
// ========== Internal Functions ==========
|
|
239
|
+
|
|
240
|
+
function _stringToAddress(string memory str) internal pure returns (address) {
|
|
241
|
+
bytes memory strBytes = bytes(str);
|
|
242
|
+
if(strBytes.length != 42){
|
|
243
|
+
revert InvalidAddressLength();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
bytes memory addrBytes = new bytes(20);
|
|
247
|
+
|
|
248
|
+
for (uint i = 0; i < 20; i++) {
|
|
249
|
+
addrBytes[i] = bytes1(
|
|
250
|
+
_hexCharToByte(strBytes[2 + i * 2]) * 16 +
|
|
251
|
+
_hexCharToByte(strBytes[3 + i * 2])
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
address addr;
|
|
256
|
+
assembly {
|
|
257
|
+
addr := mload(add(addrBytes, 20))
|
|
258
|
+
}
|
|
259
|
+
return addr;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function _hexCharToByte(bytes1 char) internal pure returns (uint8) {
|
|
263
|
+
uint8 byteValue = uint8(char);
|
|
264
|
+
if (byteValue >= uint8(bytes1('0')) && byteValue <= uint8(bytes1('9'))) {
|
|
265
|
+
return byteValue - uint8(bytes1('0'));
|
|
266
|
+
} else if (byteValue >= uint8(bytes1('a')) && byteValue <= uint8(bytes1('f'))) {
|
|
267
|
+
return 10 + byteValue - uint8(bytes1('a'));
|
|
268
|
+
} else if (byteValue >= uint8(bytes1('A')) && byteValue <= uint8(bytes1('F'))) {
|
|
269
|
+
return 10 + byteValue - uint8(bytes1('A'));
|
|
270
|
+
}
|
|
271
|
+
revert InvalidHexCharacter();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function _pushModToHook(
|
|
275
|
+
bytes32 ticker,
|
|
276
|
+
ITotemTypes.Hook hook,
|
|
277
|
+
address mod
|
|
278
|
+
) internal {
|
|
279
|
+
ITotemTypes.TotemMods storage mods = totemMods[ticker];
|
|
280
|
+
|
|
281
|
+
if (hook == ITotemTypes.Hook.Transfer) {
|
|
282
|
+
mods.transfer.push(mod);
|
|
283
|
+
} else if (hook == ITotemTypes.Hook.Mint) {
|
|
284
|
+
mods.mint.push(mod);
|
|
285
|
+
} else if (hook == ITotemTypes.Hook.Burn) {
|
|
286
|
+
mods.burn.push(mod);
|
|
287
|
+
} else if (hook == ITotemTypes.Hook.Created) {
|
|
288
|
+
revert CantUseCreatedHook();
|
|
289
|
+
} else {
|
|
290
|
+
revert UnknownHook();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function _removeFromHook(
|
|
295
|
+
bytes32 ticker,
|
|
296
|
+
ITotemTypes.Hook hook,
|
|
297
|
+
address mod
|
|
298
|
+
) internal {
|
|
299
|
+
if (!isModEnabled[ticker][hook][mod]) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
delete isModEnabled[ticker][hook][mod];
|
|
304
|
+
_removeModFromHookArray(ticker, hook, mod);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function _removeModFromHookArray(
|
|
308
|
+
bytes32 ticker,
|
|
309
|
+
ITotemTypes.Hook hook,
|
|
310
|
+
address mod
|
|
311
|
+
) internal {
|
|
312
|
+
address[] storage arr;
|
|
313
|
+
|
|
314
|
+
if (hook == ITotemTypes.Hook.Transfer) arr = totemMods[ticker].transfer;
|
|
315
|
+
else if (hook == ITotemTypes.Hook.Mint) arr = totemMods[ticker].mint;
|
|
316
|
+
else if (hook == ITotemTypes.Hook.Burn) arr = totemMods[ticker].burn;
|
|
317
|
+
else if (hook == ITotemTypes.Hook.Created) arr = totemMods[ticker].created;
|
|
318
|
+
else revert("Invalid hook");
|
|
319
|
+
|
|
320
|
+
uint256 length = arr.length;
|
|
321
|
+
for (uint256 i = 0; i < length; i++) {
|
|
322
|
+
if (arr[i] == mod) {
|
|
323
|
+
arr[i] = arr[length - 1];
|
|
324
|
+
arr.pop();
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@totems/evm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Totems EVM smart contracts for building modular token systems",
|
|
5
6
|
"author": "nsjames",
|
|
6
7
|
"license": "MIT & AGPL-3.0",
|
|
@@ -29,14 +30,15 @@
|
|
|
29
30
|
"./test/helpers": {
|
|
30
31
|
"types": "./test/helpers.d.ts",
|
|
31
32
|
"import": "./test/helpers.js",
|
|
32
|
-
"require": "./test/helpers.js"
|
|
33
|
+
"require": "./test/helpers.js",
|
|
34
|
+
"default": "./test/helpers.js"
|
|
33
35
|
},
|
|
34
36
|
"./contracts/*": "./contracts/*",
|
|
35
37
|
"./interfaces/*": "./interfaces/*",
|
|
36
38
|
"./mods/*": "./mods/*"
|
|
37
39
|
},
|
|
38
40
|
"peerDependencies": {
|
|
39
|
-
"hardhat": "^
|
|
40
|
-
"viem": "^2.
|
|
41
|
+
"hardhat": "^3.1.4",
|
|
42
|
+
"viem": "^2.44.4"
|
|
41
43
|
}
|
|
42
44
|
}
|
package/test/helpers.d.ts
CHANGED
|
@@ -27,13 +27,13 @@ export declare const ErrorSelectors: {
|
|
|
27
27
|
};
|
|
28
28
|
export declare const MIN_BASE_FEE = 500000000000000n;
|
|
29
29
|
export declare const BURNED_FEE = 100000000000000n;
|
|
30
|
-
export declare
|
|
31
|
-
Created
|
|
32
|
-
Mint
|
|
33
|
-
Burn
|
|
34
|
-
Transfer
|
|
35
|
-
TransferOwnership
|
|
36
|
-
}
|
|
30
|
+
export declare const Hook: {
|
|
31
|
+
readonly Created: 0;
|
|
32
|
+
readonly Mint: 1;
|
|
33
|
+
readonly Burn: 2;
|
|
34
|
+
readonly Transfer: 3;
|
|
35
|
+
readonly TransferOwnership: 4;
|
|
36
|
+
};
|
|
37
37
|
export declare const setupTotemsTest: (minBaseFee?: bigint, burnedFee?: bigint) => Promise<{
|
|
38
38
|
viem: any;
|
|
39
39
|
publicClient: any;
|
package/test/helpers.js
CHANGED
|
@@ -82,14 +82,13 @@ var ErrorSelectors = {
|
|
|
82
82
|
};
|
|
83
83
|
var MIN_BASE_FEE = 500000000000000n;
|
|
84
84
|
var BURNED_FEE = 100000000000000n;
|
|
85
|
-
var Hook
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
})(Hook || (Hook = {}));
|
|
85
|
+
var Hook = {
|
|
86
|
+
Created: 0,
|
|
87
|
+
Mint: 1,
|
|
88
|
+
Burn: 2,
|
|
89
|
+
Transfer: 3,
|
|
90
|
+
TransferOwnership: 4
|
|
91
|
+
};
|
|
93
92
|
var setupTotemsTest = async (minBaseFee = MIN_BASE_FEE, burnedFee = BURNED_FEE) => {
|
|
94
93
|
const { viem } = await network.connect();
|
|
95
94
|
const publicClient = await viem.getPublicClient();
|