Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Feb 23, 2024
1 parent 885cfe7 commit e02d76b
Show file tree
Hide file tree
Showing 24 changed files with 1,321 additions and 42 deletions.
2 changes: 1 addition & 1 deletion demo/api/contract/read.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Contract API Demo for endpoint:
* /api/contract/:cid/read
* /api/contract/:cid
*
* You can run this demo using the shell command:
* yarn load demo/api/contract/read
Expand Down
45 changes: 43 additions & 2 deletions demo/api/deposit/close.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,51 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid/close
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/close
*/

import { print_banner } from '@scrow/test'
import { sleep } from '@scrow/demo/util.js'
import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { depositor } from './request.js'
import { open_deposit } from './register.js'
import { signers } from '@scrow/demo/02_create_signer.js'
import { open_deposit } from '@scrow/demo/api/deposit/register.js'

// Unpack our polling config.
const [ ival, retries ] = config.poll
// Define the dpid for the deposit we are using.
const dpid = open_deposit.dpid

// Fetch the current contract data.
let res_status = await client.deposit.status(dpid),
tries = 1

print_banner('awaiting confirmation of deposit')
console.log('depending on the network, this could take a while!\n')

// While our response is ok, but the contract is not active (and we have tries):
while (
res_status.ok &&
res_status.data.deposit.status !== 'open' &&
tries < retries
) {
// Print our current status to console.
console.log(`[${tries}/${retries}] re-checking deposit in ${ival} seconds...`)
// Sleep for interval seconds.
await sleep(ival * 1000)
// Fetch the latest contract data.
res_status = await client.deposit.status(dpid)
// Increment our tries counter
tries += 1
}

// If the response failed, throw error.
if (!res_status.ok) throw new Error(res_status.error)

// Define our funder for the deposit.
const depositor = signers[0]
// Define a txfee for the close transaction.
const txfee = 1000
// Generate a lock request from the depositor.
Expand Down
72 changes: 72 additions & 0 deletions demo/api/deposit/commit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid/commit
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/commit
*/

import { print_banner } from '@scrow/test'
import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
import { signers } from '@scrow/demo/02_create_signer.js'
import { new_contract } from '@scrow/demo/05_create_contract.js'
import { new_account } from '@scrow/demo/06_request_account.js'

import {
fund_mutiny_address,
fund_regtest_address,
sleep
} from '@scrow/demo/util.js'

// Unpack account details.
const { address } = new_account
// Define how much sats we want to deposit
const amt_total = 20_000
// Also compute a total amount in bitcoin.
const btc_total = amt_total / 100_000_000

/** ========== [ Print Deposit Info ] ========== **/

switch (config.network) {
case 'mutiny':
fund_mutiny_address(address, amt_total)
break
case 'regtest':
fund_regtest_address(address, amt_total)
break
default:
print_banner('make a deposit')
console.log('copy this address :', address)
console.log('send this amount :', `${amt_total} sats || ${btc_total} btc`)
console.log('get funds here :', config.faucet, '\n')
}

await sleep(2000)

/** ========== [ Poll Deposit Status ] ========== **/

// Define our polling interval and retries.
const [ ival, retries ] = config.poll
// Poll for utxos from the account address.
const utxos = await client.oracle.poll_address(address, ival, retries, true)

print_banner('address utxos')
console.log('utxos:', utxos)

// Get the output data from the utxo.
const utxo = utxos[0].txspend
// Define our funder for the deposit.
const depositor = signers[0]
// Generate a commit request from the depositor.
const req = depositor.account.commit(new_account, new_contract, utxo)
// Deliver our commit request to the server.
const res = await client.deposit.commit(req)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack our data object.
const locked_deposit = res.data.deposit

print_banner('open deposit')
console.dir(locked_deposit, { depth: null })
console.log('\n')
8 changes: 8 additions & 0 deletions demo/api/deposit/digest.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid/digest
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/digest
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { locked_deposit } from '@scrow/demo/07_deposit_funds.js'
Expand Down
12 changes: 11 additions & 1 deletion demo/api/deposit/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/list/:pubkey
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/list
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { depositor } from '@scrow/demo/07_deposit_funds.js'
import { signers } from '@scrow/demo/02_create_signer.js'

// Define our funder for the deposit.
const depositor = signers[0]
// Generate a request token.
const req = depositor.request.deposit_list()
// Deliver the request and token.
Expand Down
18 changes: 14 additions & 4 deletions demo/api/deposit/lock.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid/lock
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/lock
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { signers } from '@scrow/demo/02_create_signer.js'
import { new_contract } from '@scrow/demo/05_create_contract.js'
import { depositor } from './request.js'
import { open_deposit } from './register.js'
import { open_deposit } from '@scrow/demo/api/deposit/register.js'

