Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
cmd committed Oct 16, 2023
1 parent 32813ac commit 96c89e9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 91 deletions.
77 changes: 17 additions & 60 deletions src/lib/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@ import { DEFAULT_DEADLINE } from '../config.js'
import { Signer } from '../signer.js'
import { create_settlment } from './spend.js'
import { now } from './util.js'
import { init_vm } from '../vm/main.js'

import {
get_path_names,
get_path_vouts,
get_pay_total,
} from './proposal.js'

import {
eval_stack,
init_vm,
start_vm
} from '../vm/main.js'

import {
AgentSession,
ContractConfig,
Expand All @@ -24,33 +19,35 @@ import {
SpendOutput
} from '../types/index.js'

import * as assert from '../assert.js'

export function create_contract (
cid : string,
proposal : ProposalData,
session : AgentSession,
options : Partial<ContractConfig> = {}
cid : string,
proposal : ProposalData,
session : AgentSession,
options : Partial<ContractConfig> = {}
) : ContractData {
const { fees = [], published = now() } = options
const {
fees = [],
moderator = null,
published = now()
} = options

return {
cid,
fees,
moderator,
published,
session,
activated : null,
balance : 0,
deadline : get_deadline(proposal, published),
expires : null,
funds : [],
expires_at : null,
outputs : get_spend_outputs(proposal, fees),
state : null,
status : 'published',
terms : proposal,
total : proposal.value + get_pay_total(fees),
tx : null,
updated_at : published,
witness : []
}
}

Expand All @@ -66,53 +63,20 @@ export function get_deadline (
}
}

export function update_contract (
contract : ContractData,
timestamp = now()
) {
const { deadline, expires, state, status, terms, witness } = contract
if (status === 'published') {
if (is_funded(contract)) {
activate_contract(contract, timestamp)
} else if (timestamp >= deadline) {
contract.status = 'canceled'
}
} else if (status === 'active') {
assert.ok(state !== null)
assert.ok(expires !== null)
const vm = start_vm(state, terms)
const ret = eval_stack(vm, witness)
if (ret.status === 'closed') {
assert.ok(ret.result !== null)
contract.status = 'closed'
} else if (timestamp >= expires) {
contract.status = 'expired'
}
}
}

export function activate_contract (
contract : ContractData,
published : number = now()
activated : number = now()
) {
const { cid, terms } = contract
return {
...contract,
effective : published,
expires : published + terms.expires,
state : init_vm(cid, terms, published),
activated,
expires : activated + terms.expires,
state : init_vm(cid, terms, activated),
status : 'active'
}
}

export function cancel_contract (
contract : ContractData,
signer : Signer
) {
console.log(signer)
return { ...contract, status : 'canceled' }
}

export function close_contract (
agent : Signer,
contract : ContractData,
Expand All @@ -121,13 +85,6 @@ export function close_contract (
return create_settlment(agent, contract, pathname)
}

export function is_funded (contract : ContractData) {
const { funds, total } = contract
const confirmed = funds.filter(e => e.confirmed).map(x => x.txinput)
const funding = confirmed.reduce((p, n) => p + Number(n.prevout.value), 0)
return funding >= total
}

export function get_spend_outputs (
prop : ProposalData,
fees : Payment[]
Expand Down
9 changes: 4 additions & 5 deletions src/schema/contract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { z } from 'zod'
import { deposit } from './deposit.js'
import { proposal } from './proposal.js'
import { vout } from './tx.js'

Expand Down Expand Up @@ -51,17 +50,17 @@ const data = z.object({
status,
tx,
activated : stamp.nullable(),
balance : num,
cid : hash,
deadline : stamp,
expires : stamp.nullable(),
expires_at : stamp.nullable(),
fees : base.payment.array(),
funds : deposit.data.array(),
outputs : output.array(),
moderator : hash.nullable(),
published : stamp,
terms : proposal.data,
total : num,
updated_at : stamp,
witness : witness.array(),
updated_at : stamp
})

const contract = { action, commit, data, output, session, state, status, tx, witness }
Expand Down
28 changes: 5 additions & 23 deletions src/types/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { TxOutput } from '@scrow/tapscript'
import { Literal } from './base.js'
import { Deposit } from './deposit.js'
import { AgentSession } from './session.js'
import { ContractState } from './vm.js'

Expand All @@ -18,16 +16,18 @@ export type SpendOutput = [

export interface ContractConfig {
fees : Payment[]
moderator : string
published : number
}

export interface ContractData {
activated : null | number
balance : number
cid : string
deadline : number
expires : null | number
expires_at : null | number
fees : Payment[]
funds : Deposit[]
moderator : string | null
outputs : SpendOutput[]
published : number
session : AgentSession
Expand All @@ -36,8 +36,7 @@ export interface ContractData {
terms : ProposalData
total : number
tx : null | TxStatus
updated_at : number,
witness : WitnessEntry[]
updated_at : number
}

export interface TxStatus {
Expand All @@ -46,20 +45,3 @@ export interface TxStatus {
txid : string
updated_at : number
}

export type WitnessEntry = [
stamp : number,
action : string,
path : string,
prog_id : string,
...args : Literal[]
]

export interface WitnessData {
action : string
args : Literal[]
path : string
prog_id : string
stamp : number
wid : string
}
19 changes: 19 additions & 0 deletions src/types/vm.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Literal } from './base.js'

export type PathMap = Map<string, PathState>
export type ProgMap = Map<string, ProgState>
export type StoreMap = Map<string, StoreState>
Expand Down Expand Up @@ -84,3 +86,20 @@ export type TaskEntry = [
action : string,
paths : string
]

export type WitnessEntry = [
stamp : number,
action : string,
path : string,
prog_id : string,
...args : Literal[]
]

export interface WitnessData {
action : string
args : Literal[]
path : string
prog_id : string
stamp : number
wid : string
}
4 changes: 1 addition & 3 deletions test/scratch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ funds.forEach(f => {
console.log(banner('funds'))
console.dir(funds, { depth : null })

contract.funds = funds

/* ------------------ [ Activation ] ------------------ */

const { state, terms } = activate_contract(contract)
Expand Down Expand Up @@ -112,7 +110,7 @@ console.dir(parse_vm(new_state), { depth : null })
const { result } = new_state

if (result !== null) {
const txdata = close_contract(agent.signer, contract, result, )
const txdata = close_contract(agent.signer, contract, result)

console.log(banner('closing tx'))
console.dir(txdata, { depth : null })
Expand Down

0 comments on commit 96c89e9

Please sign in to comment.