forked from Split-Naira/SplitNaira
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplits.ts
More file actions
171 lines (152 loc) · 5.27 KB
/
Copy pathsplits.ts
File metadata and controls
171 lines (152 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { z } from "zod";
import { CollaboratorSchema } from "../generated/contract-types.js";
import { Address } from "@stellar/stellar-sdk";
// Strict Stellar address validator used across schemas
export const stellarAddressSchema = z
.string()
.min(1, "address is required")
.superRefine((value, ctx) => {
try {
Address.fromString(value);
} catch {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "must be a valid Stellar address (classic or contract)"
});
}
});
export const collaboratorSchema = CollaboratorSchema.omit({ basis_points: true }).extend({
address: stellarAddressSchema,
basisPoints: z
.number()
.int("basisPoints must be an integer")
.positive("basisPoints must be greater than 0")
.max(10_000, "basisPoints must be <= 10000")
});
export const createSplitSchema = z
.object({
owner: stellarAddressSchema.describe("owner"),
projectId: z
.string()
.min(1, "projectId is required")
.max(32)
.regex(/^[a-zA-Z0-9_]+$/, "projectId must be alphanumeric/underscore"),
title: z.string().min(1, "title is required").max(128),
projectType: z.string().min(1, "projectType is required").max(32),
token: stellarAddressSchema.describe("token"),
collaborators: z.array(collaboratorSchema).min(2, "at least 2 collaborators are required")
})
.superRefine((payload, ctx) => {
const totalBasisPoints = payload.collaborators.reduce(
(sum, collaborator) => sum + collaborator.basisPoints,
0
);
if (totalBasisPoints !== 10_000) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["collaborators"],
message: "collaborators basisPoints must sum to exactly 10000"
});
}
const addresses = new Set<string>();
for (const collaborator of payload.collaborators) {
if (addresses.has(collaborator.address)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["collaborators"],
message: "duplicate collaborator address found"
});
break;
}
addresses.add(collaborator.address);
}
});
export const projectIdParamSchema = z
.string()
.min(1, "projectId is required")
.max(32, "projectId must be at most 32 characters")
.regex(/^[a-zA-Z0-9_]+$/, "projectId must be alphanumeric/underscore");
export const lockProjectSchema = z.object({
owner: stellarAddressSchema.describe("owner")
});
export const depositSchema = z.object({
from: stellarAddressSchema.describe("from"),
amount: z
.number()
.positive("amount must be greater than 0")
.describe("deposit amount in stroops")
});
export const updateMetadataSchema = z.object({
owner: stellarAddressSchema.describe("owner"),
title: z.string().min(1, "title is required").max(128),
projectType: z.string().min(1, "projectType is required").max(32)
});
export const updateCollaboratorsSchema = z
.object({
owner: stellarAddressSchema.describe("owner"),
collaborators: z.array(collaboratorSchema).min(2, "at least 2 collaborators are required")
})
.superRefine((payload, ctx) => {
const totalBasisPoints = payload.collaborators.reduce(
(sum, collaborator) => sum + collaborator.basisPoints,
0
);
if (totalBasisPoints !== 10_000) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["collaborators"],
message: "collaborators basisPoints must sum to exactly 10000"
});
}
const addresses = new Set<string>();
for (const collaborator of payload.collaborators) {
if (addresses.has(collaborator.address)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
path: ["collaborators"],
message: "duplicate collaborator address found"
});
break;
}
addresses.add(collaborator.address);
}
});
export const allowlistQuerySchema = z.object({
start: z.coerce.number().int().min(0).default(0),
limit: z.coerce.number().int().min(1).max(200).default(100)
});
export const listProjectsSchema = z.object({
start: z.coerce.number().int().min(0).default(0),
limit: z.coerce.number().int().min(1).max(100).default(10),
search: z.string().optional(),
type: z.string().optional(),
});
export const distributeSchema = z.object({
sourceAddress: z.string().min(1, "sourceAddress is required").optional()
});
export const historyQuerySchema = z.object({
cursor: z.string().default(""),
limit: z.coerce.number().int().min(1).max(200).default(100)
});
export const adminTokenSchema = z.object({
admin: stellarAddressSchema.describe("admin"),
token: stellarAddressSchema.describe("token")
});
export const pauseDistributionsSchema = z.object({
admin: stellarAddressSchema.describe("admin")
});
export const isTokenAllowedQuerySchema = z.object({
token: stellarAddressSchema.describe("token contract address to check")
});
export const unallocatedQuerySchema = z.object({
token: stellarAddressSchema.describe("token contract address")
});
export const withdrawUnallocatedSchema = z.object({
admin: stellarAddressSchema.describe("admin"),
token: stellarAddressSchema.describe("token contract address"),
to: stellarAddressSchema.describe("destination address"),
amount: z
.number()
.positive("amount must be greater than 0")
.describe("amount in stroops to recover")
});