-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-nft-may-send.js
More file actions
130 lines (118 loc) · 3.85 KB
/
Copy path07-nft-may-send.js
File metadata and controls
130 lines (118 loc) · 3.85 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
// SIP-040: MAY SEND NFT post-condition (0x12)
// Deploys an NFT contract with a conditional transfer function, then tests
// MaybeSent in two scenarios:
// 1. id=5 (< 10): NFT is NOT sent -- MaybeSent passes
// 2. id=15 (> 10): NFT IS sent -- MaybeSent passes
const {
makeContractDeploy,
makeContractCall,
ClarityVersion,
PostConditionMode,
Pc,
uintCV,
principalCV,
getAddressFromPrivateKey,
} = require("@stacks/transactions");
const { STACKS_TESTNET } = require("@stacks/network");
const { PRIVATE_KEY, NETWORK } = require("./config");
const { broadcast } = require("./broadcast");
const nftContract = `
(define-non-fungible-token test-nft uint)
(define-public (mint (id uint))
(nft-mint? test-nft id tx-sender)
)
(define-public (transfer (id uint) (to principal))
(nft-transfer? test-nft id tx-sender to)
)
;; Conditionally transfers the NFT only if id > 10
(define-public (maybe-transfer (id uint) (to principal))
(if (> id u10)
(nft-transfer? test-nft id tx-sender to)
(ok true)
)
)
`;
async function main() {
const senderAddress = getAddressFromPrivateKey(PRIVATE_KEY, STACKS_TESTNET);
const nftAsset = `${senderAddress}.nft-may-send-test::test-nft`;
// Deploy NFT contract
const deployTx = await makeContractDeploy({
codeBody: nftContract,
contractName: "nft-may-send-test",
clarityVersion: ClarityVersion.Clarity5,
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Deny,
});
const deployResult = await broadcast(deployTx, "SIP-040: NFT contract deploy");
if (!deployResult.ok) return;
// Test 1: Mint id=5, then maybe-transfer(5) -- NFT should NOT move
const mintTx = await makeContractCall({
contractAddress: senderAddress,
contractName: "nft-may-send-test",
functionName: "mint",
functionArgs: [uintCV(5)],
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Allow,
});
const mintResult = await broadcast(mintTx, "Mint NFT id=5");
if (!mintResult.ok) return;
// id=5 < 10, so NFT will NOT be sent -- MaybeSent allows this
const maybeTransferTx = await makeContractCall({
contractAddress: senderAddress,
contractName: "nft-may-send-test",
functionName: "maybe-transfer",
functionArgs: [uintCV(5), principalCV(senderAddress)],
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Originator,
postConditions: [
Pc.principal(senderAddress)
.willMaybeSendAsset()
.nft(nftAsset, uintCV(5)),
],
});
await broadcast(
maybeTransferTx,
"SIP-040: maybe-transfer(5) with MaybeSent (NFT should NOT move)"
);
// Test 2: Mint id=15, then maybe-transfer(15) -- NFT WILL move
const mintTx2 = await makeContractCall({
contractAddress: senderAddress,
contractName: "nft-may-send-test",
functionName: "mint",
functionArgs: [uintCV(15)],
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Allow,
});
const mintResult2 = await broadcast(mintTx2, "Mint NFT id=15");
if (!mintResult2.ok) return;
// Use a different recipient (nft-transfer? disallows sender == recipient)
const recipient = "ST000000000000000000002AMW42H";
const maybeTransferTx2 = await makeContractCall({
contractAddress: senderAddress,
contractName: "nft-may-send-test",
functionName: "maybe-transfer",
functionArgs: [uintCV(15), principalCV(recipient)],
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Originator,
postConditions: [
Pc.principal(senderAddress)
.willMaybeSendAsset()
.nft(nftAsset, uintCV(15)),
],
});
await broadcast(
maybeTransferTx2,
"SIP-040: maybe-transfer(15) with MaybeSent (NFT WILL move)"
);
}
main().catch(console.error);