Skip to content

Commit e75e3b8

Browse files
committed
fix: print events for important functions
1 parent 75001b3 commit e75e3b8

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

contrib/core-contract-tests/tests/sip-031/sip-031.test.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { project, accounts } from '../clarigen-types'; // where your [types.output] was specified
2-
import { CoreNodeEventType, projectFactory } from '@clarigen/core';
2+
import { CoreNodeEventType, cvToValue, projectFactory } from '@clarigen/core';
33
import { filterEvents, rov, txErr, txOk } from '@clarigen/test';
44
import { test, expect } from 'vitest';
55

@@ -85,8 +85,10 @@ test('updated recipient can claim', () => {
8585
const receipt = txOk(contract.claim(), accounts.wallet_1.address);
8686
expect(receipt.value).toBe(constants.INITIAL_MINT_IMMEDIATE_AMOUNT);
8787

88-
expect(receipt.events.length).toBe(1);
89-
const [event] = filterEvents(receipt.events, CoreNodeEventType.StxTransferEvent);
88+
expect(receipt.events.length).toBe(2);
89+
const stxTransferEvents = filterEvents(receipt.events, CoreNodeEventType.StxTransferEvent);
90+
expect(stxTransferEvents.length).toBe(1);
91+
const [event] = stxTransferEvents;
9092
expect(event.data.amount).toBe(`${constants.INITIAL_MINT_IMMEDIATE_AMOUNT}`);
9193
expect(event.data.recipient).toBe(accounts.wallet_1.address);
9294
expect(event.data.sender).toBe(contract.identifier);
@@ -287,4 +289,32 @@ test('calculating claimable amount at invalid block height returns 0', () => {
287289
mintInitial();
288290
const deployBlockHeight = rov(contract.getDeployBlockHeight());
289291
expect(rov(contract.calcClaimableAmount(deployBlockHeight - 1n))).toBe(0n);
290-
});
292+
});
293+
294+
test('print events are emitted when updating recipient', () => {
295+
const receipt = txOk(contract.updateRecipient(accounts.wallet_1.address), accounts.deployer.address);
296+
expect(receipt.events.length).toBe(1);
297+
const [event] = filterEvents(receipt.events, CoreNodeEventType.ContractEvent);
298+
const printData = cvToValue<{
299+
topic: string;
300+
oldRecipient: string;
301+
newRecipient: string;
302+
}>(event.data.value);
303+
expect(printData.topic).toBe('update-recipient');
304+
expect(printData.oldRecipient).toBe(accounts.deployer.address);
305+
expect(printData.newRecipient).toBe(accounts.wallet_1.address);
306+
});
307+
308+
test('print events are emitted when claiming', () => {
309+
mintInitial();
310+
const receipt = txOk(contract.claim(), accounts.deployer.address);
311+
const [event] = filterEvents(receipt.events, CoreNodeEventType.ContractEvent);
312+
const printData = cvToValue<{
313+
topic: string;
314+
claimable: string;
315+
recipient: string;
316+
}>(event.data.value);
317+
expect(printData.topic).toBe('claim');
318+
expect(printData.claimable).toBe(constants.INITIAL_MINT_IMMEDIATE_AMOUNT);
319+
expect(printData.recipient).toBe(accounts.deployer.address);
320+
});

stackslib/src/chainstate/stacks/boot/sip-031.clar

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,19 @@
3535
;;
3636
;; Returns `true` if the recipient was updated.
3737
(define-public (update-recipient (new-recipient principal)) (begin
38-
(try! (validate-caller))
39-
(var-set recipient new-recipient)
40-
(ok true)
38+
(let
39+
(
40+
(old-recipient (var-get recipient))
41+
)
42+
(try! (validate-caller))
43+
(var-set recipient new-recipient)
44+
(print {
45+
topic: "update-recipient",
46+
old-recipient: old-recipient,
47+
new-recipient: new-recipient,
48+
})
49+
(ok true)
50+
)
4151
))
4252

4353
;; Transfer all currently withdrawable STX (vested + extra) to `recipient`.
@@ -51,6 +61,11 @@
5161
(asserts! (> claimable u0) (err ERR_NOTHING_TO_CLAIM))
5262

5363
(try! (as-contract (stx-transfer? claimable tx-sender (var-get recipient))))
64+
(print {
65+
topic: "claim",
66+
claimable: claimable,
67+
recipient: (var-get recipient),
68+
})
5469
(ok claimable)
5570
)
5671
)

0 commit comments

Comments
 (0)