LoopIn
LoopIn initiates a loop in swap with the given parameters. The call returns after the swap has been set up with the swap server. From that point onwards, progress can be tracked via the SwapStatus stream that is returned from Monitor().
Source: looprpc/client.proto
gRPC
rpc LoopIn (LoopInRequest) returns (SwapResponse);
REST
HTTP Method | Path |
---|---|
POST | /v1/loop/in |
Code Samples
- gRPC
- REST
- Shell
- Javascript
- Python
const fs = require('fs');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const GRPC_HOST = 'localhost:11010'
const MACAROON_PATH = 'LOOP_DIR/regtest/loop.macaroon'
const TLS_PATH = 'LOOP_DIR/tls.cert'
const loaderOptions = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
};
const packageDefinition = protoLoader.loadSync('looprpc/client.proto', loaderOptions);
const looprpc = grpc.loadPackageDefinition(packageDefinition).looprpc;
process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA';
const tlsCert = fs.readFileSync(TLS_PATH);
const sslCreds = grpc.credentials.createSsl(tlsCert);
const macaroon = fs.readFileSync(MACAROON_PATH).toString('hex');
const macaroonCreds = grpc.credentials.createFromMetadataGenerator(function(args, callback) {
let metadata = new grpc.Metadata();
metadata.add('macaroon', macaroon);
callback(null, metadata);
});
let creds = grpc.credentials.combineChannelCredentials(sslCreds, macaroonCreds);
let client = new looprpc.SwapClient(GRPC_HOST, creds);
let request = {
amt: <int64>,
max_swap_fee: <int64>,
max_miner_fee: <int64>,
last_hop: <bytes>,
external_htlc: <bool>,
htlc_conf_target: <int32>,
label: <string>,
initiator: <string>,
route_hints: <RouteHint>,
private: <bool>,
};
client.loopIn(request, function(err, response) {
console.log(response);
});
// Console output:
// {
// "id": <string>,
// "id_bytes": <bytes>,
// "htlc_address": <string>,
// "htlc_address_p2wsh": <string>,
// "htlc_address_p2tr": <string>,
// "server_message": <string>,
// }
import codecs, grpc, os
# Generate the following 2 modules by compiling the looprpc/client.proto with the grpcio-tools.
# See https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/python.md for instructions.
import client_pb2 as looprpc, client_pb2_grpc as clientstub
GRPC_HOST = 'localhost:11010'
MACAROON_PATH = 'LOOP_DIR/regtest/loop.macaroon'
TLS_PATH = 'LOOP_DIR/tls.cert'
# create macaroon credentials
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
def metadata_callback(context, callback):
callback([('macaroon', macaroon)], None)
auth_creds = grpc.metadata_call_credentials(metadata_callback)
# create SSL credentials
os.environ['GRPC_SSL_CIPHER_SUITES'] = 'HIGH+ECDSA'
cert = open(TLS_PATH, 'rb').read()
ssl_creds = grpc.ssl_channel_credentials(cert)
# combine macaroon and SSL credentials
combined_creds = grpc.composite_channel_credentials(ssl_creds, auth_creds)
# make the request
channel = grpc.secure_channel(GRPC_HOST, combined_creds)
stub = clientstub.SwapClientStub(channel)
request = looprpc.LoopInRequest(
amt=<int64>,
max_swap_fee=<int64>,
max_miner_fee=<int64>,
last_hop=<bytes>,
external_htlc=<bool>,
htlc_conf_target=<int32>,
label=<string>,
initiator=<string>,
route_hints=<RouteHint>,
private=<bool>,
)
response = stub.LoopIn(request)
print(response)
# {
# "id": <string>,
# "id_bytes": <bytes>,
# "htlc_address": <string>,
# "htlc_address_p2wsh": <string>,
# "htlc_address_p2tr": <string>,
# "server_message": <string>,
# }
- Javascript
- Python
const fs = require('fs');
const request = require('request');
const REST_HOST = 'localhost:8081'
const MACAROON_PATH = 'LOOP_DIR/regtest/loop.macaroon'
let requestBody = {
amt: <string>, // <int64>
max_swap_fee: <string>, // <int64>
max_miner_fee: <string>, // <int64>
last_hop: <string>, // <bytes> (base64 encoded)
external_htlc: <boolean>, // <bool>
htlc_conf_target: <integer>, // <int32>
label: <string>, // <string>
initiator: <string>, // <string>
route_hints: <array>, // <RouteHint>
private: <boolean>, // <bool>
};
let options = {
url: `https://${REST_HOST}/v1/loop/in`,
// Work-around for self-signed certificates.
rejectUnauthorized: false,
json: true,
headers: {
'Grpc-Metadata-macaroon': fs.readFileSync(MACAROON_PATH).toString('hex'),
},
form: JSON.stringify(requestBody),
}
request.post(options, function(error, response, body) {
console.log(body);
});
// Console output:
// {
// "id": <string>, // <string>
// "id_bytes": <string>, // <bytes>
// "htlc_address": <string>, // <string>
// "htlc_address_p2wsh": <string>, // <string>
// "htlc_address_p2tr": <string>, // <string>
// "server_message": <string>, // <string>
// }
import base64, codecs, json, requests
REST_HOST = 'localhost:8081'
MACAROON_PATH = 'LOOP_DIR/regtest/loop.macaroon'
TLS_PATH = 'LOOP_DIR/tls.cert'
url = f'https://{REST_HOST}/v1/loop/in'
macaroon = codecs.encode(open(MACAROON_PATH, 'rb').read(), 'hex')
headers = {'Grpc-Metadata-macaroon': macaroon}
data = {
'amt': <int64>,
'max_swap_fee': <int64>,
'max_miner_fee': <int64>,
'last_hop': base64.b64encode(<bytes>),
'external_htlc': <bool>,
'htlc_conf_target': <int32>,
'label': <string>,
'initiator': <string>,
'route_hints': <RouteHint>,
'private': <bool>,
}
r = requests.post(url, headers=headers, data=json.dumps(data), verify=TLS_PATH)
print(r.json())
# {
# "id": <string>,
# "id_bytes": <bytes>,
# "htlc_address": <string>,
# "htlc_address_p2wsh": <string>,
# "htlc_address_p2tr": <string>,
# "server_message": <string>,
# }
$ loop in --help
NAME:
loop in - perform an on-chain to off-chain swap (loop in)
USAGE:
loop in [command options] amt
DESCRIPTION:
Send the amount in satoshis specified by the amt argument
off-chain.
By default the swap client will create and broadcast the
on-chain htlc. The fee priority of this transaction can
optionally be set using the conf_target flag.
The external flag can be set to publish the on chain htlc
independently. Note that this flag cannot be set with the
conf_target flag.
OPTIONS:
--amt value the amount in satoshis to loop in. To check for the minimum and maximum amounts to loop in please consult "loop terms" (default: 0)
--external expect htlc to be published externally
--conf_target value the target number of blocks the on-chain htlc broadcast by the swap client should confirm within (default: 0)
--last_hop value the pubkey of the last hop to use for this swap
--label value an optional label for this swap,limited to 500 characters. The label may not start with our reserved prefix: [reserved].
--force, -f Assumes yes during confirmation. Using this option will result in an immediate swap
--verbose, -v show expanded details
--route_hints value route hints that can each be individually used to assist in reaching the invoice's destination
--private generates and passes routehints. Should be used if the connected node is only reachable via private channels
Messages
looprpc.LoopInRequest
Source: looprpc/client.proto
Field | gRPC Type | REST Type | REST Placement |
---|---|---|---|
amt | int64 | string | body |
max_swap_fee | int64 | string | body |
max_miner_fee | int64 | string | body |
last_hop | bytes | string | body |
external_htlc | bool | boolean | body |
htlc_conf_target | int32 | integer | body |
label | string | string | body |
initiator | string | string | body |
route_hints | RouteHint[] | array | body |
private | bool | boolean | body |
looprpc.SwapResponse
Source: looprpc/client.proto
Field | gRPC Type | REST Type |
---|---|---|
id deprecated | string | string |
id_bytes | bytes | string |
htlc_address deprecated | string | string |
htlc_address_p2wsh | string | string |
htlc_address_p2tr | string | string |
server_message | string | string |