Setup Stake Pool
Stake Pool setup with cardanocli-js
Find out more about cardanocli-js here.

Dependencies

Install Node.js

Download | Node.js
Node.js

Install Node.js on Ubuntu

1
sudo snap install node --classic
Copied!

Install cardano-node

Setup Environment

Install cardanocli-js:
1
mkdir ~/node
2
cd ~/node
3
npm install cardanocli-js
Copied!
Download required config files for the cardano node. Make sure you are in the ~/node directory:
Mainnet
Testnet
1
mkdir files
2
cd files
3
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/mainnet-config.json
4
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/mainnet-byron-genesis.json
5
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/mainnet-shelley-genesis.json
6
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/mainnet-topology.json
Copied!
1
mkdir files
2
cd files
3
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/testnet-config.json
4
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/testnet-byron-genesis.json
5
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/testnet-shelley-genesis.json
6
wget https://hydra.iohk.io/job/Cardano/cardano-node/cardano-deployment/latest-finished/download/1/testnet-topology.json
Copied!
Now run the node in order to sync the Cardano blockchain. Execute from ~/node:
1
cardano-node run \
2
--topology files/mainnet-topology.json \
3
--database-path db \
4
--socket-path db/socket \
5
--host-addr 0.0.0.0 \
6
--port 3000 \
7
--config files/mainnet-config.json
Copied!
After the node is in sync we can start with the instantiation of cardanocli-js. Save the following code in a file called cardanocli.js under ~/node
Mainnet
Testnet
1
const os = require("os");
2
const path = require("path");
3
const fetch = require("sync-fetch");
4
​
5
const dir = path.join(os.homedir(), "node");
6
​
7
const cardanocliJs = new CardanocliJs({
8
era: "mary", //current era
9
dir: dir, // working directory
10
socketPath: path.join(dir,"db","socket"),
11
shelleyGenesisPath: path.join(dir,"files","mainnet-shelley-genesis.json")
12
});
13
​
14
const createWallet = (account) => {
15
cardanocliJs.addressKeyGen(account);
16
cardanocliJs.stakeAddressKeyGen(account);
17
cardanocliJs.stakeAddressBuild(account);
18
cardanocliJs.addressBuild(account);
19
return cardanocliJs.wallet(account);
20
};
21
​
22
const registerWallet = (wallet) => {
23
const account = wallet.name;
24
const keyDeposit = cardanocliJs.queryProtocolParameters().keyDeposit;
25
const stakeCert = cardanocliJs.stakeAddressRegistrationCertificate(account);
26
const balance = wallet.balance().amount.lovelace;
27
28
const tx = {
29
txIn: wallet.balance().utxo,
30
txOut: [
31
{ address: wallet.paymentAddr, amount: { lovelace: balance - keyDeposit } },
32
],
33
certs: [stakeCert],
34
witnessCount: 2,
35
};
36
const txBodyRaw = cardanocliJs.transactionBuildRaw(tx); //create raw body
37
const fee = cardanocliJs.transactionCalculateMinFee({
38
...tx,
39
txBody: txBodyRaw,
40
}); //calculate fees for the raw body
41
tx.txOut[0].amount.lovelace -= fee; //subtract the required Tx fees
42
const txBody = cardanocliJs.transactionBuildRaw({ ...tx, fee }); //final body with fees included
43
const txSigned = cardanocliJs.transactionSign({
44
txBody,
45
signingKeys: [
46
wallet.payment.skey,
47
wallet.stake.skey,
48
],
49
});
50
​
51
return txSigned;
52
};
53
​
54
const createPool = (name) => {
55
cardanocliJs.nodeKeyGenKES(name);
56
cardanocliJs.nodeKeyGen(name);
57
cardanocliJs.nodeIssueOpCert(name);
58
cardanocliJs.nodeKeyGenVRF(name);
59
return cardanocliJs.pool(name);
60
};
61
​
62
const registerPool = (pool, wallet, data) => {
63
const name = pool.name;
64
const poolId = cardanocliJs.stakePoolId(name);
65
const poolCert = cardanocliJs.stakePoolRegistrationCertificate(name, data);
66
const delegCert = cardanocliJs.stakeAddressDelegationCertificate(
67
wallet.name,
68
poolId
69
);
70
const poolDeposit = cardanocliJs.queryProtocolParameters().poolDeposit;
71
const tx = {
72
txIn: wallet.balance().utxo,
73
txOut: [
74
{
75
address: wallet.paymentAddr,
76
amount: { lovelace: wallet.balance().amount.lovelace - poolDeposit },
77
},
78
],
79
witnessCount: 3,
80
certs: [poolCert, delegCert],
81
};
82
const txBodyRaw = cardanocliJs.transactionBuildRaw(tx);
83
const fee = cardanocliJs.transactionCalculateMinFee({
84
...tx,
85
txBody: txBodyRaw,
86
});
87
tx.txOut[0].amount.lovelace -= fee;
88
const txBody = cardanocliJs.transactionBuildRaw({ ...tx, fee });
89
const txSigned = cardanocliJs.transactionSign({
90
txBody,
91
signingKeys: [wallet.payment.skey, wallet.stake.skey, pool.node.skey],
92
});
93
return txSigned;
94
};
95
​
96
module.export = {
97
cardanocliJs,
98
fetch,
99
createWallet,
100
registerWallet,
101
createPool,
102
registerPool
103
}
Copied!
1
const os = require("os");
2
const path = require("path");
3
const fetch = require("sync-fetch");
4
​
5
const dir = path.join(os.homedir(), "node");
6
​
7
const cardanocliJs = new CardanocliJs({
8
era: "mary", //current era
9
network: "testnet-magic 1097911063",
10
dir: dir, // working directory,
11
socketPath: path.join(dir,"db","socket"),
12
shelleyGenesisPath: path.join(dir,"files","testnet-shelley-genesis.json")
13
});
14
​
15
const createWallet = (account) => {
16
cardanocliJs.addressKeyGen(account);
17
cardanocliJs.stakeAddressKeyGen(account);
18
cardanocliJs.stakeAddressBuild(account);
19
cardanocliJs.addressBuild(account);
20
return cardanocliJs.wallet(account);
21
};
22
​
23
const registerWallet = (wallet) => {
24
const account = wallet.name;
25
const keyDeposit = cardanocliJs.queryProtocolParameters().keyDeposit;
26
const stakeCert = cardanocliJs.stakeAddressRegistrationCertificate(account);
27
const balance = wallet.balance().amount.lovelace;
28
29
const tx = {
30
txIn: wallet.balance().utxo,
31
txOut: [
32
{ address: wallet.paymentAddr, amount: { lovelace: balance - keyDeposit } },
33
],
34
certs: [stakeCert],
35
witnessCount: 2,
36
};
37
const txBodyRaw = cardanocliJs.transactionBuildRaw(tx); //create raw body
38
const fee = cardanocliJs.transactionCalculateMinFee({
39
...tx,
40
txBody: txBodyRaw,
41
}); //calculate fees for the raw body
42
tx.txOut[0].amount.lovelace -= fee; //subtract the required Tx fees
43
const txBody = cardanocliJs.transactionBuildRaw({ ...tx, fee }); //final body with fees included
44
const txSigned = cardanocliJs.transactionSign({
45
txBody,
46
signingKeys: [
47
wallet.payment.skey,
48
wallet.stake.skey,
49
],
50
});
51
​
52
return txSigned;
53
};
54
​
55
const createPool = (name) => {
56
cardanocliJs.nodeKeyGenKES(name);
57
cardanocliJs.nodeKeyGen(name);
58
cardanocliJs.nodeIssueOpCert(name);
59
cardanocliJs.nodeKeyGenVRF(name);
60
return cardanocliJs.pool(name);
61
};
62
​
63
const registerPool = (pool, wallet, data) => {
64
const name = pool.name;
65
const poolId = cardanocliJs.stakePoolId(name);
66
const poolCert = cardanocliJs.stakePoolRegistrationCertificate(name, data);
67
const delegCert = cardanocliJs.stakeAddressDelegationCertificate(
68
wallet.name,
69
poolId
70
);
71
const poolDeposit = cardanocliJs.queryProtocolParameters().poolDeposit;
72
const tx = {
73
txIn: wallet.balance().utxo,
74
txOut: [
75
{
76
address: wallet.paymentAddr,
77
amount: { lovelace: wallet.balance().amount.lovelace - poolDeposit },
78
},
79
],
80
witnessCount: 3,
81
certs: [poolCert, delegCert],
82
};
83
const txBodyRaw = cardanocliJs.transactionBuildRaw(tx);
84
const fee = cardanocliJs.transactionCalculateMinFee({
85
...tx,
86
txBody: txBodyRaw,
87
});
88
tx.txOut[0].amount.lovelace -= fee;
89
const txBody = cardanocliJs.transactionBuildRaw({ ...tx, fee });
90
const txSigned = cardanocliJs.transactionSign({
91
txBody,
92
signingKeys: [wallet.payment.skey, wallet.stake.skey, pool.node.skey],
93
});
94
return txSigned;
95
};
96
​
97
module.export = {
98
cardanocliJs,
99
createWallet,
100
registerWallet,
101
createPool,
102
registerPool
103
}
Copied!

