-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-ada-submit-tx.js
More file actions
223 lines (184 loc) · 6.77 KB
/
get-ada-submit-tx.js
File metadata and controls
223 lines (184 loc) · 6.77 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import fs from 'fs';
import fetch from 'node-fetch';
import {execSync} from 'child_process';
main().catch(console.error);
function cardano_cli(...args) {
// Get absolute path for proper volume mounting
const currentDir = process.cwd();
// Add user permissions to ensure Docker can write files
const userId = execSync('id -u').toString().trim();
const groupId = execSync('id -g').toString().trim();
const command = `docker run --rm -v ${currentDir}:/workspace --user ${userId}:${groupId} --entrypoint /bin/cardano-cli ghcr.io/intersectmbo/cardano-node:10.4.1 ${args.join(' ')}`;
console.log(`Executing: ${command}`);
try {
const result = execSync(command, { encoding: 'utf8' });
return result.trim();
} catch (error) {
console.error(`Error executing cardano-cli: ${error.message}`);
throw error;
}
}
async function main() {
const dataDir = "data";
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
const vkeyFile = `${dataDir}/key.vkey`;
const skeyFile = `${dataDir}/key.skey`;
const faucetTxHashFile = `${dataDir}/faucet-txHash.txt`;
const protocolParamsFile = `${dataDir}/protocol.json`;
const txRawFile = `${dataDir}/tx.raw`;
const txSignedFile = `${dataDir}/tx.signed`;
const url = `http://localhost`
await downloadFile(`${url}:5000/protocol.json`, protocolParamsFile)
// Request funds from the faucet.
// Make sure data directory exists inside Docker container
console.log(`Generating keys in ${dataDir}...`);
cardano_cli('conway', 'address', 'key-gen',
`--verification-key-file /workspace/${vkeyFile}`,
`--signing-key-file /workspace/${skeyFile}`
);
console.log(`Key generation command completed.`);
// Check if key files exist before proceeding
if (!fs.existsSync(vkeyFile) || !fs.existsSync(skeyFile)) {
console.error(`Key files not found at ${vkeyFile} or ${skeyFile}. Please check the path.`);
process.exit(1);
}
// derive pubKeyHashHex
const pubKeyHashHex = cardano_cli('conway', 'address', 'key-hash',
`--payment-verification-key-file /workspace/${vkeyFile}`
);
const faucetTxHash = await get1000tada(`${url}:8000`, pubKeyHashHex);
fs.writeFileSync(faucetTxHashFile, faucetTxHash);
const faucetTxTime = await awaitTxTimeWithKupo(`${url}:1442`, faucetTxHash);
console.log(`$Faucet tx ${faucetTxHash} added to block after ${faucetTxTime} seconds.`);
// derive address
const addr = cardano_cli('conway', 'address', 'build',
`--payment-verification-key-file /workspace/${vkeyFile}`,
`--testnet-magic 42`
);
// request utxos with kupo
const utxos = await requestKupoUtxos(`${url}:1442`, addr);
console.log(`utxos:`)
console.log(utxos)
const inputTxId = utxos[0].transaction_id;
const inputLovelace = utxos[0].value.coins;
console.log(`take utxo with txId '${inputTxId}' and ${inputLovelace} lovelace for simple pay transaction`);
let fee = 100000;
cardano_cli('conway', 'transaction', 'build-raw',
`--tx-in ${inputTxId}#0`,
`--tx-out ${addr}+3000000`,
`--tx-out ${addr}+${inputLovelace - 3000000 - fee}`,
`--fee ${fee}`,
`--out-file /workspace/${txRawFile}`
)
// download protocol parameters
await downloadFile(`${url}:5000/protocol.json`, protocolParamsFile)
// correct fee
fee = cardano_cli('conway', 'transaction', 'calculate-min-fee',
`--tx-body-file /workspace/${txRawFile}`,
`--witness-count 1`,
`--protocol-params-file /workspace/${protocolParamsFile}`
)
// fee in lovelace
fee = fee.toString().split(" ")[0]
console.log(fee)
// rebuild transaction
cardano_cli('conway', 'transaction', 'build-raw',
`--tx-in ${inputTxId}#0`,
`--tx-out ${addr}+3000000`,
`--tx-out ${addr}+${10000000000 - 3000000 - fee}`,
`--fee ${fee}`,
`--out-file /workspace/${txRawFile}`
)
// sign transaction
cardano_cli('conway', 'transaction', 'sign',
`--tx-body-file /workspace/${txRawFile}`,
`--signing-key-file /workspace/${skeyFile}`,
`--out-file /workspace/${txSignedFile}`
)
// submit TX
const txSigned = JSON.parse(fs.readFileSync(txSignedFile));
const submitResponse = await requestOgmios(`${url}:1337`, "submitTransaction", { transaction : {cbor : txSigned.cborHex}});
console.log(submitResponse)
console.log(`tx is submitted`)
const txTime = await awaitTxTimeWithKupo(`${url}:1442`, submitResponse.result.transaction.id);
console.log(`${submitResponse.result.transaction.id} added to block after ${txTime} seconds`)
};
async function get1000tada(url, pubKeyHashHex) {
console.log(`request tada from ${url} for ${pubKeyHashHex}`);
const faucetResponse = await fetch(url, {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({pubKeyHashHex : pubKeyHashHex})
});
console.log(`faucet response: ${faucetResponse.status} ${faucetResponse.statusText}`);
const data = await faucetResponse.json();
console.log(data);
return data.message.txHash;
};
// request awaitTxTime with kupo
async function awaitTxTimeWithKupo(url, txHash) {
console.log(`wait until ${txHash} added to block ...`)
const start = Date.now();
while (true) {
try {
const resp = await fetch(`${url}/matches/*@${txHash}`);
const body = await resp.json();
if (body.length > 0) return (Date.now() - start)/1000;
} catch (err) {
throw Error ("kupo requests error")
}
await new Promise((resolve) => setTimeout(() => resolve(), 4000))
}
};
//request utxos with kupo
async function requestKupoUtxos(url, addr) {
console.log(`request utxos with kupo for ${addr}`)
const resp = await fetch(`${url}/matches/${addr}`);
const body = await resp.json();
return body
};
// ogmios interface
async function requestOgmios(url, method, params) {
const resp = await fetch(`${url}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
jsonrpc: "2.0",
method,
params : params})
});
const body = await resp.json();
return body
};
function downloadFile(url, fpath) {
console.log(`download file from ${url} to ${fpath}`);
return new Promise((resolve, reject) => {
fetch(url)
.then(res => {
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}
const fileStream = fs.createWriteStream(fpath);
fileStream.on('error', error => {
reject(error);
});
res.body.on('error', error => {
fileStream.destroy();
reject(error);
});
fileStream.on('finish', () => {
fileStream.close();
console.log(`download to ${fpath} completed!`);
resolve();
});
res.body.pipe(fileStream);
})
.catch(error => {
reject(error);
});
});
}