-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
output.ts
110 lines (108 loc) · 2.96 KB
/
output.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
104
105
106
107
108
109
110
import type { BytesLike } from '@ethersproject/bytes';
import { hexlify } from '@ethersproject/bytes';
import { ZeroBytes32 } from '@fuel-ts/constants';
import type { BigNumberish } from '@fuel-ts/math';
import { toHex } from '@fuel-ts/math';
import type { Output } from '@fuel-ts/transactions';
import { OutputType } from '@fuel-ts/transactions';
export type CoinTransactionRequestOutput = {
type: OutputType.Coin;
/** Receiving address or script hash */
to: BytesLike;
/** Amount of coins to send */
amount: BigNumberish;
/** Asset ID of coins */
assetId: BytesLike;
};
export type ContractTransactionRequestOutput = {
type: OutputType.Contract;
/** Index of input contract */
inputIndex: number;
};
export type WithdrawalTransactionRequestOutput = {
type: OutputType.Withdrawal;
/** Receiving address */
to: BytesLike;
/** Amount of coins to withdraw */
amount: BigNumberish;
/** Asset ID of coins */
assetId: BytesLike;
};
export type ChangeTransactionRequestOutput = {
type: OutputType.Change;
/** Receiving address or script hash */
to: BytesLike;
/** Asset ID of coins */
assetId: BytesLike;
};
export type VariableTransactionRequestOutput = {
type: OutputType.Variable;
};
export type ContractCreatedTransactionRequestOutput = {
type: OutputType.ContractCreated;
/** Contract ID */
contractId: BytesLike;
/** State Root */
stateRoot: BytesLike;
};
export type TransactionRequestOutput =
| CoinTransactionRequestOutput
| ContractTransactionRequestOutput
| WithdrawalTransactionRequestOutput
| ChangeTransactionRequestOutput
| VariableTransactionRequestOutput
| ContractCreatedTransactionRequestOutput;
export const outputify = (value: TransactionRequestOutput): Output => {
switch (value.type) {
case OutputType.Coin: {
return {
type: OutputType.Coin,
to: hexlify(value.to),
amount: toHex(value.amount),
assetId: hexlify(value.assetId),
};
}
case OutputType.Contract: {
return {
type: OutputType.Contract,
inputIndex: value.inputIndex,
balanceRoot: ZeroBytes32,
stateRoot: ZeroBytes32,
};
}
case OutputType.Withdrawal: {
return {
type: OutputType.Withdrawal,
to: hexlify(value.to),
amount: toHex(value.amount),
assetId: hexlify(value.assetId),
};
}
case OutputType.Change: {
return {
type: OutputType.Change,
to: hexlify(value.to),
amount: toHex(0),
assetId: hexlify(value.assetId),
};
}
case OutputType.Variable: {
return {
type: OutputType.Variable,
to: ZeroBytes32,
amount: toHex(0),
assetId: ZeroBytes32,
};
}
case OutputType.ContractCreated: {
return {
type: OutputType.ContractCreated,
contractId: hexlify(value.contractId),
stateRoot: hexlify(value.stateRoot),
};
}
default: {
throw new Error('Invalid Output type');
}
}
};