Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7524d7a
wip
ciaranightingale Jul 5, 2023
de65f04
merge main
ciaranightingale Jul 5, 2023
98c7018
wip
ciaranightingale Jul 12, 2023
820d4cc
Merge branch 'main' into ciara/tba
ciaranightingale Jul 12, 2023
4755ac0
merge main
ciaranightingale Jul 24, 2023
2580d13
wip
ciaranightingale Jul 24, 2023
279b260
update with clientId & secretKey & requested changes
ciaranightingale Jul 24, 2023
c8445b9
Merge branch 'ciara/tba' of https://github.com/thirdweb-dev/js into c…
ciaranightingale Jul 24, 2023
af57f47
Merge branch 'main' into ciara/tba
ciaranightingale Jul 24, 2023
2db5249
fix build
ciaranightingale Jul 24, 2023
73a51fa
wip
ciaranightingale Jul 26, 2023
39e22b9
wip
ciaranightingale Jul 26, 2023
f34654b
Merge branch 'ciara/tba' of https://github.com/thirdweb-dev/js into c…
ciaranightingale Jul 26, 2023
7477b73
restructure
Jul 26, 2023
5ab826b
cleanup
joaquim-verges Jul 27, 2023
909ca2f
Merge branch 'main' into ciara/tba
joaquim-verges Jul 27, 2023
0c9ada2
Merge branch 'main' into ciara/tba
ciaranightingale Jul 27, 2023
cf5bb93
working code & fix factoryAddress required
ciaranightingale Jul 27, 2023
fdee3f5
Merge branch 'main' into ciara/tba
joaquim-verges Aug 9, 2023
0bdddc0
Merge branch 'main' into ciara/tba
joaquim-verges Aug 9, 2023
f11c12a
Merge branch 'main' into ciara/tba
joaquim-verges Oct 13, 2023
e93da7f
fix types
joaquim-verges Oct 14, 2023
6b4642a
linting
joaquim-verges Oct 14, 2023
8a07db2
fix build
joaquim-verges Oct 14, 2023
a8464f6
Merge branch 'main' into ciara/tba
joaquim-verges Oct 24, 2023
4c8dd1c
require account impl for now
joaquim-verges Oct 24, 2023
c9616ed
changeset
joaquim-verges Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
restructure
  • Loading branch information
