-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhodl_vault.ts
52 lines (44 loc) · 1.75 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
40
41
42
43
44
45
46
47
48
49
50
51
52
import { BITBOX } from "bitbox-sdk"
import { ECPair, HDNode } from "bitcoincashjs-lib"
import { Contract, Instance } from "cashscript"
import * as path from "path"
import { PriceOracle } from "./src/routes/v1/PriceOracle"
run()
export async function run(): Promise<void> {
// Initialise BITBOX
const network: string = "testnet"
const bitbox: BITBOX = new BITBOX({
restURL: "https://trest.bitcoin.com/v2/"
})
// Initialise HD node and owner's keypair
const rootSeed: Buffer = bitbox.Mnemonic.toSeed("")
const hdNode: HDNode = bitbox.HDNode.fromSeed(rootSeed, network)
const owner: ECPair = bitbox.HDNode.toKeyPair(bitbox.HDNode.derive(hdNode, 0))
// Initialise price oracle with a keypair
const oracle: PriceOracle = new PriceOracle(
bitbox.HDNode.toKeyPair(bitbox.HDNode.derive(hdNode, 1))
)
// Compile and instantiate HODL Vault
const HodlVault: Contract = Contract.fromCashFile(
path.join(__dirname, "./src/routes/v1/contracts/hodl_vault.cash"),
"testnet"
)
const instance: Instance = HodlVault.new(
bitbox.ECPair.toPublicKey(owner),
bitbox.ECPair.toPublicKey(oracle.keypair),
597000,
30000
)
// Get contract balance & output address + balance
const contractBalance: number = await instance.getBalance()
console.log("contract address:", instance.address)
console.log("contract balance:", contractBalance)
// Produce new oracle message and signature
// const oracleMessage: Buffer = oracle.createMessage(597000, 30000)
// const oracleSignature: Buffer = oracle.signMessage(oracleMessage)
// // Spend from the vault
// const tx: TxnDetailsResult = await instance.functions
// .spend(new Sig(owner, 0x01), oracleSignature, oracleMessage)
// .send(instance.address, 1000)
// console.log(tx)
}