Skip to content

Commit

Permalink
Merge pull request #58 from tokenbound/jw/create_account_chainid
Browse files Browse the repository at this point in the history
Added chainId parameter to account functions
  • Loading branch information
bjfresh authored Nov 30, 2023
2 parents e1b62da + f847c13 commit 2c362fc
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 151 deletions.
191 changes: 99 additions & 92 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/vite-wagmi-ethers-rainbowkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.19.9",
"viem": "^1.19.11",
"wagmi": "^1.4.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-ethers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.19.9",
"viem": "^1.19.11",
"wagmi": "^1.4.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-ethers6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.19.9",
"viem": "^1.19.11",
"wagmi": "^1.4.7"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion examples/vite-wagmi-viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"util": "^0.12.5",
"viem": "^1.19.9",
"viem": "^1.19.11",
"wagmi": "^1.4.7"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"wagmi": "wagmi generate"
},
"dependencies": {
"viem": "^1.19.9"
"viem": "^1.19.11"
},
"devDependencies": {
"@tanstack/react-query": "4.29.1",
Expand All @@ -45,7 +45,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "^5.2.2",
"viem": "^1.19.9",
"viem": "^1.19.11",
"wagmi": "^1.4.7",
"@viem/anvil": "^0.0.6",
"vite": "^4.4.9",
Expand Down
18 changes: 10 additions & 8 deletions packages/sdk/src/TokenboundClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,14 +169,14 @@ class TokenboundClient {
* @returns The tokenbound account address.
*/
public getAccount(params: GetAccountParams): `0x${string}` {
const { tokenContract, tokenId, salt = 0 } = params
const { tokenContract, tokenId, salt = 0, chainId = this.chainId } = params

try {
const getAcct = this.supportsV3 ? getTokenboundV3Account : computeAccount
return getAcct(
tokenContract,
tokenId,
this.chainId,
chainId,
this.implementationAddress,
this.registryAddress,
salt
Expand All @@ -200,14 +200,14 @@ class TokenboundClient {
data: `0x${string}`
}
> {
const { tokenContract, tokenId, salt = 0, appendedCalls = [] } = params
const { tokenContract, tokenId, salt = 0, chainId = this.chainId, appendedCalls = [] } = params

const getAcct = this.supportsV3 ? getTokenboundV3Account : computeAccount

const computedAcct = getAcct(
tokenContract,
tokenId,
this.chainId,
chainId,
this.implementationAddress,
this.registryAddress,
salt
Expand All @@ -225,7 +225,7 @@ class TokenboundClient {
const preparedBasicCreateAccount = await prepareBasicCreateAccount(
tokenContract,
tokenId,
this.chainId,
chainId,
this.implementationAddress,
this.registryAddress,
salt
Expand Down Expand Up @@ -283,7 +283,7 @@ class TokenboundClient {
public async createAccount(
params: CreateAccountParams
): Promise<{ account: `0x${string}`; txHash: `0x${string}` }> {
const { tokenContract, tokenId, salt = 0, appendedCalls = [] } = params
const { tokenContract, tokenId, salt = 0, chainId = this.chainId, appendedCalls = [] } = params

try {
let txHash: `0x${string}` | undefined
Expand All @@ -293,7 +293,7 @@ class TokenboundClient {
const computedAcct = getAcct(
tokenContract,
tokenId,
this.chainId,
chainId,
this.implementationAddress,
this.registryAddress,
salt
Expand All @@ -302,6 +302,7 @@ class TokenboundClient {
const preparedCreateAccount = await this.prepareCreateAccount({
tokenContract,
tokenId,
chainId,
salt,
appendedCalls,
})
Expand All @@ -323,7 +324,8 @@ class TokenboundClient {
this.walletClient,
this.implementationAddress,
this.registryAddress,
salt
salt,
chainId
)
}

Expand Down
7 changes: 4 additions & 3 deletions packages/sdk/src/functions/viemV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export async function createAccount(
client: WalletClient,
implementationAddress?: `0x${string}`,
registryAddress?: `0x${string}`,
salt?: number
salt?: number,
chainId?: number
): Promise<`0x${string}`> {
salt = salt ?? 0
const implementation = implementationAddress
Expand All @@ -137,7 +138,7 @@ export async function createAccount(
walletClient: client,
})

const chainId = await client.getChainId()
const _chainId = chainId || (await client.getChainId())

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

return registry.write.createAccount([
implementation,
chainId,
_chainId,
tokenContract,
tokenId,
salt,
Expand Down
37 changes: 35 additions & 2 deletions packages/sdk/src/test/TestAll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import {
getContract,
encodeAbiParameters,
parseAbiParameters,
isAddress,
} from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { CreateAccountParams, TokenboundClient } from '@tokenbound/sdk'
import {
ADDRESS_REGEX,
ANVIL_ACCOUNTS,
Expand All @@ -43,6 +43,7 @@ import { ERC_6551_DEFAULT, ERC_6551_LEGACY_V2 } from '../constants'
import { Call3, TBImplementationVersion, TBVersion } from '../types'
import { JsonRpcSigner, JsonRpcProvider } from 'ethers6'
import { erc20ABI } from 'wagmi'
import { CreateAccountParams, TokenboundClient } from '@tokenbound/sdk'

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

Expand All @@ -69,7 +70,7 @@ type TestConfig = {
version?: TBImplementationVersion
}

const ENABLED_TESTS: Array<TestConfig> = [
export const ENABLED_TESTS: Array<TestConfig> = [
{
testName: 'viem v2',
walletClient,
Expand Down Expand Up @@ -286,6 +287,38 @@ describe.each(ENABLED_TESTS)(
TIMEOUT
)

it(
'can createAccount with a custom chainId',
async () => {
const HARDHAT_CHAIN_ID = 31337

const { account, txHash } = await tokenboundClient.createAccount({
...NFT_IN_EOA,
chainId: HARDHAT_CHAIN_ID,
})
console.log('CREATED ACCT WITH CUSTOM CHAIN ID', account)

const createdAccountTxReceipt = await publicClient.waitForTransactionReceipt({
hash: txHash,
})

const bytecode = await tokenboundClient.deconstructBytecode({
accountAddress: account,
})

if (!bytecode) return false

const { chainId } = bytecode

console.log('CHAINID OF CREATED ACCT', chainId)

expect(isAddress(account)).toEqual(true)
expect(createdAccountTxReceipt.status).toBe('success')
expect(chainId).toBe(HARDHAT_CHAIN_ID)
},
TIMEOUT
)

v3OnlyIt(
'can createAccount and append multicall transaction(s) that use the deployed TBA',
async () => {
Expand Down
6 changes: 5 additions & 1 deletion packages/sdk/src/types/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ type Custom6551Implementation = Prettify<{
salt?: number
}>

export type TBAccountParams = NFTParams
export type TBAccountParams = Prettify<
NFTParams & {
chainId?: number
}
>

export type GetAccountParams = Prettify<
TBAccountParams & Partial<Custom6551Implementation>
Expand Down
Loading

0 comments on commit 2c362fc

Please sign in to comment.