Skip to content

Commit 2c362fc

Browse files
authored
Merge pull request #58 from tokenbound/jw/create_account_chainid
Added chainId parameter to account functions
2 parents e1b62da + f847c13 commit 2c362fc

File tree

11 files changed

+198
-151
lines changed

11 files changed

+198
-151
lines changed

README.md

Lines changed: 99 additions & 92 deletions
Large diffs are not rendered by default.

examples/vite-wagmi-ethers-rainbowkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0",
1919
"util": "^0.12.5",
20-
"viem": "^1.19.9",
20+
"viem": "^1.19.11",
2121
"wagmi": "^1.4.7"
2222
},
2323
"devDependencies": {

examples/vite-wagmi-ethers/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0",
1919
"util": "^0.12.5",
20-
"viem": "^1.19.9",
20+
"viem": "^1.19.11",
2121
"wagmi": "^1.4.7"
2222
},
2323
"devDependencies": {

examples/vite-wagmi-ethers6/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0",
1919
"util": "^0.12.5",
20-
"viem": "^1.19.9",
20+
"viem": "^1.19.11",
2121
"wagmi": "^1.4.7"
2222
},
2323
"devDependencies": {

examples/vite-wagmi-viem/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"react": "^18.2.0",
1818
"react-dom": "^18.2.0",
1919
"util": "^0.12.5",
20-
"viem": "^1.19.9",
20+
"viem": "^1.19.11",
2121
"wagmi": "^1.4.7"
2222
},
2323
"devDependencies": {

packages/sdk/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"wagmi": "wagmi generate"
2626
},
2727
"dependencies": {
28-
"viem": "^1.19.9"
28+
"viem": "^1.19.11"
2929
},
3030
"devDependencies": {
3131
"@tanstack/react-query": "4.29.1",
@@ -45,7 +45,7 @@
4545
"react": "^18.2.0",
4646
"react-dom": "^18.2.0",
4747
"typescript": "^5.2.2",
48-
"viem": "^1.19.9",
48+
"viem": "^1.19.11",
4949
"wagmi": "^1.4.7",
5050
"@viem/anvil": "^0.0.6",
5151
"vite": "^4.4.9",

packages/sdk/src/TokenboundClient.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,14 @@ class TokenboundClient {
169169
* @returns The tokenbound account address.
170170
*/
171171
public getAccount(params: GetAccountParams): `0x${string}` {
172-
const { tokenContract, tokenId, salt = 0 } = params
172+
const { tokenContract, tokenId, salt = 0, chainId = this.chainId } = params
173173

174174
try {
175175
const getAcct = this.supportsV3 ? getTokenboundV3Account : computeAccount
176176
return getAcct(
177177
tokenContract,
178178
tokenId,
179-
this.chainId,
179+
chainId,
180180
this.implementationAddress,
181181
this.registryAddress,
182182
salt
@@ -200,14 +200,14 @@ class TokenboundClient {
200200
data: `0x${string}`
201201
}
202202
> {
203-
const { tokenContract, tokenId, salt = 0, appendedCalls = [] } = params
203+
const { tokenContract, tokenId, salt = 0, chainId = this.chainId, appendedCalls = [] } = params
204204

205205
const getAcct = this.supportsV3 ? getTokenboundV3Account : computeAccount
206206

207207
const computedAcct = getAcct(
208208
tokenContract,
209209
tokenId,
210-
this.chainId,
210+
chainId,
211211
this.implementationAddress,
212212
this.registryAddress,
213213
salt
@@ -225,7 +225,7 @@ class TokenboundClient {
225225
const preparedBasicCreateAccount = await prepareBasicCreateAccount(
226226
tokenContract,
227227
tokenId,
228-
this.chainId,
228+
chainId,
229229
this.implementationAddress,
230230
this.registryAddress,
231231
salt
@@ -283,7 +283,7 @@ class TokenboundClient {
283283
public async createAccount(
284284
params: CreateAccountParams
285285
): Promise<{ account: `0x${string}`; txHash: `0x${string}` }> {
286-
const { tokenContract, tokenId, salt = 0, appendedCalls = [] } = params
286+
const { tokenContract, tokenId, salt = 0, chainId = this.chainId, appendedCalls = [] } = params
287287

288288
try {
289289
let txHash: `0x${string}` | undefined
@@ -293,7 +293,7 @@ class TokenboundClient {
293293
const computedAcct = getAcct(
294294
tokenContract,
295295
tokenId,
296-
this.chainId,
296+
chainId,
297297
this.implementationAddress,
298298
this.registryAddress,
299299
salt
@@ -302,6 +302,7 @@ class TokenboundClient {
302302
const preparedCreateAccount = await this.prepareCreateAccount({
303303
tokenContract,
304304
tokenId,
305+
chainId,
305306
salt,
306307
appendedCalls,
307308
})
@@ -323,7 +324,8 @@ class TokenboundClient {
323324
this.walletClient,
324325
this.implementationAddress,
325326
this.registryAddress,
326-
salt
327+
salt,
328+
chainId
327329
)
328330
}
329331

packages/sdk/src/functions/viemV2.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ export async function createAccount(
121121
client: WalletClient,
122122
implementationAddress?: `0x${string}`,
123123
registryAddress?: `0x${string}`,
124-
salt?: number
124+
salt?: number,
125+
chainId?: number
125126
): Promise<`0x${string}`> {
126127
salt = salt ?? 0
127128
const implementation = implementationAddress
@@ -137,7 +138,7 @@ export async function createAccount(
137138
walletClient: client,
138139
})
139140

140-
const chainId = await client.getChainId()
141+
const _chainId = chainId || (await client.getChainId())
141142

142143
const initData = encodeFunctionData({
143144
abi: [
@@ -154,7 +155,7 @@ export async function createAccount(
154155

155156
return registry.write.createAccount([
156157
implementation,
157-
chainId,
158+
_chainId,
158159
tokenContract,
159160
tokenId,
160161
salt,

packages/sdk/src/test/TestAll.test.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import {
1919
getContract,
2020
encodeAbiParameters,
2121
parseAbiParameters,
22+
isAddress,
2223
} from 'viem'
2324
import { privateKeyToAccount } from 'viem/accounts'
24-
import { CreateAccountParams, TokenboundClient } from '@tokenbound/sdk'
2525
import {
2626
ADDRESS_REGEX,
2727
ANVIL_ACCOUNTS,
@@ -43,6 +43,7 @@ import { ERC_6551_DEFAULT, ERC_6551_LEGACY_V2 } from '../constants'
4343
import { Call3, TBImplementationVersion, TBVersion } from '../types'
4444
import { JsonRpcSigner, JsonRpcProvider } from 'ethers6'
4545
import { erc20ABI } from 'wagmi'
46+
import { CreateAccountParams, TokenboundClient } from '@tokenbound/sdk'
4647

4748
export const pool = Number(process.env.VITEST_POOL_ID ?? 1)
4849

@@ -69,7 +70,7 @@ type TestConfig = {
6970
version?: TBImplementationVersion
7071
}
7172

72-
const ENABLED_TESTS: Array<TestConfig> = [
73+
export const ENABLED_TESTS: Array<TestConfig> = [
7374
{
7475
testName: 'viem v2',
7576
walletClient,
@@ -286,6 +287,38 @@ describe.each(ENABLED_TESTS)(
286287
TIMEOUT
287288
)
288289

290+
it(
291+
'can createAccount with a custom chainId',
292+
async () => {
293+
const HARDHAT_CHAIN_ID = 31337
294+
295+
const { account, txHash } = await tokenboundClient.createAccount({
296+
...NFT_IN_EOA,
297+
chainId: HARDHAT_CHAIN_ID,
298+
})
299+
console.log('CREATED ACCT WITH CUSTOM CHAIN ID', account)
300+
301+
const createdAccountTxReceipt = await publicClient.waitForTransactionReceipt({
302+
hash: txHash,
303+
})
304+
305+
const bytecode = await tokenboundClient.deconstructBytecode({
306+
accountAddress: account,
307+
})
308+
309+
if (!bytecode) return false
310+
311+
const { chainId } = bytecode
312+
313+
console.log('CHAINID OF CREATED ACCT', chainId)
314+
315+
expect(isAddress(account)).toEqual(true)
316+
expect(createdAccountTxReceipt.status).toBe('success')
317+
expect(chainId).toBe(HARDHAT_CHAIN_ID)
318+
},
319+
TIMEOUT
320+
)
321+
289322
v3OnlyIt(
290323
'can createAccount and append multicall transaction(s) that use the deployed TBA',
291324
async () => {

packages/sdk/src/types/params.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ type Custom6551Implementation = Prettify<{
7676
salt?: number
7777
}>
7878

79-
export type TBAccountParams = NFTParams
79+
export type TBAccountParams = Prettify<
80+
NFTParams & {
81+
chainId?: number
82+
}
83+
>
8084

8185
export type GetAccountParams = Prettify<
8286
TBAccountParams & Partial<Custom6551Implementation>

0 commit comments

Comments
 (0)