Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Feb 25, 2024
1 parent 6833735 commit 3753e4c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 31 deletions.
10 changes: 5 additions & 5 deletions demo/client/contract/payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { print_banner } from '@scrow/test'

const funder = signers[0]

contract.on('payment', () => {
print_banner('new payment')
console.dir(account.payments, { depth : null })
console.log('\n')
})
// contract.on('payment', () => {
// print_banner('new payment')
// console.dir(account.payments, { depth : null })
// console.log('\n')
// })

await deposit.lock(contract.data, funder)
17 changes: 9 additions & 8 deletions src/client/class/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { EscrowSigner } from './signer.js'

import {
ContractData,
ContractDigest,
ContractStatus,
DraftData
} from '@/types/index.js'
import { update_contract } from '@/lib/contract.js'

interface EscrowContractConfig {
refresh_ival : number
Expand Down Expand Up @@ -122,9 +124,8 @@ export class EscrowContract extends EventEmitter <{
try {
if (this.is_stale || force) {
const res = await this._status()
if (res.status !== this.status) {
const data = await this._digest()
this._update(data)
if (res.updated) {
this._update(res.contract)
} else {
this._updated = now()
}
Expand All @@ -149,14 +150,14 @@ export class EscrowContract extends EventEmitter <{
const api = this.client.contract
const res = await api.status(this.cid)
if (!res.ok) throw new Error(res.error)
return res.data.contract
return res.data
}

_update (data : ContractData) {
const changed = (data.status !== this.status)
async _update (updated : ContractData | ContractDigest) {
const changed = (updated.status !== this.status)
try {

this._data = data
const data = this.data
this._data = await update_contract(data, updated)
this._updated = now()
if (changed) this.emit('status', data.status)
this.emit('update', this)
Expand Down
16 changes: 9 additions & 7 deletions src/client/class/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
ContractData,
DepositAccount,
DepositData,
DepositDigest,
DepositStatus,
TxOutput
} from '@/types/index.js'
import { update_deposit } from '@/lib/deposit.js'

interface EscrowDepositConfig {
refresh_ival : number
Expand Down Expand Up @@ -113,9 +115,8 @@ export class EscrowDeposit extends EventEmitter <{
try {
if (this.is_stale || force) {
const res = await this._status()
if (res.status !== this.status) {
const data = await this._digest()
this._update(data)
if (res.updated) {
this._update(res.deposit)
} else {
this._updated = now()
}
Expand All @@ -140,13 +141,14 @@ export class EscrowDeposit extends EventEmitter <{
const api = this.client.deposit
const res = await api.status(this.dpid)
if (!res.ok) throw new Error(res.error)
return res.data.deposit
return res.data
}

_update (data : DepositData) {
const changed = (data.status !== this.status)
async _update (updated : DepositData | DepositDigest) {
const changed = (updated.status !== this.status)
try {
this._data = data
const data = this.data
this._data = await update_deposit(data, updated)
this._updated = now()
if (changed) this.emit('status', data.status)
this.emit('update', this)
Expand Down
14 changes: 14 additions & 0 deletions src/lib/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import {
import {
ContractConfig,
ContractData,
ContractDigest,
ContractStatus,
MemberData,
PaymentEntry,
ProposalData,
SpendTemplate
} from '../types/index.js'

import * as schema from '@/schema/index.js'

const GET_INIT_CONTRACT = () => {
return {
activated : null,
Expand Down Expand Up @@ -86,6 +89,17 @@ export function create_contract (
})
}

/**
* Update a contract using a digest.
*/
export async function update_contract (
data : ContractData,
digest : ContractDigest
) {
const parser = schema.contract.data
return parser.parseAsync({ ...data, ...digest })
}

/**
* Compute the hash identifier
* for an escrow contract.
Expand Down
16 changes: 15 additions & 1 deletion src/lib/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ import {
Network,
AgentSession,
RegisterRequest,
CommitRequest
CommitRequest,
DepositDigest
} from '../types/index.js'

import * as schema from '@/schema/index.js'

/**
* Initialization object for deposit state.
*/
Expand Down Expand Up @@ -96,6 +99,17 @@ export function create_deposit (
return sort_record(deposit)
}

/**
* Update a contract using a digest.
*/
export async function update_deposit (
data : DepositData,
digest : DepositDigest
) {
const parser = schema.deposit.data
return parser.parseAsync({ ...data, ...digest })
}

/**
* Compute a context object for a deposit account.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ export function is_literal (value : unknown) : value is Literal {
return false
}

export function is_stamp (value : unknown) : value is number {
return (
typeof value === 'number' &&
value > 500_000_000 &&
value <= Number.MAX_SAFE_INTEGER
)
}

export function is_uint (
value : unknown,
max_val = Number.MAX_SAFE_INTEGER
Expand Down
1 change: 0 additions & 1 deletion src/types/api/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { DepositDigest } from '../deposit.js'
import { MemberData } from '../draft.js'
import { RecordStatus } from './index.js'
import { ProposalData } from '../proposal.js'
import { WitnessData } from '../program.js'
import { StateData } from '../vm.js'
Expand Down
10 changes: 6 additions & 4 deletions src/types/api/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ContractData } from '../contract.js'
import { CovenantData } from '../covenant.js'
import { RecordStatus } from './index.js'
import { TxOutput } from '../tx.js'

import {
DepositAccount,
DepositData,
DepositDigest,
DepositStatus
} from '../deposit.js'

export interface AccountRequest {
Expand Down Expand Up @@ -53,8 +51,12 @@ export interface DepositDigestResponse {
deposit : DepositDigest
}

export interface DepositStatusResponse {
deposit : RecordStatus<DepositStatus>
export type DepositStatusResponse = {
deposit : DepositDigest
updated : true
} | {
deposit : undefined
updated : false
}

export interface FundingDataResponse {
Expand Down
5 changes: 0 additions & 5 deletions src/types/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,3 @@ export interface ErrorResponse {
error : string
status : number
}

export interface RecordStatus<T> {
status : T
updated_at : number
}

0 comments on commit 3753e4c

Please sign in to comment.