cln-plugin 0.6.0

A CLN plugin library. Write your plugin in Rust.
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
#include "config.h"
#include <bitcoin/psbt.h>
#include <bitcoin/script.h>
#include <ccan/array_size/array_size.h>
#include <ccan/err/err.h>
#include <ccan/tal/str/str.h>
#include <ccan/tal/tal.h>
#include <common/addr.h>
#include <common/json_param.h>
#include <common/json_parse.h>
#include <common/json_stream.h>
#include <common/psbt_open.h>
#include <common/splice_script.h>
#include <inttypes.h>
#include <plugins/spender/splice.h>

struct abort_pkg {
	struct splice_cmd *splice_cmd;
	enum jsonrpc_errcode code;
	char *str;
};

static const char *cmd_state_string(enum splice_cmd_state state)
{
	switch (state) {
		case SPLICE_CMD_NONE:
			return "                    ";
		case SPLICE_CMD_PENDING:
			return "       PENDING      ";
		case SPLICE_CMD_INIT:
			return "        INIT        ";
		case SPLICE_CMD_UPDATE:
			return "       UPDATE       ";
		case SPLICE_CMD_UPDATE_NEEDS_CHANGES:
			return "UPDATE_NEEDS_CHANGES";
		case SPLICE_CMD_UPDATE_DONE:
			return "     UPDATE_DONE    ";
		case SPLICE_CMD_RECVED_SIGS:
			return "     RECVED_SIGS    ";
		case SPLICE_CMD_DONE:
			return "        DONE        ";
	}
	return NULL;
}

static void add_to_debug_log(struct splice_cmd *scmd, const char *phase)
{
	char **log = &scmd->debug_log;
	if (!*log)
		return;

	tal_append_fmt(log, "#%d: (%s)\n", ++scmd->debug_counter, phase);

	for (size_t i = 0; i < tal_count(scmd->actions); i++) {
		struct splice_script_result *action = scmd->actions[i];
		struct splice_cmd_action_state *state = scmd->states[i];
		bool simulate_wallet_amount = false;
		bool hide_fee = false;

		if (action->onchain_wallet && action->pays_fee) {
			if (amount_sat_is_zero(action->out_sat)) {
				simulate_wallet_amount = true;
				action->out_sat = scmd->needed_funds;
			} else {
				hide_fee = true;
				action->pays_fee = false;
			}
		}

		tal_append_fmt(log, "[%s] %s\n",
			       cmd_state_string(state->state),
			       splice_to_string(tmpctx, action));

		if (simulate_wallet_amount)
			action->out_sat = AMOUNT_SAT(0);

		if (hide_fee)
			action->pays_fee = true;
	}
}

static void debug_log_to_json(struct json_stream *response,
			      const char *debug_log)
{
	char **lines = tal_strsplit(tmpctx, debug_log, "\n", STR_NO_EMPTY);

	for (size_t i = 0; lines[i]; i++)
		json_add_string(response, NULL, lines[i]);
}

static struct command_result *make_error(struct command *cmd,
					 struct abort_pkg *abort_pkg,
					 const char *phase)
{
	struct splice_cmd *splice_cmd = abort_pkg->splice_cmd;
	char *str = abort_pkg->str;
	struct json_stream *response = jsonrpc_stream_fail(cmd,
							   abort_pkg->code,
							   str ?: phase);

	if (splice_cmd->debug_log) {
		json_array_start(response, "log");
		debug_log_to_json(response, splice_cmd->debug_log);
		json_array_end(response);
	}

	tal_free(abort_pkg);

	return command_finished(cmd, response);
}

static struct command_result *unreserve_get_result(struct command *cmd,
						   const char *methodname,
						   const char *buf,
						   const jsmntok_t *result,
						   struct abort_pkg *abort_pkg)
{
	struct splice_cmd *splice_cmd = abort_pkg->splice_cmd;
	struct json_stream *response;
	struct bitcoin_tx *tx;
	u8 *tx_bytes;

	if (splice_cmd->wetrun) {

		response = jsonrpc_stream_success(cmd);
		if (splice_cmd->psbt) {
			json_add_psbt(response, "psbt", splice_cmd->psbt);

			tx = bitcoin_tx_with_psbt(tmpctx, splice_cmd->psbt);
			tx_bytes = linearize_tx(tmpctx, tx);
			json_add_hex(response, "tx", tx_bytes,
				     tal_bytelen(tx_bytes));
			json_add_txid(response, "txid",
				      &splice_cmd->final_txid);
		}

		if (splice_cmd->debug_log) {
			json_array_start(response, "log");
			debug_log_to_json(response, splice_cmd->debug_log);
			json_array_end(response);
		}

		tal_free(abort_pkg);
		return command_finished(cmd, response);
	}

	return make_error(cmd, abort_pkg, "unreserve_get_result");
}

static struct command_result *abort_get_result(struct command *cmd,
					       const char *methodname,
					       const char *buf,
					       const jsmntok_t *result,
					       struct abort_pkg *abort_pkg)
{
	struct out_req *req;
	struct splice_cmd *splice_cmd = abort_pkg->splice_cmd;

	plugin_log(cmd->plugin, LOG_DBG,
		   "unreserveinputs(psbt:%p)", splice_cmd->psbt);

	if (!splice_cmd->psbt)
		return make_error(cmd, abort_pkg, "abort_get_result");

	req = jsonrpc_request_start(cmd, "unreserveinputs",
				    unreserve_get_result, forward_error,
				    abort_pkg);

	json_add_psbt(req->js, "psbt", splice_cmd->psbt);

	return send_outreq(req);
}

static struct command_result *do_fail(struct command *cmd,
				      struct splice_cmd *splice_cmd,
				      enum jsonrpc_errcode code,
				      const char *str TAKES)
{
	struct out_req *req;
	struct abort_pkg *abort_pkg;
	size_t added;

	/* If we encounter an error, wetrun is canceled */
	splice_cmd->wetrun = false;

	plugin_log(cmd->plugin, LOG_DBG,
		   "splice_error(psbt:%p, splice_cmd:%p, str: %s)",
		   splice_cmd->psbt, splice_cmd, str ?: "");

	abort_pkg = tal(cmd->plugin, struct abort_pkg);
	abort_pkg->splice_cmd = tal_steal(abort_pkg, splice_cmd);
	abort_pkg->str = tal_strdup(abort_pkg, str);
	abort_pkg->code = code;

	req = jsonrpc_request_start(cmd, "abort_channels",
				    abort_get_result, forward_error, abort_pkg);

	added = 0;
	json_array_start(req->js, "channel_ids");
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		if (splice_cmd->actions[i]->channel_id) {
			added++;
			json_add_channel_id(req->js, NULL,
					    splice_cmd->actions[i]->channel_id);
		}
	}
	json_array_end(req->js);

	if (!added) {
		plugin_log(cmd->plugin, LOG_DBG,
			   "No channels were stfu'ed, skipping to unreserve"
			   " (psbt:%p)", splice_cmd->psbt);
		return abort_get_result(cmd, NULL, NULL, NULL, abort_pkg);
	}

	return send_outreq(req);
}

static struct command_result *splice_error(struct command *cmd,
					   const char *methodname,
					   const char *buf,
					   const jsmntok_t *error,
					   struct splice_cmd *splice_cmd)
{
	char *str = tal_fmt(NULL, "%s: %.*s",
			    methodname,
			    error->end - error->start,
			    buf + error->start);

	return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS, take(str));
}

struct splice_index_pkg {
	struct splice_cmd *splice_cmd;
	size_t index;
};

static struct command_result *splice_error_pkg(struct command *cmd,
					       const char *methodname,
					       const char *buf,
					       const jsmntok_t *error,
					       struct splice_index_pkg *pkg)
{
	struct command_result *res = splice_error(cmd, methodname, buf, error, pkg->splice_cmd);

	tal_free(pkg);

	return res;
}

static struct splice_script_result *input_wallet(struct splice_cmd *splice_cmd,
						 size_t *index)
{
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		struct splice_script_result *action = splice_cmd->actions[i];
		if (action->onchain_wallet
		    && !action->in_ppm
		    && amount_sat_is_zero(action->in_sat)) {
			if (index)
				*index = i;
			return action;
		}
	}
	return NULL;
}

static struct splice_script_result *output_wallet(struct splice_cmd *splice_cmd)
{
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		struct splice_script_result *action = splice_cmd->actions[i];
		if (!action->onchain_wallet)
			continue;
		if (action->in_ppm || !amount_sat_is_zero(action->in_sat))
			return action;
	}
	return NULL;
}

static struct splice_script_result *fee_action(struct splice_cmd *splice_cmd)
{
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		struct splice_script_result *action = splice_cmd->actions[i];
		if (action->pays_fee)
			return action;
	}
	return NULL;
}

static struct command_result *notice_missing_funds(struct command *cmd,
						   struct splice_cmd *splice_cmd,
						   struct amount_sat *missing_funds,
						   struct amount_sat funds_needed,
						   struct amount_sat *funds_available)
{
	if (!amount_sat_add(missing_funds, *missing_funds, funds_needed))
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				    "Unable to add for missing_funds");
	if (!amount_sat_sub(missing_funds, *missing_funds, *funds_available))
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				    "Unable to sub for missing_funds");
	*funds_available = AMOUNT_SAT(0);
	plugin_log(cmd->plugin, LOG_DBG, "  missing_funds detected, now %s",
		   fmt_amount_sat(tmpctx, *missing_funds));
	return NULL;
}

