Skip to content

Commit

Permalink
feat: mpay response compatibility with pay
Browse files Browse the repository at this point in the history
  • Loading branch information
michael1011 committed Mar 5, 2024
1 parent eef0544 commit 1ee2a15
Show file tree
Hide file tree
Showing 11 changed files with 291 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/VersionCheck.ts
Expand Up @@ -87,11 +87,11 @@ class VersionCheck {
},
[ClnClient.serviceNameHold]: {
minimal: '0.0.3',
maximal: '0.0.4',
maximal: '0.0.5',
},
[MpayClient.serviceName]: {
minimal: '0.1.0',
maximal: '0.1.0',
maximal: '0.1.1',
},
[LndClient.serviceName]: {
minimal: '0.16.0',
Expand Down
18 changes: 18 additions & 0 deletions lib/proto/mpay/mpay_pb.d.ts
Expand Up @@ -379,6 +379,18 @@ export class PayResponse extends jspb.Message {
setFeeMsat(value: number): PayResponse;
getTime(): number;
setTime(value: number): PayResponse;
getDestination(): string;
setDestination(value: string): PayResponse;
getAmountMsat(): number;
setAmountMsat(value: number): PayResponse;
getAmountSentMsat(): number;
setAmountSentMsat(value: number): PayResponse;
getParts(): number;
setParts(value: number): PayResponse;
getStatus(): string;
setStatus(value: string): PayResponse;
getCreatedAt(): number;
setCreatedAt(value: number): PayResponse;

serializeBinary(): Uint8Array;
toObject(includeInstance?: boolean): PayResponse.AsObject;
Expand All @@ -396,6 +408,12 @@ export namespace PayResponse {
paymentPreimage: string,
feeMsat: number,
time: number,
destination: string,
amountMsat: number,
amountSentMsat: number,
parts: number,
status: string,
createdAt: number,
}
}

Expand Down
182 changes: 181 additions & 1 deletion lib/proto/mpay/mpay_pb.js
Expand Up @@ -2945,7 +2945,13 @@ proto.mpay.PayResponse.toObject = function(includeInstance, msg) {
paymentHash: jspb.Message.getFieldWithDefault(msg, 1, ""),
paymentPreimage: jspb.Message.getFieldWithDefault(msg, 2, ""),
feeMsat: jspb.Message.getFieldWithDefault(msg, 3, 0),
time: jspb.Message.getFieldWithDefault(msg, 4, 0)
time: jspb.Message.getFieldWithDefault(msg, 4, 0),
destination: jspb.Message.getFieldWithDefault(msg, 5, ""),
amountMsat: jspb.Message.getFieldWithDefault(msg, 6, 0),
amountSentMsat: jspb.Message.getFieldWithDefault(msg, 7, 0),
parts: jspb.Message.getFieldWithDefault(msg, 8, 0),
status: jspb.Message.getFieldWithDefault(msg, 9, ""),
createdAt: jspb.Message.getFieldWithDefault(msg, 10, 0)
};

if (includeInstance) {
Expand Down Expand Up @@ -2998,6 +3004,30 @@ proto.mpay.PayResponse.deserializeBinaryFromReader = function(msg, reader) {
var value = /** @type {number} */ (reader.readUint64());
msg.setTime(value);
break;
case 5:
var value = /** @type {string} */ (reader.readString());
msg.setDestination(value);
break;
case 6:
var value = /** @type {number} */ (reader.readUint64());
msg.setAmountMsat(value);
break;
case 7:
var value = /** @type {number} */ (reader.readUint64());
msg.setAmountSentMsat(value);
break;
case 8:
var value = /** @type {number} */ (reader.readUint32());
msg.setParts(value);
break;
case 9:
var value = /** @type {string} */ (reader.readString());
msg.setStatus(value);
break;
case 10:
var value = /** @type {number} */ (reader.readUint64());
msg.setCreatedAt(value);
break;
default:
reader.skipField();
break;
Expand Down Expand Up @@ -3055,6 +3085,48 @@ proto.mpay.PayResponse.serializeBinaryToWriter = function(message, writer) {
f
);
}
f = message.getDestination();
if (f.length > 0) {
writer.writeString(
5,
f
);
}
f = message.getAmountMsat();
if (f !== 0) {
writer.writeUint64(
6,
f
);
}
f = message.getAmountSentMsat();
if (f !== 0) {
writer.writeUint64(
7,
f
);
}
f = message.getParts();
if (f !== 0) {
writer.writeUint32(
8,
f
);
}
f = message.getStatus();
if (f.length > 0) {
writer.writeString(
9,
f
);
}
f = message.getCreatedAt();
if (f !== 0) {
writer.writeUint64(
10,
f
);
}
};


Expand Down Expand Up @@ -3130,6 +3202,114 @@ proto.mpay.PayResponse.prototype.setTime = function(value) {
};


/**
* optional string destination = 5;
* @return {string}
*/
proto.mpay.PayResponse.prototype.getDestination = function() {
return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 5, ""));
};


/**
* @param {string} value
* @return {!proto.mpay.PayResponse} returns this
*/
proto.mpay.PayResponse.prototype.setDestination = function(value) {
return jspb.Message.setProto3StringField(this, 5, value);
};


/**
* optional uint64 amount_msat = 6;
* @return {number}
*/
proto.mpay.PayResponse.prototype.getAmountMsat = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 6, 0));
};


/**
* @param {number} value
* @return {!proto.mpay.PayResponse} returns this
*/
proto.mpay.PayResponse.prototype.setAmountMsat = function(value) {
return jspb.Message.setProto3IntField(this, 6, value);
};


