@taqueria/plugin-taquito 0.22.2 → 0.23.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/transfer.ts CHANGED
@@ -1,149 +1,109 @@
1
1
  import {
2
- getAccountPrivateKey,
3
2
  getAddressOfAlias,
4
3
  getCurrentEnvironment,
5
4
  getCurrentEnvironmentConfig,
6
- getDefaultAccount,
7
- getNetworkConfig,
8
5
  getParameter,
9
- getSandboxAccountConfig,
10
- getSandboxAccountNames,
11
- getSandboxConfig,
12
6
  sendAsyncErr,
13
- sendErr,
14
7
  sendJsonRes,
15
8
  } from '@taqueria/node-sdk';
16
- import { Environment, RequestArgs } from '@taqueria/node-sdk/types';
9
+ import { Environment } from '@taqueria/node-sdk/types';
17
10
  import { Expr, Parser } from '@taquito/michel-codec';
18
- import { importKey, InMemorySigner } from '@taquito/signer';
19
- import { TezosToolkit } from '@taquito/taquito';
11
+ import { TezosToolkit, WalletOperationBatch } from '@taquito/taquito';
12
+ import { BatchWalletOperation } from '@taquito/taquito/dist/types/wallet/batch-operation';
13
+ import {
14
+ configureToolKitForNetwork,
15
+ configureToolKitForSandbox,
16
+ getEnvTypeAndNodeConfig,
17
+ handleOpsError,
18
+ TransferOpts as Opts,
19
+ } from './common';
20
20
 
21
- interface Opts extends RequestArgs.t {
22
- contract: string;
23
- tez?: string;
24
- param?: string;
25
- entrypoint?: string;
26
- }
21
+ export type ContractInfo = {
22
+ contractAlias: string;
23
+ contractAddress: string;
24
+ parameter: string;
25
+ entrypoint: string;
26
+ mutezTransfer: number;
27
+ };
27
28
 
28
29
  type TableRow = {
29
30
  contractAlias: string;
30
31
  contractAddress: string;
31
- tezTransfer: string;
32
32
  parameter: string;
33
33
  entrypoint: string;
34
+ mutezTransfer: string;
34
35
  destination: string;
35
36
  };
36
37
 
37
- const getFirstAccountAlias = (sandboxName: string, opts: Opts) => {
38
- const aliases = getSandboxAccountNames(opts)(sandboxName);
39
- return aliases.shift();
40
- };
41
-
42
- const configureToolKitWithSandbox = async (parsedArgs: Opts, sandboxName: string): Promise<TezosToolkit> => {
43
- const sandbox = getSandboxConfig(parsedArgs)(sandboxName);
44
- if (!sandbox) {
45
- return sendAsyncErr(
46
- `The current environment is configured to use a sandbox called '${sandboxName}'; however, no sandbox of this name has been configured in .taq/config.json.`,
47
- );
48
- }
49
-
50
- let defaultAccount = getDefaultAccount(parsedArgs)(sandboxName);
51
- if (!defaultAccount) {
52
- const first = getFirstAccountAlias(sandboxName, parsedArgs);
53
- if (first) {
54
- defaultAccount = getSandboxAccountConfig(parsedArgs)(sandboxName)(first);
55
- sendErr(
56
- `Warning: A default account has not been specified for sandbox ${sandboxName}. Taqueria will use the account ${first} for this operation.\nA default account can be specified in .taq/config.json at JSON path: sandbox.${sandboxName}.accounts.default\n`,
57
- );
58
- }
59
- }
60
- if (!defaultAccount) {
61
- return sendAsyncErr(`No accounts are available for the sandbox called ${sandboxName} to perform the operation.`);
62
- }
63
-
64
- const tezos = new TezosToolkit(sandbox.rpcUrl as string);
65
- tezos.setProvider({
66
- signer: new InMemorySigner((defaultAccount.secretKey as string).replace(/^unencrypted:/, '')),
67
- });
68
- return tezos;
69
- };
70
-
71
- const configureToolKitWithNetwork = async (parsedArgs: Opts, networkName: string): Promise<TezosToolkit> => {
72
- const network = getNetworkConfig(parsedArgs)(networkName);
73
- if (!network) {
74
- return sendAsyncErr(
75
- `The current environment is configured to use a network called '${networkName}'; however, no network of this name has been configured in .taq/config.json.`,
76
- );
77
- }
78
-
79
- const tezos = new TezosToolkit(network.rpcUrl as string);
80
- const key = await getAccountPrivateKey(parsedArgs, network, 'taqRootAccount');
81
- await importKey(tezos, key);
82
- return tezos;
83
- };
84
-
85
- const configureTezosToolKit = (parsedArgs: Opts, env: Environment.t): Promise<TezosToolkit> => {
86
- const targetConstraintErrMsg = 'Each environment can only have one target, be it a sandbox or a network';
87
- if (env.sandboxes?.length === 1 && env.networks?.length === 1) return sendAsyncErr(targetConstraintErrMsg);
88
- if (env.sandboxes?.length === 1) return configureToolKitWithSandbox(parsedArgs, env.sandboxes[0]);
89
- if (env.networks?.length === 1) return configureToolKitWithNetwork(parsedArgs, env.networks[0]);
90
- return sendAsyncErr(targetConstraintErrMsg);
91
- };
92
-
93
38
  const isContractAddress = (contract: string): boolean =>
94
39
  contract.startsWith('tz1') || contract.startsWith('tz2') || contract.startsWith('tz3') || contract.startsWith('KT1');
95
40
 
