-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTokenAgentFactory_v0.8.2_Sepolia_0xB6426d5E4B6515E627Ff510424978eBe223c39C4.sol
More file actions
906 lines (848 loc) · 41.7 KB
/
Copy pathTokenAgentFactory_v0.8.2_Sepolia_0xB6426d5E4B6515E627Ff510424978eBe223c39C4.sol
File metadata and controls
906 lines (848 loc) · 41.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
/**
*Submitted for verification at Etherscan.io on 2024-09-10
*/
pragma solidity ^0.8.27;
// ----------------------------------------------------------------------------
// TokenAgent with factory v0.8.2 testing
//
// https://github.com/bokkypoobah/TokenAgent
//
// Deployed to Sepolia
// - WETH 0x07391dbE03e7a0DEa0fce6699500da081537B6c3
// - TokenAgent template 0x5446e959103b19e983848FB53d9fbD096eDb21A9
// - TokenAgentFactory 0xB6426d5E4B6515E627Ff510424978eBe223c39C4
//
// TODO:
// - FILL for ERC-721/1155?
//
// SPDX-License-Identifier: MIT
//
// Enjoy. (c) BokkyPooBah / Bok Consulting Pty Ltd 2024. The MIT Licence.
// ----------------------------------------------------------------------------
// import "hardhat/console.sol";
/// @notice https://github.com/optionality/clone-factory/blob/32782f82dfc5a00d103a7e61a17a5dedbd1e8e9d/contracts/CloneFactory.sol
/*
The MIT License (MIT)
Copyright (c) 2018 Murray Software, LLC.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//solhint-disable max-line-length
//solhint-disable no-inline-assembly
contract CloneFactory {
function createClone(address target) internal returns (address result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(clone, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(clone, 0x14), targetBytes)
mstore(add(clone, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
result := create(0, clone, 0x37)
}
}
function isClone(address target, address query) internal view returns (bool result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(clone, 0x363d3d373d3d3d363d7300000000000000000000000000000000000000000000)
mstore(add(clone, 0xa), targetBytes)
mstore(add(clone, 0x1e), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
let other := add(clone, 0x40)
extcodecopy(query, other, 0, 0x2d)
result := and(
eq(mload(clone), mload(other)),
eq(mload(add(clone, 0xd)), mload(add(other, 0xd)))
)
}
}
}
// End CloneFactory.sol
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed owner, address indexed spender, uint tokens);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint balance);
function allowance(address owner, address spender) external view returns (uint remaining);
function transfer(address to, uint tokens) external returns (bool success);
function approve(address spender, uint tokens) external returns (bool success);
function transferFrom(address from, address to, uint tokens) external returns (bool success);
}
interface WETH is IERC20 {
receive() external payable;
function deposit() external payable;
function withdraw(uint wad) external;
}
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
interface IERC721Partial {
function transferFrom(address from, address to, uint tokenId) external;
}
interface IERC1155Partial {
function safeTransferFrom(address from, address to, uint id, uint amount, bytes calldata data) external;
}
type Account is address; // 2^160
type Count is uint16; // 2^16 = 65,536
type Index is uint32; // 2^32 = 4,294,967,296
type Nonce is uint24; // 2^24 = 16,777,216
type Price is uint128; // 2^128 = 340, 282,366,920,938,463,463, 374,607,431,768,211,456
type Token is address; // 2^160
type TokenId is uint; // 2^256 = 115,792, 089,237,316,195,423,570, 985,008,687,907,853,269, 984,665,640,564,039,457, 584,007,913,129,639,936
type TokenId16 is uint16; // 2^16 = 65,536
type Tokens is uint128; // 2^128 = 340, 282,366,920,938,463,463, 374,607,431,768,211,456
type Unixtime is uint40; // 2^40 = 1,099,511,627,776. For Unixtime, 1,099,511,627,776 seconds = 34865.285000507356672 years
enum BuySell { BUY, SELL }
enum Execution { FILL, FILLORKILL }
enum TokenIdType { TOKENID256, TOKENID16 }
enum TokenType { UNKNOWN, ERC20, ERC721, ERC1155, INVALID }
bytes4 constant ERC721_INTERFACE = 0x80ac58cd;
bytes4 constant ERC1155_INTERFACE = 0xd9b67a26;
Token constant THEDAO = Token.wrap(0xBB9bc244D798123fDe783fCc1C72d3Bb8C189413);
library ArrayUtils {
function indexOfTokenIds(TokenId[] memory tokenIds, TokenId target) internal pure returns (uint) {
if (tokenIds.length > 0) {
uint left;
uint right = tokenIds.length - 1;
uint mid;
while (left <= right) {
mid = (left + right) / 2;
if (TokenId.unwrap(tokenIds[mid]) < TokenId.unwrap(target)) {
left = mid + 1;
} else if (TokenId.unwrap(tokenIds[mid]) > TokenId.unwrap(target)) {
if (mid < 1) {
break;
}
right = mid - 1;
} else {
return mid;
}
}
}
return type(uint).max;
}
function indexOfTokenId16s(TokenId16[] memory tokenIds, TokenId16 target) internal pure returns (uint) {
if (tokenIds.length > 0) {
uint left;
uint right = tokenIds.length - 1;
uint mid;
while (left <= right) {
mid = (left + right) / 2;
if (TokenId16.unwrap(tokenIds[mid]) < TokenId16.unwrap(target)) {
left = mid + 1;
} else if (TokenId16.unwrap(tokenIds[mid]) > TokenId16.unwrap(target)) {
if (mid < 1) {
break;
}
right = mid - 1;
} else {
return mid;
}
}
}
return type(uint).max;
}
}
/// @notice Ownership
contract Owned {
Account public owner;
Account public pendingOwner;
event OwnershipTransferStarted(Account indexed previousOwner, Account indexed newOwner, Unixtime timestamp);
event OwnershipTransferred(Account indexed previousOwner, Account indexed newOwner, Unixtime timestamp);
error AlreadyInitialised();
error NotOwner();
error Owner();
error NotNewOwner();
modifier onlyOwner {
if (msg.sender != Account.unwrap(owner)) {
revert NotOwner();
}
_;
}
modifier notOwner {
if (msg.sender == Account.unwrap(owner)) {
revert Owner();
}
_;
}
function initOwned(Account _owner) internal {
if (Account.unwrap(owner) != address(0)) {
revert AlreadyInitialised();
}
owner = _owner;
}
function transferOwnership(Account _pendingOwner) public onlyOwner {
pendingOwner = _pendingOwner;
emit OwnershipTransferStarted(owner, _pendingOwner, Unixtime.wrap(uint40(block.timestamp)));
}
function acceptOwnership() public {
if (msg.sender != Account.unwrap(pendingOwner)) {
revert NotNewOwner();
}
emit OwnershipTransferred(owner, pendingOwner, Unixtime.wrap(uint40(block.timestamp)));
owner = pendingOwner;
pendingOwner = Account.wrap(address(0));
}
function recoverTokens(IERC20 token, Tokens tokens) public onlyOwner {
if (address(token) == address(0)) {
payable(msg.sender).transfer((Tokens.unwrap(tokens) == 0 ? address(this).balance : Tokens.unwrap(tokens)));
} else {
token.transfer(msg.sender, Tokens.unwrap(tokens) == 0 ? token.balanceOf(address(this)) : Tokens.unwrap(tokens));
}
}
function recoverERC721Token(IERC721Partial token, TokenId tokenId) public onlyOwner {
token.transferFrom(address(this), msg.sender, TokenId.unwrap(tokenId));
}
function recoverERC1155Token(IERC1155Partial token, TokenId tokenId, Tokens tokens) public onlyOwner {
token.safeTransferFrom(address(this), msg.sender, TokenId.unwrap(tokenId), Tokens.unwrap(tokens), "");
}
}
/// @notice Reentrancy guard
contract NonReentrancy {
modifier nonReentrant() {
assembly {
if tload(0) { revert(0, 0) }
tstore(0, 1)
}
_;
assembly {
tstore(0, 0)
}
}
}
/// @notice Token information
contract TokenInfo {
bytes4 constant InvalidID = 0xffffffff;
bytes4 constant ERC165ID = 0x01ffc9a7;
function doesContractImplementInterface(address _contract, bytes4 _interfaceId) internal view returns (bool) {
uint success;
uint result;
(success, result) = _noThrowCall(_contract, ERC165ID);
if ((success == 0) || (result == 0)) {
return false;
}
(success, result) = _noThrowCall(_contract, InvalidID);
if ((success == 0) || (result !=0 )) {
return false;
}
(success, result) = _noThrowCall(_contract, _interfaceId);
if ((success == 1) && (result == 1)) {
return true;
}
return false;
}
function _noThrowCall(address _contract, bytes4 _interfaceId) view internal returns (uint success, uint result) {
bytes4 erc165ID = ERC165ID;
assembly {
let x := mload(0x40) // Find empty storage location using "free memory pointer"
mstore(x, erc165ID) // Place signature at beginning of empty storage
mstore(add(x, 0x04), _interfaceId) // Place first argument directly next to signature
success := staticcall(
30000, // 30k gas
_contract, // To addr
x, // Inputs are stored at location x
0x24, // Inputs are 36 bytes long
x, // Store output over input (saves space)
0x20) // Outputs are 32 bytes long
result := mload(x) // Load the result
}
}
function _decimals(Token token) internal view returns (uint8 __d) {
try IERC20(Token.unwrap(token)).decimals() returns (uint8 _d) {
__d = _d;
} catch {
if (Token.unwrap(token) == Token.unwrap(THEDAO)) {
return 16;
} else {
__d = type(uint8).max;
}
}
}
function _getTokenType(Token token) internal view returns (TokenType _tokenType) {
if (Token.unwrap(token).code.length > 0) {
if (doesContractImplementInterface(Token.unwrap(token), ERC721_INTERFACE)) {
_tokenType = TokenType.ERC721;
} else if (doesContractImplementInterface(Token.unwrap(token), ERC1155_INTERFACE)) {
_tokenType = TokenType.ERC1155;
} else {
if (_decimals(token) != type(uint8).max) {
_tokenType = TokenType.ERC20;
} else {
_tokenType = TokenType.INVALID;
}
}
} else {
_tokenType = TokenType.INVALID;
}
}
function getTokenTypes(Token[] memory tokens) public view returns (TokenType[] memory _tokenTypes) {
_tokenTypes = new TokenType[](tokens.length);
for (uint i = 0; i < tokens.length; i++) {
_tokenTypes[i] = _getTokenType(tokens[i]);
}
}
}
/// @notice User owned TokenAgent
contract TokenAgent is TokenInfo, Owned, NonReentrancy {
struct AddOffer {
Token token; // 160 bits
BuySell buySell; // 8 bits
Unixtime expiry; // 40 bits
Count count; // 16 bits
Price[] prices; // token/WETH 18dp
TokenId[] tokenIds; // ERC-721/1155
Tokens[] tokenss; // ERC-20/1155
}
struct UpdateOffer {
Index index; // 160 bits
Unixtime expiry; // 40 bits
Count count; // 16 bits
Price[] prices; // token/WETH 18dp
TokenId[] tokenIds; // ERC-721/1155
Tokens[] tokenss; // ERC-20/1155
}
struct Offer {
Token token; // 160 bits
BuySell buySell; // 8 bits
Unixtime expiry; // 40 bits
Count count; // 16 bits
Nonce nonce; // 24 bits
TokenIdType tokenIdType; // 8 bits
Price[] prices; // token/WETH 18dp
TokenId16[] tokenId16s; // ERC-721/1155, when max[tokenIds...] < 2 ** 16
TokenId[] tokenIds; // ERC-721/1155, when max[tokenIds...] >= 2 ** 16
Tokens[] tokenss; // ERC-20/1155
Tokens[] useds; // ERC-20
}
struct OfferInfo {
uint index;
Token token;
TokenType tokenType;
BuySell buySell;
Unixtime expiry;
Count count;
Nonce nonce;
Price[] prices;
TokenId[] tokenIds;
Tokens[] tokenss;
Tokens[] useds;
}
struct TradeInput {
Index index; // 32 bits
Price price; // 128 bits min - ERC-20 max average when buying, min average when selling; ERC-721/1155 max total price when buying, min total price when selling
Execution execution; // 8 bits - Only for ERC-20 - FILL or FILLORKILL
TokenId[] tokenIds;
Tokens[] tokenss;
}
WETH public weth;
Nonce public nonce;
mapping(Token => TokenType) tokenTypes;
Offer[] public offers;
event InternalTransfer(address indexed from, address indexed to, uint ethers, Unixtime timestamp);
event Offered(Index index, Account indexed maker, Token indexed token, TokenType tokenType, BuySell buySell, Unixtime expiry, Count count, Nonce nonce, Price[] prices, TokenId[] tokenIds, Tokens[] tokenss, Unixtime timestamp);
event OfferUpdated(Index index, Account indexed maker, Token indexed token, TokenType tokenType, BuySell buySell, Unixtime expiry, Count count, Nonce nonce, Price[] prices, TokenId[] tokenIds, Tokens[] tokenss, Unixtime timestamp);
event OffersInvalidated(Nonce newNonce, Unixtime timestamp);
event Traded(Index index, Account indexed taker, Account indexed maker, Token indexed token, TokenType tokenType, BuySell makerBuySell, uint[] prices, uint[] tokenIds, uint[] tokenss, Price price, Unixtime timestamp);
error CannotOfferWETH();
error ExecutedAveragePriceGreaterThanSpecified(Price executedAveragePrice, Price tradeAveragePrice);
error ExecutedAveragePriceLessThanSpecified(Price executedAveragePrice, Price tradeAveragePrice);
error ExecutedTotalPriceGreaterThanSpecified(Price executedTotalPrice, Price tradeTotalPrice);
error ExecutedTotalPriceLessThanSpecified(Price executedTotalPrice, Price tradeTotalPrice);
error InsufficentEthersRemaining(uint ethersRequested, uint ethersRemaining);
error InsufficentTokensRemaining(Tokens tokensRequested, Tokens tokensRemaining);
error InsufficentCountRemaining(Count requested, Count remaining);
error InvalidInputData(string reason);
error InvalidOffer(Nonce offerNonce, Nonce currentNonce);
error InvalidIndex(Index index);
error InvalidToken(Token token);
error InvalidTokenId(TokenId tokenId);
error OfferExpired(Index index, Unixtime expiry);
error TokenIdsMustBeSortedWithNoDuplicates();
constructor() {
}
function init(WETH _weth, Account owner) external {
super.initOwned(owner);
weth = _weth;
}
function invalidateOrders() external onlyOwner {
nonce = Nonce.wrap(Nonce.unwrap(nonce) + 1);
emit OffersInvalidated(nonce, Unixtime.wrap(uint40(block.timestamp)));
}
// AddOffer:
// ERC-20
// prices[price0], tokenIds[], tokenss[]
// prices[price0], tokenIds[], tokenss[tokens0, tokens1, ...]
// prices[price0, price1, ...], tokenIds[], tokenss[tokens0, tokens1, ...]
// ERC-721
// prices[price0], tokenIds[], tokenss[]
// prices[price0], tokenIds[tokenId0, tokenId1, ...], tokenss[]
// prices[price0, price1, ...], tokenIds[tokenId0, tokenId1, ...], tokenss[]
// ERC-1155
// prices[price0], tokenIds[], tokenss[]
// prices[price0], tokenIds[tokenId0, tokenId1, ...], tokenss[]
// prices[price0, price1, ...], tokenIds[tokenId0, tokenId1, ...], tokenss[tokens0, tokens1, ...]
function addOffers(AddOffer[] calldata inputs) external onlyOwner {
for (uint i = 0; i < inputs.length; i++) {
AddOffer memory input = inputs[i];
TokenType tokenType = tokenTypes[input.token];
if (Token.unwrap(input.token) == address(weth)) {
revert CannotOfferWETH();
}
if (tokenType == TokenType.UNKNOWN) {
tokenType = _getTokenType(input.token);
if (tokenType == TokenType.INVALID) {
revert InvalidToken(input.token);
}
tokenTypes[input.token] = tokenType;
}
Offer storage offer = offers.push();
offer.token = input.token;
offer.buySell = input.buySell;
offer.expiry = input.expiry;
offer.count = input.count;
offer.nonce = nonce;
if (input.prices.length == 0) {
revert InvalidInputData("all: prices array must contain at least one price");
} else if (tokenType == TokenType.ERC20 && input.prices.length != 1 && input.tokenss.length != input.prices.length) {
revert InvalidInputData("ERC-20: tokenss array length must match prices array length");
} else if (tokenType == TokenType.ERC721 && input.prices.length != 1 && input.tokenIds.length != input.prices.length) {
revert InvalidInputData("ERC-721: tokenIds array length must match prices array length");
} else if (tokenType == TokenType.ERC1155 && input.tokenIds.length != input.tokenss.length) {
revert InvalidInputData("ERC-1155: tokenIds and tokenss array length must match");
} else if (tokenType == TokenType.ERC1155 && input.prices.length != 1 && input.tokenIds.length != input.prices.length) {
revert InvalidInputData("ERC-1155: tokenIds and tokenss array length must match prices array length");
}
offer.prices = input.prices;
if (tokenType == TokenType.ERC721 || tokenType == TokenType.ERC1155) {
if (input.tokenIds.length > 1) {
for (uint j = 1; j < input.tokenIds.length; j++) {
if (TokenId.unwrap(input.tokenIds[j - 1]) >= TokenId.unwrap(input.tokenIds[j])) {
revert TokenIdsMustBeSortedWithNoDuplicates();
}
}
}
if (input.tokenIds.length > 0) {
uint maxTokenId;
for (uint j = 0; j < input.tokenIds.length; j++) {
if (maxTokenId < TokenId.unwrap(input.tokenIds[j])) {
maxTokenId = TokenId.unwrap(input.tokenIds[j]);
}
}
if (maxTokenId < 2 ** 16) {
offer.tokenIdType = TokenIdType.TOKENID16;
}
}
if (offer.tokenIdType == TokenIdType.TOKENID16) {
for (uint j = 0; j < input.tokenIds.length; j++) {
offer.tokenId16s.push(TokenId16.wrap(uint16(TokenId.unwrap(input.tokenIds[j]))));
}
} else {
offer.tokenIds = input.tokenIds;
}
}
if (tokenType == TokenType.ERC20 || tokenType == TokenType.ERC1155) {
offer.tokenss = input.tokenss;
for (uint j = 0; j < input.tokenss.length; j++) {
offer.useds.push();
}
}
emit Offered(Index.wrap(uint32(offers.length - 1)), Account.wrap(msg.sender), offer.token, tokenType, offer.buySell, offer.expiry, offer.count, nonce, input.prices, input.tokenIds, input.tokenss, Unixtime.wrap(uint40(block.timestamp)));
}
}
// Cannot update token and buySell
function updateOffers(UpdateOffer[] calldata inputs) external onlyOwner {
for (uint i = 0; i < inputs.length; i++) {
UpdateOffer memory input = inputs[i];
uint index = Index.unwrap(input.index);
Offer storage offer = offers[index];
offer.expiry = input.expiry;
offer.count = input.count;
offer.nonce = nonce;
TokenType tokenType = tokenTypes[offer.token];
if (input.prices.length == 0) {
revert InvalidInputData("all: prices array must contain at least one price");
} else if (tokenType == TokenType.ERC20 && input.prices.length != 1 && input.tokenss.length != input.prices.length) {
revert InvalidInputData("ERC-20: tokenss array length must match prices array length");
} else if (tokenType == TokenType.ERC721 && input.prices.length != 1 && input.tokenIds.length != input.prices.length) {
revert InvalidInputData("ERC-721: tokenIds array length must match prices array length");
} else if (tokenType == TokenType.ERC1155 && input.tokenIds.length != input.tokenss.length) {
revert InvalidInputData("ERC-1155: tokenIds and tokenss array length must match");
} else if (tokenType == TokenType.ERC1155 && input.prices.length != 1 && input.tokenIds.length != input.prices.length) {
revert InvalidInputData("ERC-1155: tokenIds and tokenss array length must match prices array length");
}
offer.prices = input.prices;
if (tokenType == TokenType.ERC721 || tokenType == TokenType.ERC1155) {
if (input.tokenIds.length > 1) {
for (uint j = 1; j < input.tokenIds.length; j++) {
if (TokenId.unwrap(input.tokenIds[j - 1]) >= TokenId.unwrap(input.tokenIds[j])) {
revert TokenIdsMustBeSortedWithNoDuplicates();
}
}
}
if (input.tokenIds.length > 0) {
uint maxTokenId;
for (uint j = 0; j < input.tokenIds.length; j++) {
if (maxTokenId < TokenId.unwrap(input.tokenIds[j])) {
maxTokenId = TokenId.unwrap(input.tokenIds[j]);
}
}
if (maxTokenId < 2 ** 16) {
offer.tokenIdType = TokenIdType.TOKENID16;
}
}
if (offer.tokenIdType == TokenIdType.TOKENID16) {
for (uint j = 0; j < input.tokenIds.length; j++) {
offer.tokenId16s.push(TokenId16.wrap(uint16(TokenId.unwrap(input.tokenIds[j]))));
}
} else {
offer.tokenIds = input.tokenIds;
}
}
if (tokenType == TokenType.ERC20 || tokenType == TokenType.ERC1155) {
offer.tokenss = input.tokenss;
}
emit OfferUpdated(Index.wrap(uint32(index)), Account.wrap(msg.sender), offer.token, tokenType, offer.buySell, offer.expiry, offer.count, nonce, input.prices, input.tokenIds, input.tokenss, Unixtime.wrap(uint40(block.timestamp)));
}
}
// TradeInput:
// ERC-20
// tokenIds[], tokenss[tokens0]
// ERC-721
// tokenIds[tokenId0, tokenId1, ...], tokenss[]
// ERC-1155
// tokenIds[tokenId0, tokenId1, ...], tokenss[tokens0, tokens1, ...]
function trade(TradeInput[] calldata inputs, bool paymentsInEth) external payable nonReentrant notOwner {
uint totalEth = msg.value;
if (totalEth > 0) {
emit InternalTransfer(msg.sender, address(this), totalEth, Unixtime.wrap(uint40(block.timestamp)));
}
uint takerToOwnerTotal;
uint ownerToTakerTotal;
for (uint i = 0; i < inputs.length; i++) {
TradeInput memory input = inputs[i];
if (Index.unwrap(input.index) >= offers.length) {
revert InvalidIndex(input.index);
}
Offer storage offer = offers[Index.unwrap(input.index)];
TokenType tokenType = tokenTypes[offer.token];
if (Nonce.unwrap(offer.nonce) != Nonce.unwrap(nonce)) {
revert InvalidOffer(offer.nonce, nonce);
}
if (Unixtime.unwrap(offer.expiry) != 0 && block.timestamp > Unixtime.unwrap(offer.expiry)) {
revert OfferExpired(input.index, offer.expiry);
}
uint price;
uint[] memory prices_;
uint[] memory tokenIds_;
uint[] memory tokenss_;
if (tokenType == TokenType.ERC20) {
if (input.tokenss.length != 1) {
revert InvalidInputData("Expecting single tokens input");
}
uint tokens = uint(Tokens.unwrap(input.tokenss[0]));
uint totalTokens;
uint totalWETHTokens;
prices_ = new uint[](offer.prices.length);
tokenIds_ = new uint[](0);
tokenss_ = new uint[](offer.prices.length);
uint k;
for (uint j = 0; j < offer.prices.length && tokens > 0; j++) {
uint _price = Price.unwrap(offer.prices[j]);
if (Tokens.unwrap(offer.tokenss[j]) > Tokens.unwrap(offer.useds[j])) {
uint remaining = Tokens.unwrap(offer.tokenss[j]) - Tokens.unwrap(offer.useds[j]);
if (tokens >= remaining) {
totalTokens += remaining;
totalWETHTokens += remaining * _price / 10**18;
offer.useds[j] = Tokens.wrap(Tokens.unwrap(offer.useds[j]) + uint128(remaining));
prices_[k] = _price;
tokenss_[k] = remaining;
tokens -= remaining;
} else {
totalTokens += tokens;
totalWETHTokens += tokens * _price / 10**18;
offer.useds[j] = Tokens.wrap(Tokens.unwrap(offer.useds[j]) + uint128(tokens));
prices_[k] = _price;
tokenss_[k] = tokens;
tokens = 0;
}
k++;
}
}
if (input.execution == Execution.FILLORKILL && totalTokens < uint(Tokens.unwrap(input.tokenss[0]))) {
revert InsufficentTokensRemaining(input.tokenss[0], Tokens.wrap(uint128(totalTokens)));
}
if (totalTokens > 0) {
price = totalWETHTokens * 10**18 / totalTokens;
if (offer.buySell == BuySell.BUY) {
if (price < Price.unwrap(input.price)) {
revert ExecutedAveragePriceLessThanSpecified(Price.wrap(uint128(price)), input.price);
}
IERC20(Token.unwrap(offer.token)).transferFrom(msg.sender, Account.unwrap(owner), totalTokens);
ownerToTakerTotal += totalWETHTokens;
} else {
if (price > Price.unwrap(input.price)) {
revert ExecutedAveragePriceGreaterThanSpecified(Price.wrap(uint128(price)), input.price);
}
takerToOwnerTotal += totalWETHTokens;
IERC20(Token.unwrap(offer.token)).transferFrom(Account.unwrap(owner), msg.sender, totalTokens);
}
}
} else if (tokenType == TokenType.ERC721) {
if (Count.unwrap(offer.count) != type(uint16).max) {
if (Count.unwrap(offer.count) < input.tokenIds.length) {
revert InsufficentCountRemaining(Count.wrap(uint16(input.tokenIds.length)), offer.count);
}
offer.count = Count.wrap(Count.unwrap(offer.count) - uint16(input.tokenIds.length));
}
prices_ = new uint[](input.tokenIds.length);
tokenIds_ = new uint[](input.tokenIds.length);
tokenss_ = new uint[](0);
for (uint j = 0; j < input.tokenIds.length; j++) {
uint p;
if (offer.tokenIds.length > 0) {
uint k;
if (offer.tokenIdType == TokenIdType.TOKENID16) {
if (TokenId.unwrap(input.tokenIds[j]) < 2 ** 16) {
k = ArrayUtils.indexOfTokenId16s(offer.tokenId16s, TokenId16.wrap(uint16(TokenId.unwrap(input.tokenIds[j]))));
} else {
k = type(uint).max;
}
} else {
k = ArrayUtils.indexOfTokenIds(offer.tokenIds, input.tokenIds[j]);
}
if (k == type(uint).max) {
revert InvalidTokenId(input.tokenIds[j]);
}
if (offer.prices.length == offer.tokenIds.length) {
p = Price.unwrap(offer.prices[k]);
} else {
p = Price.unwrap(offer.prices[0]);
}
} else {
p = Price.unwrap(offer.prices[0]);
}
prices_[j] = p;
tokenIds_[j] = TokenId.unwrap(input.tokenIds[j]);
price += p;
if (offer.buySell == BuySell.BUY) {
IERC721Partial(Token.unwrap(offer.token)).transferFrom(msg.sender, Account.unwrap(owner), TokenId.unwrap(input.tokenIds[j]));
} else {
IERC721Partial(Token.unwrap(offer.token)).transferFrom(Account.unwrap(owner), msg.sender, TokenId.unwrap(input.tokenIds[j]));
}
}
if (offer.buySell == BuySell.BUY) {
if (price < Price.unwrap(input.price)) {
revert ExecutedTotalPriceLessThanSpecified(Price.wrap(uint128(price)), input.price);
}
ownerToTakerTotal += price;
} else {
if (price > Price.unwrap(input.price)) {
revert ExecutedTotalPriceGreaterThanSpecified(Price.wrap(uint128(price)), input.price);
}
takerToOwnerTotal += price;
}
} else if (tokenType == TokenType.ERC1155) {
if (input.tokenIds.length != input.tokenss.length) {
revert InvalidInputData("tokenIds must have the same length as tokenss");
}
uint totalCount;
for (uint j = 0; j < input.tokenss.length; j++) {
totalCount += Tokens.unwrap(input.tokenss[j]);
}
if (Count.unwrap(offer.count) < totalCount) {
revert InsufficentCountRemaining(Count.wrap(uint16(totalCount)), offer.count);
}
if (Count.unwrap(offer.count) != type(uint16).max) {
offer.count = Count.wrap(Count.unwrap(offer.count) - uint16(totalCount));
}
prices_ = new uint[](input.tokenIds.length);
tokenIds_ = new uint[](input.tokenIds.length);
tokenss_ = new uint[](input.tokenIds.length);
for (uint j = 0; j < input.tokenIds.length; j++) {
uint p;
if (offer.tokenIds.length > 0) {
uint k;
if (offer.tokenIdType == TokenIdType.TOKENID16) {
if (TokenId.unwrap(input.tokenIds[j]) < 2 ** 16) {
k = ArrayUtils.indexOfTokenId16s(offer.tokenId16s, TokenId16.wrap(uint16(TokenId.unwrap(input.tokenIds[j]))));
} else {
k = type(uint).max;
}
} else {
k = ArrayUtils.indexOfTokenIds(offer.tokenIds, input.tokenIds[j]);
}
if (k == type(uint).max) {
revert InvalidTokenId(input.tokenIds[j]);
}
if (Tokens.unwrap(offer.useds[k]) + Tokens.unwrap(input.tokenss[j]) > Tokens.unwrap(offer.tokenss[k])) {
revert InsufficentCountRemaining(Count.wrap(uint16(Tokens.unwrap(input.tokenss[j]))), Count.wrap(uint16(Tokens.unwrap(offer.tokenss[k]))));
}
offer.useds[k] = Tokens.wrap(Tokens.unwrap(offer.useds[k]) + uint128(Tokens.unwrap(input.tokenss[j])));
if (offer.prices.length == offer.tokenIds.length) {
p = Price.unwrap(offer.prices[k]);
} else {
p = Price.unwrap(offer.prices[0]);
}
} else {
p = Price.unwrap(offer.prices[0]);
}
prices_[j] = p;
tokenIds_[j] = uint(TokenId.unwrap(input.tokenIds[j]));
tokenss_[j] = uint(Tokens.unwrap(input.tokenss[j]));
price += p * uint(Tokens.unwrap(input.tokenss[j]));
if (offer.buySell == BuySell.BUY) {
IERC1155Partial(Token.unwrap(offer.token)).safeTransferFrom(msg.sender, Account.unwrap(owner), TokenId.unwrap(input.tokenIds[j]), Tokens.unwrap(input.tokenss[j]), "");
} else {
IERC1155Partial(Token.unwrap(offer.token)).safeTransferFrom(Account.unwrap(owner), msg.sender, TokenId.unwrap(input.tokenIds[j]), Tokens.unwrap(input.tokenss[j]), "");
}
}
if (offer.buySell == BuySell.BUY) {
if (price < Price.unwrap(input.price)) {
revert ExecutedTotalPriceLessThanSpecified(Price.wrap(uint128(price)), input.price);
}
ownerToTakerTotal += price;
} else {
if (price > Price.unwrap(input.price)) {
revert ExecutedTotalPriceGreaterThanSpecified(Price.wrap(uint128(price)), input.price);
}
takerToOwnerTotal += price;
}
}
emit Traded(input.index, Account.wrap(msg.sender), owner, offer.token, tokenType, offer.buySell, prices_, tokenIds_, tokenss_, Price.wrap(uint128(price)), Unixtime.wrap(uint40(block.timestamp)));
}
if (takerToOwnerTotal < ownerToTakerTotal) {
uint diff = ownerToTakerTotal - takerToOwnerTotal;
if (paymentsInEth) {
weth.transferFrom(Account.unwrap(owner), address(this), diff);
weth.withdraw(diff);
emit InternalTransfer(address(this), msg.sender, diff, Unixtime.wrap(uint40(block.timestamp)));
payable(msg.sender).transfer(diff);
} else {
weth.transferFrom(Account.unwrap(owner), msg.sender, diff);
}
} else if (takerToOwnerTotal > ownerToTakerTotal){
uint diff = takerToOwnerTotal - ownerToTakerTotal;
if (paymentsInEth) {
if (diff > totalEth) {
revert InsufficentEthersRemaining(diff, totalEth);
}
totalEth -= diff;
emit InternalTransfer(address(this), address(weth), diff, Unixtime.wrap(uint40(block.timestamp)));
weth.deposit{value: diff}();
weth.transfer(Account.unwrap(owner), diff);
} else {
weth.transferFrom(msg.sender, Account.unwrap(owner), diff);
}
}
if (totalEth > 0) {
emit InternalTransfer(address(this), msg.sender, totalEth, Unixtime.wrap(uint40(block.timestamp)));
payable(msg.sender).transfer(totalEth);
}
}
receive() external payable {
}
function getOffersInfo(uint from, uint to) public view returns (OfferInfo[] memory results) {
uint start = from < offers.length ? from : offers.length;
uint end = to < offers.length ? to : offers.length;
results = new OfferInfo[](end - start);
uint k;
for (uint i = start; i < end; i++) {
if (i < offers.length) {
Offer memory offer = offers[i];
TokenId[] memory tokenIds;
if (offer.tokenIdType == TokenIdType.TOKENID16) {
tokenIds = new TokenId[](offer.tokenId16s.length);
for (uint j = 0; j < offer.tokenId16s.length; j++) {
tokenIds[j] = TokenId.wrap(uint(TokenId16.unwrap(offer.tokenId16s[j])));
}
} else {
tokenIds = offer.tokenIds;
}
results[k++] = OfferInfo(i, offer.token, tokenTypes[offer.token], offer.buySell, offer.expiry, offer.count, offer.nonce, offer.prices, tokenIds, offer.tokenss, offer.useds);
}
}
}
}
/// @notice TokenAgent factory
contract TokenAgentFactory is CloneFactory {
struct TokenAgentRecord {
TokenAgent tokenAgent;
Index indexByOwner;
}
struct TokenAgentInfo {
Index index;
Index indexByOwner;
TokenAgent tokenAgent;
Account owner;
}
WETH public weth;
TokenAgent public tokenAgentTemplate;
TokenAgentRecord[] public tokenAgentRecords;
mapping(Account => Index[]) public tokenAgentIndicesByOwners;
mapping(TokenAgent => Index) public tokenAgentIndex;
event NewTokenAgent(TokenAgent indexed tokenAgent, Account indexed owner, Index indexed index, Index indexByOwner, Unixtime timestamp);
error NotInitialised();
error GoAway();
constructor() {
tokenAgentTemplate = new TokenAgent();
}
function init(WETH _weth, TokenAgent _tokenAgentTemplate) public {
if (address(weth) != address(0)) {
revert GoAway();
}
weth = _weth;
tokenAgentTemplate = _tokenAgentTemplate;
}
function newTokenAgent() public {
if (address(weth) == address(0)) {
revert NotInitialised();
}
TokenAgent tokenAgent = TokenAgent(payable(createClone(address(tokenAgentTemplate))));
tokenAgent.init(weth, Account.wrap(msg.sender));
tokenAgentRecords.push(TokenAgentRecord(tokenAgent, Index.wrap(uint32(tokenAgentIndicesByOwners[Account.wrap(msg.sender)].length))));
tokenAgentIndicesByOwners[Account.wrap(msg.sender)].push(Index.wrap(uint32(tokenAgentRecords.length - 1)));
emit NewTokenAgent(tokenAgent, Account.wrap(msg.sender), Index.wrap(uint32(tokenAgentRecords.length - 1)), Index.wrap(uint32(tokenAgentIndicesByOwners[Account.wrap(msg.sender)].length - 1)), Unixtime.wrap(uint40(block.timestamp)));
}
function tokenAgentsLength() public view returns (uint) {
return tokenAgentRecords.length;
}
function tokenAgentsByOwnerLength(Account owner) public view returns (uint) {
return tokenAgentIndicesByOwners[owner].length;
}
function getTokenAgentsInfo(uint from, uint to) public view returns (TokenAgentInfo[] memory results) {
uint start = from < tokenAgentRecords.length ? from : tokenAgentRecords.length;
uint end = to < tokenAgentRecords.length ? to : tokenAgentRecords.length;
results = new TokenAgentInfo[](end - start);
uint k;
for (uint i = start; i < end; i++) {
results[k++] = TokenAgentInfo(Index.wrap(uint32(i)), tokenAgentRecords[i].indexByOwner, tokenAgentRecords[i].tokenAgent, tokenAgentRecords[i].tokenAgent.owner());
}
}
function getTokenAgentsByOwnerInfo(Account owner, uint from, uint to) public view returns (TokenAgentInfo[] memory results) {
Index[] memory indices = tokenAgentIndicesByOwners[owner];
uint start = from < indices.length ? from : indices.length;
uint end = to < indices.length ? to : indices.length;
results = new TokenAgentInfo[](end - start);
uint k;
for (uint i = start; i < end; i++) {
uint index = Index.unwrap(indices[i]);
results[k++] = TokenAgentInfo(Index.wrap(uint32(index)), Index.wrap(uint32(i)), tokenAgentRecords[index].tokenAgent, tokenAgentRecords[index].tokenAgent.owner());
}
}
}