-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpowerofsoon.spec.ts
394 lines (382 loc) · 19.7 KB
/
powerofsoon.spec.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
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
import { expect } from 'chai';
import {
Account,
Blockchain,
bnToBigInt,
expectToThrow,
mintTokens,
nameToBigInt,
symbolCodeToBigInt,
} from '@proton/vert';
import { Asset, Name, TimePointSec } from '@greymass/eosio';
import { Assets, Config } from 'proton-tsc/atomicassets';
import {
COLLECTION_CYPHER_GANG,
COLLECTION_PIXELHEROES,
TOKEN_XPR,
TOKEN_XUSDC,
eosio_assert,
} from '../helpers/common.ts';
import { NFT, createTestCollection, initialAdminColEdit, transferNfts } from '../helpers/atomicassets.helper.ts';
import {
Auction,
ERROR_AUCTION_NOT_FINISHED,
addTokens,
announceAuction,
auctionBid,
regMarket,
} from '../helpers/atomicmarket.helper.ts';
import {
ERROR_AUCTION_HAS_BIDS,
ERROR_AUCTION_NOT_EXISTS,
ERROR_AUCTION_STILL_RUNNING,
ERROR_INVALID_AUCTION_SELLER,
ERROR_INVALID_REAUCT_DURATION,
ERROR_INVALID_START_PRICE,
ERROR_MARKET_BALANCE_NOT_FOUND,
ERROR_MISSING_REQUIRED_AUTHORITY_POWEROFSOON,
ONE_DAY,
ONE_HOUR,
ONE_WEEK,
ORACLES_FEED_INDEX_XPRUSD,
ORACLES_FEED_NAME_XPRUSD,
SILVER_AUCTION_START_PRICE_USD,
TWO_WEEKS,
} from './powerofsoon.constants.ts';
import { Globals as SoonMarketGlobals } from '../soonmarket/soonmarket.tables';
const blockchain = new Blockchain();
// deploy contract to test
const powerofsoon = blockchain.createContract('powerofsoon', 'powerofsoon/target/powerofsoon.contract');
const soonmarket = blockchain.createContract('soonmarket', 'soonmarket/target/soonmarket.contract');
// deploy contracts required for testing
const eosioToken = blockchain.createContract('eosio.token', 'node_modules/proton-tsc/external/eosio.token/eosio.token');
const atomicassets = blockchain.createContract('atomicassets', 'external/atomicassets/atomicassets');
const atomicmarket = blockchain.createContract('atomicmarket', 'external/atomicmarket/atomicmarket');
const xtokens = blockchain.createContract('xtokens', 'node_modules/proton-tsc/external/xtokens/xtokens');
const oracles = blockchain.createContract('oracles', 'node_modules/proton-tsc/external/oracles/oracles');
// create accounts
const [soonfinance, protonpunk, pixelheroes, marco, mitch] = blockchain.createAccounts(
'soonfinance',
'protonpunk',
'pixelheroes',
'marco',
'mitch',
);
// helpers
const initContracts = async (...contracts: Array<Account>): Promise<void> => {
for (const contract of contracts) {
await contract.actions.init().send();
}
};
const mintFreeSpot = async (recipient: Account, memo: string) => {
await powerofsoon.actions.mintfreespot([recipient.name.toString(), memo]).send();
};
const mintAuctSpot = async (duration: u32) => {
await powerofsoon.actions.mintauctspot([duration]).send();
};
const claimAuctInc = async (actor: Account, auctionId: u64) => {
await powerofsoon.actions.claimauctinc([auctionId]).send(`${actor.name.toString()}@active`);
};
const getAccountBalance = (contract: Account, accountName: string, symbol: string) => {
const accountBigInt = nameToBigInt(Name.from(accountName));
const symcodeBigInt = symbolCodeToBigInt(Asset.SymbolCode.from(symbol));
return contract.tables.accounts(accountBigInt).getTableRow(symcodeBigInt);
};
const getAaConfig = (): Config => atomicassets.tables.config().getTableRows()[0];
const getSoonMarketGlobals = (): SoonMarketGlobals => soonmarket.tables.globals().getTableRows()[0];
let silverSpots: Array<NFT> = [];
let goldSpot: NFT;
let cypherToAuction: NFT;
const XPR_USD_PRICE = 0.0007857865;
let initSnapshot: number;
before(async () => {
blockchain.resetTables();
await initContracts(atomicassets, atomicmarket);
await initialAdminColEdit(atomicassets);
// atomicassets
await createTestCollection(atomicassets, powerofsoon, marco); // minting all spot NFTs to marco
await createTestCollection(atomicassets, protonpunk);
await createTestCollection(atomicassets, pixelheroes);
// tokens
await mintTokens(eosioToken, TOKEN_XPR.name, TOKEN_XPR.precision, 100_000_000.0, 10_000_000, [marco, mitch]);
await mintTokens(xtokens, TOKEN_XUSDC.name, TOKEN_XUSDC.precision, 2_588_268_654.84833, 20_000, [marco, mitch]);
// atomicmarket
await regMarket(atomicmarket, soonmarket);
await addTokens(atomicmarket, eosioToken, [TOKEN_XPR]);
await addTokens(atomicmarket, xtokens, [TOKEN_XUSDC]);
// get spot nft id (no need to check collection because marco owns only spot nfts)
const spotNfts: Array<NFT> = atomicassets.tables
.assets(nameToBigInt(marco.name))
.getTableRows(undefined, { limit: 4 });
goldSpot = spotNfts[0];
silverSpots.push(spotNfts[1], spotNfts[2], spotNfts[3]);
// get cypher nft to auction
cypherToAuction = atomicassets.tables
.assets(nameToBigInt(protonpunk.name))
.getTableRows(undefined, { limit: 1 })[0];
// set correct spot ids for tests
await soonmarket.actions.setspots([Number.parseInt(goldSpot.asset_id), silverSpots[0].template_id]).send();
// add verified collection
await soonmarket.actions.addverified([COLLECTION_CYPHER_GANG, 'testing cool shit here :-)']).send();
// add blacklisted collection
await soonmarket.actions.addblacklist([COLLECTION_PIXELHEROES, 'testing cool shit here :-)']).send();
const soonmarketGlobals = getSoonMarketGlobals();
expect(soonmarketGlobals.goldSpotId).equal(goldSpot.asset_id);
expect(soonmarketGlobals.silverSpotTemplateId).equal(silverSpots[0].template_id);
expect(soonmarketGlobals.verifiedCount).equal(1);
expect(soonmarketGlobals.blacklistCount).equal(1);
// set up xprUsd feed
await oracles.actions
.setfeed([
'oracles',
ORACLES_FEED_INDEX_XPRUSD,
ORACLES_FEED_NAME_XPRUSD,
'',
'mean_median',
'double',
[
{ key: 'data_same_provider_limit', value: 10 },
{ key: 'data_window_size', value: 210 },
{ key: 'min_provider_wait_sec', value: 0 },
],
['oracles'],
])
.send();
// feed
await oracles.actions.feed(['oracles', ORACLES_FEED_INDEX_XPRUSD, { d_double: XPR_USD_PRICE }]).send();
initSnapshot = blockchain.store.snapshot();
});
beforeEach(async () => {
blockchain.store.revertTo(initSnapshot);
});
describe('PowerOfSoon', () => {
describe('core spot handling', () => {
it('silver spot (burn, mint, auction)', async () => {
let spotNft = atomicassets.tables.assets(nameToBigInt(marco.name)).getTableRow(silverSpots[0].asset_id);
expect(spotNft).not.undefined;
// promote by transferring Spot NFT with valid memo to soonmarket
await transferNfts(
atomicassets,
marco,
soonmarket,
[silverSpots[0]],
`collection ${COLLECTION_CYPHER_GANG}`,
);
// soonmarket will transfer NFT to powerofsoon and shouldn't own the NFT
spotNft = atomicassets.tables.assets(nameToBigInt(soonmarket.name)).getTableRow(silverSpots[0].asset_id);
expect(spotNft).undefined;
// powerofsoon will burn it, so it doesn't own it anymore
spotNft = atomicassets.tables.assets(nameToBigInt(powerofsoon.name)).getTableRow(silverSpots[0].asset_id);
expect(spotNft).undefined;
const auction = atomicmarket.tables.auctions().getTableRows(undefined, { limit: 1 })[0];
// starting at 0, the end_time must equal the duration of the auction
expect(auction.end_time).equal(ONE_WEEK);
const expectedStartingPrice = Math.round((SILVER_AUCTION_START_PRICE_USD * 10000) / XPR_USD_PRICE);
expect(auction.current_bid).equal(Asset.fromUnits(expectedStartingPrice, '4,XPR').toString());
});
it('gold spot logic promoting collection (re-auction)', async () => {
// marco is owner, powerofsoon not
let spotNft = atomicassets.tables.assets(nameToBigInt(marco.name)).getTableRow(goldSpot.asset_id);
expect(spotNft).not.undefined;
spotNft = atomicassets.tables.assets(nameToBigInt(powerofsoon.name)).getTableRow(goldSpot.asset_id);
expect(spotNft).undefined;
// promote by transferring Spot NFT with valid memo to soonmarket
await transferNfts(atomicassets, marco, soonmarket, [goldSpot], `collection ${COLLECTION_CYPHER_GANG}`);
// atomicmarket is new owner because the gold spot will immediately be re-auctioned
spotNft = atomicassets.tables.assets(nameToBigInt(atomicmarket.name)).getTableRow(goldSpot.asset_id);
expect(spotNft).not.undefined;
// get gold spot auction
const auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(1));
expect(auction.asset_ids[0]).equal(goldSpot.asset_id);
// expect the gold spot auction to end after two weeks + delay of one day
expect(auction.end_time).equal(TWO_WEEKS + ONE_DAY);
});
it('gold spot logic promoting auction (re-auction)', async () => {
// marco is owner, powerofsoon not
let spotNft = atomicassets.tables.assets(nameToBigInt(marco.name)).getTableRow(goldSpot.asset_id);
expect(spotNft).not.undefined;
spotNft = atomicassets.tables.assets(nameToBigInt(powerofsoon.name)).getTableRow(goldSpot.asset_id);
expect(spotNft).undefined;
// announce & start auction
await announceAuction(
atomicmarket,
protonpunk,
[cypherToAuction],
1_337,
TOKEN_XPR,
ONE_DAY,
soonmarket,
atomicassets,
true,
);
// get auction
let auction: Auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(1));
const promotedAuctionEnd = auction.end_time;
// promote by transferring gold spot with valid memo to soonmarket
await transferNfts(atomicassets, marco, soonmarket, [goldSpot], `auction ${auction.auction_id}`);
// atomicmarket is new owner because the gold spot will immediately be re-auctioned
spotNft = atomicassets.tables.assets(nameToBigInt(atomicmarket.name)).getTableRow(goldSpot.asset_id);
expect(spotNft).not.undefined;
// get gold spot auction
auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(2));
expect(auction.asset_ids[0]).equal(goldSpot.asset_id);
// expect the gold spot auction to end one day after the promoted auction ends
expect(auction.end_time).equal(promotedAuctionEnd + ONE_DAY);
});
});
describe('primary spot issuance', () => {
it('mint free spot', async () => {
let mitchNfts: Array<Assets> = atomicassets.tables.assets(nameToBigInt(mitch.name)).getTableRows();
expect(mitchNfts).empty;
const expectedAssetId = getAaConfig().asset_counter;
await mintFreeSpot(mitch, 'free spot for mitch ;-)');
mitchNfts = atomicassets.tables.assets(nameToBigInt(mitch.name)).getTableRows();
expect(mitchNfts).not.empty;
const spotNft: Assets = atomicassets.tables.assets(nameToBigInt(mitch.name)).getTableRow(expectedAssetId);
expect(spotNft.template_id).equal(getSoonMarketGlobals().silverSpotTemplateId);
});
it('mint and auction spot', async () => {
let marketNfts: Array<Assets> = atomicassets.tables.assets(nameToBigInt(atomicmarket.name)).getTableRows();
expect(marketNfts).empty;
const auctions: Array<Auction> = atomicmarket.tables.auctions().getTableRows();
expect(auctions).empty;
const expectedAssetId = getAaConfig().asset_counter;
await mintAuctSpot(ONE_HOUR);
marketNfts = atomicassets.tables.assets(nameToBigInt(atomicmarket.name)).getTableRows();
expect(marketNfts).not.empty;
const spotNft: Assets = atomicassets.tables
.assets(nameToBigInt(atomicmarket.name))
.getTableRow(expectedAssetId);
expect(spotNft.template_id).equal(getSoonMarketGlobals().silverSpotTemplateId);
const auction: Auction = atomicmarket.tables.auctions().getTableRows()[0];
expect(auction.asset_ids[0]).equal(expectedAssetId);
expect(auction.end_time).equal(ONE_HOUR);
});
});
describe('auction handling', () => {
it('claim auction income', async () => {
await mintAuctSpot(ONE_HOUR);
// get auction
let auction: Auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(1));
const bidAmount = SILVER_AUCTION_START_PRICE_USD / XPR_USD_PRICE;
await auctionBid(atomicmarket, mitch, auction.auction_id, bidAmount, TOKEN_XPR, eosioToken, soonmarket);
auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(auction.auction_id));
await expectToThrow(claimAuctInc(marco, auction.auction_id), eosio_assert(ERROR_AUCTION_NOT_FINISHED));
blockchain.addTime(TimePointSec.fromInteger(ONE_HOUR + 1));
let atomicmarketBalance = getAccountBalance(eosioToken, 'atomicmarket', 'XPR');
let powerofsoonBalance = getAccountBalance(eosioToken, 'powerofsoon', 'XPR');
let soonmarketBalance = getAccountBalance(eosioToken, 'powerofsoon', 'XPR');
let soonfinanceBalance = getAccountBalance(eosioToken, 'soonfinance', 'XPR');
expect(atomicmarketBalance.balance).equal(`${bidAmount.toFixed(4)} XPR`);
expect(powerofsoonBalance).undefined;
expect(soonmarketBalance).undefined;
expect(soonfinanceBalance).undefined;
// marco is allowed to execute the action, anybody is
await claimAuctInc(marco, auction.auction_id);
// soonmarket will get 2% and powerofsoon will get 83% + 15% = 98% of the income
// powerofsoon will automatically forward the earnings to soonfinance
atomicmarketBalance = getAccountBalance(eosioToken, 'atomicmarket', 'XPR');
powerofsoonBalance = getAccountBalance(eosioToken, 'powerofsoon', 'XPR');
soonmarketBalance = getAccountBalance(eosioToken, 'soonmarket', 'XPR');
soonfinanceBalance = getAccountBalance(eosioToken, 'soonfinance', 'XPR');
// balance now initialized, but zero because it's forwarded
expect(powerofsoonBalance.balance).equal('0.0000 XPR');
const bid = Number.parseInt(auction.current_bid.split(' ')[0].replace('.', ''));
const xprAssetSymbol = Asset.Symbol.from('4,XPR');
const powerofsoonIncome = Asset.from(((bid / 100) * 98) / 10000, xprAssetSymbol);
expect(soonfinanceBalance.balance).equal(powerofsoonIncome.toString());
// remains on atomicmarket for now
const soonmarketIncome = Asset.from(((bid / 100) * 2) / 10000, xprAssetSymbol);
expect(atomicmarketBalance.balance).equal(soonmarketIncome.toString());
// still undefined because unclaimed
expect(soonmarketBalance).undefined;
// marco is allowed to execute the action, anybody is
await soonmarket.actions.clmktbalance([]).send(`${marco.name.toString()}@active`);
atomicmarketBalance = getAccountBalance(eosioToken, 'atomicmarket', 'XPR');
soonmarketBalance = getAccountBalance(eosioToken, 'soonmarket', 'XPR');
soonfinanceBalance = getAccountBalance(eosioToken, 'soonfinance', 'XPR');
expect(atomicmarketBalance.balance).equal('0.0000 XPR');
expect(soonmarketBalance.balance).equal('0.0000 XPR');
expect(soonfinanceBalance.balance).equal(`${bidAmount.toFixed(4)} XPR`);
});
it('cancel auction with automated re-auctioning', async () => {
await mintAuctSpot(ONE_HOUR);
blockchain.addTime(TimePointSec.fromInteger(ONE_HOUR + 1));
const auction: Auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(1));
// marco is allowed to execute the action, anybody is
await powerofsoon.actions.cancelauct([auction.auction_id]).send(`${marco.name.toString()}@active`);
// expect a new auction running with default duration
const newAuction: Auction = atomicmarket.tables.auctions().getTableRow(bnToBigInt(2));
expect(newAuction.end_time).equal(blockchain.timestamp.toMilliseconds() / 1000 + ONE_WEEK);
});
});
describe('revert paths', () => {
it('reject setting start price with 0', async () => {
await expectToThrow(
powerofsoon.actions.setstartpric([0, 1]).send(),
eosio_assert(ERROR_INVALID_START_PRICE),
);
await expectToThrow(
powerofsoon.actions.setstartpric([1, 0]).send(),
eosio_assert(ERROR_INVALID_START_PRICE),
);
});
it('reject setting re-auction duration < one day', async () => {
await expectToThrow(
powerofsoon.actions.setreauctdur([ONE_DAY - 1]).send(),
eosio_assert(ERROR_INVALID_REAUCT_DURATION),
);
});
it('reject claiming non existing market balance', async () => {
await expectToThrow(
powerofsoon.actions.clmktbalance().send(),
eosio_assert(ERROR_MARKET_BALANCE_NOT_FOUND),
);
});
it('reject cancel of auction', async () => {
await expectToThrow(powerofsoon.actions.cancelauct([1]).send(), eosio_assert(ERROR_AUCTION_NOT_EXISTS));
await announceAuction(
atomicmarket,
marco,
[silverSpots[0]],
100,
TOKEN_XPR,
ONE_HOUR,
soonmarket,
atomicassets,
true,
);
blockchain.addTime(TimePointSec.fromInteger(ONE_HOUR + 1));
await expectToThrow(powerofsoon.actions.cancelauct([1]).send(), eosio_assert(ERROR_INVALID_AUCTION_SELLER));
await powerofsoon.actions.mintauctspot([ONE_HOUR]).send();
await expectToThrow(powerofsoon.actions.cancelauct([2]).send(), eosio_assert(ERROR_AUCTION_STILL_RUNNING));
const bidAmount = SILVER_AUCTION_START_PRICE_USD / XPR_USD_PRICE;
await auctionBid(atomicmarket, mitch, 2, bidAmount, TOKEN_XPR, eosioToken, soonmarket);
blockchain.addTime(TimePointSec.fromInteger(ONE_HOUR + 1));
await expectToThrow(powerofsoon.actions.cancelauct([2]).send(), eosio_assert(ERROR_AUCTION_HAS_BIDS));
});
it('reject with missing authority', async () => {
const sender = `${marco.name}@active`;
await expectToThrow(
powerofsoon.actions.setstartpric([1, 1]).send(sender),
ERROR_MISSING_REQUIRED_AUTHORITY_POWEROFSOON,
);
await expectToThrow(
powerofsoon.actions.setreauctdur([TWO_WEEKS]).send(sender),
ERROR_MISSING_REQUIRED_AUTHORITY_POWEROFSOON,
);
await expectToThrow(
powerofsoon.actions.mintfreespot([marco.name, 'should fail']).send(sender),
ERROR_MISSING_REQUIRED_AUTHORITY_POWEROFSOON,
);
await expectToThrow(
powerofsoon.actions.mintauctspot([ONE_HOUR]).send(sender),
ERROR_MISSING_REQUIRED_AUTHORITY_POWEROFSOON,
);
await expectToThrow(
powerofsoon.actions.auctlatest([ONE_HOUR]).send(sender),
ERROR_MISSING_REQUIRED_AUTHORITY_POWEROFSOON,
);
});
});
});