-
Notifications
You must be signed in to change notification settings - Fork 17
/
deployModule.ts
112 lines (97 loc) · 3.25 KB
/
deployModule.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
import {
AccountAddress,
AccountTransaction,
AccountTransactionHeader,
AccountTransactionSignature,
AccountTransactionType,
DeployModulePayload,
signTransaction,
TransactionExpiry,
parseWallet,
buildAccountSigner,
} from '@concordium/web-sdk';
import { ConcordiumGRPCNodeClient } from '@concordium/web-sdk/nodejs';
import { credentials } from '@grpc/grpc-js';
import { readFileSync } from 'node:fs';
import { parseEndpoint } from '../shared/util.js';
import meow from 'meow';
const cli = meow(
`
Usage
$ yarn run-example <path-to-this-file> [options]
Required
--wallet-file, -w A path to a wallet export file from a Concordium wallet
--module-file, -m The filepath to the smart contract module
Options
--help, -h Displays this message
--endpoint, -e Specify endpoint of the form "address:port", defaults to localhost:20000
`,
{
importMeta: import.meta,
flags: {
walletFile: {
type: 'string',
alias: 'w',
isRequired: true,
},
moduleFile: {
type: 'string',
alias: 'm',
isRequired: true,
},
endpoint: {
type: 'string',
alias: 'e',
default: 'localhost:20000',
},
},
}
);
const [address, port] = parseEndpoint(cli.flags.endpoint);
const client = new ConcordiumGRPCNodeClient(
address,
Number(port),
credentials.createInsecure()
);
/**
* The following example demonstrates how a module can be deployed.
*/
(async () => {
// #region documentation-snippet
const walletFile = readFileSync(cli.flags.walletFile, 'utf8');
const wallet = parseWallet(walletFile);
const sender = AccountAddress.fromBase58(wallet.value.address);
// Get the wasm file as a buffer.
const wasmModule = Buffer.from(readFileSync(cli.flags.moduleFile));
// Note that if built using cargo-concordium `1.0.0`, the version should be added
// to the payload. In `2.0.0` and newer, the version is prepended into the module
// itself. To deploy a V0 module, which has been built with cargo-concordium
// version below 2, you should add the version field to the payload.
const deployModule: DeployModulePayload = {
source: wasmModule,
};
const header: AccountTransactionHeader = {
expiry: TransactionExpiry.futureMinutes(60),
nonce: (await client.getNextAccountNonce(sender)).nonce,
sender,
};
const deployModuleTransaction: AccountTransaction = {
header,
payload: deployModule,
type: AccountTransactionType.DeployModule,
};
// Sign transaction
const signer = buildAccountSigner(wallet);
const signature: AccountTransactionSignature = await signTransaction(
deployModuleTransaction,
signer
);
const transactionHash = await client.sendAccountTransaction(
deployModuleTransaction,
signature
);
console.log('Transaction submitted, waiting for finalization...');
const status = await client.waitForTransactionFinalization(transactionHash);
console.dir(status, { depth: null, colors: true });
// #endregion documentation-snippet
})();