@tetherto/wdk-wallet-evm 1.0.0-beta.7 → 1.0.0-beta.8

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tetherto/wdk-wallet-evm",
3
- "version": "1.0.0-beta.7",
3
+ "version": "1.0.0-beta.8",
4
4
  "description": "A simple package to manage BIP-32 wallets for evm blockchains.",
5
5
  "keywords": [
6
6
  "wdk",
@@ -0,0 +1,75 @@
1
+ // Copyright 2024 Tether Operations Limited
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ 'use strict'
16
+
17
+ import { AbiCoder, concat, decodeBase64, hexlify } from 'ethers'
18
+
19
+ /** @typedef {import('ethers').Provider} Provider */
20
+
21
+ /**
22
+ * @internal
23
+ * @typedef {Object} MulticallRequest
24
+ * @property {string} to - The target contract address.
25
+ * @property {string} data - The ABI-encoded calldata.
26
+ */
27
+
28
+ /**
29
+ * @internal
30
+ * @typedef {Object} MulticallResult
31
+ * @property {boolean} status - Whether the call succeeded.
32
+ * @property {string} data - The ABI-encoded return data.
33
+ */
34
+
35
+ /**
36
+ * Compiled multicall constructor bytecode. Executes calls via staticcall and
37
+ * returns results without deploying. Source: ethers-io/ext-provider-multicall (MIT).
38
+ *
39
+ * @type {string}
40
+ */
41
+ const _MULTICALL_BYTECODE = hexlify(decodeBase64('YIBgQFI0gBVhABBXYACA/VtQYEBRYQSxOAOAYQSxgzmBAWBAgZBSYQAvkWECWlZbYACBUWAB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AfgwESYQNoV2AAgP1bgVFgAWBAYAIKA4ERFWEDgVdhA4FhAa9WW2EDlGAfggFgHxkWYCABYQIGVluBgVKLYCCDhgEBERVhA6lXYACA/VthA7qCYCCDAWAghwFhAjZWW4BgIIQBUlBQgIRSUFBgIIIBkVBgIIMBklBhAt9WW5eWUFBQUFBQUFZbf05Ie3EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYABSYDJgBFJgJGAA/VtgAGBAggGEg1JgQGAghAFSgIRRgINSYGCFAZFQYGBgIIIChgEBklBgIIYBYABbgoEQFWEEo1dgXxmHhgMBhFKBUYBRFRWGUmAggQFRkFBgQGAghwFSgFGAYECIAVJhBICBYGCJAWAghQFhAjZWW2AfAWAfGRaVkJUBYGABlFBgIJOEAZORkJEBkGABAWEEQFZbUJKXllBQUFBQUFBW/g=='))
42
+
43
+ /**
44
+ * Executes multiple read-only contract calls in a single RPC round-trip.
45
+ *
46
+ * @internal
47
+ * @param {Provider} provider - An ethers provider.
48
+ * @param {MulticallRequest[]} calls - The calls to batch.
49
+ * @returns {Promise<MulticallResult[]>} The result of each call.
50
+ */
51
+ export async function multicall (provider, calls) {
52
+ if (calls.length === 0) {
53
+ return []
54
+ }
55
+
56
+ const data = concat([
57
+ _MULTICALL_BYTECODE,
58
+ AbiCoder.defaultAbiCoder().encode(
59
+ ['tuple(address, bytes)[]'],
60
+ [calls.map(call => [call.to, call.data])]
61
+ )
62
+ ])
63
+
64
+ const resultData = await provider.call({ data })
65
+
66
+ const [, results] = AbiCoder.defaultAbiCoder().decode(
67
+ ['uint', 'tuple(bool, bytes)[]'],
68
+ resultData
69
+ )
70
+
71
+ return results.map(result => ({
72
+ status: result[0],
73
+ data: result[1]
74
+ }))
75
+ }
@@ -16,7 +16,8 @@
16
16
 
17
17
  import { WalletAccountReadOnly } from '@tetherto/wdk-wallet'
18
18
 
19
- import { BrowserProvider, Contract, JsonRpcProvider, verifyMessage, verifyTypedData } from 'ethers'
19
+ import { BrowserProvider, Contract, Interface, JsonRpcProvider, verifyMessage, verifyTypedData } from 'ethers'
20
+ import { multicall } from './multicall.js'
20
21
 
21
22
  /** @typedef {import('ethers').Provider} Provider */
22
23
  /** @typedef {import('ethers').Eip1193Provider} Eip1193Provider */
@@ -132,6 +133,43 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
132
133
  return balance
133
134
  }
134
135
 
136
+ /**
137
+ * Returns the account balances for multiple tokens.
138
+ *
139
+ * @param {string[]} tokenAddresses - The smart contract addresses of the tokens.
140
+ * @returns {Promise<Record<string, bigint>>} A mapping of token addresses to their balances (in base units).
141
+ */
142
+ async getTokenBalances (tokenAddresses) {
143
+ if (!this._provider) {
144
+ throw new Error(
145
+ 'The wallet must be connected to a provider to retrieve token balances.'
146
+ )
147
+ }
148
+
149
+ if (tokenAddresses.length === 0) {
150
+ return {}
151
+ }
152
+
153
+ const address = await this.getAddress()
154
+ const iface = new Interface(['function balanceOf(address owner) view returns (uint256)'])
155
+ const calldata = iface.encodeFunctionData('balanceOf', [address])
156
+
157
+ const calls = tokenAddresses.map(tokenAddress => ({
158
+ to: tokenAddress,
159
+ data: calldata
160
+ }))
161
+
162
+ const results = await multicall(this._provider, calls)
163
+
164
+ return tokenAddresses.reduce((acc, tokenAddress, index) => {
165
+ const result = results[index]
166
+ acc[tokenAddress] = result.status
167
+ ? iface.decodeFunctionResult('balanceOf', result.data)[0]
168
+ : 0n
169
+ return acc
170
+ }, {})
171
+ }
172
+
135
173
  /**
136
174
  * Quotes the costs of a send transaction operation.
137
175
  *
@@ -47,6 +47,13 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
47
47
  * @returns {Promise<bigint>} The token balance (in base unit).
48
48
  */
49
49
  getTokenBalance(tokenAddress: string): Promise<bigint>;
50
+ /**
51
+ * Returns the account balances for multiple tokens.
52
+ *
53
+ * @param {string[]} tokenAddresses - The smart contract addresses of the tokens.
54
+ * @returns {Promise<Record<string, bigint>>} A mapping of token addresses to their balances (in base units).
55
+ */
56
+ getTokenBalances(tokenAddresses: string[]): Promise<Record<string, bigint>>;
50
57
  /**
51
58
  * Quotes the costs of a send transaction operation.
52
59
  *