@@ -1799,10 +1799,100 @@ func deletePayments(ctx *cli.Context) error {
17991799 return nil
18001800}
18011801
1802+ var estimateRouteFeeCommand = cli.Command {
1803+ Name : "estimateroutefee" ,
1804+ Category : "Payments" ,
1805+ Usage : "Estimate routing fees based on a destination or an invoice." ,
1806+ Action : actionDecorator (estimateRouteFee ),
1807+ Flags : []cli.Flag {
1808+ cli.StringFlag {
1809+ Name : "dest" ,
1810+ Usage : "the 33-byte hex-encoded public key for the " +
1811+ "probe destination. If it is specified then " +
1812+ "the amt flag is required. If it isn't " +
1813+ "specified then the pay_req field has to." ,
1814+ },
1815+ cli.Int64Flag {
1816+ Name : "amt" ,
1817+ Usage : "the payment amount expressed in satoshis " +
1818+ "that should be probed for. This field is " +
1819+ "mandatory if dest is specified." ,
1820+ },
1821+ cli.StringFlag {
1822+ Name : "pay_req" ,
1823+ Usage : "a zpay32 encoded payment request which is " +
1824+ "used to probe. If the destination is " +
1825+ "not public then route hints are scanned for " +
1826+ "a public node." ,
1827+ },
1828+ cli.DurationFlag {
1829+ Name : "timeout" ,
1830+ Usage : "a deadline for the probe attempt. Only " +
1831+ "applicable if pay_req is specified." ,
1832+ Value : paymentTimeout ,
1833+ },
1834+ },
1835+ }
1836+
1837+ func estimateRouteFee (ctx * cli.Context ) error {
1838+ ctxc := getContext ()
1839+ conn := getClientConn (ctx , false )
1840+ defer conn .Close ()
1841+
1842+ client := routerrpc .NewRouterClient (conn )
1843+
1844+ req := & routerrpc.RouteFeeRequest {}
1845+
1846+ switch {
1847+ case ctx .IsSet ("dest" ) && ctx .IsSet ("pay_req" ):
1848+ return fmt .Errorf ("either dest or pay_req can be set" )
1849+
1850+ case ctx .IsSet ("dest" ) && ! ctx .IsSet ("amt" ):
1851+ return fmt .Errorf ("amt is required when dest is set" )
1852+
1853+ case ctx .IsSet ("dest" ):
1854+ dest , err := hex .DecodeString (ctx .String ("dest" ))
1855+ if err != nil {
1856+ return err
1857+ }
1858+
1859+ if len (dest ) != 33 {
1860+ return fmt .Errorf ("dest node pubkey must be exactly " +
1861+ "33 bytes, is instead: %v" , len (dest ))
1862+ }
1863+
1864+ amtSat := ctx .Int64 ("amt" )
1865+ if amtSat == 0 {
1866+ return fmt .Errorf ("non-zero amount required" )
1867+ }
1868+
1869+ req .Dest = dest
1870+ req .AmtSat = amtSat
1871+
1872+ case ctx .IsSet ("pay_req" ):
1873+ req .PaymentRequest = stripPrefix (ctx .String ("pay_req" ))
1874+ if ctx .IsSet ("timeout" ) {
1875+ req .Timeout = uint32 (ctx .Duration ("timeout" ).Seconds ())
1876+ }
1877+
1878+ default :
1879+ return fmt .Errorf ("fee estimation arguments missing" )
1880+ }
1881+
1882+ resp , err := client .EstimateRouteFee (ctxc , req )
1883+ if err != nil {
1884+ return err
1885+ }
1886+
1887+ printRespJSON (resp )
1888+
1889+ return nil
1890+ }
1891+
18021892// ESC is the ASCII code for escape character.
18031893const ESC = 27
18041894
1805- // clearCode defines a terminal escape code to clear the currently line and move
1895+ // clearCode defines a terminal escape code to clear the current line and move
18061896// the cursor up.
18071897var clearCode = fmt .Sprintf ("%c[%dA%c[2K" , ESC , 1 , ESC )
18081898
0 commit comments