-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess-smart-contract-job.test.ts
150 lines (134 loc) · 4.9 KB
/
process-smart-contract-job.test.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
import { bufferCV, cvToHex, tupleCV, uintCV } from '@stacks/transactions';
import { MockAgent, setGlobalDispatcher } from 'undici';
import { MIGRATIONS_DIR, PgStore } from '../../src/pg/pg-store';
import {
DbSipNumber,
DbSmartContractInsert,
DbToken,
DbTokenType,
TOKENS_COLUMNS,
} from '../../src/pg/types';
import { ProcessSmartContractJob } from '../../src/token-processor/queue/job/process-smart-contract-job';
import { ENV } from '../../src/env';
import { cycleMigrations } from '@hirosystems/api-toolkit';
import { insertAndEnqueueTestContract } from '../helpers';
describe('ProcessSmartContractJob', () => {
let db: PgStore;
beforeEach(async () => {
ENV.PGDATABASE = 'postgres';
db = await PgStore.connect({ skipMigrations: true });
await cycleMigrations(MIGRATIONS_DIR);
});
afterEach(async () => {
await db.close();
});
test('enqueues 1 token per FT contract', async () => {
const job = await insertAndEnqueueTestContract(db, 'ABCD.test-ft', DbSipNumber.sip010);
const processor = new ProcessSmartContractJob({
db,
job,
});
await processor.work();
const tokens = await db.sql<DbToken[]>`SELECT ${db.sql(TOKENS_COLUMNS)} FROM tokens`;
expect(tokens.count).toBe(1);
expect(tokens[0].type).toBe(DbTokenType.ft);
expect(tokens[0].smart_contract_id).toBe(1);
});
test('enqueues all tokens per NFT contract', async () => {
const agent = new MockAgent();
agent.disableNetConnect();
agent
.get(`http://${ENV.STACKS_NODE_RPC_HOST}:${ENV.STACKS_NODE_RPC_PORT}`)
.intercept({
path: '/v2/contracts/call-read/ABCD/test-nft/get-last-token-id',
method: 'POST',
})
.reply(200, {
okay: true,
result: cvToHex(uintCV(5)),
});
setGlobalDispatcher(agent);
const job = await insertAndEnqueueTestContract(db, 'ABCD.test-nft', DbSipNumber.sip009);
const processor = new ProcessSmartContractJob({
db,
job,
});
await processor.work();
const tokens = await db.sql<DbToken[]>`SELECT ${db.sql(TOKENS_COLUMNS)} FROM tokens`;
expect(tokens.count).toBe(5);
expect(tokens[0].type).toBe(DbTokenType.nft);
expect(tokens[0].smart_contract_id).toBe(1);
});
test('ignores NFT contract that exceeds max token count', async () => {
const agent = new MockAgent();
agent.disableNetConnect();
agent
.get(`http://${ENV.STACKS_NODE_RPC_HOST}:${ENV.STACKS_NODE_RPC_PORT}`)
.intercept({
path: '/v2/contracts/call-read/ABCD/test-nft/get-last-token-id',
method: 'POST',
})
.reply(200, {
okay: true,
result: cvToHex(uintCV(10000000000)),
});
setGlobalDispatcher(agent);
const job = await insertAndEnqueueTestContract(db, 'ABCD.test-nft', DbSipNumber.sip009);
const processor = new ProcessSmartContractJob({
db,
job,
});
await processor.work();
const tokens = await db.sql<DbToken[]>`SELECT ${db.sql(TOKENS_COLUMNS)} FROM tokens`;
expect(tokens.count).toBe(0);
});
// test('enqueues minted tokens for SFT contract', async () => {
// const address = 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9';
// const contractId = `${address}.key-alex-autoalex-v1`;
// const values: DbSmartContractInsert = {
// principal: contractId,
// sip: DbSipNumber.sip013,
// abi: '"some"',
// tx_id: '0x123456',
// block_height: 1,
// };
// const job = await db.chainhook.insertAndEnqueueSmartContract({ values });
// // Create mint events.
// const event1: BlockchainDbContractLog = {
// contract_identifier: contractId,
// sender_address: address,
// value: cvToHex(
// tupleCV({
// type: bufferCV(Buffer.from('sft_mint')),
// recipient: bufferCV(Buffer.from(address)),
// 'token-id': uintCV(3),
// amount: uintCV(1000),
// })
// ),
// };
// const event2: BlockchainDbContractLog = {
// contract_identifier: contractId,
// sender_address: address,
// value: cvToHex(
// tupleCV({
// type: bufferCV(Buffer.from('sft_mint')),
// recipient: bufferCV(Buffer.from(address)),
// 'token-id': uintCV(7),
// amount: uintCV(2000),
// })
// ),
// };
// const apiDb = new MockPgBlockchainApiStore();
// apiDb.contractLogsByContract = [event1, event2];
// const processor = new ProcessSmartContractJob({ db, job, apiDb });
// await processor.work();
// const tokens = await db.sql<DbToken[]>`SELECT ${db.sql(TOKENS_COLUMNS)} FROM tokens`;
// expect(tokens.count).toBe(2);
// expect(tokens[0].type).toBe(DbTokenType.sft);
// expect(tokens[0].smart_contract_id).toBe(1);
// expect(tokens[0].token_number).toBe('3');
// expect(tokens[1].type).toBe(DbTokenType.sft);
// expect(tokens[1].smart_contract_id).toBe(1);
// expect(tokens[1].token_number).toBe('7');
// });
});