// Define our funder for the deposit.
const depositor = signers[0]
// Define the dpid for the deposit we are using.
const dpid = open_deposit.dpid
// Generate a lock request from the depositor.
const lock_req = depositor.account.lock(new_contract, open_deposit)
const req = depositor.account.lock(new_contract, open_deposit)
// Deliver the request and token.
const res = await client.deposit.lock(dpid, lock_req)
const res = await client.deposit.lock(dpid, req)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack our response data.
Expand Down
8 changes: 8 additions & 0 deletions demo/api/deposit/read.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/read
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { locked_deposit } from '@scrow/demo/07_deposit_funds.js'
Expand Down
37 changes: 15 additions & 22 deletions demo/api/deposit/register.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid/register
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/register
*/

import { print_banner } from '@scrow/test'
import { config } from '@scrow/demo/00_demo_config.js'
import { client } from '@scrow/demo/01_create_client.js'
Expand All @@ -10,7 +18,7 @@ import {
} from '@scrow/demo/util.js'

// Unpack account details.
const { address, deposit_pk, sequence, spend_xpub }= new_account
const { address, deposit_pk, sequence, spend_xpub } = new_account
// Define how much sats we want to deposit
const amt_total = 20_000
// Also compute a total amount in bitcoin.
Expand All @@ -36,35 +44,20 @@ await sleep(2000)

/** ========== [ Poll Deposit Status ] ========== **/

// Define our polling interval and retries.
const [ ival, retries ] = config.poll

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

// While there are no utxos (and we still have tries):
while (utxos.length === 0 && tries < retries) {
// Print current status to console.
console.log(`[${tries}/${retries}] checking address in ${ival} seconds...`)
// Sleep for interval number of secords.
await sleep(ival * 1000)
// Check again for utxos at address.
utxos = await client.oracle.get_address_utxos(address)
// Increment our tries counter
tries += 1
}

// If we still have no utxos, throw error.
if (utxos.length === 0) throw new Error('utxo not found')
// Poll for utxos from the account address.
const utxos = await client.oracle.poll_address(address, ival, retries, true)

print_banner('address utxos')
console.log('utxos:', utxos)

// Get the output data from the utxo.
const utxo = utxos[0].txspend
const utxo = utxos[0].txspend
// Create a registration request.
const reg_req = { deposit_pk, sequence, spend_xpub, utxo }
const req = { deposit_pk, sequence, spend_xpub, utxo }
// Deliver our registration request to the server.
const res = await client.deposit.register(reg_req)
const res = await client.deposit.register(req)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack our data object.
Expand Down
10 changes: 9 additions & 1 deletion demo/api/deposit/request.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/request
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/request
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { signers } from '@scrow/demo/02_create_signer.js'

// Define our funder for the deposit.
export const depositor = signers[0]
const depositor = signers[0]
// Define our deposit locktime.
const locktime = 60 * 60 // 1 hour locktime
// Get an account request from the funder device.
Expand Down
24 changes: 24 additions & 0 deletions demo/api/deposit/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Deposit API Demo for endpoint:
* /api/deposit/:dpid/status
*
* You can run this demo using the shell command:
* yarn load demo/api/deposit/status
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { locked_deposit } from '@scrow/demo/07_deposit_funds.js'

// Define the deposit id we will use.
const dpid = locked_deposit.dpid
// Request to read a deposit via dpid.
const res = await client.deposit.status(dpid)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack the data response
const deposit = res.data.deposit

print_banner('deposit status')
console.dir(deposit, { depth : null })
console.log('\n')
14 changes: 11 additions & 3 deletions demo/api/witness/read.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Witness API Demo for endpoint:
* /api/witness/:wid
*
* You can run this demo using the shell command:
* yarn load demo/api/witness/read
*/

import { print_banner } from '@scrow/test'
import { client } from '@scrow/demo/01_create_client.js'
import { witness } from '@scrow/demo/09_settle_contract.js'
Expand All @@ -9,8 +17,8 @@ const res = await client.witness.read(wid)
// Check the response is valid.
if (!res.ok) throw new Error(res.error)
// Unpack the data object.
const fetched_witness = res.data.witness
const statement = res.data.witness

print_banner('witness')
console.dir(fetched_witness, { depth : null })
print_banner('witness statement')
console.dir(statement, { depth : null })
console.log('\n')
Loading

0 comments on commit e02d76b

Please sign in to comment.