-
Notifications
You must be signed in to change notification settings - Fork 80
/
mecenas.ts
35 lines (28 loc) · 1.42 KB
/
mecenas.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
import { stringify } from '@bitauth/libauth';
import { Contract, ElectrumNetworkProvider } from 'cashscript';
import { compileFile } from 'cashc';
import { URL } from 'url';
// Import Alice and Bob's pkh and Alice's address from common.ts
import { aliceAddress, alicePkh, bobPkh } from './common.js';
// Compile the Mecenas contract to an artifact object
const artifact = compileFile(new URL('mecenas.cash', import.meta.url));
// Initialise a network provider for network operations on CHIPNET
const provider = new ElectrumNetworkProvider('chipnet');
// Instantiate a new contract using the compiled artifact and network provider
// AND providing the constructor parameters:
// (recipient: alicePkh, funder: bobPkh, pledge: 10000)
const contract = new Contract(artifact, [alicePkh, bobPkh, 10000n], { provider });
// Get contract balance & output address + balance
console.log('contract address:', contract.address);
console.log('contract balance:', await contract.getBalance());
console.log('contract opcount:', contract.opcount);
console.log('contract bytesize:', contract.bytesize);
// Call the transfer function with any signature
// Will send one pledge amount to alice, and send change back to the contract
// Manually set fee to 1000 because this is hardcoded in the contract
const tx = await contract.functions
.receive()
.to(aliceAddress, 10000n)
.withHardcodedFee(1000n)
.send();
console.log('transaction details:', stringify(tx));