Skip to content

Commit 2e612e3

Browse files
authored
chore: NEAR transaction callback arguments (#948)
* chore: NEAR transaction callback arguments * bump near-api-js
1 parent e25862d commit 2e612e3

File tree

8 files changed

+82
-41
lines changed

8 files changed

+82
-41
lines changed

packages/payment-processor/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"@requestnetwork/utils": "0.35.0",
4747
"@superfluid-finance/sdk-core": "0.3.2",
4848
"ethers": "5.5.1",
49-
"near-api-js": "0.42.0",
49+
"near-api-js": "1.1.0",
5050
"tslib": "2.3.1"
5151
},
5252
"devDependencies": {

packages/payment-processor/src/payment/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { encodeRequestErc20Approval } from './encoder-approval';
2121
import { encodeRequestPayment } from './encoder-payment';
2222
import { IPreparedTransaction } from './prepared-transaction';
2323
import { IRequestPaymentOptions } from './settings';
24+
export { INearTransactionCallback } from './utils-near';
2425

2526
export const noConversionNetworks = [
2627
ExtensionTypes.ID.PAYMENT_NETWORK_ERC777_STREAM,

packages/payment-processor/src/payment/near-conversion.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import {
99
getAmountToPay,
1010
getPaymentExtensionVersion,
1111
} from './utils';
12-
import { isNearNetwork, processNearPaymentWithConversion } from './utils-near';
12+
import {
13+
INearTransactionCallback,
14+
isNearNetwork,
15+
processNearPaymentWithConversion,
16+
} from './utils-near';
1317
import { IConversionPaymentSettings } from '.';
1418
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
1519

@@ -24,6 +28,7 @@ export async function payNearConversionRequest(
2428
walletConnection: WalletConnection,
2529
paymentSettings: IConversionPaymentSettings,
2630
amount?: BigNumberish,
31+
callback?: INearTransactionCallback,
2732
): Promise<void> {
2833
validateRequest(request, PaymentTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE);
2934

@@ -59,6 +64,7 @@ export async function payNearConversionRequest(
5964
paymentSettings.maxToSpend,
6065
maxRateTimespan || '0',
6166
version,
67+
callback,
6268
);
6369
}
6470

packages/payment-processor/src/payment/near-input-data.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
getAmountToPay,
1010
getPaymentExtensionVersion,
1111
} from './utils';
12-
import { isNearNetwork, processNearPayment } from './utils-near';
12+
import { INearTransactionCallback, isNearNetwork, processNearPayment } from './utils-near';
1313

1414
/**
1515
* processes the transaction to pay a Near request.
@@ -21,6 +21,7 @@ export async function payNearInputDataRequest(
2121
request: ClientTypes.IRequestData,
2222
walletConnection: WalletConnection,
2323
amount?: BigNumberish,
24+
callback?: INearTransactionCallback,
2425
): Promise<void> {
2526
if (!request.currencyInfo.network || !isNearNetwork(request.currencyInfo.network)) {
2627
throw new Error('request.currencyInfo should be a Near network');
@@ -42,5 +43,6 @@ export async function payNearInputDataRequest(
4243
paymentAddress,
4344
paymentReference,
4445
version,
46+
callback,
4547
);
4648
}

packages/payment-processor/src/payment/utils-near.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import {
66
NearConversionNativeTokenPaymentDetector,
77
} from '@requestnetwork/payment-detection';
88

9+
/**
10+
* Callback arguments for the Near web wallet.
11+
* @member callbackUrl called upon transaction approval
12+
* @member callbackMeta (according to Near docs: `meta` will be attached to the `callbackUrl` as a url search param)
13+
*/
14+
export interface INearTransactionCallback {
15+
callbackUrl?: string;
16+
meta?: string;
17+
}
18+
919
export const isValidNearAddress = async (nearNetwork: Near, address: string): Promise<boolean> => {
1020
try {
1121
await nearNetwork.connection.provider.query(`account/${address}`, '');
@@ -44,6 +54,7 @@ export const processNearPayment = async (
4454
to: string,
4555
paymentReference: string,
4656
version = '0.2.0',
57+
callback: INearTransactionCallback | undefined = undefined,
4758
): Promise<void> => {
4859
if (version !== '0.2.0') {
4960
if (version === '0.1.0') {
@@ -66,14 +77,15 @@ export const processNearPayment = async (
6677
viewMethods: [],
6778
},
6879
) as any;
69-
await contract.transfer_with_reference(
70-
{
80+
await contract.transfer_with_reference({
81+
args: {
7182
to,
7283
payment_reference: paymentReference,
7384
},
74-
GAS_LIMIT_NATIVE,
75-
amount.toString(),
76-
);
85+
gas: GAS_LIMIT_NATIVE,
86+
amount: amount.toString(),
87+
...callback,
88+
});
7789
return;
7890
} catch (e) {
7991
throw new Error(`Could not pay Near request. Got ${e.message}`);
@@ -99,6 +111,7 @@ export const processNearPaymentWithConversion = async (
99111
maxToSpend: BigNumberish,
100112
maxRateTimespan = '0',
101113
version = '0.1.0',
114+
callback: INearTransactionCallback | undefined = undefined,
102115
): Promise<void> => {
103116
if (version !== '0.1.0') {
104117
throw new Error('Native Token with conversion payments on Near only support v0.1.0 extensions');
@@ -120,8 +133,8 @@ export const processNearPaymentWithConversion = async (
120133
viewMethods: [],
121134
},
122135
) as any;
123-
await contract.transfer_with_reference(
124-
{
136+
await contract.transfer_with_reference({
137+
args: {
125138
payment_reference: paymentReference,
126139
to,
127140
amount,
@@ -130,9 +143,10 @@ export const processNearPaymentWithConversion = async (
130143
fee_amount: feeAmount,
131144
max_rate_timespan: maxRateTimespan,
132145
},
133-
GAS_LIMIT_CONVERSION_TO_NATIVE,
134-
maxToSpend.toString(),
135-
);
146+
gas: GAS_LIMIT_CONVERSION_TO_NATIVE,
147+
amount: maxToSpend.toString(),
148+
...callback,
149+
});
136150
return;
137151
} catch (e) {
138152
throw new Error(`Could not pay Near request. Got ${e.message}`);

packages/payment-processor/test/payment/any-to-near.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ describe('payNearWithConversionRequest', () => {
6565
paymentAddress,
6666
);
6767

68-
await payNearConversionRequest(request, mockedNearWalletConnection, conversionSettings);
68+
await payNearConversionRequest(
69+
request,
70+
mockedNearWalletConnection,
71+
conversionSettings,
72+
undefined,
73+
{ callbackUrl: 'https://some.callback.url', meta: 'param' },
74+
);
6975
expect(paymentSpy).toHaveBeenCalledWith(
7076
expect.anything(),
7177
'aurora',
@@ -78,6 +84,10 @@ describe('payNearWithConversionRequest', () => {
7884
conversionSettings.maxToSpend,
7985
'0',
8086
'0.1.0',
87+
{
88+
callbackUrl: 'https://some.callback.url',
89+
meta: 'param',
90+
},
8191
);
8292
});
8393
it('throws when tyring to pay another payment extension', async () => {

packages/payment-processor/test/payment/index.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,18 @@ describe('payNearInputDataRequest', () => {
180180
},
181181
};
182182

183-
await payNearInputDataRequest(request, mockedNearWalletConnection, '1');
183+
await payNearInputDataRequest(request, mockedNearWalletConnection, '1', {
184+
callbackUrl: 'https://some.callback.url',
185+
meta: 'param',
186+
});
184187
expect(paymentSpy).toHaveBeenCalledWith(
185188
expect.anything(),
186189
'aurora',
187190
'1',
188191
'0x789',
189192
'700912030bd973e3',
190193
'0.2.0',
194+
{ callbackUrl: 'https://some.callback.url', meta: 'param' },
191195
);
192196
});
193197
it('throws when tyring to pay another payment extension', async () => {

yarn.lock

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8478,16 +8478,21 @@ [email protected]:
84788478
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.1.3.tgz#beca005408f642ebebea80b042b4d18d2ac0ee6b"
84798479
integrity sha512-GkTiFpjFtUzU9CbMeJ5iazkCzGL3jrhzerzZIuqLABjbwRaFt33I9tUdSNryIptM+RxDet6OKm2WnLXzW51KsQ==
84808480

8481-
[email protected].0, bn.js@^5.0.0, bn.js@^5.1.1, bn.js@^5.1.2, bn.js@^5.1.3:
8482-
version "5.2.0"
8483-
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
8484-
integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==
8481+
[email protected].1, bn.js@^5.2.0:
8482+
version "5.2.1"
8483+
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70"
8484+
integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ==
84858485

84868486
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.10.0, bn.js@^4.11.0, bn.js@^4.11.1, bn.js@^4.11.6, bn.js@^4.11.8, bn.js@^4.11.9, bn.js@^4.4.0, bn.js@^4.8.0:
84878487
version "4.12.0"
84888488
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
84898489
integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA==
84908490

8491+
bn.js@^5.0.0, bn.js@^5.1.1, bn.js@^5.1.2, bn.js@^5.1.3:
8492+
version "5.2.0"
8493+
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
8494+
integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==
8495+
84918496
[email protected], body-parser@^1.16.0:
84928497
version "1.19.0"
84938498
resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.19.0.tgz#96b2709e57c9c4e09a6fd66a8fd979844f69f08a"
@@ -8521,13 +8526,12 @@ boolbase@^1.0.0, boolbase@~1.0.0:
85218526
resolved "https://registry.yarnpkg.com/boolbase/-/boolbase-1.0.0.tgz#68dff5fbe60c51eb37725ea9e3ed310dcc1e776e"
85228527
integrity sha1-aN/1++YMUes3cl6p4+0xDcwed24=
85238528

8524-
borsh@^0.4.0:
8525-
version "0.4.0"
8526-
resolved "https://registry.yarnpkg.com/borsh/-/borsh-0.4.0.tgz#9dd6defe741627f1315eac2a73df61421f6ddb9f"
8527-
integrity sha512-aX6qtLya3K0AkT66CmYWCCDr77qsE9arV05OmdFpmat9qu8Pg9J5tBUPDztAW5fNh/d/MyVG/OYziP52Ndzx1g==
8529+
borsh@^0.7.0:
8530+
version "0.7.0"
8531+
resolved "https://registry.yarnpkg.com/borsh/-/borsh-0.7.0.tgz#6e9560d719d86d90dc589bca60ffc8a6c51fec2a"
8532+
integrity sha512-CLCsZGIBCFnPtkNnieW/a8wmreDmfUtjU2m9yHrzPXIlNbqVs0AQrSatSG6vdNYUqdc83tkQi2eHfF98ubzQLA==
85288533
dependencies:
8529-
"@types/bn.js" "^4.11.5"
8530-
bn.js "^5.0.0"
8534+
bn.js "^5.2.0"
85318535
bs58 "^4.0.0"
85328536
text-encoding-utf-8 "^1.0.2"
85338537

@@ -11965,14 +11969,14 @@ error-ex@^1.2.0, error-ex@^1.3.1:
1196511969
dependencies:
1196611970
is-arrayish "^0.2.1"
1196711971

11968-
error-polyfill@^0.1.2:
11969-
version "0.1.2"
11970-
resolved "https://registry.yarnpkg.com/error-polyfill/-/error-polyfill-0.1.2.tgz#05b27de7d126cbc9d42a96f45faab28a98a0b28b"
11971-
integrity sha512-8uhnXlJuhFkmIfhw2tAHtWQGpXcw5rrc0dhuY3bhn8tBHvh6l0oL9VJvR2suqx9eltglKKhVPv8luPQy+UxLTA==
11972+
error-polyfill@^0.1.3:
11973+
version "0.1.3"
11974+
resolved "https://registry.yarnpkg.com/error-polyfill/-/error-polyfill-0.1.3.tgz#df848b61ad8834f7a5db69a70b9913df86721d15"
11975+
integrity sha512-XHJk60ufE+TG/ydwp4lilOog549iiQF2OAPhkk9DdiYWMrltz5yhDz/xnKuenNwP7gy3dsibssO5QpVhkrSzzg==
1197211976
dependencies:
1197311977
capability "^0.2.5"
1197411978
o3 "^1.0.3"
11975-
u3 "^0.1.0"
11979+
u3 "^0.1.1"
1197611980

1197711981
es-abstract@^1.17.2, es-abstract@^1.18.0-next.1, es-abstract@^1.18.0-next.2:
1197811982
version "1.18.0"
@@ -19429,16 +19433,16 @@ natural-compare@^1.4.0:
1942919433
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
1943019434
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=
1943119435

19432-
near-api-js@0.42.0:
19433-
version "0.42.0"
19434-
resolved "https://registry.yarnpkg.com/near-api-js/-/near-api-js-0.42.0.tgz#1f773791e930a349be65b866752bbae67df4388d"
19435-
integrity sha512-YtDR0sXHL2CclXZdscqUIjoc4Vp/o09CSY1lQ6mygSNbWp5ghLAR1kNBQnALGGQhYC6NLBCzJQJZMb2IQr2WzA==
19436+
near-api-js@1.1.0:
19437+
version "1.1.0"
19438+
resolved "https://registry.yarnpkg.com/near-api-js/-/near-api-js-1.1.0.tgz#907e807f052c1f043c6fbf28f61872de3c02235a"
19439+
integrity sha512-qYKv1mYsaDZc2uYndhS+ttDhR9+60qFc+ZjD6lWsAxr3ZskMjRwPffDGQZYhC7BRDQMe1HEbk6d5mf+TVm0Lqg==
1943619440
dependencies:
19437-
bn.js "5.2.0"
19438-
borsh "^0.4.0"
19441+
bn.js "5.2.1"
19442+
borsh "^0.7.0"
1943919443
bs58 "^4.0.0"
1944019444
depd "^2.0.0"
19441-
error-polyfill "^0.1.2"
19445+
error-polyfill "^0.1.3"
1944219446
http-errors "^1.7.2"
1944319447
js-sha256 "^0.9.0"
1944419448
mustache "^4.0.0"
@@ -26150,10 +26154,10 @@ typical@^2.6.0, typical@^2.6.1:
2615026154
resolved "https://registry.yarnpkg.com/typical/-/typical-2.6.1.tgz#5c080e5d661cbbe38259d2e70a3c7253e873881d"
2615126155
integrity sha1-XAgOXWYcu+OCWdLnCjxyU+hziB0=
2615226156

26153-
u3@^0.1.0:
26154-
version "0.1.0"
26155-
resolved "https://registry.yarnpkg.com/u3/-/u3-0.1.0.tgz#0060927663b68353c539cda99e9511d6687edd9d"
26156-
integrity sha1-AGCSdmO2g1PFOc2pnpUR1mh+3Z0=
26157+
u3@^0.1.1:
26158+
version "0.1.1"
26159+
resolved "https://registry.yarnpkg.com/u3/-/u3-0.1.1.tgz#5f52044f42ee76cd8de33148829e14528494b73b"
26160+
integrity sha512-+J5D5ir763y+Am/QY6hXNRlwljIeRMZMGs0cT6qqZVVzzT3X3nFPXVyPOFRMOR4kupB0T8JnCdpWdp6Q/iXn3w==
2615726161

2615826162
ua-parser-js@^0.7.18:
2615926163
version "0.7.25"

0 commit comments

Comments
 (0)