-
Notifications
You must be signed in to change notification settings - Fork 17
/
bakerAdd.ts
118 lines (103 loc) · 3.31 KB
/
bakerAdd.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
import {
AccountAddress,
AccountTransaction,
AccountTransactionHeader,
AccountTransactionType,
signTransaction,
TransactionExpiry,
CcdAmount,
generateBakerKeys,
ConfigureBakerPayload,
OpenStatus,
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
Options
--help, -h Displays this message
--stake, -s The amount of stake to delegate in microccd, defaults to the minimum 14,000,000,000
--endpoint, -e Specify endpoint of the form "address:port", defaults to localhost:20000
`,
{
importMeta: import.meta,
flags: {
walletFile: {
type: 'string',
alias: 'w',
isRequired: true,
},
stake: {
type: 'number',
alias: 's',
default: 14000000000,
},
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 an account can be configured to
* be a baker.
*/
(async () => {
// #region documentation-snippet
// Read wallet-file
const walletFile = readFileSync(cli.flags.walletFile, 'utf8');
const wallet = parseWallet(walletFile);
const sender = AccountAddress.fromBase58(wallet.value.address);
const signer = buildAccountSigner(wallet);
const header: AccountTransactionHeader = {
expiry: TransactionExpiry.futureMinutes(60),
nonce: (await client.getNextAccountNonce(sender)).nonce,
sender,
};
const bakerKeys = generateBakerKeys(sender);
const configureBakerPayload: ConfigureBakerPayload = {
stake: CcdAmount.fromMicroCcd(cli.flags.stake),
restakeEarnings: true,
openForDelegation: OpenStatus.OpenForAll,
keys: bakerKeys,
metadataUrl: 'www.url.for.metadata',
transactionFeeCommission: 10000,
bakingRewardCommission: 10000,
finalizationRewardCommission: 100000,
};
const configureBakerAccountTransaction: AccountTransaction = {
header: header,
payload: configureBakerPayload,
type: AccountTransactionType.ConfigureBaker,
};
// Sign transaction
const signature = await signTransaction(
configureBakerAccountTransaction,
signer
);
const transactionHash = await client.sendAccountTransaction(
configureBakerAccountTransaction,
signature
);
console.log('Transaction submitted, waiting for finalization...');
const status = await client.waitForTransactionFinalization(transactionHash);
console.dir(status, { depth: null, colors: true });
// #endregion documentation-snippet
})();