/
prepare.ts
125 lines (115 loc) · 3.13 KB
/
prepare.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import {
OriginateParams,
RPCOriginationOperation,
TransferParams,
RPCTransferOperation,
DelegateParams,
RPCDelegateOperation,
RegisterDelegateParams,
} from '../operations/types';
import { DEFAULT_FEE, DEFAULT_GAS_LIMIT, DEFAULT_STORAGE_LIMIT } from '../constants';
import { ml2mic, sexp2mic } from '@taquito/utils';
import { Schema } from '@taquito/michelson-encoder';
import { format } from '../format';
import { OpKind } from '@taquito/rpc';
export const createOriginationOperation = async ({
code,
init,
balance = '0',
delegate,
storage,
fee = DEFAULT_FEE.ORIGINATION,
gasLimit = DEFAULT_GAS_LIMIT.ORIGINATION,
storageLimit = DEFAULT_STORAGE_LIMIT.ORIGINATION,
}: OriginateParams) => {
// tslint:disable-next-line: strict-type-predicates
if (storage !== undefined && init !== undefined) {
throw new Error(
'Storage and Init cannot be set a the same time. Please either use storage or init but not both.'
);
}
const contractCode = Array.isArray(code) ? code : ml2mic(code);
let contractStorage: object;
if (storage !== undefined) {
const schema = new Schema(contractCode[1].args[0]);
contractStorage = schema.Encode(storage);
} else {
contractStorage = typeof init === 'string' ? sexp2mic(init) : init;
}
const script = {
code: Array.isArray(code) ? code : ml2mic(code),
storage: contractStorage,
};
const operation: RPCOriginationOperation = {
kind: OpKind.ORIGINATION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
balance: format('tz', 'mutez', balance).toString(),
script,
};
if (delegate) {
operation.delegate = delegate;
}
return operation;
};
export const createTransferOperation = async ({
to,
amount,
parameter,
fee = DEFAULT_FEE.TRANSFER,
gasLimit = DEFAULT_GAS_LIMIT.TRANSFER,
storageLimit = DEFAULT_STORAGE_LIMIT.TRANSFER,
mutez = false,
rawParam = false,
}: TransferParams) => {
const operation: RPCTransferOperation = {
kind: OpKind.TRANSACTION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
amount: mutez ? amount.toString() : format('tz', 'mutez', amount).toString(),
destination: to,
};
if (parameter) {
operation.parameters = rawParam
? parameter
: typeof parameter === 'string'
? sexp2mic(parameter)
: parameter;
}
return operation;
};
export const createSetDelegateOperation = async ({
delegate,
source,
fee = DEFAULT_FEE.DELEGATION,
gasLimit = DEFAULT_GAS_LIMIT.DELEGATION,
storageLimit = DEFAULT_STORAGE_LIMIT.DELEGATION,
}: DelegateParams) => {
const operation: RPCDelegateOperation = {
kind: OpKind.DELEGATION,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
delegate,
};
return operation;
};
export const createRegisterDelegateOperation = async (
{
fee = DEFAULT_FEE.DELEGATION,
gasLimit = DEFAULT_GAS_LIMIT.DELEGATION,
storageLimit = DEFAULT_STORAGE_LIMIT.DELEGATION,
}: RegisterDelegateParams,
source: string
) => {
return {
kind: OpKind.DELEGATION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
delegate: source,
} as RPCDelegateOperation;
};