96
- const getContractInfo = async (parsedArgs: Opts, env: Environment.t, tezos: TezosToolkit): Promise<TableRow> => {
41
+ const getContractInfo = async (parsedArgs: Opts, env: Environment.t): Promise<ContractInfo> => {
97
42
  const contract = parsedArgs.contract;
98
43
  return {
99
44
  contractAlias: isContractAddress(contract) ? 'N/A' : contract,
100
45
  contractAddress: isContractAddress(contract) ? contract : await getAddressOfAlias(env, contract),
101
- tezTransfer: parsedArgs.tez ?? '0',
102
46
  parameter: parsedArgs.param ? await getParameter(parsedArgs, parsedArgs.param) : 'Unit',
103
47
  entrypoint: parsedArgs.entrypoint ?? 'default',
104
- destination: tezos.rpc.getRpcUrl(),
48
+ mutezTransfer: parseInt(parsedArgs.mutez ?? '0'),
105
49
  };
106
50
  };
107
51
 
108
- const performTransferOp = (tezos: TezosToolkit, contractInfo: TableRow, parsedArgs: Opts): Promise<string> => {
109
- return tezos.contract
110
- .transfer({
52
+ const createBatchForTransfer = (tezos: TezosToolkit, contractsInfo: ContractInfo[]): WalletOperationBatch =>
53
+ contractsInfo.reduce((acc, contractInfo) =>
54
+ acc.withTransfer({
111
55
  to: contractInfo.contractAddress,
112
- amount: parseFloat(contractInfo.tezTransfer),
56
+ amount: contractInfo.mutezTransfer,
113
57
  parameter: {
114
58
  entrypoint: contractInfo.entrypoint,
115
59
  value: new Parser().parseMichelineExpression(contractInfo.parameter) as Expr,
116
60
  },
117
- })
118
- .then(op => op.confirmation().then(() => op.hash))
119
- .catch(err => {
120
- if (err instanceof Error) {
121
- if (/empty_implicit_contract/.test(err.message)) {
122
- const result = (err.message).match(/(?<="implicit":")tz[^"]+(?=")/);
123
- const publicKeyHash = result ? result[0] : undefined;
124
- if (publicKeyHash) {
125
- return sendAsyncErr(
126
- `The account ${publicKeyHash} for the target environment, "${
127
- getCurrentEnvironment(parsedArgs)
128
- }", may not be funded\nTo fund this account:\n1. Go to https://teztnets.xyz and click "Faucet" of the target testnet\n2. Copy and paste the above key into the 'wallet address field\n3. Request some Tez (Note that you might need to wait for a few seconds for the network to register the funds)`,
129
- );
130
- }
131
- }
132
- }
133
- return sendAsyncErr(`Error during transfer operation:\n${err} ${JSON.stringify(err, null, 2)}`);
134
- });
61
+ mutez: true,
62
+ }), tezos.wallet.batch());
63
+
64
+ export const performTransferOps = async (
65
+ tezos: TezosToolkit,
66
+ env: string,
67
+ contractsInfo: ContractInfo[],
68
+ ): Promise<BatchWalletOperation> => {
69
+ const batch = createBatchForTransfer(tezos, contractsInfo);
70
+ try {
71
+ const op = await batch.send();
72
+ await op.confirmation();
73
+ return op;
74
+ } catch (err) {
75
+ return handleOpsError(err, env);
76
+ }
135
77
  };
136
78
 
137
- export const transfer = async (parsedArgs: Opts): Promise<void> => {
79
+ const prepContractInfoForDisplay = (tezos: TezosToolkit, contractInfo: ContractInfo): TableRow => {
80
+ return {
81
+ contractAlias: contractInfo.contractAlias,
82
+ contractAddress: contractInfo.contractAddress,
83
+ parameter: contractInfo.parameter,
84
+ entrypoint: contractInfo.entrypoint,
85
+ mutezTransfer: contractInfo.mutezTransfer.toString(),
86
+ destination: tezos.rpc.getRpcUrl(),
87
+ };
88
+ };
89
+
90
+ const transfer = async (parsedArgs: Opts): Promise<void> => {
138
91
  const env = getCurrentEnvironmentConfig(parsedArgs);
139
- if (!env) return sendAsyncErr(`There is no environment called ${parsedArgs.env} in your config.json.`);
92
+ if (!env) return sendAsyncErr(`There is no environment called ${parsedArgs.env} in your config.json`);
140
93
  try {
141
- const tezos = await configureTezosToolKit(parsedArgs, env);
142
- const contractInfo = await getContractInfo(parsedArgs, env, tezos);
143
- await performTransferOp(tezos, contractInfo, parsedArgs);
144
- return sendJsonRes([contractInfo]);
94
+ const [envType, nodeConfig] = await getEnvTypeAndNodeConfig(parsedArgs, env);
95
+ const tezos = await (envType === 'Network'
96
+ ? configureToolKitForNetwork(parsedArgs, nodeConfig, parsedArgs.sender)
97
+ : configureToolKitForSandbox(nodeConfig, parsedArgs.sender));
98
+
99
+ const contractInfo = await getContractInfo(parsedArgs, env);
100
+
101
+ await performTransferOps(tezos, getCurrentEnvironment(parsedArgs), [contractInfo]);
102
+
103
+ const contractInfoForDisplay = prepContractInfoForDisplay(tezos, contractInfo);
104
+ return sendJsonRes([contractInfoForDisplay]);
145
105
  } catch {
146
- return sendAsyncErr('No operations performed.');
106
+ return sendAsyncErr('No operations performed');
147
107
  }
148
108
  };
149
109