|
1 | 1 | import { stringify } from '@bitauth/libauth';
|
2 |
| -import { BITBOX } from 'bitbox-sdk'; |
3 | 2 | import { Contract, SignatureTemplate, ElectrumNetworkProvider } from 'cashscript';
|
4 | 3 | import { compileFile } from 'cashc';
|
5 |
| -import path from 'path'; |
6 |
| -import { PriceOracle } from './PriceOracle.js'; |
7 |
| - |
8 |
| -run(); |
9 |
| -async function run(): Promise<void> { |
10 |
| - // Initialise BITBOX |
11 |
| - const bitbox = new BITBOX(); |
12 |
| - |
13 |
| - // Initialise HD node and owner's keypair |
14 |
| - const rootSeed = bitbox.Mnemonic.toSeed('CashScript'); |
15 |
| - const hdNode = bitbox.HDNode.fromSeed(rootSeed); |
16 |
| - |
17 |
| - const owner = bitbox.HDNode.toKeyPair(bitbox.HDNode.derive(hdNode, 0)); |
18 |
| - const ownerPk = bitbox.ECPair.toPublicKey(owner); |
19 |
| - |
20 |
| - // Initialise price oracle with a keypair |
21 |
| - const oracleKeypair = bitbox.HDNode.toKeyPair(bitbox.HDNode.derive(hdNode, 1)); |
22 |
| - const oraclePk = bitbox.ECPair.toPublicKey(oracleKeypair); |
23 |
| - const oracle = new PriceOracle(oracleKeypair); |
24 |
| - |
25 |
| - // Compile the HodlVault contract to an artifact object |
26 |
| - const artifact = compileFile(path.join(__dirname, 'hodl_vault.cash')); |
27 |
| - |
28 |
| - // Initialise a network provider for network operations on TESTNET3 |
29 |
| - const provider = new ElectrumNetworkProvider('testnet3'); |
30 |
| - |
31 |
| - // Instantiate a new contract using the compiled artifact and network provider |
32 |
| - // AND providing the constructor parameters |
33 |
| - const parameters = [ownerPk, oraclePk, 597000, 30000]; |
34 |
| - const contract = new Contract(artifact, parameters, provider); |
35 |
| - |
36 |
| - // Get contract balance & output address + balance |
37 |
| - console.log('contract address:', contract.address); |
38 |
| - console.log('contract balance:', await contract.getBalance()); |
39 |
| - |
40 |
| - // Produce new oracle message and signature |
41 |
| - const oracleMessage = oracle.createMessage(597000, 30000); |
42 |
| - const oracleSignature = oracle.signMessage(oracleMessage); |
43 |
| - |
44 |
| - // Spend from the vault |
45 |
| - const tx = await contract.functions |
46 |
| - .spend(new SignatureTemplate(owner), oracleSignature, oracleMessage) |
47 |
| - .to(contract.address, 1000) |
48 |
| - .send(); |
49 |
| - |
50 |
| - console.log(stringify(tx)); |
51 |
| -} |
| 4 | +import { URL } from 'url'; |
| 5 | + |
| 6 | +// Import keys and price oracle from common.ts |
| 7 | +import { |
| 8 | + alicePriv, |
| 9 | + alicePub, |
| 10 | + oracle, |
| 11 | + oraclePub, |
| 12 | +} from './common.js'; |
| 13 | + |
| 14 | +// Compile the HodlVault contract to an artifact object |
| 15 | +const artifact = compileFile(new URL('hodl_vault.cash', import.meta.url)); |
| 16 | + |
| 17 | +// Initialise a network provider for network operations on TESTNET4 |
| 18 | +const provider = new ElectrumNetworkProvider('testnet4'); |
| 19 | + |
| 20 | +// Instantiate a new contract using the compiled artifact and network provider |
| 21 | +// AND providing the constructor parameters |
| 22 | +const parameters = [alicePub, oraclePub, 100000n, 30000n]; |
| 23 | +const contract = new Contract(artifact, parameters, provider); |
| 24 | + |
| 25 | +// Get contract balance & output address + balance |
| 26 | +console.log('contract address:', contract.address); |
| 27 | +console.log('contract balance:', await contract.getBalance()); |
| 28 | + |
| 29 | +// Produce new oracle message and signature |
| 30 | +const oracleMessage = oracle.createMessage(100000n, 30000n); |
| 31 | +const oracleSignature = oracle.signMessage(oracleMessage); |
| 32 | + |
| 33 | +// Spend from the vault |
| 34 | +const tx = await contract.functions |
| 35 | + .spend(new SignatureTemplate(alicePriv), oracleSignature, oracleMessage) |
| 36 | + .to(contract.address, 1000n) |
| 37 | + .send(); |
| 38 | + |
| 39 | +console.log(stringify(tx)); |
0 commit comments