/**
* optional uint64 amount_sent_msat = 7;
* @return {number}
*/
proto.mpay.PayResponse.prototype.getAmountSentMsat = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 7, 0));
};


/**
* @param {number} value
* @return {!proto.mpay.PayResponse} returns this
*/
proto.mpay.PayResponse.prototype.setAmountSentMsat = function(value) {
return jspb.Message.setProto3IntField(this, 7, value);
};


/**
* optional uint32 parts = 8;
* @return {number}
*/
proto.mpay.PayResponse.prototype.getParts = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 8, 0));
};


/**
* @param {number} value
* @return {!proto.mpay.PayResponse} returns this
*/
proto.mpay.PayResponse.prototype.setParts = function(value) {
return jspb.Message.setProto3IntField(this, 8, value);
};


/**
* optional string status = 9;
* @return {string}
*/
proto.mpay.PayResponse.prototype.getStatus = function() {
return /** @type {string} */ (jspb.Message.getFieldWithDefault(this, 9, ""));
};


/**
* @param {string} value
* @return {!proto.mpay.PayResponse} returns this
*/
proto.mpay.PayResponse.prototype.setStatus = function(value) {
return jspb.Message.setProto3StringField(this, 9, value);
};


/**
* optional uint64 created_at = 10;
* @return {number}
*/
proto.mpay.PayResponse.prototype.getCreatedAt = function() {
return /** @type {number} */ (jspb.Message.getFieldWithDefault(this, 10, 0));
};


/**
* @param {number} value
* @return {!proto.mpay.PayResponse} returns this
*/
proto.mpay.PayResponse.prototype.setCreatedAt = function(value) {
return jspb.Message.setProto3IntField(this, 10, value);
};





Expand Down
2 changes: 1 addition & 1 deletion tools/plugins/hold/protos/hold_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions tools/plugins/mpay/pay/mpay.py
Expand Up @@ -10,7 +10,7 @@
from plugins.mpay.pay.excludes import Excludes, ExcludesPayment
from plugins.mpay.pay.invoice_check import InvoiceChecker
from plugins.mpay.pay.payer import Payer
from plugins.mpay.pay.sendpay import PaymentHelper, PaymentResult
from plugins.mpay.pay.sendpay import STATUS_COMPLETE, PaymentHelper, PaymentResult
from plugins.mpay.utils import fee_with_percent, format_error


Expand Down Expand Up @@ -110,12 +110,19 @@ def _check_for_paid(self, payment_hash: str) -> PaymentResult | None:
if len(res) == 0:
return None

amount = Millisatoshi(sum(int(pay["amount_msat"]) for pay in res))
amount_sent = Millisatoshi(sum(int(pay["amount_sent_msat"]) for pay in res))

return PaymentResult(
destination=res[0]["destination"],
payment_hash=payment_hash,
payment_preimage=res[0]["preimage"],
fee_msat=Millisatoshi(
sum(int(pay["amount_sent_msat"]) - int(pay["amount_msat"]) for pay in res)
),
parts=len(res),
amount_msat=amount,
amount_sent_msat=amount_sent,
fee_msat=amount_sent - amount,
status=STATUS_COMPLETE,
created_at=res[0]["created_at"],
time=0,
)

Expand Down
27 changes: 25 additions & 2 deletions tools/plugins/mpay/pay/sendpay.py
Expand Up @@ -8,13 +8,25 @@

PERMANENT_ERRORS = ["WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS"]

STATUS_COMPLETE = "complete"


@dataclass
class PaymentResult:
destination: str

payment_hash: str
payment_preimage: str

amount_msat: Millisatoshi
amount_sent_msat: Millisatoshi

parts: int
status: str
fee_msat: Millisatoshi

time: int
created_at: int

def to_dict(self) -> dict[str, Any]:
return {k: int(v) if isinstance(v, Millisatoshi) else v for k, v in self.__dict__.items()}
Expand Down Expand Up @@ -67,11 +79,22 @@ def send(self, route: Route, bolt11: str, decoded: dict[str, Any]) -> PaymentRes

try:
wait = self._pl.rpc.waitsendpay(pay["payment_hash"])

created_at = wait["created_at"]
amount_sent = wait["amount_sent_msat"]
amount = wait["amount_msat"]

return PaymentResult(
destination=route.route[-1]["id"],
payment_hash=pay["payment_hash"],
payment_preimage=wait["payment_preimage"],
fee_msat=wait["amount_sent_msat"] - wait["amount_msat"],
time=wait["completed_at"] - wait["created_at"],
status=STATUS_COMPLETE,
amount_msat=Millisatoshi(amount),
amount_sent_msat=Millisatoshi(amount_sent),
fee_msat=Millisatoshi(amount_sent - amount),
parts=1,
created_at=created_at,
time=wait["completed_at"] - created_at,
)
except RpcError as e:
raise PaymentError(e.error) from None
6 changes: 6 additions & 0 deletions tools/plugins/mpay/protos/mpay.proto
Expand Up @@ -88,6 +88,12 @@ message PayResponse {
string payment_preimage = 2;
uint64 fee_msat = 3;
uint64 time = 4;
string destination = 5;
uint64 amount_msat = 6;
uint64 amount_sent_msat = 7;
uint32 parts = 8;
string status = 9;
uint64 created_at = 10;
}

message ResetPathMemoryRequest {}
Expand Down
20 changes: 10 additions & 10 deletions tools/plugins/mpay/protos/mpay_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1ee2a15

Please sign in to comment.