#define NOTICE_MISSING(missing_funds, funds_needed, funds_available) \
	{ \
		struct command_result *result; \
		result = notice_missing_funds(cmd, splice_cmd, missing_funds, \
					      funds_needed, funds_available); \
		if (result) \
			return result; \
	}

 /* Because wallets increase the fee paying for the fee (due to inputs
  * increasing transaction size), this process must be inherently recursive.
  *
  * Adding to the complication is that wallets may take a percentage of total
  * funds in the splice before accomodating the fee.
  *
  * Finally, any percentage based receiver or contributor of funds may also be
  * responsible for the fee.
  *
  * Supporting all this means we need to build a solver that can be executed
  * repeatidly, solving what can be solved on each pass. Some answers inherently
  * require answers from prior passes.
  *
  * This method can be called repeatidly and it will solve more of the splice
  * each time. It must be called once at the end with `final_pass` set to true
  * to resolve ambigious percentages and place fees in some cases.
  *
  * After calling with `final_pass` you are free to call it again and in this
  * mode it works as an error checker, filling the `extra_funds`,
  * `missing_funds`, and `non_wallet_demand` values for verification.
  */
static struct command_result *calc_in_ppm_and_fee(struct command *cmd,
						  struct splice_cmd *splice_cmd,
						  struct amount_sat onchain_fee,
						  bool final_pass,
						  struct amount_sat *extra_funds,
						  struct amount_sat *missing_funds,
						  struct amount_sat *non_wallet_demand)
{
	struct splice_script_result *action, *last_ppm_action;
	struct amount_sat out_sats;
	bool sub_fee_from_general;
	struct needed_sats;
	int ppm_actions;
	struct splice_script_result *in_wallet, *out_wallet;


	add_to_debug_log(splice_cmd, "calc_in_ppm_and_fee");

	plugin_log(cmd->plugin, LOG_DBG, "calc_in_ppm_and_fee starting"
		   " calculations%s", final_pass ? " FINALIZING PASS" : "");

	out_sats = splice_cmd->initial_funds;
	sub_fee_from_general = true;

	in_wallet = input_wallet(splice_cmd, NULL);
	out_wallet = output_wallet(splice_cmd);

	/* First add all sats going into general fund */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		if (action->pays_fee) {
			sub_fee_from_general = false;
			/* Has the onchain fee been finalized? */
			if (action->onchain_wallet) {
				if (!amount_sat_is_zero(action->out_sat)
				    || !amount_sat_is_zero(action->in_sat))
					sub_fee_from_general = true;
			}
			/* If we're the input wallet -- the fee may be finalized
			 * on the output wallet instead. Check there. */
			if (action == in_wallet && out_wallet) {
				if (!amount_sat_is_zero(out_wallet->out_sat)
				    || !amount_sat_is_zero(out_wallet->in_sat)) {
					sub_fee_from_general = true;
				}

			}
		}
		plugin_log(cmd->plugin, LOG_DBG, " plus %s (pays_fee %s, "
			   "out_ppm %u, out_sat %s, in_ppm %u, in_sat %s)",
			   fmt_amount_sat(tmpctx, action->out_sat),
			   action->pays_fee ? "yes" : "no",
			   action->out_ppm,
			   fmt_amount_sat(tmpctx, action->out_sat),
			   action->in_ppm,
			   fmt_amount_sat(tmpctx, action->in_sat));
		if (!amount_sat_add(&out_sats, out_sats, action->out_sat))
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Unable to add out_sats");
		if (action->out_ppm && !action->onchain_wallet)
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Unable to resolve out_ppm");
	}

	*non_wallet_demand = AMOUNT_SAT(0);
	*missing_funds = AMOUNT_SAT(0);

	/* Now take away all sats being spent by general fund */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		plugin_log(cmd->plugin, LOG_DBG, " minus %s",
			   fmt_amount_sat(tmpctx, action->in_sat));
		/* Subtract used funds from out_sats */
		if (!amount_sat_sub(&out_sats, out_sats, action->in_sat))
			NOTICE_MISSING(missing_funds, action->in_sat,
				       &out_sats);
		if (action->onchain_wallet)
			continue;
		/* Add up non_wallet_demand (needed for wallet out_ppm) */
		if (!amount_sat_add(non_wallet_demand, *non_wallet_demand,
				    action->in_sat))
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				       "Unable to add to non_wallet_demand");
	}

	/* Reduce non_wallet_demand by sats added from channels */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		if (!action->channel_id)
			continue;
		if (!amount_sat_sub(non_wallet_demand, *non_wallet_demand,
				    action->out_sat))
			*non_wallet_demand = AMOUNT_SAT(0);
	}

	/* If no one voulenteers to pay the fee, we take it out of the general
	 * fund. */
	if (sub_fee_from_general) {

		plugin_log(cmd->plugin, LOG_DBG, " remove %s fee from general"
			   " fund %s",
			   fmt_amount_sat(tmpctx, onchain_fee),
			   fmt_amount_sat(tmpctx, out_sats));

		if (!amount_sat_sub(&out_sats, out_sats, onchain_fee))
			NOTICE_MISSING(missing_funds, onchain_fee, &out_sats);

		if (!amount_sat_add(non_wallet_demand, *non_wallet_demand,
				    onchain_fee))
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				       "Unable to add to non_wallet_demand");
	}

	*extra_funds = out_sats;

	plugin_log(cmd->plugin, LOG_DBG, " general fund is %s",
		   fmt_amount_sat(tmpctx, out_sats));

	ppm_actions = 0;

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		struct amount_sat sat;
		action = splice_cmd->actions[i];
		if (action->in_ppm) {
			/* ppm percentage calculation:
			 * action->in_sat = out_sats * in_ppm / 1000000 */
			if (!amount_sat_mul(&sat, out_sats, action->in_ppm))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Unable to mul sats & in_ppm");

			sat = amount_sat_div(sat, 1000000);

			if (!amount_sat_add(&action->in_sat, action->in_sat, sat))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Unable to add sats & in_sat");
			if (!amount_sat_is_zero(sat) || final_pass) {
				plugin_log(cmd->plugin, LOG_DBG,
					   " resolving percentage, in_ppm calc"
					   " %u of %s = %s",
					   action->in_ppm,
					   fmt_amount_sat(tmpctx, out_sats),
					   fmt_amount_sat(tmpctx, sat));
				action->in_ppm = 0;
			}

			/* Remove used sats from extra_funds */
			if (!amount_sat_sub(extra_funds, *extra_funds, sat)) {
				/* If we can't do that then add to missing */
				NOTICE_MISSING(missing_funds, sat,
					       extra_funds);
			}

			ppm_actions++;
			last_ppm_action = action;
		}

		if (final_pass && action->pays_fee
		    && !amount_sat_is_zero(action->in_sat)) {
			plugin_log(cmd->plugin, LOG_DBG,
				   " subtracting fee of %s from %s",
				   fmt_amount_sat(tmpctx, onchain_fee),
				   fmt_amount_sat(tmpctx, action->in_sat));
			if (!amount_sat_sub(&action->in_sat, action->in_sat,
					    onchain_fee))
				NOTICE_MISSING(missing_funds, onchain_fee,
					       &action->in_sat);
			action->pays_fee = false;
		}

		if (action->pays_fee && !sub_fee_from_general) {
			plugin_log(cmd->plugin, LOG_DBG,
				   " action pays fee, so removing fee %s from"
				   " extra_funds %s",
				   fmt_amount_sat(tmpctx, onchain_fee),
				   fmt_amount_sat(tmpctx, *extra_funds));
			if (!amount_sat_sub(extra_funds, *extra_funds,
					    onchain_fee))
				NOTICE_MISSING(missing_funds, onchain_fee,
					       extra_funds);
		}

		/* Onchain wallet fees are handled seperately */
		if (action->onchain_wallet)
			continue;

		if (!final_pass)
			continue;

		/* If this item pays the fee, subtract it from either their
		 * in_sats or add it to out_sats. */
		if (action->pays_fee && !amount_sat_is_zero(action->in_sat)) {
			plugin_log(cmd->plugin, LOG_DBG, " sub fee %s",
				   fmt_amount_sat(tmpctx, onchain_fee));
			if (!amount_sat_sub(&action->in_sat, action->in_sat,
					    onchain_fee))
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Unable to sub fee from"
						    " item in_sat");
		}
		if (action->pays_fee && !amount_sat_is_zero(action->out_sat)) {
			plugin_log(cmd->plugin, LOG_DBG, "add fee %s",
				   fmt_amount_sat(tmpctx, onchain_fee));
			if (!amount_sat_add(&action->out_sat, action->out_sat,
					    onchain_fee))
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Unable to add fee to"
						    " item out_sat");
		}
	}

	/* Because of percentage based rounding, we can lose ~1 sat per
	 * percentage amount receiver. If extra sats is at or below 1 per
	 * receiver, we simply dump it in the last ppm receiver. */
	if (ppm_actions && !amount_sat_is_zero(*extra_funds)
	    && amount_sat_less_eq(*extra_funds, amount_sat(ppm_actions))) {
		plugin_log(cmd->plugin, LOG_DBG,
			   " placing %s lost during rounding",
			   fmt_amount_sat(tmpctx, *extra_funds));
		if (!amount_sat_add(&last_ppm_action->in_sat,
			       last_ppm_action->in_sat,
			       *extra_funds))
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				       "Failed to add extra sats");
		*extra_funds = AMOUNT_SAT(0);
	}

	plugin_log(cmd->plugin, LOG_DBG, "calc_in_ppm_and_fee finished."
		   " out_sats: %s, extra_funds: %s, missing_funds: %s,"
		   " non_wallet_demand: %s",
		   fmt_amount_sat(tmpctx, out_sats),
		   fmt_amount_sat(tmpctx, *extra_funds),
		   fmt_amount_sat(tmpctx, *missing_funds),
		   fmt_amount_sat(tmpctx, *non_wallet_demand));

	/*  validate result */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		if (!action->channel_id)
			continue;
		if (!amount_sat_is_zero(action->in_sat))
			continue;
		if (!amount_sat_is_zero(action->out_sat))
			continue;
		if (!amount_sat_is_zero(action->lease_sat))
			continue;
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				    "Each channel action must include non-zero"
				    " in sats, out sats, or lease sats.");
	}

	return NULL;
}

