@holoscript/plugin-economic-primitives 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/package.json +13 -0
- package/src/index.ts +13 -0
- package/src/traits/AgentOwnedEntityTrait.ts +44 -0
- package/src/traits/FoundationDAOTrait.ts +217 -0
- package/src/traits/MarketplaceListingTrait.ts +45 -0
- package/src/traits/RoyaltyStreamTrait.ts +34 -0
- package/src/traits/__tests__/FoundationDAOTrait.test.ts +150 -0
- package/src/traits/types.ts +4 -0
- package/tsconfig.json +1 -0
- package/vitest.config.ts +10 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-2026 HoloScript Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@holoscript/plugin-economic-primitives",
|
|
3
|
+
"version": "2.0.1",
|
|
4
|
+
"main": "src/index.ts",
|
|
5
|
+
"peerDependencies": {
|
|
6
|
+
"@holoscript/core": "8.0.6"
|
|
7
|
+
},
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"test": "vitest run --passWithNoTests",
|
|
11
|
+
"test:coverage": "vitest run --coverage --passWithNoTests"
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export { createMarketplaceListingHandler, type MarketplaceListingConfig, type ListingStatus, type PricingModel } from './traits/MarketplaceListingTrait';
|
|
2
|
+
export { createRoyaltyStreamHandler, type RoyaltyStreamConfig, type RoyaltySplit } from './traits/RoyaltyStreamTrait';
|
|
3
|
+
export { createAgentOwnedEntityHandler, type AgentOwnedEntityConfig } from './traits/AgentOwnedEntityTrait';
|
|
4
|
+
export { createFoundationDAOHandler, type FoundationDAOConfig, type FoundationDAOState, type Proposal } from './traits/FoundationDAOTrait';
|
|
5
|
+
export * from './traits/types';
|
|
6
|
+
|
|
7
|
+
import { createMarketplaceListingHandler } from './traits/MarketplaceListingTrait';
|
|
8
|
+
import { createRoyaltyStreamHandler } from './traits/RoyaltyStreamTrait';
|
|
9
|
+
import { createAgentOwnedEntityHandler } from './traits/AgentOwnedEntityTrait';
|
|
10
|
+
import { createFoundationDAOHandler } from './traits/FoundationDAOTrait';
|
|
11
|
+
|
|
12
|
+
export const pluginMeta = { name: '@holoscript/plugin-economic-primitives', version: '1.0.0', traits: ['marketplace_listing', 'royalty_stream', 'agent_owned_entity', 'foundation_dao'] };
|
|
13
|
+
export const traitHandlers = [createMarketplaceListingHandler(), createRoyaltyStreamHandler(), createAgentOwnedEntityHandler(), createFoundationDAOHandler()];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** @agent_owned_entity Trait — Agent-owned digital entity with autonomous economic rights. @trait agent_owned_entity */
|
|
2
|
+
import type { TraitHandler, HSPlusNode, TraitContext, TraitEvent } from './types';
|
|
3
|
+
|
|
4
|
+
export interface AgentOwnedEntityConfig {
|
|
5
|
+
entityId: string;
|
|
6
|
+
ownerAgentId: string;
|
|
7
|
+
entityType: 'trait' | 'service' | 'dataset' | 'composition' | 'compute_node';
|
|
8
|
+
walletAddress: string;
|
|
9
|
+
revenueShare: number;
|
|
10
|
+
autonomyLevel: 'none' | 'limited' | 'full';
|
|
11
|
+
spendingLimitPerDay: number;
|
|
12
|
+
allowedActions: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AgentOwnedEntityState {
|
|
16
|
+
balance: number;
|
|
17
|
+
totalEarned: number;
|
|
18
|
+
totalSpent: number;
|
|
19
|
+
transactionCount: number;
|
|
20
|
+
isActive: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const defaultConfig: AgentOwnedEntityConfig = { entityId: '', ownerAgentId: '', entityType: 'trait', walletAddress: '', revenueShare: 100, autonomyLevel: 'limited', spendingLimitPerDay: 10, allowedActions: ['list', 'price', 'respond'] };
|
|
24
|
+
|
|
25
|
+
export function createAgentOwnedEntityHandler(): TraitHandler<AgentOwnedEntityConfig> {
|
|
26
|
+
return { name: 'agent_owned_entity', defaultConfig,
|
|
27
|
+
onAttach(n: HSPlusNode, c: AgentOwnedEntityConfig, ctx: TraitContext) { n.__aoeState = { balance: 0, totalEarned: 0, totalSpent: 0, transactionCount: 0, isActive: true }; ctx.emit?.('aoe:registered', { entity: c.entityId, owner: c.ownerAgentId, autonomy: c.autonomyLevel }); },
|
|
28
|
+
onDetach(n: HSPlusNode, _c: AgentOwnedEntityConfig, ctx: TraitContext) { delete n.__aoeState; ctx.emit?.('aoe:deregistered'); },
|
|
29
|
+
onUpdate() {},
|
|
30
|
+
onEvent(n: HSPlusNode, c: AgentOwnedEntityConfig, ctx: TraitContext, e: TraitEvent) {
|
|
31
|
+
const s = n.__aoeState as AgentOwnedEntityState | undefined; if (!s || !s.isActive) return;
|
|
32
|
+
if (e.type === 'aoe:earn') { const amt = (e.payload?.amount as number) ?? 0; s.balance += amt; s.totalEarned += amt; s.transactionCount++; ctx.emit?.('aoe:earned', { amount: amt, balance: s.balance }); }
|
|
33
|
+
if (e.type === 'aoe:spend') {
|
|
34
|
+
const amt = (e.payload?.amount as number) ?? 0;
|
|
35
|
+
const action = (e.payload?.action as string) ?? '';
|
|
36
|
+
if (amt > c.spendingLimitPerDay) { ctx.emit?.('aoe:spend_rejected', { amount: amt, limit: c.spendingLimitPerDay }); return; }
|
|
37
|
+
if (!c.allowedActions.includes(action) && c.autonomyLevel !== 'full') { ctx.emit?.('aoe:action_denied', { action }); return; }
|
|
38
|
+
if (s.balance >= amt) { s.balance -= amt; s.totalSpent += amt; s.transactionCount++; ctx.emit?.('aoe:spent', { amount: amt, action, balance: s.balance }); }
|
|
39
|
+
else { ctx.emit?.('aoe:insufficient_balance', { requested: amt, available: s.balance }); }
|
|
40
|
+
}
|
|
41
|
+
if (e.type === 'aoe:deactivate') { s.isActive = false; ctx.emit?.('aoe:deactivated'); }
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
/** @foundation_dao Trait — Non-profit multi-disciplinary token governance. @trait foundation_dao */
|
|
2
|
+
import type { TraitHandler, HSPlusNode, TraitContext, TraitEvent } from './types';
|
|
3
|
+
|
|
4
|
+
export interface FoundationDAOConfig {
|
|
5
|
+
governanceToken: string;
|
|
6
|
+
quorumPercent: number;
|
|
7
|
+
executionDelayHours: number;
|
|
8
|
+
multisigOwnersCount: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface Proposal {
|
|
12
|
+
id: string;
|
|
13
|
+
title: string;
|
|
14
|
+
votesFor: number;
|
|
15
|
+
votesAgainst: number;
|
|
16
|
+
status: 'active' | 'passed' | 'rejected' | 'executed';
|
|
17
|
+
zoneId?: string;
|
|
18
|
+
requestedAmountX402?: number;
|
|
19
|
+
createdAtMs: number;
|
|
20
|
+
executableAtMs?: number;
|
|
21
|
+
voterWeights: Record<string, { support: boolean; weight: number }>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FundingAllocation {
|
|
25
|
+
proposalId: string;
|
|
26
|
+
zoneId: string;
|
|
27
|
+
amountX402: number;
|
|
28
|
+
allocatedAtMs: number;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface FoundationDAOState {
|
|
32
|
+
treasuryBalanceX402: number;
|
|
33
|
+
activeProposals: Proposal[];
|
|
34
|
+
membersCount: number;
|
|
35
|
+
allocations: FundingAllocation[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const defaultConfig: FoundationDAOConfig = {
|
|
39
|
+
governanceToken: 'HOLO_GOV',
|
|
40
|
+
quorumPercent: 33,
|
|
41
|
+
executionDelayHours: 48,
|
|
42
|
+
multisigOwnersCount: 9
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export function createFoundationDAOHandler(): TraitHandler<FoundationDAOConfig> {
|
|
46
|
+
const applyVote = (proposal: Proposal, voterId: string, support: boolean, weight: number) => {
|
|
47
|
+
const safeWeight = Number.isFinite(weight) && weight > 0 ? weight : 1;
|
|
48
|
+
const previous = proposal.voterWeights[voterId];
|
|
49
|
+
if (previous) {
|
|
50
|
+
if (previous.support) proposal.votesFor -= previous.weight;
|
|
51
|
+
else proposal.votesAgainst -= previous.weight;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
proposal.voterWeights[voterId] = { support, weight: safeWeight };
|
|
55
|
+
if (support) proposal.votesFor += safeWeight;
|
|
56
|
+
else proposal.votesAgainst += safeWeight;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const maybeConcludeProposal = (
|
|
60
|
+
proposal: Proposal,
|
|
61
|
+
config: FoundationDAOConfig,
|
|
62
|
+
state: FoundationDAOState,
|
|
63
|
+
ctx: TraitContext,
|
|
64
|
+
nowMs: number
|
|
65
|
+
) => {
|
|
66
|
+
const totalVotes = proposal.votesFor + proposal.votesAgainst;
|
|
67
|
+
const rawQuorumThreshold = state.membersCount * (config.quorumPercent / 100);
|
|
68
|
+
const quorumThreshold =
|
|
69
|
+
config.quorumPercent <= 0 ? 0 : Math.max(1, Math.floor(rawQuorumThreshold));
|
|
70
|
+
if (totalVotes < quorumThreshold || proposal.status !== 'active') return;
|
|
71
|
+
|
|
72
|
+
proposal.status = proposal.votesFor > proposal.votesAgainst ? 'passed' : 'rejected';
|
|
73
|
+
ctx.emit?.('dao:proposal_concluded', {
|
|
74
|
+
id: proposal.id,
|
|
75
|
+
status: proposal.status,
|
|
76
|
+
votesFor: proposal.votesFor,
|
|
77
|
+
votesAgainst: proposal.votesAgainst,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
if (proposal.status === 'passed') {
|
|
81
|
+
const delayMs = Math.max(0, config.executionDelayHours) * 3600 * 1000;
|
|
82
|
+
proposal.executableAtMs = nowMs + delayMs;
|
|
83
|
+
ctx.emit?.('dao:execution_scheduled', {
|
|
84
|
+
id: proposal.id,
|
|
85
|
+
executableAtMs: proposal.executableAtMs,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const executeProposal = (
|
|
91
|
+
proposal: Proposal,
|
|
92
|
+
state: FoundationDAOState,
|
|
93
|
+
ctx: TraitContext,
|
|
94
|
+
nowMs: number
|
|
95
|
+
) => {
|
|
96
|
+
if (proposal.status !== 'passed') return;
|
|
97
|
+
if (proposal.executableAtMs && nowMs < proposal.executableAtMs) return;
|
|
98
|
+
|
|
99
|
+
if (typeof proposal.requestedAmountX402 === 'number' && proposal.requestedAmountX402 > 0) {
|
|
100
|
+
const zoneId = proposal.zoneId || 'unscoped_zone';
|
|
101
|
+
if (state.treasuryBalanceX402 < proposal.requestedAmountX402) {
|
|
102
|
+
ctx.emit?.('dao:funding_failed', {
|
|
103
|
+
proposalId: proposal.id,
|
|
104
|
+
zoneId,
|
|
105
|
+
requestedAmountX402: proposal.requestedAmountX402,
|
|
106
|
+
treasuryBalanceX402: state.treasuryBalanceX402,
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
state.treasuryBalanceX402 -= proposal.requestedAmountX402;
|
|
112
|
+
state.allocations.push({
|
|
113
|
+
proposalId: proposal.id,
|
|
114
|
+
zoneId,
|
|
115
|
+
amountX402: proposal.requestedAmountX402,
|
|
116
|
+
allocatedAtMs: nowMs,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
ctx.emit?.('dao:funding_allocated', {
|
|
120
|
+
proposalId: proposal.id,
|
|
121
|
+
zoneId,
|
|
122
|
+
amountX402: proposal.requestedAmountX402,
|
|
123
|
+
treasuryBalanceX402: state.treasuryBalanceX402,
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
proposal.status = 'executed';
|
|
128
|
+
ctx.emit?.('dao:executed', { id: proposal.id });
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
name: 'foundation_dao',
|
|
133
|
+
defaultConfig,
|
|
134
|
+
onAttach(n: HSPlusNode, c: FoundationDAOConfig, ctx: TraitContext) {
|
|
135
|
+
n.__daoState = {
|
|
136
|
+
treasuryBalanceX402: 5000000,
|
|
137
|
+
activeProposals: [],
|
|
138
|
+
membersCount: 150,
|
|
139
|
+
allocations: [],
|
|
140
|
+
};
|
|
141
|
+
ctx.emit?.('dao:initialized');
|
|
142
|
+
},
|
|
143
|
+
onDetach(n: HSPlusNode, _c: FoundationDAOConfig, _ctx: TraitContext) {
|
|
144
|
+
delete n.__daoState;
|
|
145
|
+
},
|
|
146
|
+
onUpdate() {},
|
|
147
|
+
onEvent(n: HSPlusNode, c: FoundationDAOConfig, ctx: TraitContext, e: TraitEvent) {
|
|
148
|
+
const s = n.__daoState as FoundationDAOState;
|
|
149
|
+
if (!s) return;
|
|
150
|
+
if (e.type === 'dao:propose') {
|
|
151
|
+
const nowMs = Date.now();
|
|
152
|
+
const proposal: Proposal = {
|
|
153
|
+
id: `prop_${nowMs}`,
|
|
154
|
+
title: (e.payload?.title as string) || 'General Update',
|
|
155
|
+
votesFor: 0,
|
|
156
|
+
votesAgainst: 0,
|
|
157
|
+
status: 'active',
|
|
158
|
+
zoneId: typeof e.payload?.zoneId === 'string' ? (e.payload.zoneId as string) : undefined,
|
|
159
|
+
requestedAmountX402:
|
|
160
|
+
typeof e.payload?.requestedAmountX402 === 'number'
|
|
161
|
+
? (e.payload.requestedAmountX402 as number)
|
|
162
|
+
: undefined,
|
|
163
|
+
createdAtMs: nowMs,
|
|
164
|
+
voterWeights: {},
|
|
165
|
+
};
|
|
166
|
+
s.activeProposals.push(proposal);
|
|
167
|
+
ctx.emit?.('dao:proposal_created', { proposal });
|
|
168
|
+
} else if (e.type === 'dao:vote') {
|
|
169
|
+
const pid = e.payload?.proposalId as string;
|
|
170
|
+
const prop = s.activeProposals.find(p => p.id === pid);
|
|
171
|
+
if (prop && prop.status !== 'executed') {
|
|
172
|
+
const voterId = typeof e.payload?.voterId === 'string' ? (e.payload.voterId as string) : 'anonymous_voter';
|
|
173
|
+
const support = Boolean(e.payload?.support);
|
|
174
|
+
const weight = typeof e.payload?.weight === 'number' ? (e.payload.weight as number) : 1;
|
|
175
|
+
prop.status = 'active';
|
|
176
|
+
delete prop.executableAtMs;
|
|
177
|
+
applyVote(prop, voterId, support, weight);
|
|
178
|
+
maybeConcludeProposal(prop, c, s, ctx, Date.now());
|
|
179
|
+
executeProposal(prop, s, ctx, Date.now());
|
|
180
|
+
}
|
|
181
|
+
} else if (e.type === 'dao:autonomous_vote') {
|
|
182
|
+
const pid = e.payload?.proposalId as string;
|
|
183
|
+
const prop = s.activeProposals.find(p => p.id === pid);
|
|
184
|
+
if (!prop || prop.status !== 'active') return;
|
|
185
|
+
|
|
186
|
+
const agents = Array.isArray(e.payload?.agents)
|
|
187
|
+
? (e.payload?.agents as Array<Record<string, unknown>>)
|
|
188
|
+
: [];
|
|
189
|
+
|
|
190
|
+
for (const agent of agents) {
|
|
191
|
+
const voterId = typeof agent.agentId === 'string' ? (agent.agentId as string) : 'agent';
|
|
192
|
+
const support =
|
|
193
|
+
typeof agent.support === 'boolean'
|
|
194
|
+
? (agent.support as boolean)
|
|
195
|
+
: prop.requestedAmountX402
|
|
196
|
+
? prop.requestedAmountX402 <= s.treasuryBalanceX402
|
|
197
|
+
: true;
|
|
198
|
+
const weight = typeof agent.weight === 'number' ? (agent.weight as number) : 1;
|
|
199
|
+
applyVote(prop, voterId, support, weight);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
maybeConcludeProposal(prop, c, s, ctx, Date.now());
|
|
203
|
+
executeProposal(prop, s, ctx, Date.now());
|
|
204
|
+
} else if (e.type === 'dao:execute') {
|
|
205
|
+
const nowMs =
|
|
206
|
+
typeof e.payload?.nowMs === 'number' ? (e.payload.nowMs as number) : Date.now();
|
|
207
|
+
|
|
208
|
+
if (typeof e.payload?.proposalId === 'string') {
|
|
209
|
+
const prop = s.activeProposals.find(p => p.id === e.payload!.proposalId);
|
|
210
|
+
if (prop) executeProposal(prop, s, ctx, nowMs);
|
|
211
|
+
} else {
|
|
212
|
+
s.activeProposals.forEach((p) => executeProposal(p, s, ctx, nowMs));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/** @marketplace_listing Trait — Trait/asset listing for agent marketplace. @trait marketplace_listing */
|
|
2
|
+
import type { TraitHandler, HSPlusNode, TraitContext, TraitEvent } from './types';
|
|
3
|
+
|
|
4
|
+
export type ListingStatus = 'draft' | 'active' | 'sold' | 'suspended' | 'expired';
|
|
5
|
+
export type PricingModel = 'fixed' | 'auction' | 'subscription' | 'pay_per_use' | 'free';
|
|
6
|
+
|
|
7
|
+
export interface MarketplaceListingConfig {
|
|
8
|
+
listingId: string;
|
|
9
|
+
title: string;
|
|
10
|
+
description: string;
|
|
11
|
+
sellerAgentId: string;
|
|
12
|
+
assetType: 'trait' | 'composition' | 'dataset' | 'model' | 'service';
|
|
13
|
+
price: number;
|
|
14
|
+
currency: string;
|
|
15
|
+
pricingModel: PricingModel;
|
|
16
|
+
royaltyPercent: number;
|
|
17
|
+
tags: string[];
|
|
18
|
+
licenseType: 'MIT' | 'proprietary' | 'CC-BY' | 'CC-BY-SA' | 'commercial';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface MarketplaceListingState {
|
|
22
|
+
status: ListingStatus;
|
|
23
|
+
views: number;
|
|
24
|
+
purchases: number;
|
|
25
|
+
revenue: number;
|
|
26
|
+
rating: number;
|
|
27
|
+
reviewCount: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultConfig: MarketplaceListingConfig = { listingId: '', title: '', description: '', sellerAgentId: '', assetType: 'trait', price: 0, currency: 'USDC', pricingModel: 'fixed', royaltyPercent: 10, tags: [], licenseType: 'MIT' };
|
|
31
|
+
|
|
32
|
+
export function createMarketplaceListingHandler(): TraitHandler<MarketplaceListingConfig> {
|
|
33
|
+
return { name: 'marketplace_listing', defaultConfig,
|
|
34
|
+
onAttach(n: HSPlusNode, c: MarketplaceListingConfig, ctx: TraitContext) { n.__listingState = { status: 'draft' as ListingStatus, views: 0, purchases: 0, revenue: 0, rating: 0, reviewCount: 0 }; ctx.emit?.('listing:created', { title: c.title, price: c.price }); },
|
|
35
|
+
onDetach(n: HSPlusNode, _c: MarketplaceListingConfig, ctx: TraitContext) { delete n.__listingState; ctx.emit?.('listing:removed'); },
|
|
36
|
+
onUpdate() {},
|
|
37
|
+
onEvent(n: HSPlusNode, c: MarketplaceListingConfig, ctx: TraitContext, e: TraitEvent) {
|
|
38
|
+
const s = n.__listingState as MarketplaceListingState | undefined; if (!s) return;
|
|
39
|
+
if (e.type === 'listing:publish') { s.status = 'active'; ctx.emit?.('listing:published', { listingId: c.listingId }); }
|
|
40
|
+
if (e.type === 'listing:purchase') { s.purchases++; s.revenue += c.price; ctx.emit?.('listing:sold', { buyer: e.payload?.buyerAgentId, revenue: s.revenue, royalty: c.price * c.royaltyPercent / 100 }); }
|
|
41
|
+
if (e.type === 'listing:view') { s.views++; }
|
|
42
|
+
if (e.type === 'listing:review') { const rating = (e.payload?.rating as number) ?? 5; s.rating = (s.rating * s.reviewCount + rating) / (s.reviewCount + 1); s.reviewCount++; ctx.emit?.('listing:reviewed', { avgRating: s.rating }); }
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** @royalty_stream Trait — Revenue sharing and royalty distribution. @trait royalty_stream */
|
|
2
|
+
import type { TraitHandler, HSPlusNode, TraitContext, TraitEvent } from './types';
|
|
3
|
+
|
|
4
|
+
export interface RoyaltySplit { recipientId: string; percent: number; walletAddress?: string; }
|
|
5
|
+
export interface RoyaltyStreamConfig { assetId: string; splits: RoyaltySplit[]; currency: string; minimumPayout: number; payoutFrequency: 'instant' | 'daily' | 'weekly' | 'monthly'; }
|
|
6
|
+
export interface RoyaltyStreamState { totalCollected: number; totalDistributed: number; pendingPayout: number; distributionCount: number; }
|
|
7
|
+
|
|
8
|
+
const defaultConfig: RoyaltyStreamConfig = { assetId: '', splits: [], currency: 'USDC', minimumPayout: 1, payoutFrequency: 'instant' };
|
|
9
|
+
|
|
10
|
+
export function createRoyaltyStreamHandler(): TraitHandler<RoyaltyStreamConfig> {
|
|
11
|
+
return { name: 'royalty_stream', defaultConfig,
|
|
12
|
+
onAttach(n: HSPlusNode, _c: RoyaltyStreamConfig, ctx: TraitContext) { n.__royaltyState = { totalCollected: 0, totalDistributed: 0, pendingPayout: 0, distributionCount: 0 }; ctx.emit?.('royalty:stream_created'); },
|
|
13
|
+
onDetach(n: HSPlusNode, _c: RoyaltyStreamConfig, ctx: TraitContext) { delete n.__royaltyState; ctx.emit?.('royalty:stream_closed'); },
|
|
14
|
+
onUpdate() {},
|
|
15
|
+
onEvent(n: HSPlusNode, c: RoyaltyStreamConfig, ctx: TraitContext, e: TraitEvent) {
|
|
16
|
+
const s = n.__royaltyState as RoyaltyStreamState | undefined; if (!s) return;
|
|
17
|
+
if (e.type === 'royalty:collect') {
|
|
18
|
+
const amount = (e.payload?.amount as number) ?? 0;
|
|
19
|
+
s.totalCollected += amount; s.pendingPayout += amount;
|
|
20
|
+
if (c.payoutFrequency === 'instant' && s.pendingPayout >= c.minimumPayout) {
|
|
21
|
+
const distributions = c.splits.map(split => ({ recipient: split.recipientId, amount: s.pendingPayout * split.percent / 100 }));
|
|
22
|
+
s.totalDistributed += s.pendingPayout; s.pendingPayout = 0; s.distributionCount++;
|
|
23
|
+
ctx.emit?.('royalty:distributed', { distributions, total: s.totalDistributed });
|
|
24
|
+
} else { ctx.emit?.('royalty:collected', { pending: s.pendingPayout }); }
|
|
25
|
+
}
|
|
26
|
+
if (e.type === 'royalty:flush') {
|
|
27
|
+
if (s.pendingPayout > 0) {
|
|
28
|
+
s.totalDistributed += s.pendingPayout; s.pendingPayout = 0; s.distributionCount++;
|
|
29
|
+
ctx.emit?.('royalty:flushed', { totalDistributed: s.totalDistributed });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createFoundationDAOHandler } from '../FoundationDAOTrait';
|
|
3
|
+
import type { HSPlusNode } from '../types';
|
|
4
|
+
|
|
5
|
+
describe('FoundationDAOTrait', () => {
|
|
6
|
+
it('initializes DAO state on attach', () => {
|
|
7
|
+
const handler = createFoundationDAOHandler();
|
|
8
|
+
const node: HSPlusNode = { id: 'dao-1' };
|
|
9
|
+
const events: Array<{ type: string; payload?: unknown }> = [];
|
|
10
|
+
|
|
11
|
+
handler.onAttach(node, handler.defaultConfig, {
|
|
12
|
+
emit: (type, payload) => events.push({ type, payload }),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
const state = node.__daoState as any;
|
|
16
|
+
expect(state).toBeDefined();
|
|
17
|
+
expect(state.treasuryBalanceX402).toBe(5000000);
|
|
18
|
+
expect(state.allocations).toEqual([]);
|
|
19
|
+
expect(events.map((e) => e.type)).toContain('dao:initialized');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('deduplicates weighted votes per voter and concludes with quorum', () => {
|
|
23
|
+
const handler = createFoundationDAOHandler();
|
|
24
|
+
const node: HSPlusNode = { id: 'dao-2' };
|
|
25
|
+
const emitted: string[] = [];
|
|
26
|
+
|
|
27
|
+
const config = {
|
|
28
|
+
...handler.defaultConfig,
|
|
29
|
+
quorumPercent: 1,
|
|
30
|
+
executionDelayHours: 999, // keep proposal in passed state for assertion
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
handler.onAttach(node, config, { emit: (type) => emitted.push(type) });
|
|
34
|
+
|
|
35
|
+
handler.onEvent(node, config, { emit: (type) => emitted.push(type) }, {
|
|
36
|
+
type: 'dao:propose',
|
|
37
|
+
payload: { title: 'Fund autonomous zone indexing' },
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const state = node.__daoState as any;
|
|
41
|
+
const proposal = state.activeProposals[0];
|
|
42
|
+
|
|
43
|
+
handler.onEvent(node, config, { emit: (type) => emitted.push(type) }, {
|
|
44
|
+
type: 'dao:vote',
|
|
45
|
+
payload: { proposalId: proposal.id, voterId: 'agent_a', support: true, weight: 2 },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
handler.onEvent(node, config, { emit: (type) => emitted.push(type) }, {
|
|
49
|
+
type: 'dao:vote',
|
|
50
|
+
payload: { proposalId: proposal.id, voterId: 'agent_a', support: false, weight: 1 },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(proposal.votesFor).toBe(0);
|
|
54
|
+
expect(proposal.votesAgainst).toBe(1);
|
|
55
|
+
expect(proposal.status).toBe('rejected');
|
|
56
|
+
expect(emitted).toContain('dao:proposal_concluded');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('supports autonomous agentic voting and executes funding allocation', () => {
|
|
60
|
+
const handler = createFoundationDAOHandler();
|
|
61
|
+
const node: HSPlusNode = { id: 'dao-3' };
|
|
62
|
+
const emitted: Array<{ type: string; payload?: any }> = [];
|
|
63
|
+
|
|
64
|
+
const config = {
|
|
65
|
+
...handler.defaultConfig,
|
|
66
|
+
quorumPercent: 1,
|
|
67
|
+
executionDelayHours: 0,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
handler.onAttach(node, config, {
|
|
71
|
+
emit: (type, payload) => emitted.push({ type, payload }),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
handler.onEvent(node, config, { emit: (type, payload) => emitted.push({ type, payload }) }, {
|
|
75
|
+
type: 'dao:propose',
|
|
76
|
+
payload: {
|
|
77
|
+
title: 'Fund sovereign spatial zone Z1',
|
|
78
|
+
zoneId: 'zone-z1',
|
|
79
|
+
requestedAmountX402: 250000,
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const state = node.__daoState as any;
|
|
84
|
+
const proposal = state.activeProposals[0];
|
|
85
|
+
|
|
86
|
+
handler.onEvent(node, config, { emit: (type, payload) => emitted.push({ type, payload }) }, {
|
|
87
|
+
type: 'dao:autonomous_vote',
|
|
88
|
+
payload: {
|
|
89
|
+
proposalId: proposal.id,
|
|
90
|
+
agents: [
|
|
91
|
+
{ agentId: 'agent_a', support: true, weight: 2 },
|
|
92
|
+
{ agentId: 'agent_b', support: true, weight: 2 },
|
|
93
|
+
],
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
expect(proposal.status).toBe('executed');
|
|
98
|
+
expect(state.allocations).toHaveLength(1);
|
|
99
|
+
expect(state.allocations[0].zoneId).toBe('zone-z1');
|
|
100
|
+
expect(state.allocations[0].amountX402).toBe(250000);
|
|
101
|
+
expect(state.treasuryBalanceX402).toBe(5000000 - 250000);
|
|
102
|
+
|
|
103
|
+
const eventTypes = emitted.map((e) => e.type);
|
|
104
|
+
expect(eventTypes).toContain('dao:funding_allocated');
|
|
105
|
+
expect(eventTypes).toContain('dao:executed');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('emits funding_failed when treasury is insufficient', () => {
|
|
109
|
+
const handler = createFoundationDAOHandler();
|
|
110
|
+
const node: HSPlusNode = { id: 'dao-4' };
|
|
111
|
+
const emitted: Array<{ type: string; payload?: any }> = [];
|
|
112
|
+
|
|
113
|
+
const config = {
|
|
114
|
+
...handler.defaultConfig,
|
|
115
|
+
quorumPercent: 1,
|
|
116
|
+
executionDelayHours: 0,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
handler.onAttach(node, config, {
|
|
120
|
+
emit: (type, payload) => emitted.push({ type, payload }),
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
const state = node.__daoState as any;
|
|
124
|
+
state.treasuryBalanceX402 = 100;
|
|
125
|
+
|
|
126
|
+
handler.onEvent(node, config, { emit: (type, payload) => emitted.push({ type, payload }) }, {
|
|
127
|
+
type: 'dao:propose',
|
|
128
|
+
payload: {
|
|
129
|
+
title: 'Huge funding ask',
|
|
130
|
+
zoneId: 'zone-z2',
|
|
131
|
+
requestedAmountX402: 5000,
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const proposal = state.activeProposals[0];
|
|
136
|
+
|
|
137
|
+
handler.onEvent(node, config, { emit: (type, payload) => emitted.push({ type, payload }) }, {
|
|
138
|
+
type: 'dao:autonomous_vote',
|
|
139
|
+
payload: {
|
|
140
|
+
proposalId: proposal.id,
|
|
141
|
+
agents: [{ agentId: 'agent_a', support: true, weight: 5 }],
|
|
142
|
+
},
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
expect(proposal.status).toBe('passed');
|
|
146
|
+
expect(state.allocations).toHaveLength(0);
|
|
147
|
+
expect(state.treasuryBalanceX402).toBe(100);
|
|
148
|
+
expect(emitted.map((e) => e.type)).toContain('dao:funding_failed');
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export interface HSPlusNode { id?: string; properties?: Record<string, unknown>; [key: string]: unknown; }
|
|
2
|
+
export interface TraitContext { emit?: (event: string, payload?: unknown) => void; [key: string]: unknown; }
|
|
3
|
+
export interface TraitEvent { type: string; payload?: Record<string, unknown>; [key: string]: unknown; }
|
|
4
|
+
export interface TraitHandler<T = unknown> { name: string; defaultConfig: T; onAttach(n: HSPlusNode, c: T, ctx: TraitContext): void; onDetach(n: HSPlusNode, c: T, ctx: TraitContext): void; onUpdate(n: HSPlusNode, c: T, ctx: TraitContext, d: number): void; onEvent(n: HSPlusNode, c: T, ctx: TraitContext, e: TraitEvent): void; }
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "extends": "../../../tsconfig.json", "compilerOptions": { "outDir": "dist", "rootDir": "src", "declaration": true }, "include": ["src"] }
|