Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Jan 25, 2024
1 parent d81ee65 commit 71a5277
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
11 changes: 11 additions & 0 deletions demo/00_demo_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ const faucets = {
testnet : 'https://bitcoinfaucet.uo1.net'
}

const poll_rates = {
mutiny : [ 10, 6 ],
regtest : [ 10, 6 ],
signet : [ 60, 30 ],
testnet : [ 60, 30 ]
}

export const network = process.argv.slice(2)[0] ?? 'signet'

export const config = configs[network as keyof typeof configs]
export const faucet = faucets[network as keyof typeof faucets]
export const poll = poll_rates[network as keyof typeof faucets]

export const members = [ 'alice', 'bob', 'carol' ]

export const sleep = (ms : number) => new Promise(res => setTimeout(res, ms))
3 changes: 0 additions & 3 deletions demo/04_roles_and_endorse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { print_banner } from '@scrow/test'

import { signers } from './02_create_signer.js'
import { roles, template } from './03_build_proposal.js'
import { get_proposal_id } from '@/lib/proposal.js'

const [ a_signer, b_signer, c_signer ] = signers

Expand All @@ -16,8 +15,6 @@ print_banner('completed proposal')
console.dir(proposal, { depth : null })

const signatures = signers.map(mbr => {
const prop_id = get_proposal_id(proposal)
console.log(`key ${mbr.pubkey} endorsing id ${prop_id}`)
return mbr.proposal.endorse(proposal)
})

Expand Down
17 changes: 8 additions & 9 deletions demo/07_deposit_funds.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { print_banner } from '@scrow/test'

import { faucet } from './00_demo_config.js'
import { faucet, poll, sleep } from './00_demo_config.js'

import { client } from './01_create_client.js'
import { signers } from './02_create_signer.js'
import { contract } from './05_create_contract.js'
Expand All @@ -10,32 +11,30 @@ const { address, agent_id } = account

const vin_fee = contract.feerate * 65
const amt_total = contract.total + vin_fee
const btc_total = amt_total / 100_000_000

print_banner('make a deposit')

console.log('copy this address :', address)
console.log('send this amount :', amt_total, 'sats')
console.log('get funds here :', faucet)
console.log('send this amount :', `${amt_total} sats || ${btc_total} btc`)
console.log('get funds here :', faucet, '\n')

const ival = 30,
retries = 6,
sleep = (ms : number) => new Promise(res => setTimeout(res, ms))
const [ ival, retries ] = poll

let curr = 1,
utxos = await client.oracle.get_address_utxos(address)

console.log('\n')

while (utxos.length === 0 && curr < retries) {
console.log(`[${curr}/${retries}] checking address in ${ival} seconds...`)
await sleep(ival * 1000)
utxos = await client.oracle.get_address_utxos(address)
console.log('utxos:', utxos)
curr += 1
}

if (utxos.length === 0) throw new Error('utxo not found')

console.log('\nutxo:', utxos[0])

// Request the member to sign
const signer = signers[0]
const utxo = utxos[0].txspend
Expand Down
10 changes: 5 additions & 5 deletions demo/08_check_contract.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { print_banner } from '@scrow/test'

import { poll, sleep } from './00_demo_config.js'
import { client } from './01_create_client.js'
import { cid } from './07_deposit_funds.js'

const ival = 10,
retries = 6,
sleep = (ms : number) => new Promise(res => setTimeout(res, ms))
const [ ival, retries ] = poll

let curr = 1,
res = await client.contract.read(cid)

console.log('\n')
print_banner('awaiting on-chain confirmation of funds')

console.log('depending on the network, this could take a while!\n')

while (res.ok && res.data.contract.activated === null && curr < retries) {
console.log(`[${curr}/${retries}] re-checking contract in ${ival} seconds...`)
Expand Down
20 changes: 19 additions & 1 deletion demo/09_settle_contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { print_banner } from '@scrow/test'
import { WitnessData } from '@scrow/core'

import { sleep } from './00_demo_config.js'
import { client } from './01_create_client.js'
import { signers } from './02_create_signer.js'
import { active_contract } from './08_check_contract.js'
Expand Down Expand Up @@ -30,5 +31,22 @@ const res = await client.contract.submit(contract.cid, witness)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)

const settled_contract = res.data.contract

print_banner('settled contract')
console.dir(res.data.contract, { depth : null })
console.dir(settled_contract, { depth : null })

if (!settled_contract.spent) {
throw new Error('failed to spend contract!')
}

print_banner('final transaction')

console.log('waiting a few seconds for tx to propagate the pool...\n')
await sleep(5000)

const txdata = await client.oracle.get_txdata(settled_contract.spent_txid)

console.dir(txdata, { depth : null })

print_banner('demo complete!')
4 changes: 2 additions & 2 deletions src/lib/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ export async function get_fee_target (
const feerate = quotes[index]
// If feerate does not exist, throw an error.
if (typeof feerate !== 'number') {
throw new Error('No quote available for target: ' + index)
throw new Error('No quote available for fee target: ' + index)
}
// Else, return feerate from oracle.
return feerate
}

export async function fetcher<T> (
export async function fetcher <T> (
input : URL | RequestInfo,
init ?: RequestInit,
fetcher = fetch
Expand Down

0 comments on commit 71a5277

Please sign in to comment.