static struct command_result *continue_splice(struct command *cmd,
					      struct splice_cmd *splice_cmd);

static bool json_to_msat_to_sat(const char *buffer, const jsmntok_t *tok,
				struct amount_sat *sat)
{
	struct amount_msat msat;

	if (!json_to_msat(buffer, tok, &msat))
		return false;
	return amount_msat_to_sat(sat, msat);
}

static struct splice_script_result *make_wallet(struct splice_cmd *splice_cmd)
{
	struct splice_script_result *action;
	struct splice_cmd_action_state *state;

	action = talz(splice_cmd->actions, struct splice_script_result);
	state = talz(splice_cmd->states, struct splice_cmd_action_state);

	action->onchain_wallet = true;
	state->state = SPLICE_CMD_NONE;

	tal_arr_expand(&splice_cmd->actions, action);
	tal_arr_expand(&splice_cmd->states, state);

	return action;
}

static struct command_result *addpsbt_get_result(struct command *cmd,
			const char *methodname,
			const char *buf,
			const jsmntok_t *result,
			struct splice_index_pkg *pkg)
{
	struct splice_cmd *splice_cmd = pkg->splice_cmd;
	size_t index = pkg->index;
	struct splice_script_result *action = splice_cmd->actions[index];
	const jsmntok_t *tok;
	struct amount_sat excess_sat;
	struct splice_script_result *out_wallet;

	tal_free(pkg);
	tok = json_get_member(buf, result, "psbt");

	tal_free(splice_cmd->psbt);
	splice_cmd->psbt = json_to_psbt(splice_cmd, buf, tok);
	assert(splice_cmd->psbt);

	tok = json_get_member(buf, result, "excess_msat");
	if (tok) {
		if (!json_to_msat_to_sat(buf, tok, &excess_sat))
			return command_fail_badparam(cmd, "addpsbt", buf, tok,
						     "invalid excess_msat");

		if (!amount_sat_is_zero(excess_sat)) {
			if (!amount_sat_add(&action->out_sat, action->out_sat,
					   excess_sat))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Unable to add excess sats");

			plugin_log(cmd->plugin, LOG_DBG,
				   "Received input(s) with %s",
				   fmt_amount_sat(tmpctx, action->out_sat));

			out_wallet = output_wallet(splice_cmd);
			plugin_log(cmd->plugin, LOG_DBG,
				   "Adding excess sats back into out wallet %s"
				   " which already has %s",
				   fmt_amount_sat(tmpctx, excess_sat),
				   out_wallet
				   	? fmt_amount_sat(tmpctx,
				   		out_wallet->in_sat)
				   	: "(NO WALLET)");

			if (!out_wallet) {
				plugin_log(cmd->plugin, LOG_DBG, "Generating"
					   " output wallet.");
				out_wallet = make_wallet(splice_cmd);
			}

			if (out_wallet) {
				if (!amount_sat_add(&out_wallet->in_sat,
						       out_wallet->in_sat,
						       excess_sat))
					return do_fail(cmd, splice_cmd,
						       JSONRPC2_INVALID_PARAMS,
						       "Unable to add excess"
						       " sats to existing"
						       " wallet output");
			}
			else {
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Putting change back into same"
					       " wallet outpoint not yet"
					       " supported");
			}
		}
	}

	tok = json_get_member(buf, result, "emergency_sat");
	if (tok) {
		if (!amount_sat_is_zero(splice_cmd->emergency_sat))
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Internal error: two"
					    " emergency_sat");
		if (!json_to_msat_to_sat(buf, tok, &splice_cmd->emergency_sat))
			return command_fail_badparam(cmd, "addpsbt", buf, tok,
						     "invalid emergency_sat");
	}

	return continue_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *onchain_wallet_fund(struct command *cmd,
						  struct splice_cmd *splice_cmd,
						  size_t index,
						  struct amount_sat already_funded)
{
	struct splice_script_result *action = splice_cmd->actions[index];
	struct splice_cmd_action_state *state = splice_cmd->states[index];
	struct out_req *req;
	struct splice_index_pkg *pkg;
	struct amount_sat sats;
	const char *command;
	bool addinginputs = !amount_sat_is_zero(action->out_sat);

	pkg = tal(cmd->plugin, struct splice_index_pkg);
	pkg->splice_cmd = splice_cmd;
	pkg->index = index;

	command = "addpsbtoutput";
	if (addinginputs) {
		command = "addpsbtinput";
		splice_cmd->wallet_inputs_to_signed++;
	}

	req = jsonrpc_request_start(cmd, command,
				    addpsbt_get_result,
				    splice_error_pkg, pkg);

	if (addinginputs) {
		sats = action->out_sat;
		if (!amount_sat_sub(&sats, sats, already_funded))
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
				       tal_fmt(tmpctx,
				       "Internal error; unable to sub"
				       " already_funded %s sats from out_stats"
				       " %s onchain_wallet_fund",
				       fmt_amount_sat(tmpctx, already_funded),
				       fmt_amount_sat(tmpctx, sats)));
		json_add_sats(req->js, "satoshi", sats);
		assert(splice_cmd->feerate_per_kw);
		json_add_u32(req->js, "min_feerate", splice_cmd->feerate_per_kw);

		plugin_log(cmd->plugin, LOG_DBG, "Adding input of at least %s",
			   fmt_amount_sat(tmpctx, sats));
	}
	else {
		json_add_sats(req->js, "satoshi", action->in_sat);

		plugin_log(cmd->plugin, LOG_DBG, "Adding output of %s",
			   fmt_amount_sat(tmpctx, action->in_sat));
	}

	json_add_psbt(req->js, "initialpsbt", splice_cmd->psbt);
	json_add_bool(req->js, "add_initiator_serial_ids", true);
	if (addinginputs)
		json_add_bool(req->js, "mark_our_inputs", true);

	if (state->state == SPLICE_CMD_PENDING) {
		plugin_log(cmd->plugin, LOG_DBG, "Not marking index %d done"
			   " because it is pending", (int)index);
	} else {
		plugin_log(cmd->plugin, LOG_DBG, "Marking index %d done",
			   (int)index);
		state->state = SPLICE_CMD_DONE;
	}

	return send_outreq(req);
}

static struct command_result *feerate_get_result(struct command *cmd,
			const char *method,
			const char *buf,
			const jsmntok_t *result,
			struct splice_cmd *splice_cmd)
{
	const jsmntok_t *tok = json_get_member(buf, result, "perkw");
	tok = json_get_member(buf, tok, "splice");

	if (!json_to_u32(buf, tok, &splice_cmd->feerate_per_kw))
		return command_fail_badparam(cmd, "opening", buf,
					     tok, "invalid u32");

	if (!splice_cmd->feerate_per_kw)
		return command_fail(splice_cmd->cmd,
					 JSONRPC2_INVALID_PARAMS,
					 "Failed to load a default feerate");

	plugin_log(cmd->plugin, LOG_DBG,
		   "got feerate %"PRIu32" perkw", splice_cmd->feerate_per_kw);

	return continue_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *load_feerate(struct command *cmd,
					   struct splice_cmd *splice_cmd)
{
	struct out_req *req;

	req = jsonrpc_request_start(cmd, "feerates",
				    feerate_get_result, splice_error,
				    splice_cmd);

	json_add_string(req->js, "style", "perkw");

	return send_outreq(req);
}

static struct amount_sat wallet_funding_amnt(struct splice_cmd *splice_cmd)
{
	struct amount_sat wallet_funding = AMOUNT_SAT(0);

	/* Being unable to wallet inputs shouldn't happen, so we log UNUSUAL */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		if (splice_cmd->actions[i]->onchain_wallet) {
			if (!amount_sat_add(&wallet_funding, wallet_funding,
					    splice_cmd->actions[i]->out_sat))
				plugin_log(splice_cmd->cmd->plugin, LOG_UNUSUAL,
					   "Failed to add out_sat %s to"
					   " wallet_funding %s",
					   fmt_amount_sat(tmpctx, splice_cmd->actions[i]->out_sat),
					   fmt_amount_sat(tmpctx, wallet_funding));
		}
	}

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		if (splice_cmd->actions[i]->onchain_wallet) {
			if (!amount_sat_sub(&wallet_funding, wallet_funding,
					    splice_cmd->actions[i]->in_sat))
				wallet_funding = AMOUNT_SAT(0);
		}
	}

	return wallet_funding;
}

static bool unresolved_wallet_inputs(struct splice_cmd *splice_cmd)
{
	struct amount_sat wallet_funding = wallet_funding_amnt(splice_cmd);

	return amount_sat_less(wallet_funding, splice_cmd->needed_funds);
}

