-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.ts
98 lines (84 loc) · 2.23 KB
/
handler.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
"use strict";
import { TezosToolkit } from "@taquito/taquito";
import { InMemorySigner } from "@taquito/signer";
const Tezos = new TezosToolkit("https://mainnet.api.tez.ie/");
Tezos.setProvider({
signer: new InMemorySigner(process.env.PRIVATE_KEY || ""),
});
const getContractMethods = async (event) => {
if (!event.body) {
return {
statusCode: 400,
message: "No body passed in",
};
}
const { contract_address } = JSON.parse(event.body);
if (!contract_address) {
return {
statusCode: 400,
message: "You must include a contract address",
};
}
try {
const contract = await Tezos.contract.at(contract_address);
let methods = contract.parameterSchema.ExtractSignatures();
return {
statusCode: 200,
body: JSON.stringify(methods, null, 2),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
}
};
const estimateGasForMethod = async (event) => {
if (!event.body) {
return {
statusCode: 400,
message: "No body passed in",
};
}
const { methodName, methodArguments, amount, contract_address } = JSON.parse(
event.body
);
if (!methodName || !methodArguments || !contract_address) {
return {
statusCode: 400,
message:
"You must provide a contract_address, methodName, and methodArguments",
};
}
try {
const contract = await Tezos.contract.at(contract_address);
const operation = await contract.methodsObject[methodName](
methodArguments
).toTransferParams({
amount: amount ?? 2,
});
console.log({ operation });
const estimate = await Tezos.estimate.transfer(operation);
return {
statusCode: 200,
body: JSON.stringify(
{
gasLimit: estimate.gasLimit,
minimalFeeMutez: estimate.minimalFeeMutez,
storageLimit: estimate.storageLimit,
suggestedFeeMutez: estimate.suggestedFeeMutez,
totalCost: estimate.totalCost,
usingBaseFeeMutez: estimate.usingBaseFeeMutez,
},
null,
2
),
};
} catch (error) {
return {
statusCode: 500,
body: JSON.stringify(error),
};
}
};
export { getContractMethods, estimateGasForMethod };