-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-contract-call-to-constant.js
More file actions
75 lines (67 loc) · 2.31 KB
/
02-contract-call-to-constant.js
File metadata and controls
75 lines (67 loc) · 2.31 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
// SIP-039: Allow contract-call? to a constant
// Deploys two contracts: a target contract and a caller that uses a constant
// as the contract-call? target. Before epoch 3.4 this would fail at runtime
// with RuntimeCheckErrorKind::ContractCallExpectName.
const {
makeContractDeploy,
makeContractCall,
ClarityVersion,
PostConditionMode,
getAddressFromPrivateKey,
} = require("@stacks/transactions");
const { STACKS_TESTNET } = require("@stacks/network");
const { PRIVATE_KEY, NETWORK } = require("./config");
const { broadcast } = require("./broadcast");
// Step 1: Deploy the target contract
const targetCode = `
(define-public (ping)
(ok "pong")
)
`;
// Step 2: Deploy the caller contract that uses a constant for the target
const callerCode = `
(define-constant TARGET_CONTRACT .call-target-test)
(define-public (call-ping)
(contract-call? TARGET_CONTRACT ping)
)
`;
async function main() {
// Deploy target first
const targetTx = await makeContractDeploy({
codeBody: targetCode,
contractName: "call-target-test",
clarityVersion: ClarityVersion.Clarity5,
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Deny,
});
await broadcast(targetTx, "SIP-039: target contract for constant call");
// Deploy caller — needs target to exist, so run this script again after
// target is confirmed, or just wait
const callerTx = await makeContractDeploy({
codeBody: callerCode,
contractName: "call-constant-test",
clarityVersion: ClarityVersion.Clarity5,
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Deny,
});
const callerResult = await broadcast(callerTx, "SIP-039: contract-call? to constant");
if (!callerResult.ok) return;
// Step 3: Call the caller contract to exercise contract-call? to a constant
const senderAddress = getAddressFromPrivateKey(PRIVATE_KEY, STACKS_TESTNET);
const callTx = await makeContractCall({
contractAddress: senderAddress,
contractName: "call-constant-test",
functionName: "call-ping",
functionArgs: [],
senderKey: PRIVATE_KEY,
network: NETWORK,
fee: 50000,
postConditionMode: PostConditionMode.Allow,
});
await broadcast(callTx, "SIP-039: invoke call-ping (contract-call? to constant)");
}
main().catch(console.error);