static size_t calc_weight(struct splice_cmd *splice_cmd,
			  bool simulate_wallet_outputs)
{
	struct splice_script_result *action;
	struct plugin *plugin = splice_cmd->cmd->plugin;
	struct wally_psbt *psbt = splice_cmd->psbt;
	size_t lweight = 0, weight = 0;
	size_t extra_inputs = 0;
	size_t extra_outputs = 0;
	bool add_wallet_output = output_wallet(splice_cmd) ? true : false;

	plugin_log(plugin, LOG_DBG, "Counting potenetial tx weight;");

	/* BOLT #2:
	 * The rest of the transaction bytes' fees are the responsibility of
	 * the peer who contributed that input or output via `tx_add_input` or
	 * `tx_add_output`, at the agreed upon `feerate`.
	 */
	for (size_t i = 0; i < psbt->num_inputs; i++) {
		weight += psbt_input_get_weight(psbt, i, PSBT_GUESS_2OF2);
		plugin_log(plugin, LOG_DBG, " Counting input; weight: %lu",
			   weight - lweight);
		lweight = weight;
	}

	if (unresolved_wallet_inputs(splice_cmd)) {
		add_wallet_output = true;
		weight += bitcoin_tx_input_weight(false,
						  bitcoin_tx_input_witness_weight(UTXO_P2TR) - 1);
		plugin_log(plugin, LOG_DBG, " Simulating input (wallet);"
			   " weight: %lu", weight - lweight);
		lweight = weight;
	}

	/* Count the splice input manually */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		if (action->channel_id) {
			weight += bitcoin_tx_input_weight(false,
							  bitcoin_tx_2of2_input_witness_weight() - 1);
			plugin_log(plugin, LOG_DBG, " Simulating input"
				   " (channel); weight:"
				   " %lu", weight - lweight);
			lweight = weight;
			extra_inputs++;
		}
	}

	/* Count the splice outputs manually */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		if (action->onchain_wallet) {
			if (!amount_sat_is_zero(action->in_sat) || action->in_ppm)
				add_wallet_output = true;
			assert(!splice_cmd->actions[i]->channel_id);
		}
		if (splice_cmd->actions[i]->channel_id) {
			weight += bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2WSH_LEN);
			plugin_log(plugin, LOG_DBG, " Simulating output"
				   " (channel); weight:"
				   " %lu", weight - lweight);
			lweight = weight;

			extra_outputs++;
		}
	}

	if (simulate_wallet_outputs && add_wallet_output) {
		weight += bitcoin_tx_output_weight(BITCOIN_SCRIPTPUBKEY_P2TR_LEN);
		extra_outputs++;
		plugin_log(plugin, LOG_DBG, " Simulating output"
			   " (wallet); weight:"
			   " %lu", weight - lweight);
		lweight = weight;
	}

	for (size_t i = 0; i < psbt->num_outputs; i++) {
		weight += psbt_output_get_weight(psbt, i);
		plugin_log(plugin, LOG_DBG, " Adding output; weight: %lu",
			   weight - lweight);
		lweight = weight;
	}

	/* BOLT #2:
	 * The *initiator* is responsible for paying the fees for the following fields,
	 * to be referred to as the `common fields`.
	 *
  	 * - version
  	 * - segwit marker + flag
  	 * - input count
  	 * - output count
  	 * - locktime
	 */
	weight += bitcoin_tx_core_weight(psbt->num_inputs + extra_inputs,
					 psbt->num_outputs + extra_outputs);
	plugin_log(plugin, LOG_DBG, " Adding bitcoin_tx_core_weight;"
		   " weight: %lu", weight - lweight);
	lweight = weight;

	plugin_log(plugin, LOG_DBG, "Total weight: %lu", weight);
	return weight;
}

static struct command_result *splice_init_get_result(struct command *cmd,
			    const char *methodname,
			    const char *buf,
			    const jsmntok_t *result,
			    struct splice_cmd *splice_cmd)
{
	const jsmntok_t *tok = json_get_member(buf, result, "psbt");

	tal_free(splice_cmd->psbt);
	splice_cmd->psbt = json_to_psbt(splice_cmd, buf, tok);

	return continue_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *splice_init(struct command *cmd,
					  struct splice_cmd *splice_cmd,
					  size_t index)
{
	struct splice_script_result *action = splice_cmd->actions[index];
	struct splice_cmd_action_state *state = splice_cmd->states[index];
	struct out_req *req;

	req = jsonrpc_request_start(cmd, "splice_init",
				    splice_init_get_result, splice_error,
				    splice_cmd);

	json_add_channel_id(req->js, "channel_id", action->channel_id);
	if (!amount_sat_is_zero(action->in_sat)) {
		json_add_u64(req->js, "relative_amount",
			     action->in_sat.satoshis);  /* Raw: signed RPC */
	} else if (!amount_sat_is_zero(action->out_sat)) {
		json_add_string(req->js, "relative_amount",
				tal_fmt(req->js, "-%"PRIu64,
					action->out_sat.satoshis)); /* Raw: signed RPC */
	} else {
		json_add_sats(req->js, "relative_amount", amount_sat(0));
	}
	json_add_psbt(req->js, "initialpsbt", splice_cmd->psbt);
	json_add_u32(req->js, "feerate_per_kw", splice_cmd->feerate_per_kw);
	json_add_bool(req->js, "skip_stfu", true);
	json_add_bool(req->js, "force_feerate", splice_cmd->force_feerate);

	state->state = SPLICE_CMD_INIT;

	return send_outreq(req);
}

static struct command_result *splice_update_get_result(struct command *cmd,
			      const char *methodname,
			      const char *buf,
			      const jsmntok_t *result,
			      struct splice_index_pkg *pkg)
{
	size_t index = pkg->index;
	struct splice_cmd *splice_cmd = pkg->splice_cmd;
	struct splice_cmd_action_state *state = splice_cmd->states[index];
	const jsmntok_t *tok;
	struct wally_psbt *psbt;
	enum splice_cmd_state old_state = state->state;
	bool got_sigs;

	tal_free(pkg);

	/* DTODO: juggle serial ids correctly for cross-channel splice */
	tok = json_get_member(buf, result, "psbt");
	psbt = json_to_psbt(splice_cmd, buf, tok);

	if (psbt_contribs_changed(splice_cmd->psbt, psbt))
		for (size_t i = 0; i < tal_count(splice_cmd->states); i++)
			if (splice_cmd->actions[i]->channel_id)
				splice_cmd->states[i]->state = SPLICE_CMD_UPDATE_NEEDS_CHANGES;

	assert(psbt);
	tal_free(splice_cmd->psbt);
	splice_cmd->psbt = tal_steal(splice_cmd, psbt);

	tok = json_get_member(buf, result, "signatures_secured");
	if (!json_to_bool(buf, tok, &got_sigs))
		return command_fail_badparam(cmd, "signatures_secured", buf,
					     tok, "invalid bool");

	if (old_state != SPLICE_CMD_UPDATE)
		state->state = SPLICE_CMD_UPDATE;
	else
		state->state = got_sigs ? SPLICE_CMD_RECVED_SIGS : SPLICE_CMD_UPDATE_DONE;

	return continue_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *splice_update(struct command *cmd,
					    struct splice_cmd *splice_cmd,
					    size_t index)
{
	struct splice_script_result *action = splice_cmd->actions[index];
	struct out_req *req;
	struct splice_index_pkg *pkg = tal(cmd->plugin, struct splice_index_pkg);

	pkg->splice_cmd = splice_cmd;
	pkg->index = index;

	plugin_log(cmd->plugin, LOG_DBG,
		   "splice_update(channel_id:%s)",
		   fmt_channel_id(tmpctx, action->channel_id));

	req = jsonrpc_request_start(cmd, "splice_update",
				    splice_update_get_result, splice_error_pkg,
				    pkg);

	json_add_channel_id(req->js, "channel_id", action->channel_id);
	json_add_psbt(req->js, "psbt", splice_cmd->psbt);

	return send_outreq(req);
}

static struct command_result *signpsbt_get_result(struct command *cmd,
						  const char *methodname,
						  const char *buf,
						  const jsmntok_t *result,
						  struct splice_cmd *splice_cmd)
{
	const jsmntok_t *tok = json_get_member(buf, result, "signed_psbt");
	struct channel_id *channel_ids;

	tal_free(splice_cmd->psbt);

	splice_cmd->psbt = json_to_psbt(splice_cmd, buf, tok);
	splice_cmd->wallet_inputs_to_signed = 0;

	/* After signing we add channel_ids to the PSBT for splice_signed */
	channel_ids = tal_arr(NULL, struct channel_id, 0);
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++)
		if (splice_cmd->actions[i]->channel_id)
			tal_arr_expand(&channel_ids,
				       *splice_cmd->actions[i]->channel_id);

	psbt_set_channel_ids(splice_cmd->psbt, channel_ids);
	tal_free(channel_ids);

	return continue_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *signpsbt(struct command *cmd,
				       struct splice_cmd *splice_cmd)
{
	struct out_req *req;
	size_t num_to_be_signed;

	req = jsonrpc_request_start(cmd, "signpsbt",
				    signpsbt_get_result, splice_error,
				    splice_cmd);

	/* Use input markers to identify which inputs
	 * are ours, only sign those */
	json_array_start(req->js, "signonly");
	num_to_be_signed = 0;
	for (size_t i = 0; i < splice_cmd->psbt->num_inputs; i++) {
		if (psbt_input_is_ours(&splice_cmd->psbt->inputs[i])) {
			json_add_num(req->js, NULL, i);
			num_to_be_signed++;
		}
	}
	json_array_end(req->js);

	json_add_psbt(req->js, "psbt", splice_cmd->psbt);

	/* If we have no inputs to be signed, skip ahead */
	if (!num_to_be_signed) {
		splice_cmd->wallet_inputs_to_signed = 0;
		return continue_splice(splice_cmd->cmd, splice_cmd);
	}

	return send_outreq(req);
}

static struct splice_script_result *requires_our_sigs(struct splice_cmd *splice_cmd,
						      size_t *index,
						      bool *multiple_require_sigs)
{
	struct splice_script_result *action = NULL;
	*index = UINT32_MAX;
	*multiple_require_sigs = false;
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		if (splice_cmd->states[i]->state == SPLICE_CMD_UPDATE_DONE) {
			/* There can only be one node that requires our sigs */
			if (action) {
				*multiple_require_sigs = true;
				return NULL;
			}
			action = splice_cmd->actions[i];
			*index = i;
		}
	}
	return action;
}

static struct command_result *splice_signed_get_result(struct command *cmd,
			      const char *methodname,
			      const char *buf,
			      const jsmntok_t *result,
			      struct splice_index_pkg *pkg)
{
	size_t index = pkg->index;
	struct splice_cmd *splice_cmd = pkg->splice_cmd;
	const jsmntok_t *tok;

