-
Notifications
You must be signed in to change notification settings - Fork 9
/
objkt_settle_english_auction_v3.ts
136 lines (121 loc) · 4.45 KB
/
objkt_settle_english_auction_v3.ts
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
import get from 'lodash/get';
import omit from 'lodash/omit';
import { assert, object, string, Describe } from 'superstruct';
import { TezosAddress, IsoDateString, PositiveInteger, PgBigInt, ContractAddress } from '../../../lib/validators';
import { TransactionHandler, TokenEvent, SaleEventInterface } from '../../../types';
import { findDiff, createEventId, isTezLikeCurrency, extractObjktCurrency } from '../../../lib/utils';
import { CURRENCY_MAPPINGS, OBJKT_CONTRACT_ENGLISH_AUCTION_V3, SALE_INTERFACE } from '../../../consts';
import {
tokenSaleEventFields,
currencyField,
reserveField,
startTimeField,
endTimeField,
priceField,
editionsField,
priceIncrementField,
auctionIdField,
} from '../event-fields-meta';
export const EVENT_TYPE_OBJKT_SETTLE_ENGLISH_AUCTION_V3 = 'OBJKT_CONTRACT_ENGLISH_AUCTION_V3';
export interface ObjktSettleEnglishAuctionV3Event extends TokenEvent {
type: typeof EVENT_TYPE_OBJKT_SETTLE_ENGLISH_AUCTION_V3;
implements?: SaleEventInterface;
seller_address: string;
buyer_address: string;
currency: string;
reserve: string;
start_time: string;
end_time: string;
price: string;
extension_time: string;
//royalties: string;
price_increment: string;
auction_id: string;
}
const ObjktSettleEnglishAuctionEventSchema: Describe<Omit<ObjktSettleEnglishAuctionV3Event, 'type' | 'implements'>> = object({
id: string(),
opid: PgBigInt,
timestamp: IsoDateString,
level: PositiveInteger,
fa2_address: ContractAddress,
token_id: string(),
ophash: string(),
seller_address: TezosAddress,
buyer_address: TezosAddress,
currency: string(),
reserve: PgBigInt,
start_time: IsoDateString,
end_time: IsoDateString,
price: PgBigInt,
extension_time: PgBigInt,
//royalties: PgBigInt,
price_increment: PgBigInt,
auction_id: PgBigInt,
});
const ObjktSettleEnglishAuctionHandler: TransactionHandler<ObjktSettleEnglishAuctionV3Event> = {
source: 'transaction',
type: EVENT_TYPE_OBJKT_SETTLE_ENGLISH_AUCTION_V3,
meta: {
eventDescription: `An English auction was settled on objkt.com. Either the token was bought or the auction ended without a sale. (marketplace contract: KT18iSHoRW1iogamADWwQSDoZa3QkN4izkqj).`,
eventFields: [
...tokenSaleEventFields,
currencyField,
reserveField,
startTimeField,
endTimeField,
priceField,
editionsField,
priceIncrementField,
auctionIdField,
],
},
accept: {
entrypoint: 'settle_auction',
target_address: OBJKT_CONTRACT_ENGLISH_AUCTION_V3,
},
exec: (transaction) => {
const auctionId = get(transaction, 'parameter.value');
const diff = findDiff(transaction.diffs!, 574016, 'auctions', ['remove_key'], auctionId);
const sellerAddress = get(diff, 'content.value.creator');
const fa2Address = get(diff, 'content.value.token.address');
const tokenId = get(diff, 'content.value.token.token_id');
const reserve = get(diff, 'content.value.reserve');
const startTime = get(diff, 'content.value.start_time');
const endTime = get(diff, 'content.value.end_time');
const extensionTime = get(diff, 'content.value.extension_time');
const priceIncrement = get(diff, 'content.value.price_increment');
const highestBidderAddress = get(diff, 'content.value.highest_bidder');
const currentPrice = get(diff, 'content.value.current_price');
const currency = extractObjktCurrency(get(diff, 'content.value.currency'));
if (!currency || !isTezLikeCurrency(currency)) {
throw new Error(`unsupported currency`);
}
const id = createEventId(EVENT_TYPE_OBJKT_SETTLE_ENGLISH_AUCTION_V3, transaction);
const event: ObjktSettleEnglishAuctionV3Event = {
id,
type: EVENT_TYPE_OBJKT_SETTLE_ENGLISH_AUCTION_V3,
opid: String(transaction.id),
ophash: transaction.hash,
timestamp: transaction.timestamp,
level: transaction.level,
fa2_address: fa2Address,
token_id: tokenId,
seller_address: sellerAddress,
buyer_address: highestBidderAddress,
currency: currency,
reserve,
start_time: startTime,
end_time: endTime,
extension_time: extensionTime,
price_increment: priceIncrement,
auction_id: auctionId,
price: currentPrice,
};
if (event.seller_address !== event.buyer_address) {
event.implements = SALE_INTERFACE;
}
assert(omit(event, ['type', 'implements']), ObjktSettleEnglishAuctionEventSchema);
return event;
},
};
export default ObjktSettleEnglishAuctionHandler;