ikethirdweb committed Jul 26, 2023
commit 7477b730c8be654c462b34d832e35979d69a465b
37 changes: 32 additions & 5 deletions packages/wallets/src/evm/connectors/smart-wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chain, getChainByChainId } from "@thirdweb-dev/chains";
import { Chain, getChainByChainId, getChainBySlug } from "@thirdweb-dev/chains";
import { ConnectParams, Connector } from "../../interfaces/connector";
import { ERC4337EthersProvider } from "./lib/erc4337-provider";
import { getVerifyingPaymaster } from "./lib/paymaster";
Expand Down Expand Up @@ -28,19 +28,21 @@ export class SmartWalletConnector extends Connector<SmartWalletConnectionArgs> {
private aaProvider: ERC4337EthersProvider | undefined;
private accountApi: AccountAPI | undefined;
personalWallet?: EVMWallet;
chainId?: number;

constructor(config: SmartWalletConfig) {
super();
this.config = config;
}

async initialize(personalWallet: EVMWallet, factoryAddress: string = this.config.factoryAddress) {
async initialize(personalWallet: EVMWallet) {
const config = this.config;
const originalProvider = getChainProvider(config.chain, {
clientId: config.clientId,
secretKey: config.secretKey,
}) as providers.BaseProvider;
const chainSlug = await this.getChainSlug(config.chain, originalProvider);
this.chainId = await this.getChainId(config.chain, originalProvider);
const bundlerUrl =
this.config.bundlerUrl || `https://${chainSlug}.bundler.thirdweb.com`;
const paymasterUrl =
Expand All @@ -62,7 +64,7 @@ export class SmartWalletConnector extends Connector<SmartWalletConnectionArgs> {
this.config.secretKey,
)
: undefined,
factoryAddress: factoryAddress,
factoryAddress: config.factoryAddress,
factoryInfo: config.factoryInfo || this.defaultFactoryInfo(),
accountInfo: config.accountInfo || this.defaultAccountInfo(),
clientId: config.clientId,
Expand Down Expand Up @@ -134,7 +136,7 @@ export class SmartWalletConnector extends Connector<SmartWalletConnectionArgs> {
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
updateChains(chains: Chain[]): void { }
updateChains(chains: Chain[]): void {}

/**
* Execute a single transaction
Expand Down Expand Up @@ -220,7 +222,7 @@ export class SmartWalletConnector extends Connector<SmartWalletConnectionArgs> {

protected defaultFactoryInfo(): FactoryContractInfo {
return {
createAccount: async (factory: SmartContract, owner: string) => {
createAccount: async (factory, owner) => {
return factory.prepare("createAccount", [
owner,
ethers.utils.toUtf8Bytes(""),
Expand Down Expand Up @@ -276,4 +278,29 @@ export class SmartWalletConnector extends Connector<SmartWalletConnectionArgs> {
throw new Error(`Invalid network: ${chainOrRpc}`);
}
}

protected async getChainId(
chainOrRpc: ChainOrRpcUrl,
provider: ethers.providers.Provider,
): Promise<number> {
if (typeof chainOrRpc === "object") {
return chainOrRpc.chainId;
}
if (typeof chainOrRpc === "number") {
const chain = getChainByChainId(chainOrRpc);
return chain.chainId;
}
if (typeof chainOrRpc === "string") {
if (chainOrRpc.startsWith("http") || chainOrRpc.startsWith("ws")) {
// if it's a url, try to get the chain id from the provider
const chainId = (await provider.getNetwork()).chainId;
const chain = getChainByChainId(chainId);
return chain.chainId;
}
// otherwise its the network name
return getChainBySlug(chainOrRpc).chainId;
} else {
throw new Error(`Invalid network: ${chainOrRpc}`);
}
}
}
144 changes: 67 additions & 77 deletions packages/wallets/src/evm/connectors/token-bound-smart-wallet/index.ts
Original file line number Diff line number Diff line change
@@ -1,86 +1,76 @@
import {
AccountContractInfo,
FactoryContractInfo,
TokenBoundSmartWalletConfig,
} from "./types"
import { EVMWallet } from "../../interfaces";
import * as module from "../smart-wallet";
AccountContractInfo,
FactoryContractInfo,
TokenBoundSmartWalletConfig,
} from "./types";
import { ethers } from "ethers";
import { SmartContract } from "@thirdweb-dev/sdk";

let SmartWalletConnector = module.SmartWalletConnector;
import { SmartWalletConnector } from "../smart-wallet";
import { getChainByChainId } from "@thirdweb-dev/chains";

export class TokenBoundSmartWalletConnector extends SmartWalletConnector {
protected config: TokenBoundSmartWalletConfig;
protected config: TokenBoundSmartWalletConfig;

constructor(config: TokenBoundSmartWalletConfig) {
const tokenBoundSmartWalletConfig: TokenBoundSmartWalletConfig = {
chain: config.chain,
factoryAddress: config.factoryAddress,
clientId: config.clientId,
secretKey: config.secretKey,
gasless: config.gasless,
bundlerUrl: config.bundlerUrl,
paymasterUrl: config.paymasterUrl,
paymasterAPI: config.paymasterAPI,
entryPointAddress: config.entryPointAddress,
tokenContract: config.tokenContract,
tokenId: config.tokenId,
implementation: config.implementation,
}
super(tokenBoundSmartWalletConfig);
this.config = config;
}
constructor(config: TokenBoundSmartWalletConfig) {
config.factoryAddress =
config.factoryAddress || "0x02101dfB77FDE026414827Fdc604ddAF224F0921";

async initialize(personalWallet: EVMWallet) {
const factoryAddress = this.config.factoryAddress || "0x02101dfB77FDE026414827Fdc604ddAF224F0921";
await super.initialize(personalWallet, factoryAddress);
}
super(config);
this.config = config;
}

protected defaultAccountInfo(): AccountContractInfo {
return {
execute: async (account, target, value, data) => {
return account.prepare("executeCall", [target, value, data]);
},
getNonce: async (account) => {
return account.call("nonce", []);
},
};
}
protected defaultAccountInfo(): AccountContractInfo {
return {
execute: async (account, target, value, data) => {
return account.prepare("executeCall", [target, value, data]);
},
getNonce: async (account) => {
return account.call("nonce", []);
},
};
}

protected defaultFactoryInfo(): FactoryContractInfo {
return {
createAccount: async (factory: SmartContract, owner: string, {
implementation,
chainId,
tokenContract,
tokenId,
salt = 1 // Set default value to 1 here
}: {
implementation: SmartContract,
chainId: Number,
tokenContract: SmartContract,
tokenId: Number,
salt?: Number // This line makes salt optional
}) => {
return factory.prepare("createAccount", [
implementation,
chainId,
tokenContract,
tokenId,
salt,
ethers.utils.toUtf8Bytes(""),
]);
},
getAccountAddress: async (factory, owner, { implementation, chainId, tokenContract, tokenId, salt = 1 }) => {
return await factory.call("address", [
implementation,
chainId,
tokenContract,
tokenId,
salt
]);
},
};
}
}
protected defaultFactoryInfo(): FactoryContractInfo {
return {
createAccount: async (
factory,
owner,
{
implementation = this.config.implementation,
chainId = this.chainId,
tokenContract = this.config.tokenContract,
tokenId = this.config.tokenId,
salt = 1,
},
) => {
return factory.prepare("createAccount", [
implementation,
chainId,
tokenContract,
tokenId,
salt,
ethers.utils.toUtf8Bytes(""),
]);
},
getAccountAddress: async (
factory,
owner,
{
implementation = this.config.implementation,
chainId = this.chainId,
tokenContract = this.config.tokenContract,
tokenId = this.config.tokenId,
salt = 1,
},
) => {
return await factory.call("address", [
implementation,
chainId,
tokenContract,
tokenId,
salt,
]);
},
};
}
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
import type {
SmartContract,
Transaction,
} from "@thirdweb-dev/sdk";
import type {
BigNumber,
BigNumberish,
ContractInterface,
} from "ethers";
import { WalletConnectReceiverConfig } from "../../../core/types/walletConnect";
import type { SmartContract, Transaction } from "@thirdweb-dev/sdk";
import type { BigNumber, BigNumberish, ContractInterface } from "ethers";
import { SmartWalletConfig } from "../smart-wallet/types";

export type TokenBoundSmartWalletConfig = {
tokenContract: SmartContract;
tokenId: Number;
implementation: SmartContract;
} & SmartWalletConfig & ContractInfoInput &
WalletConnectReceiverConfig;
tokenContract: string;
tokenId: Number;
implementation: string;
} & SmartWalletConfig;

export type ContractInfoInput = {
factoryInfo?: FactoryContractInfo;
accountInfo?: AccountContractInfo;
factoryInfo?: FactoryContractInfo;
accountInfo?: AccountContractInfo;
};

export type FactoryContractInfo = {
abi?: ContractInterface;
createAccount: (
factory: SmartContract,
owner: string,
tokenInfo: {
implementation: SmartContract,
chainId: Number,
tokenContract: SmartContract,
tokenId: Number,
salt?: Number
}
) => Promise<Transaction>;
getAccountAddress: (
factory: SmartContract,
owner: string,
tokenInfo: {
implementation: SmartContract,
chainId: Number,
tokenContract: SmartContract,
tokenId: Number,
salt: Number
}
) => Promise<string>;
abi?: ContractInterface;
createAccount: (
factory: SmartContract,
owner: string,
tokenInfo: {
implementation: string;
chainId: Number;
tokenContract: string;
tokenId: Number;
salt?: Number;
},
) => Promise<Transaction>;
getAccountAddress: (
factory: SmartContract,
owner: string,
tokenInfo: {
implementation: string;
chainId: Number;
tokenContract: string;
tokenId: Number;
salt: Number;
},
) => Promise<string>;
};

export type AccountContractInfo = {
abi?: ContractInterface;
getNonce: (account: SmartContract) => Promise<BigNumber>;
execute: (
account: SmartContract,
target: string,
value: BigNumberish,
data: string,
) => Promise<Transaction>;
};
abi?: ContractInterface;
getNonce: (account: SmartContract) => Promise<BigNumber>;
execute: (
account: SmartContract,
target: string,
value: BigNumberish,
data: string,
) => Promise<Transaction>;
};
Loading