	tal_free(pkg);

	tok = json_get_member(buf, result, "psbt");
	tal_free(splice_cmd->psbt);
	splice_cmd->psbt = json_to_psbt(splice_cmd, buf, tok);

	tok = json_get_member(buf, result, "txid");
	if (!json_to_txid(buf, tok, &splice_cmd->final_txid))
		return command_fail_badparam(cmd, "txid", buf,
					     tok, "invalid txid");

	splice_cmd->states[index]->state = SPLICE_CMD_DONE;

	return continue_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *splice_signed_error_pkg(struct command *cmd,
						      const char *methodname,
						      const char *buf,
						      const jsmntok_t *error,
						      struct splice_index_pkg *pkg)
{
	struct splice_cmd *splice_cmd = pkg->splice_cmd;
	struct abort_pkg *abort_pkg;

	splice_cmd->wetrun = false;

	abort_pkg = tal(cmd->plugin, struct abort_pkg);
	abort_pkg->splice_cmd = tal_steal(abort_pkg, pkg->splice_cmd);
	abort_pkg->str = tal_strndup(abort_pkg, buf + error->start,
				     error->end - error->start);
	abort_pkg->code = -1;

	tal_free(pkg);

	return make_error(cmd, abort_pkg, "splice_signed_error");
}

static struct command_result *splice_signed(struct command *cmd,
					    struct splice_cmd *splice_cmd,
					    size_t index)
{
	struct splice_script_result *action = splice_cmd->actions[index];
	struct out_req *req;
	struct splice_index_pkg *pkg;

	pkg = tal(cmd->plugin, struct splice_index_pkg);
	pkg->splice_cmd = splice_cmd;
	pkg->index = index;

	req = jsonrpc_request_start(cmd, "splice_signed",
				    splice_signed_get_result,
				    splice_signed_error_pkg,
				    pkg);

	json_add_channel_id(req->js, "channel_id", action->channel_id);
	json_add_psbt(req->js, "psbt", splice_cmd->psbt);

	return send_outreq(req);
}

static struct command_result *check_emergency_sat(struct command *cmd,
						  struct splice_cmd *splice_cmd)
{
	struct amount_sat to_wallet = AMOUNT_SAT(0);
	if (amount_sat_is_zero(splice_cmd->emergency_sat))
		return NULL;

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		struct splice_script_result *action = splice_cmd->actions[i];
		if (action->onchain_wallet)
			if (!amount_sat_add(&to_wallet, to_wallet,
					    action->in_sat))
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Unable to amount_sat_add"
						    " wallet amounts for"
						    " emergency_sat calc");
	}

	if (!amount_sat_greater_eq(to_wallet, splice_cmd->emergency_sat))
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
			       tal_fmt(tmpctx,
				       "Amount going to onchain wallet %s is"
				       " not enough to meet the emergency"
				       " minimum of %s",
				       fmt_amount_sat(tmpctx, to_wallet),
				       fmt_amount_sat(tmpctx, splice_cmd->emergency_sat)));

	return NULL;
}

static struct command_result *handle_wetrun(struct command *cmd,
					    struct splice_cmd *splice_cmd)
{
	struct out_req *req;
	struct abort_pkg *abort_pkg;
	size_t added;

	abort_pkg = tal(cmd->plugin, struct abort_pkg);
	abort_pkg->splice_cmd = tal_steal(abort_pkg, splice_cmd);
	abort_pkg->str = NULL;

	req = jsonrpc_request_start(cmd, "abort_channels",
				    abort_get_result, forward_error, abort_pkg);

	added = 0;
	json_array_start(req->js, "channel_ids");
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		if (splice_cmd->actions[i]->channel_id) {
			added++;
			json_add_channel_id(req->js, NULL,
					    splice_cmd->actions[i]->channel_id);
		}
	}
	json_array_end(req->js);

	if (!added)
		return unreserve_get_result(cmd, NULL, NULL, NULL, abort_pkg);

	return send_outreq(req);
}

/* Before calling, ensure `onchain_fee` is accounted for in `needed_funds`.
 *
 * If we need to fund from the onchain wallet this requires another pass so
 * `onchain_fee` will be subtracted out of `needed_funds` and an
 * `onchain_wallet_fund` command is returned.
 *
 * If we are finished funding or we can take the needed funds out of a wallet
 * output, we return NULL for success and leave `needed_funds` unmolested.
 */
static struct command_result *handle_wallet_fund(struct command *cmd,
						 struct splice_cmd *splice_cmd,
						 struct amount_sat onchain_fee)
{
	size_t index;
	struct splice_script_result *input, *output;
	struct amount_sat already_funded, wallet_funding, missing_funds;

	wallet_funding = wallet_funding_amnt(splice_cmd);

	if (!amount_sat_sub(&missing_funds, splice_cmd->needed_funds,
			    wallet_funding))
		return do_fail(cmd, splice_cmd,
			       JSONRPC2_INVALID_PARAMS,
			       "Failed to calculate missing funds");

	plugin_log(cmd->plugin, LOG_INFORM, "handle_wallet_fund needed_funds"
		   " %s, current_funds %s, missing_funds %s",
		   fmt_amount_sat(tmpctx, splice_cmd->needed_funds),
		   fmt_amount_sat(tmpctx, wallet_funding),
		   fmt_amount_sat(tmpctx, missing_funds));

	input = input_wallet(splice_cmd, &index);
	output = output_wallet(splice_cmd);

	if (!input)
		return do_fail(cmd, splice_cmd,
			       JSONRPC2_INVALID_PARAMS,
			       "Can't fund wallet with no input wallet");

	/* Can we fund the input by just subtracting from the output? */
	if (output && amount_sat_greater(output->in_sat, missing_funds)) {

		plugin_log(cmd->plugin, LOG_INFORM, "Taking %s"
			   " from output wallet %s to cover fee",
			   fmt_amount_sat(tmpctx, missing_funds),
			   fmt_amount_sat(tmpctx, output->in_sat));

		if (!amount_sat_sub(&output->in_sat, output->in_sat,
				    missing_funds))
			return do_fail(cmd, splice_cmd,
				       JSONRPC2_INVALID_PARAMS,
				       tal_fmt(tmpctx,
				       "Failed to subtract fee amount %s"
				       " from output %s",
				       fmt_amount_sat(tmpctx, missing_funds),
				       fmt_amount_sat(tmpctx, output->in_sat)));

		return NULL;
	}

	already_funded = input->out_sat;

	if (!amount_sat_add(&input->out_sat, input->out_sat, missing_funds))
		return do_fail(cmd, splice_cmd,
			       JSONRPC2_INVALID_PARAMS,
			       "Unable to add missing funds to wallet input");

	/* Retruning `onchain_wallet_fund` means we will need to go around
	 * again and the caller will added the new `onchain_fee` to
	 * `needed_funds`, so we must take the now old amount out. */
	if (!amount_sat_sub(&splice_cmd->needed_funds,
			    splice_cmd->needed_funds,
			    onchain_fee))
		return do_fail(cmd, splice_cmd,
			       JSONRPC2_INVALID_PARAMS,
			       "Internal error; unable"
			       " to subtract fee from"
			       " needed_funds");

	plugin_log(cmd->plugin, LOG_INFORM, "Requesting funding"
		   " amount + %s fee wallet inputs for %s"
		   " with %s already_funded",
		   fmt_amount_sat(tmpctx, onchain_fee),
		   fmt_amount_sat(tmpctx, input->out_sat),
		   fmt_amount_sat(tmpctx, already_funded));

	return onchain_wallet_fund(cmd, splice_cmd,
				   index,
				   already_funded);
}

static struct command_result *handle_fee_and_ppm(struct command *cmd,
						 struct splice_cmd *splice_cmd)
{
	struct command_result *result;
	struct amount_sat onchain_fee;
	size_t weight;
	struct amount_sat extra_funds, missing_funds, non_wallet_demand, sat;
	struct splice_script_result *funding_wallet_action = NULL;
	size_t funding_wallet_index;

	funding_wallet_action = input_wallet(splice_cmd, &funding_wallet_index);

	/* We calculate the weight with simulated wallet */
	weight = calc_weight(splice_cmd, true);
	onchain_fee = amount_tx_fee(splice_cmd->feerate_per_kw, weight);

	plugin_log(cmd->plugin, LOG_INFORM,
		   "Splice fee is %s at %"PRIu32" perkw (%.02f sat/vB) "
		   "on tx where our weight units are %lu",
		   fmt_amount_sat(tmpctx, onchain_fee),
		   splice_cmd->feerate_per_kw,
		   4 * splice_cmd->feerate_per_kw / 1000.0f,
		   weight);

	/* If the wallet pays the fee, we need to add input(s) to cover
	 * it. This can potentially need to be done mulitple times since
	 * adding an input increases the needed fee. */
	if (funding_wallet_action && funding_wallet_action->pays_fee
		&& !funding_wallet_action->out_ppm
		&& splice_cmd->states[funding_wallet_index]->state != SPLICE_CMD_PENDING) {

		result = calc_in_ppm_and_fee(cmd, splice_cmd,
					     onchain_fee,
					     false,
					     &extra_funds,
					     &missing_funds,
					     &non_wallet_demand);
		if (result)
			return result;

		if (!amount_sat_add(&splice_cmd->needed_funds,
				    splice_cmd->needed_funds,
				    onchain_fee))
			return do_fail(cmd, splice_cmd,
				       JSONRPC2_INVALID_PARAMS,
				       "Internal error; unable to add"
				       " fee to needed_funds");

		/* We need to add wallet funds (again?) */
		if (unresolved_wallet_inputs(splice_cmd)) {
			result = handle_wallet_fund(cmd, splice_cmd,
						    onchain_fee);
			if (result)
				return result;
		}
	}

