-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from babylonlabs-io/feat/ui-components
feat: add state & core functionality
- Loading branch information
Showing
20 changed files
with
1,069 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { IWallet, Network, IProvider, type NetworkConfig, type WalletMetadata } from "@/core/types"; | ||
|
||
interface Options<P extends IProvider> { | ||
id: string; | ||
name: string; | ||
icon: string; | ||
docs: string; | ||
networks: Network[]; | ||
origin: any; | ||
provider: P | null; | ||
} | ||
|
||
const defaultWalletGetter = (key: string) => (context: any) => context[key]; | ||
|
||
export class Wallet<P extends IProvider> implements IWallet { | ||
readonly id: string; | ||
readonly origin: any; | ||
readonly name: string; | ||
readonly icon: string; | ||
readonly docs: string; | ||
readonly networkds: Network[]; | ||
readonly provider: P | null = null; | ||
|
||
static create = async <P extends IProvider>(metadata: WalletMetadata<P>, context: any, config: NetworkConfig) => { | ||
const { | ||
id, | ||
wallet: walletGetter, | ||
name: nameGetter, | ||
icon: iconGetter, | ||
docs = "", | ||
networks = [], | ||
createProvider, | ||
} = metadata; | ||
|
||
const options: Options<P> = { | ||
id, | ||
name: "", | ||
icon: "", | ||
origin: null, | ||
provider: null, | ||
docs, | ||
networks, | ||
}; | ||
|
||
if (walletGetter) { | ||
const getWallet = typeof walletGetter === "string" ? defaultWalletGetter(walletGetter) : walletGetter; | ||
|
||
options.origin = getWallet(context, config) ?? null; | ||
options.provider = options.origin ? createProvider(options.origin, config) : null; | ||
} else { | ||
options.origin = null; | ||
options.provider = createProvider(null, config); | ||
} | ||
|
||
if (typeof nameGetter === "string") { | ||
options.name = nameGetter ?? ""; | ||
} else { | ||
options.name = options.origin ? await nameGetter(options.origin, config) : ""; | ||
} | ||
|
||
if (typeof iconGetter === "string") { | ||
options.icon = iconGetter ?? ""; | ||
} else { | ||
options.icon = options.origin ? await iconGetter(options.origin, config) : ""; | ||
} | ||
|
||
return new Wallet(options); | ||
}; | ||
|
||
constructor({ id, origin, name, icon, docs, networks, provider }: Options<P>) { | ||
this.id = id; | ||
this.origin = origin; | ||
this.name = name; | ||
this.icon = icon; | ||
this.docs = docs; | ||
this.networkds = networks; | ||
this.provider = provider; | ||
} | ||
|
||
get installed() { | ||
return Boolean(this.provider); | ||
} | ||
|
||
async connect() { | ||
if (!this.provider) { | ||
throw Error("Provider not found"); | ||
} | ||
|
||
await this.provider.connectWallet(); | ||
|
||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { Wallet } from "@/core/Wallet"; | ||
import type { NetworkConfig, IProvider, IChain, ConnectMetadata } from "@/core/types"; | ||
|
||
export class WalletConnector<P extends IProvider> implements IChain { | ||
connectedWallet: Wallet<P> | null = null; | ||
|
||
static async create<P extends IProvider>( | ||
metadata: ConnectMetadata<P>, | ||
config: NetworkConfig, | ||
context: any, | ||
): Promise<WalletConnector<P>> { | ||
const wallets: Wallet<P>[] = []; | ||
|
||
for (const walletMetadata of metadata.wallets) { | ||
wallets.push(await Wallet.create(walletMetadata, context, config)); | ||
} | ||
|
||
return new WalletConnector(metadata.chain, metadata.icon, wallets); | ||
} | ||
|
||
constructor( | ||
public readonly chain: string, | ||
public readonly icon: string, | ||
public readonly wallets: Wallet<P>[], | ||
) {} | ||
|
||
async connect(name: string) { | ||
const wallet = this.wallets.find((wallet) => wallet.name.toLowerCase() === name.toLowerCase()); | ||
|
||
this.connectedWallet = (await wallet?.connect()) ?? null; | ||
|
||
return this.connectedWallet; | ||
} | ||
} |
Oops, something went wrong.