salt-sdk 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Salt SDK
2
+
3
+ ## Usage
4
+
5
+ ```typescript
6
+ import { Salt } from 'salt-sdk';
7
+ import { ethers } from 'ethers';
8
+
9
+ // Get an ethers signer & wallet
10
+ let provider = new ethers.providers.JsonRpcProvider('your-rpc-provider');
11
+ let walletWithProvider = new ethers.Wallet('', provider);
12
+
13
+ const sdk = new Salt();
14
+ await sdk.authenticate();
15
+
16
+ await sdk.transfer({
17
+ accountId: '6824aa9c27c0fa91b32800a4',
18
+ to: '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE',
19
+ value: '1000',
20
+ chainId: 421614,
21
+ decimals: 18,
22
+ type: 'Native',
23
+ signer: walletWithProvider,
24
+ sendingProvider: provider,
25
+ });
26
+ ```
@@ -0,0 +1,160 @@
1
+ import { B as BaseError, g as getUrl, s as stringify, d as decodeErrorResult, i as isAddressEqual, l as localBatchGatewayUrl, a as localBatchGatewayRequest, c as call, b as concat, e as encodeAbiParameters, H as HttpRequestError, f as isHex } from "./index-_fV99sY9.js";
2
+ class OffchainLookupError extends BaseError {
3
+ constructor({ callbackSelector, cause, data, extraData, sender, urls }) {
4
+ super(cause.shortMessage || "An error occurred while fetching for an offchain result.", {
5
+ cause,
6
+ metaMessages: [
7
+ ...cause.metaMessages || [],
8
+ cause.metaMessages?.length ? "" : [],
9
+ "Offchain Gateway Call:",
10
+ urls && [
11
+ " Gateway URL(s):",
12
+ ...urls.map((url) => ` ${getUrl(url)}`)
13
+ ],
14
+ ` Sender: ${sender}`,
15
+ ` Data: ${data}`,
16
+ ` Callback selector: ${callbackSelector}`,
17
+ ` Extra data: ${extraData}`
18
+ ].flat(),
19
+ name: "OffchainLookupError"
20
+ });
21
+ }
22
+ }
23
+ class OffchainLookupResponseMalformedError extends BaseError {
24
+ constructor({ result, url }) {
25
+ super("Offchain gateway response is malformed. Response data must be a hex value.", {
26
+ metaMessages: [
27
+ `Gateway URL: ${getUrl(url)}`,
28
+ `Response: ${stringify(result)}`
29
+ ],
30
+ name: "OffchainLookupResponseMalformedError"
31
+ });
32
+ }
33
+ }
34
+ class OffchainLookupSenderMismatchError extends BaseError {
35
+ constructor({ sender, to }) {
36
+ super("Reverted sender address does not match target contract address (`to`).", {
37
+ metaMessages: [
38
+ `Contract address: ${to}`,
39
+ `OffchainLookup sender address: ${sender}`
40
+ ],
41
+ name: "OffchainLookupSenderMismatchError"
42
+ });
43
+ }
44
+ }
45
+ const offchainLookupSignature = "0x556f1830";
46
+ const offchainLookupAbiItem = {
47
+ name: "OffchainLookup",
48
+ type: "error",
49
+ inputs: [
50
+ {
51
+ name: "sender",
52
+ type: "address"
53
+ },
54
+ {
55
+ name: "urls",
56
+ type: "string[]"
57
+ },
58
+ {
59
+ name: "callData",
60
+ type: "bytes"
61
+ },
62
+ {
63
+ name: "callbackFunction",
64
+ type: "bytes4"
65
+ },
66
+ {
67
+ name: "extraData",
68
+ type: "bytes"
69
+ }
70
+ ]
71
+ };
72
+ async function offchainLookup(client, { blockNumber, blockTag, data, to }) {
73
+ const { args } = decodeErrorResult({
74
+ data,
75
+ abi: [offchainLookupAbiItem]
76
+ });
77
+ const [sender, urls, callData, callbackSelector, extraData] = args;
78
+ const { ccipRead } = client;
79
+ const ccipRequest_ = ccipRead && typeof ccipRead?.request === "function" ? ccipRead.request : ccipRequest;
80
+ try {
81
+ if (!isAddressEqual(to, sender))
82
+ throw new OffchainLookupSenderMismatchError({ sender, to });
83
+ const result = urls.includes(localBatchGatewayUrl) ? await localBatchGatewayRequest({
84
+ data: callData,
85
+ ccipRequest: ccipRequest_
86
+ }) : await ccipRequest_({ data: callData, sender, urls });
87
+ const { data: data_ } = await call(client, {
88
+ blockNumber,
89
+ blockTag,
90
+ data: concat([
91
+ callbackSelector,
92
+ encodeAbiParameters([{ type: "bytes" }, { type: "bytes" }], [result, extraData])
93
+ ]),
94
+ to
95
+ });
96
+ return data_;
97
+ } catch (err) {
98
+ throw new OffchainLookupError({
99
+ callbackSelector,
100
+ cause: err,
101
+ data,
102
+ extraData,
103
+ sender,
104
+ urls
105
+ });
106
+ }
107
+ }
108
+ async function ccipRequest({ data, sender, urls }) {
109
+ let error = new Error("An unknown error occurred.");
110
+ for (let i = 0; i < urls.length; i++) {
111
+ const url = urls[i];
112
+ const method = url.includes("{data}") ? "GET" : "POST";
113
+ const body = method === "POST" ? { data, sender } : void 0;
114
+ const headers = method === "POST" ? { "Content-Type": "application/json" } : {};
115
+ try {
116
+ const response = await fetch(url.replace("{sender}", sender.toLowerCase()).replace("{data}", data), {
117
+ body: JSON.stringify(body),
118
+ headers,
119
+ method
120
+ });
121
+ let result;
122
+ if (response.headers.get("Content-Type")?.startsWith("application/json")) {
123
+ result = (await response.json()).data;
124
+ } else {
125
+ result = await response.text();
126
+ }
127
+ if (!response.ok) {
128
+ error = new HttpRequestError({
129
+ body,
130
+ details: result?.error ? stringify(result.error) : response.statusText,
131
+ headers: response.headers,
132
+ status: response.status,
133
+ url
134
+ });
135
+ continue;
136
+ }
137
+ if (!isHex(result)) {
138
+ error = new OffchainLookupResponseMalformedError({
139
+ result,
140
+ url
141
+ });
142
+ continue;
143
+ }
144
+ return result;
145
+ } catch (err) {
146
+ error = new HttpRequestError({
147
+ body,
148
+ details: err.message,
149
+ url
150
+ });
151
+ }
152
+ }
153
+ throw error;
154
+ }
155
+ export {
156
+ ccipRequest,
157
+ offchainLookup,
158
+ offchainLookupAbiItem,
159
+ offchainLookupSignature
160
+ };