	/* Now we're ready to calculate wallet funding in_ppm. This is
	 * a special case where we take a percentage of the
	 * non_wallet_demand. */
	if (funding_wallet_action && funding_wallet_action->out_ppm) {

		result = calc_in_ppm_and_fee(cmd, splice_cmd,
					     onchain_fee,
					     false,
					     &extra_funds,
					     &missing_funds,
					     &non_wallet_demand);
		if (result)
			return result;

		if (!amount_sat_mul(&sat, non_wallet_demand,
				    funding_wallet_action->out_ppm))
			return do_fail(cmd, splice_cmd,
				       JSONRPC2_INVALID_PARAMS,
				       "Unable to mul sats & out_ppm");
		sat = amount_sat_div(sat, 1000000);

		plugin_log(cmd->plugin, LOG_DBG,
			   "Processing wallet percentage,"
			   " non_wallet_demand %s * %uppm = %s",
			   fmt_amount_sat(tmpctx, non_wallet_demand),
			   funding_wallet_action->out_ppm,
			   fmt_amount_sat(tmpctx, sat));

		/* Add onchain fee to `sat` if wallet pays the fee */
		if (funding_wallet_action->pays_fee) {
			if (!amount_sat_add(&sat, sat, onchain_fee))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Failed to add onchain fee to"
					       " ppm funding wallet");

			plugin_log(cmd->plugin, LOG_DBG,
				   "Adding onchain_fee %s = %s",
				   fmt_amount_sat(tmpctx, onchain_fee),
				   fmt_amount_sat(tmpctx, sat));
		}

		if (!amount_sat_is_zero(extra_funds)) {
			plugin_log(cmd->plugin, LOG_DBG,
				   "Extra funds %s",
				   fmt_amount_sat(tmpctx, extra_funds));
		}

		/* Marking `out_ppm` as resolved allows the next pass
		 * here to drop down past this block */
		funding_wallet_action->out_ppm = 0;

		/* If sat resolves to real number, add it to `needed_funds` and
		 * fund it */
		if (!amount_sat_is_zero(sat)) {

			/* PENDING is a special case that funds the wallet but
			 * keeps it from being marked DONE by the funder */
			splice_cmd->states[funding_wallet_index]->state = SPLICE_CMD_PENDING;

			if (!amount_sat_add(&splice_cmd->needed_funds,
					    splice_cmd->needed_funds,
					    sat))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Internal error; unable"
					       " to add fee to"
					       " needed_funds (wallet"
					       " ppm)");

			plugin_log(cmd->plugin, LOG_DBG,
				   "Wallet funding pass for sats"
				   " %s, total %s",
				   fmt_amount_sat(tmpctx, sat),
				   fmt_amount_sat(tmpctx, splice_cmd->needed_funds));

			result = handle_wallet_fund(cmd, splice_cmd,
						    AMOUNT_SAT(0));
			if (result)
				return result;
		}
	}

	/* Here we know `funding_wallet_action->out_ppm` is resolved
	 * but we need to check for repeat funding needs */

	/* If adding funds required more funds to pay for fees, we must repeat
	 * the funding operation started by the `out_ppm` block */
	if (funding_wallet_action
	    && splice_cmd->states[funding_wallet_index]->state == SPLICE_CMD_PENDING) {

		result = calc_in_ppm_and_fee(cmd, splice_cmd,
					     onchain_fee,
					     false,
					     &extra_funds,
					     &missing_funds,
					     &non_wallet_demand);
		if (result)
			return result;

		/* Are we done? */
		if (amount_sat_is_zero(missing_funds)) {
			plugin_log(cmd->plugin, LOG_DBG,
				   "Wallet percentage processing done because"
				   " we have no missing funds");
			splice_cmd->states[funding_wallet_index]->state = SPLICE_CMD_NONE;
		} else if (funding_wallet_action->pays_fee
			|| !fee_action(splice_cmd)) {
			/* We only do extra rounds if our wallet pays the fee
			 * or if no one is paying fee (ie fee is paid from)
			 * general funds */

			if (amount_sat_greater(missing_funds, onchain_fee))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       tal_fmt(tmpctx,
					       "Internal error; should never"
					       " need an extra pass on ppm"
					       " wallet funding that is larger"
					       " than onchain_fee."
					       " missing_funds %s,"
					       " onchain_fee %s",
					       fmt_amount_sat(tmpctx, missing_funds),
					       fmt_amount_sat(tmpctx, onchain_fee)));

			if (!amount_sat_add(&splice_cmd->needed_funds,
					    splice_cmd->needed_funds,
					    missing_funds))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Internal error; unable"
					       " to add fee to"
					       " needed_funds (wallet"
					       " ppm)");

			plugin_log(cmd->plugin, LOG_DBG,
				   "Extra wallet funding pass for missing sats"
				   " %s, total %s",
				   fmt_amount_sat(tmpctx, missing_funds),
				   fmt_amount_sat(tmpctx, splice_cmd->needed_funds));

			result = handle_wallet_fund(cmd, splice_cmd,
						    AMOUNT_SAT(0));
			if (result)
				return result;
			else
				splice_cmd->states[funding_wallet_index]->state = SPLICE_CMD_NONE;
		} else {
			splice_cmd->states[funding_wallet_index]->state = SPLICE_CMD_NONE;
		}
	}

	/* Success! */
	plugin_log(cmd->plugin, LOG_INFORM, "Wallet funding done");

	/* Do a final pass to update values */
	result = calc_in_ppm_and_fee(cmd, splice_cmd,
				     onchain_fee,
				     true,
				     &extra_funds,
				     &missing_funds,
				     &non_wallet_demand);
	if (result)
		return result;

	/* One more pass to check for missing or extra funds */
	result = calc_in_ppm_and_fee(cmd, splice_cmd,
				     onchain_fee,
				     false,
				     &extra_funds,
				     &missing_funds,
				     &non_wallet_demand);
	if (result)
		return result;

	if (!amount_sat_is_zero(extra_funds))
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
			       tal_fmt(tmpctx,
				       "Script calculation ended with"
				       " unclaimed extra funds %s",
				       fmt_amount_sat(tmpctx,
				       		      extra_funds)));
	if (!amount_sat_is_zero(missing_funds))
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
			       tal_fmt(tmpctx,
				       "Script is missing %s funds",
				       fmt_amount_sat(tmpctx,
				       		      missing_funds)));

	return NULL;
}

static struct command_result *continue_splice(struct command *cmd,
					      struct splice_cmd *splice_cmd)
{
	struct splice_script_result *action;
	struct splice_cmd_action_state *state;
	struct command_result *result;
	size_t index;
	bool multiple_require_sigs;
	struct splice_script_result *funding_wallet_action = NULL;
	size_t funding_wallet_index;

	add_to_debug_log(splice_cmd, "continue_splice");

	if (!splice_cmd->feerate_per_kw)
		return load_feerate(cmd, splice_cmd);

	funding_wallet_action = input_wallet(splice_cmd, &funding_wallet_index);

	/* On first pass we add wallet actions that contribute funds but only
	 * if it is a static amount */
	if (funding_wallet_action
	    && splice_cmd->states[funding_wallet_index]->state == SPLICE_CMD_NONE
	    && !funding_wallet_action->out_ppm && !funding_wallet_action->pays_fee) {

		funding_wallet_action->out_sat = splice_cmd->needed_funds;
		plugin_log(cmd->plugin, LOG_INFORM, "funding static"
			   " wallet inputs for %s",
			   fmt_amount_sat(tmpctx, funding_wallet_action->out_sat));
		return onchain_wallet_fund(cmd, splice_cmd,
					   funding_wallet_index, AMOUNT_SAT(0));
	}

	if (!splice_cmd->fee_calculated) {

		result = handle_fee_and_ppm(cmd, splice_cmd);
		if (result)
			return result;

		splice_cmd->fee_calculated = true;
	}

	/* Only after fee calcualtion can we add wallet actions taking funds */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		if (state->state != SPLICE_CMD_NONE)
			continue;
		if (!action->onchain_wallet)
			continue;
		/* Add output for wallet funds */
		if (amount_sat_less(action->in_sat, chainparams->dust_limit)) {
			plugin_log(cmd->plugin, LOG_INFORM, "Adding a"
				   " wallet output of %s is below"
				   " dust_limit of %s. Leaving dust as"
				   " contribution to fee",
				   fmt_amount_sat(tmpctx, action->in_sat),
				   fmt_amount_sat(tmpctx, chainparams->dust_limit));
		} else {
			return onchain_wallet_fund(cmd, splice_cmd, i,
						   AMOUNT_SAT(0));
		}
		state->state = SPLICE_CMD_DONE;
	}

	result = check_emergency_sat(cmd, splice_cmd);
	if (result)
		return result;

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		if (state->state != SPLICE_CMD_NONE)
			continue;
		if (!action->channel_id)
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Internal error; should not get"
					    " here with non-channels with state"
					    " NONE");
		return splice_init(cmd, splice_cmd, i);
	}

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		if (state->state == SPLICE_CMD_INIT
			|| state->state == SPLICE_CMD_UPDATE_NEEDS_CHANGES)
			return splice_update(cmd, splice_cmd, i);
	}

	/* It is possible to receive a signature when we do splice_update with
	 * no changes. Therefore wetrun must abort here to prevent any of our
	 * peers locking up funds */
	if (splice_cmd->wetrun)
		return handle_wetrun(cmd, splice_cmd);

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		if (state->state == SPLICE_CMD_UPDATE)
			return splice_update(cmd, splice_cmd, i);
	}

	/* The signpsbt operation also adds channel_ids to psbt */
	if (splice_cmd->wallet_inputs_to_signed)
		return signpsbt(cmd, splice_cmd);

	if (requires_our_sigs(splice_cmd, &index, &multiple_require_sigs))
		return splice_signed(cmd, splice_cmd, index);

	if (multiple_require_sigs)
		return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
			       "Requested splice is impossible because multiple"
			       " peers demand they do not sign first. Someone"
			       " must sign first.");

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		if (i != index && state->state == SPLICE_CMD_RECVED_SIGS)
			return splice_signed(cmd, splice_cmd, i);
	}

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++)
		assert(splice_cmd->states[i]->state == SPLICE_CMD_DONE);

	add_to_debug_log(splice_cmd, "continue_splice-finished");

	struct json_stream *response = jsonrpc_stream_success(cmd);
	json_add_psbt(response, "psbt", splice_cmd->psbt);
	json_add_txid(response, "txid", &splice_cmd->final_txid);
	if (splice_cmd->debug_log) {
		json_array_start(response, "log");
		debug_log_to_json(response, splice_cmd->debug_log);
		json_array_end(response);
	}
	return command_finished(cmd, response);
}

