-
Notifications
You must be signed in to change notification settings - Fork 80
/
transfer_with_timeout.ts
45 lines (36 loc) · 1.63 KB
/
transfer_with_timeout.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
import { stringify } from '@bitauth/libauth';
import { compileFile } from 'cashc';
import { Contract, ElectrumNetworkProvider, SignatureTemplate } from 'cashscript';
import { URL } from 'url';
// Import Bob and Alice's keys from common.ts
import {
alicePriv,
alicePub,
bobPriv,
bobPub,
} from './common.js';
// Compile the TransferWithTimeout contract
const artifact = compileFile(new URL('transfer_with_timeout.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:
// { sender: alicePk, recipient: bobPk, timeout: 1000000 } - timeout is a past block
const contract = new Contract(artifact, [alicePub, bobPub, 100000n], { provider });
// Get contract balance & output address + balance
console.log('contract address:', contract.address);
console.log('contract balance:', await contract.getBalance());
// Call the transfer function with bob's signature
// Allows bob to claim the money that alice sent him
const transferTx = await contract.functions
.transfer(new SignatureTemplate(bobPriv))
.to(contract.address, 10000n)
.send();
console.log('transfer transaction details:', stringify(transferTx));
// Call the timeout function with alice's signature
// Allows alice to reclaim the money she sent as the timeout is in the past
const timeoutTx = await contract.functions
.timeout(new SignatureTemplate(alicePriv))
.to(contract.address, 10000n)
.send();
console.log('timeout transaction details:', stringify(timeoutTx));