Skip to content

Commit 487a72c

Browse files
committed
feat(validateTransfer): optional amount field & overloading of validateTransfer that returns amount
1.According to the specification [amount is optional](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount), so validation of payment for an unknown amount will be useful. 2. However, wants to know the amount of transfer. In our case, to avoid repeating actions that already happen in `validateTransfer`, has been created the overload that returns tuple, where the second part will be amount.
1 parent d947eca commit 487a72c

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

core/src/validateTransfer.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface ValidateTransferFields {
3232
/** `recipient` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#recipient). */
3333
recipient: Recipient;
3434
/** `amount` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#amount). */
35-
amount: Amount;
35+
amount?: Amount;
3636
/** `spl-token` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#spl-token). */
3737
splToken?: SPLToken;
3838
/** `reference` in the [Solana Pay spec](https://github.com/solana-labs/solana-pay/blob/master/SPEC.md#reference). */
@@ -51,12 +51,38 @@ export interface ValidateTransferFields {
5151
*
5252
* @throws {ValidateTransferError}
5353
*/
54+
55+
export function validateTransfer(
56+
connection: Connection,
57+
signature: TransactionSignature,
58+
{ recipient, amount, splToken, reference, memo }: ValidateTransferFields & { amount: Amount },
59+
options?: { commitment?: Finality }
60+
): Promise<TransactionResponse>;
61+
62+
/**
63+
* Check that a given transaction contains a valid Solana Pay transfer.
64+
*
65+
* @param connection - A connection to the cluster.
66+
* @param signature - The signature of the transaction to validate.
67+
* @param fields - Fields of a Solana Pay transfer request to validate.
68+
* @param options - Options for `getTransaction`.
69+
*
70+
* @throws {ValidateTransferError}
71+
*/
72+
73+
export function validateTransfer(
74+
connection: Connection,
75+
signature: TransactionSignature,
76+
{ recipient, amount, splToken, reference, memo }: ValidateTransferFields,
77+
options?: { commitment?: Finality }
78+
): Promise<[TransactionResponse, amount: Amount]>;
79+
5480
export async function validateTransfer(
5581
connection: Connection,
5682
signature: TransactionSignature,
5783
{ recipient, amount, splToken, reference, memo }: ValidateTransferFields,
5884
options?: { commitment?: Finality }
59-
): Promise<TransactionResponse> {
85+
): Promise<TransactionResponse | [TransactionResponse, Amount]> {
6086
const response = await connection.getTransaction(signature, options);
6187
if (!response) throw new ValidateTransferError('not found');
6288

@@ -79,7 +105,9 @@ export async function validateTransfer(
79105
const [preAmount, postAmount] = splToken
80106
? await validateSPLTokenTransfer(instruction, message, meta, recipient, splToken, reference)
81107
: await validateSystemTransfer(instruction, message, meta, recipient, reference);
82-
if (postAmount.minus(preAmount).lt(amount)) throw new ValidateTransferError('amount not transferred');
108+
109+
const delta = postAmount.minus(preAmount);
110+
if (delta.lt(amount ?? 1)) throw new ValidateTransferError('amount not transferred');
83111

84112
if (memo !== undefined) {
85113
// Memo instruction must be the second to last instruction
@@ -88,7 +116,9 @@ export async function validateTransfer(
88116
validateMemo(instruction, memo);
89117
}
90118

91-
return response;
119+
if (amount) return response;
120+
121+
return [response, delta];
92122
}
93123

94124
function validateMemo(instruction: TransactionInstruction, memo: string): void {

0 commit comments

Comments
 (0)