-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.ts
103 lines (84 loc) · 2.44 KB
/
transaction.ts
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
import { PostCondition, StacksTransaction } from "@stacks/transactions";
export interface ResultAssets {
stx: Record<string, string>;
burns: Record<string, string>;
tokens: Record<string, Record<string, string>>;
assets: Record<string, Record<string, string>>;
}
export interface TransactionResultOk<Ok> {
value: Ok;
response?: ResponseOk<Ok>;
isOk: true;
events: any[];
costs?: {
[key: string]: any;
runtime: number;
};
assets?: ResultAssets;
// TODO: add events
}
export interface TransactionResultErr<Err> {
value: Err;
response?: ResponseErr<Err>;
costs?: {
[key: string]: any;
runtime: number;
};
isOk: false;
}
export type TransactionResult<Ok, Err> =
| TransactionResultOk<Ok>
| TransactionResultErr<Err>;
export interface TransactionReceiptBase<Ok, Err> {
getResult: () => Promise<TransactionResult<Ok, Err>>;
}
export interface WebTransactionReceipt<Ok, Err>
extends TransactionReceiptBase<Ok, Err> {
txId: string;
stacksTransaction: StacksTransaction;
}
export interface TestTransacionReceipt<Ok, Err>
extends TransactionReceiptBase<Ok, Err> {
result: TransactionResult<Ok, Err>;
}
export type TransactionReceipt<Ok, Err> =
| WebTransactionReceipt<Ok, Err>
| TestTransacionReceipt<Ok, Err>
| TransactionReceiptBase<Ok, Err>;
export interface WebSignerOptions {
postConditions?: PostCondition[];
}
export interface TestSignerOptions {
sender: string;
}
export type SubmitOptions = TestSignerOptions | WebSignerOptions;
export type Submitter<Ok, Err> = (
options: SubmitOptions
) => Promise<TransactionReceipt<Ok, Err>>;
interface ResponseOk<Ok> {
value: Ok;
}
interface ResponseErr<Err> {
value: Err;
}
export type Response<Ok, Err> = ResponseOk<Ok> | ResponseErr<Err>;
export interface Transaction<Ok, Err> {
submit: Submitter<Ok, Err>;
}
export async function tx<A, B>(tx: Transaction<A, B>, sender: string) {
const receipt = await tx.submit({ sender });
const result = await receipt.getResult();
return result;
}
export async function txOk<A, B>(_tx: Transaction<A, B>, sender: string) {
const result = await tx(_tx, sender);
if (!result.isOk)
throw new Error(`Expected transaction ok, got error: ${result.value}`);
return result;
}
export async function txErr<A, B>(_tx: Transaction<A, B>, sender: string) {
const result = await tx(_tx, sender);
if (result.isOk)
throw new Error(`Expected transaction error, got ok: ${result.value}`);
return result;
}