static struct command_result *execute_splice(struct command *cmd,
					      struct splice_cmd *splice_cmd)
{
	struct splice_script_result *action;
	struct splice_cmd_action_state *state;
	struct wally_psbt_output *output;
	u64 serial_id;
	int pays_fee;
	u8 *scriptpubkey;

	/* Basic validation */
	pays_fee = 0;
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		int dest_count = 0;
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];

		if (action->out_ppm && !action->onchain_wallet)
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Should be no out_ppm on final"
					    " except for the wallet");
		if (splice_cmd->actions[i]->pays_fee) {
			if (pays_fee)
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Only one item may pay fee");
			pays_fee++;
		}
		if (splice_cmd->actions[i]->channel_id)
			dest_count++;
		if (splice_cmd->actions[i]->bitcoin_address)
			dest_count++;
		if (splice_cmd->actions[i]->onchain_wallet)
			dest_count++;
		if (dest_count < 1)
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Must specify 1 destination per");
		if (dest_count > 1)
			return do_fail(cmd, splice_cmd, JSONRPC2_INVALID_PARAMS,
					    "Too many destinations per");

		/* If user specifies both sats in and out, we just use the
		 * larger of the two and subtract the smaller. */
		if (amount_sat_greater(action->in_sat, action->out_sat)) {
			if (!amount_sat_sub(&action->in_sat, action->in_sat,
					    action->out_sat))
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Unable to sub out_sat from"
						    " in_sat");
			action->out_sat = amount_sat(0);
		} else {
			if (!amount_sat_sub(&action->out_sat, action->out_sat,
					    action->in_sat))
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Unable to sub in_sat from"
						    " out_sat");
			action->in_sat = amount_sat(0);
		}
	}

	add_to_debug_log(splice_cmd, "execute_splice");

	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		char *bitcoin_address;

		/* `out_ppm` is the percent to take out of the action.
		 * If it is set to '*' we get a value of UINT32_MAX.
		 * In this case we treat it as "take 100% out of the action." */
		if (action->out_ppm == UINT32_MAX)
			action->out_ppm = 1000000;

		/* Load (only one) feerate if user provided one */
		if (action->feerate_per_kw) {
			if (splice_cmd->feerate_per_kw)
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Only one item may set"
						    " feerate");
			splice_cmd->feerate_per_kw = action->feerate_per_kw;
		}

		/* Fund out to bitcoin address */
		if (action->bitcoin_address) {
			if (!amount_sat_is_zero(action->in_sat))
				return do_fail(cmd, splice_cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Cannot fund from bitcoin"
						    " address");
			if (!decode_scriptpubkey_from_addr(cmd, chainparams,
							   action->bitcoin_address,
							   &scriptpubkey))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Bitcoin address"
					       " unrecognized");

			/* Reencode scriptpubkey to addr for verification */
			bitcoin_address = encode_scriptpubkey_to_addr(tmpctx,
								      chainparams,
								      scriptpubkey,
								      tal_bytelen(scriptpubkey));
			if (!bitcoin_address)
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Bitcoin scriptpubkey failed"
					       " reencoding for address");

			if (!strcmp(bitcoin_address, action->bitcoin_address))
				return do_fail(cmd, splice_cmd,
					       JSONRPC2_INVALID_PARAMS,
					       "Bitcoin scriptpubkey failed"
					       " validation for address");

			output = psbt_append_output(splice_cmd->psbt,
						    scriptpubkey,
						    action->in_sat);

			/* DTODO: support dynamic address payouts (percent) */

			serial_id = psbt_new_output_serial(splice_cmd->psbt,
					       		   TX_INITIATOR);
			psbt_output_set_serial_id(splice_cmd->psbt, output,
						  serial_id);

			state->state = SPLICE_CMD_DONE;

			add_to_debug_log(splice_cmd,
					 "execute_splice-load_btcaddress");
		}
	}

	/* Set needed funds to the wallet contributions. */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		state = splice_cmd->states[i];
		if (action->onchain_wallet
		    && !amount_sat_is_zero(action->out_sat)) {
			splice_cmd->needed_funds = action->out_sat;
			plugin_log(cmd->plugin, LOG_INFORM, "setting"
				   " needed_funds to %s",
				   fmt_amount_sat(tmpctx,
				   		  splice_cmd->needed_funds));
			action->out_sat = AMOUNT_SAT(0);
		}
	}

	return continue_splice(cmd, splice_cmd);
}

static struct command_result *adjust_pending_out_ppm(struct splice_script_result **actions,
						     struct channel_id channel_id,
						     struct amount_sat available_funds,
						     struct splice_cmd *splice_cmd)
{
	for (size_t i = 0; i < tal_count(actions); i++) {
		if (!actions[i]->channel_id)
			continue;
		if (!channel_id_eq(actions[i]->channel_id, &channel_id))
			continue;
		/* Skip channels not using out_ppm */
		if (!actions[i]->out_ppm)
			continue;

		/* For now max (asterisks) means 100% but that may change in the
		 * future */
		if (actions[i]->out_ppm == UINT32_MAX)
			actions[i]->out_ppm = 1000000;

		/* ppm percentage calculation:
		 * action->out_sat = available_funds * out_ppm / 1000000 */
		if (!amount_sat_mul(&actions[i]->out_sat, available_funds,
				    actions[i]->out_ppm))
			return command_fail(splice_cmd->cmd, JSONRPC2_INVALID_PARAMS,
					    "Unable to mul sats(%s) &"
					    " out_ppm(%"PRIu32") for channel id"
					    " %s",
					    fmt_amount_sat(tmpctx, available_funds),
					    actions[i]->out_ppm,
					    fmt_channel_id(tmpctx, &channel_id));
		actions[i]->out_sat = amount_sat_div(actions[i]->out_sat,
						     1000000);
		actions[i]->out_ppm = 0;
	}

	return NULL;
}

static struct command_result *stfu_channels_get_result(struct command *cmd,
			      const char *methodname,
			      const char *buf,
			      const jsmntok_t *toks,
			      struct splice_cmd *splice_cmd)
{
	const jsmntok_t *jchannels, *jchannel;
	size_t i;
	const char *err;
	struct command_result *result;

	jchannels = json_get_member(buf, toks, "channels");
	json_for_each_arr(i, jchannel, jchannels) {
		struct channel_id channel_id;
		struct amount_sat sat;

		memset(&channel_id, 0, sizeof(channel_id));
		memset(&sat, 0, sizeof(sat));

		err = json_scan(tmpctx, buf, jchannel,
				"{channel_id?:%,available_msat?:%}",
				JSON_SCAN(json_to_channel_id, &channel_id),
				JSON_SCAN(json_to_msat_to_sat, &sat));
		if (err)
			errx(1, "Bad stfu_channels.channels %zu: %s",
			     i, err);

		result = adjust_pending_out_ppm(splice_cmd->actions,
						channel_id, sat, splice_cmd);
		if (result)
			return result;
	}

	return execute_splice(splice_cmd->cmd, splice_cmd);
}

static struct command_result *splice_dryrun(struct command *cmd,
					    struct splice_cmd *splice_cmd)
{
	char **lines;
 	unsigned int i;
	struct json_stream *response;
	const char *str;

	response = jsonrpc_stream_success(cmd);
	json_array_start(response, "dryrun");

	str = splicearr_to_string(response, splice_cmd->actions);
 	lines = tal_strsplit(response, take(str), "\n", STR_NO_EMPTY);
 	for (i = 0; lines[i] != NULL; i++)
 		json_add_string(response, NULL, lines[i]);
	json_array_end(response);
	return command_finished(cmd, response);
}

static struct command_result *handle_splice_cmd(struct command *cmd,
						struct splice_cmd *splice_cmd)
{
	struct out_req *req;

	if (splice_cmd->dryrun)
		return splice_dryrun(cmd, splice_cmd);

	req = jsonrpc_request_start(cmd, "stfu_channels",
				    stfu_channels_get_result,
				    splice_error, splice_cmd);

	json_array_start(req->js, "channel_ids");
	/* We begin by stfu'ing and getting available balance on all MAX reqs */
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++)
		if (splice_cmd->actions[i]->channel_id)
			json_add_channel_id(req->js, NULL,
					    splice_cmd->actions[i]->channel_id);
	json_array_end(req->js);

	return send_outreq(req);
}