Stake Pool Registration

With the command nodewe open the Node.js interpreter. This is where we are going to register the pool. We are going to register a single owner pool in this example:
1
$ node
2
Welcome to Node.js v14.15.0.
3
Type ".help" for more information.
4
>
Copied!

Register Wallet

1
> const c = require("./cardanocli.js")
2
> const cardanocliJs = c.cardanocliJs
3
>
4
> const wallet = c.createWallet("Main")
5
> wallet.paymentAddr
6
addrdfhuj239hfj832f32jf2i3jf3hf923
Copied!
1
> wallet.balance().amount
2
{ lovelace: 5000000 }
Copied!
1
> const tx_wallet = c.registerWallet(wallet)
2
> const txHash_wallet = cardanocliJs.transactionSubmit(tx_wallet)
3
> txHash_wallet
4
hfs98dh823hf9823h93f2h9
Copied!

Register Pool

1
> const pool = c.createPool("MyPool")
2
> const metaUrl = "https://pool.com/poolmeta.json"
3
> const poolData = {
4
pledge: cardanocliJs.toLovelace(100),
5
margin: 0.015
6
cost: cardanocliJs.toLovelace(340),
7
owners: [wallet.stake.vkey],
8
rewardAccount: wallet.stake.vkey,
9
relays: [
10
{host: "relay.one.example", port: 3001},
11
{ip: "123.456.78.9", port: 3001}
12
],
13
url: metaUrl,
14
metaHash: cardanocliJs.stakePoolMetadataHash(c.fetch(metaUrl).text())
15
}
16
> const tx_pool = registerPool(pool,wallet,poolData)
17
> const txHash_pool = cardanocliJs.transactionSubmit(tx_pool)
18
> txHash_pool
19
hf9h9832hfu9jhuf9hewfhe
Copied!
Last modified 7mo ago