-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
input.ts
72 lines (70 loc) · 2.28 KB
/
input.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
import type { BytesLike } from '@ethersproject/bytes';
import { arrayify, hexlify } from '@ethersproject/bytes';
import { ZeroBytes32 } from '@fuel-ts/constants';
import type { BigNumberish } from '@fuel-ts/math';
import type { Input } from '@fuel-ts/transactions';
import { InputType } from '@fuel-ts/transactions';
export type CoinTransactionRequestInput = {
type: InputType.Coin;
/** UTXO ID */
id: BytesLike;
/** Owning address or script hash */
owner: BytesLike;
/** Amount of coins */
amount: BigNumberish;
/** Asset ID of the coins */
assetId: BytesLike;
/** Index of witness that authorizes spending the coin */
witnessIndex: number;
/** UTXO being spent must have been created at least this many blocks ago */
maturity?: BigNumberish;
/** Predicate bytecode */
predicate?: BytesLike;
/** Predicate input data (parameters) */
predicateData?: BytesLike;
};
export type ContractTransactionRequestInput = {
type: InputType.Contract;
/** Contract ID */
contractId: BytesLike;
};
export type TransactionRequestInput = CoinTransactionRequestInput | ContractTransactionRequestInput;
export const inputify = (value: TransactionRequestInput): Input => {
switch (value.type) {
case InputType.Coin: {
const predicate = arrayify(value.predicate ?? '0x');
const predicateData = arrayify(value.predicateData ?? '0x');
return {
type: InputType.Coin,
utxoID: {
transactionId: hexlify(arrayify(value.id).slice(0, 32)),
outputIndex: arrayify(value.id)[32],
},
owner: hexlify(value.owner),
amount: BigInt(value.amount),
assetId: hexlify(value.assetId),
witnessIndex: value.witnessIndex,
maturity: BigInt(value.maturity ?? 0),
predicateLength: predicate.length,
predicateDataLength: predicateData.length,
predicate: hexlify(predicate),
predicateData: hexlify(predicateData),
};
}
case InputType.Contract: {
return {
type: InputType.Contract,
utxoID: {
transactionId: ZeroBytes32,
outputIndex: 0,
},
balanceRoot: ZeroBytes32,
stateRoot: ZeroBytes32,
contractID: hexlify(value.contractId),
};
}
default: {
throw new Error('Invalid Input type');
}
}
};