static struct command_result *
validate_splice_cmd(struct splice_cmd *splice_cmd)
{
	struct splice_script_result *action;
	int paying_fee_count = 0;
	for (size_t i = 0; i < tal_count(splice_cmd->actions); i++) {
		action = splice_cmd->actions[i];
		if (action->pays_fee) {
			if (paying_fee_count)
				return command_fail(splice_cmd->cmd,
						    JSONRPC2_INVALID_PARAMS,
						    "Only one item may pay the"
						    " fee");
			paying_fee_count++;
		}
		if (action->bitcoin_address && action->in_ppm)
			return command_fail(splice_cmd->cmd,
					    JSONRPC2_INVALID_PARAMS,
					    "Dynamic bitcoin address amounts"
					    " not supported for now");
		if (action->bitcoin_address)
			return command_fail(splice_cmd->cmd,
					    JSONRPC2_INVALID_PARAMS,
					    "Paying out to bitcoin addresses"
					    " not supported for now.");
	}

	return NULL;
}

static struct command_result *listpeerchannels_get_result(struct command *cmd,
				 const char *methodname,
				 const char *buf,
				 const jsmntok_t *toks,
				 struct splice_cmd *splice_cmd)
{
	struct splice_script_error *error;
	struct splice_script_chan **channels;
	struct command_result *result;
	const jsmntok_t *jchannels, *jchannel;
	char **lines;
	struct json_stream *response;
	const char *str;
	size_t i;
	const char *err;

	channels = tal_arr(tmpctx, struct splice_script_chan*, 0);
	jchannels = json_get_member(buf, toks, "channels");
	json_for_each_arr(i, jchannel, jchannels) {
		tal_arr_expand(&channels, tal(channels,
					      struct splice_script_chan));

		err = json_scan(tmpctx, buf, jchannel,
				"{peer_id?:%,channel_id?:%}",
				JSON_SCAN(json_to_node_id,
					  &channels[i]->node_id),
				JSON_SCAN(json_to_channel_id,
					  &channels[i]->chan_id));
		if (err)
			errx(1, "Bad listpeerchannels.channels %zu: %s",
			     i, err);
	}

	if (splice_cmd->script) {
		error = parse_splice_script(splice_cmd, splice_cmd->script,
					    channels,  &splice_cmd->actions);
		if (error) {
			response = jsonrpc_stream_fail(cmd,
						       JSONRPC2_INVALID_PARAMS,
						       "Splice script compile"
						       " failed");

			json_array_start(response, "compiler_error");

			str = fmt_splice_script_compiler_error(response,
							       splice_cmd->script,
							       error);
		 	lines = tal_strsplit(response, take(str), "\n",
		 			     STR_NO_EMPTY);
		 	for (i = 0; lines[i] != NULL; i++)
		 		json_add_string(response, NULL, lines[i]);
			json_array_end(response);
			return command_finished(cmd, response);
		}

		splice_cmd->states = tal_arr(splice_cmd,
					     struct splice_cmd_action_state*,
					     tal_count(splice_cmd->actions));

		for (i = 0; i < tal_count(splice_cmd->states); i++) {
			splice_cmd->states[i] = tal(splice_cmd->states,
						    struct splice_cmd_action_state);
			splice_cmd->states[i]->state = SPLICE_CMD_NONE;
		}
	}

	assert(splice_cmd->actions);

	result = validate_splice_cmd(splice_cmd);
	if (result)
		return result;

	return handle_splice_cmd(splice_cmd->cmd, splice_cmd);
}

static struct command_result *
json_splice(struct command *cmd, const char *buf, const jsmntok_t *params)
{
	struct out_req *req;
	const char *script;
	const jsmntok_t *json;
	struct wally_psbt *psbt;
	bool *dryrun, *force_feerate, *debug_log, *wetrun;
	struct str_or_arr *str_or_arr;

	if (!param(cmd, buf, params,
		   p_opt("script_or_json", param_string_or_array, &str_or_arr),
		   p_opt_def("dryrun", param_bool, &dryrun, false),
		   p_opt_def("force_feerate", param_bool, &force_feerate,
		   	     false),
		   p_opt_def("debug_log", param_bool, &debug_log, false),
		   p_opt_dev("dev-wetrun", param_bool, &wetrun, false),
		   NULL))
		return command_param_failed();

	if (!str_or_arr)
		return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
				    "Must pass 'script_or_json'");

	script = str_or_arr->str;
	json = str_or_arr->arr;

	psbt = create_psbt(cmd, 0, 0, 0);

	struct splice_cmd *splice_cmd = tal(cmd, struct splice_cmd);

	splice_cmd->cmd = cmd;
	splice_cmd->script = tal_steal(splice_cmd, script);
	splice_cmd->psbt = tal_steal(splice_cmd, psbt);
	splice_cmd->dryrun = *dryrun;
	splice_cmd->wetrun = *wetrun;
	splice_cmd->feerate_per_kw = 0;
	splice_cmd->force_feerate = *force_feerate;
	splice_cmd->wallet_inputs_to_signed = 0;
	splice_cmd->fee_calculated = false;
	splice_cmd->initial_funds = AMOUNT_SAT(0);
	splice_cmd->emergency_sat = AMOUNT_SAT(0);
	splice_cmd->debug_log = *debug_log ? tal_strdup(splice_cmd, "") : NULL;
	splice_cmd->debug_counter = 0;
	splice_cmd->needed_funds = AMOUNT_SAT(0);
	memset(&splice_cmd->final_txid, 0, sizeof(splice_cmd->final_txid));

	/* If script validates as json, parse it as json instead */
	if (json) {
		if (!json_to_splice(splice_cmd, buf, json,
				    &splice_cmd->actions))
			return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
					    "splice json failed validation");

		splice_cmd->states = tal_arr(splice_cmd,
					     struct splice_cmd_action_state*,
					     tal_count(splice_cmd->actions));

		for (size_t i = 0; i < tal_count(splice_cmd->states); i++) {
			splice_cmd->states[i] = tal(splice_cmd->states,
						    struct splice_cmd_action_state);
			splice_cmd->states[i]->state = SPLICE_CMD_NONE;
		}
	}

	req = jsonrpc_request_start(cmd, "listpeerchannels",
				    listpeerchannels_get_result,
				    splice_error, splice_cmd);

	return send_outreq(req);
}

static struct command_result *
json_splicein(struct command *cmd, const char *buf, const jsmntok_t *params)
{
	struct out_req *req;
	const char *channel, *amount;
	struct splice_cmd *splice_cmd;
	char *script;

	if (!param(cmd, buf, params,
		   p_req("channel", param_string, &channel),
		   p_req("amount", param_string, &amount),
		   NULL))
		return command_param_failed();

	script = tal_fmt(NULL,
			 "wallet -> %s + fee; %s -> %s",
			 amount, amount, channel);

	splice_cmd = tal(cmd, struct splice_cmd);

	splice_cmd->cmd = cmd;
	splice_cmd->script = tal_steal(splice_cmd, script);
	splice_cmd->psbt = create_psbt(splice_cmd, 0, 0, 0);
	splice_cmd->dryrun = false;
	splice_cmd->wetrun = false;
	splice_cmd->feerate_per_kw = 0;
	splice_cmd->force_feerate = false;
	splice_cmd->wallet_inputs_to_signed = 0;
	splice_cmd->fee_calculated = false;
	splice_cmd->initial_funds = AMOUNT_SAT(0);
	splice_cmd->emergency_sat = AMOUNT_SAT(0);
	splice_cmd->debug_log = NULL;
	splice_cmd->debug_counter = 0;
	splice_cmd->needed_funds = AMOUNT_SAT(0);
	memset(&splice_cmd->final_txid, 0, sizeof(splice_cmd->final_txid));

	req = jsonrpc_request_start(cmd, "listpeerchannels",
				    listpeerchannels_get_result,
				    splice_error, splice_cmd);

	return send_outreq(req);
}

static struct command_result *
json_spliceout(struct command *cmd, const char *buf, const jsmntok_t *params)
{
	struct out_req *req;
	const char *channel, *amount, *destination;
	struct splice_cmd *splice_cmd;
	bool *force_feerate;
	char *script;

	if (!param(cmd, buf, params,
		   p_req("channel", param_string, &channel),
		   p_req("amount", param_string, &amount),
		   p_opt("destination", param_string, &destination),
		   p_opt_def("force_feerate", param_bool, &force_feerate,
		   	     false),
		   NULL))
		return command_param_failed();

	if (!destination)
		destination = "wallet";

	script = tal_fmt(NULL,
			 "%s -> %s + fee; 100%% -> %s",
			 channel, amount, destination);

	splice_cmd = tal(cmd, struct splice_cmd);

	splice_cmd->cmd = cmd;
	splice_cmd->script = tal_steal(splice_cmd, script);
	splice_cmd->psbt = create_psbt(splice_cmd, 0, 0, 0);
	splice_cmd->dryrun = false;
	splice_cmd->wetrun = false;
	splice_cmd->feerate_per_kw = 0;
	splice_cmd->force_feerate = *force_feerate;
	splice_cmd->wallet_inputs_to_signed = 0;
	splice_cmd->fee_calculated = false;
	splice_cmd->initial_funds = AMOUNT_SAT(0);
	splice_cmd->emergency_sat = AMOUNT_SAT(0);
	splice_cmd->debug_log = NULL;
	splice_cmd->debug_counter = 0;
	splice_cmd->needed_funds = AMOUNT_SAT(0);
	memset(&splice_cmd->final_txid, 0, sizeof(splice_cmd->final_txid));

	req = jsonrpc_request_start(cmd, "listpeerchannels",
				    listpeerchannels_get_result,
				    splice_error, splice_cmd);

	return send_outreq(req);
}

const struct plugin_command splice_commands[] = {
	{
		"dev-splice",
		json_splice
	},
	{
		"splicein",
		json_splicein
	},
	{
		"spliceout",
		json_spliceout
	},
};
const size_t num_splice_commands = ARRAY_SIZE(splice_commands);