-
Notifications
You must be signed in to change notification settings - Fork 80
/
hodl_vault.ts
39 lines (31 loc) · 1.33 KB
/
hodl_vault.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
import { stringify } from '@bitauth/libauth';
import { Contract, SignatureTemplate, ElectrumNetworkProvider } from 'cashscript';
import { compileFile } from 'cashc';
import { URL } from 'url';
// Import keys and price oracle from common.ts
import {
alicePriv,
alicePub,
oracle,
oraclePub,
} from './common.js';
// Compile the HodlVault contract to an artifact object
const artifact = compileFile(new URL('hodl_vault.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
const parameters = [alicePub, oraclePub, 100000n, 30000n];
const contract = new Contract(artifact, parameters, { provider });
// Get contract balance & output address + balance
console.log('contract address:', contract.address);
console.log('contract balance:', await contract.getBalance());
// Produce new oracle message and signature
const oracleMessage = oracle.createMessage(100000n, 30000n);
const oracleSignature = oracle.signMessage(oracleMessage);
// Spend from the vault
const tx = await contract.functions
.spend(new SignatureTemplate(alicePriv), oracleSignature, oracleMessage)
.to(contract.address, 1000n)
.send();
console.log(stringify(tx));