Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature- update appMetadata #1779

Merged
merged 20 commits into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
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
12 changes: 12 additions & 0 deletions docs/src/routes/docs/[...3]modules/[...1]core/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ type RecommendedInjectedWallets = {
}
```

**`updateAppMetadata`**

If you need to update your Application Metadata after initialization, you can call the `updateAppMetadata` function with the new configuration

```typescript
onboard.state.actions.updateAppMetadata({
logo: `<svg width="100%" height="100%" viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 0L0.0100002 6L4 10L0.0100002 14.01L0 20H12V14L8 10L12 6.01V0H0ZM10 14.5V18H2V14.5L6 10.5L10 14.5Z" fill="#929BED"/>
</svg>`,
description: 'Updated Description!'
})
```

---

#### connect
Expand Down
8 changes: 4 additions & 4 deletions docs/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4437,10 +4437,10 @@
"@walletconnect/window-getters" "^1.0.1"
tslib "1.14.1"

"@web3-onboard/blocto@^2.0.0":
version "2.0.0-alpha.1"
resolved "https://registry.yarnpkg.com/@web3-onboard/blocto/-/blocto-2.0.0-alpha.1.tgz#8781646d98520b80be090c622e218b9dfea12221"
integrity sha512-YBhsjkVdL6Z/SXoQeFNt5Rjj1WxlXg1oKOBOZOqfioPuMwmi8RJOLcLi7evhVUdGs5T8sYCBWg+eDeA/K6XK7w==
"@web3-onboard/blocto@^2.0.0-alpha.1":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@web3-onboard/blocto/-/blocto-2.0.0.tgz#eb631e911b7c13ad11089bb3574e97b673f14463"
integrity sha512-99/YzRuE9VBXWVLI6tAKxwFvCXq7WV24a3rM76yY5yVEEeNfewh8PWQ/2Yfz71Vfi6YWVPKzGo3LHspz4caTzA==
dependencies:
"@blocto/sdk" "^0.4.6"
"@web3-onboard/common" "^2.3.1"
Expand Down
14 changes: 14 additions & 0 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,20 @@ type RecommendedInjectedWallets = {
}
```

**`updateAppMetadata`**

If you need to update your Application Metadata after initialization, you can call the `updateAppMetadata` function with the new configuration

```typescript
onboard.state.actions.updateAppMetadata({
logo: `<svg width="100%" height="100%" viewBox="0 0 12 20" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M0 0L0.0100002 6L4 10L0.0100002 14.01L0 20H12V14L8 10L12 6.01V0H0ZM10 14.5V18H2V14.5L6 10.5L10 14.5Z" fill="#929BED"/>
</svg>`,
description: 'Updated Description!'
})
```

---

**`connect`**
An object that allows for customization of the Connect Modal and accepts the type ConnectModalOptions.

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/core",
"version": "2.20.2",
"version": "2.20.3-alpha.1",
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { getDevice } from './utils.js'

export let configuration: Configuration = {
svelteInstance: null,
appMetadata: null,
apiKey: null,
device: getDevice(),
initialWalletInit: [],
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export const APP_INITIAL_STATE: AppState = {
connect: {
showSidebar: true,
disableClose: false
}
},
appMetadata: null,
}

export const STORAGE_KEYS = {
Expand Down
11 changes: 7 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import {
setPrimaryWallet,
setWalletModules,
updateConnectModal,
updateTheme
updateTheme,
updateAppMetadata
} from './store/actions.js'
import type { PatchedEIP1193Provider } from '@web3-onboard/transaction-preview'
import { getBlocknativeSdk } from './services.js'
Expand All @@ -53,7 +54,8 @@ const API = {
updateBalances,
updateAccountCenter,
setPrimaryWallet,
updateTheme
updateTheme,
updateAppMetadata
}
}
}
Expand Down Expand Up @@ -92,7 +94,7 @@ function init(options: InitOptions): OnboardAPI {
const {
wallets,
chains,
appMetadata = null,
appMetadata,
i18n,
accountCenter,
apiKey,
Expand Down Expand Up @@ -208,7 +210,6 @@ function init(options: InitOptions): OnboardAPI {
const app = svelteInstance || mountApp(theme, disableFontDownload)

updateConfiguration({
appMetadata,
svelteInstance: app,
apiKey,
initialWalletInit: wallets,
Expand All @@ -217,6 +218,8 @@ function init(options: InitOptions): OnboardAPI {
unstoppableResolution
})

appMetadata && updateAppMetadata(appMetadata)

if (apiKey && transactionPreview) {
const getBnSDK = async () => {
transactionPreview.init({
Expand Down
28 changes: 24 additions & 4 deletions packages/core/src/store/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Chain, WalletInit, WalletModule } from '@web3-onboard/common'
import type { AppMetadata, Chain, WalletInit, WalletModule } from '@web3-onboard/common'
import { nanoid } from 'nanoid'
import { dispatch } from './index.js'
import { configuration } from '../configuration.js'
Expand Down Expand Up @@ -29,7 +29,8 @@ import type {
ConnectModalOptions,
UpdateConnectModalAction,
Theme,
UpdateChainsAction
UpdateChainsAction,
UpdateAppMetadataAction
} from '../types.js'

import {
Expand All @@ -45,7 +46,8 @@ import {
validateNotify,
validateConnectModalUpdate,
validateUpdateTheme,
validateSetChainOptions
validateSetChainOptions,
validateAppMetadataUpdate
} from '../validation.js'

import {
Expand All @@ -63,7 +65,8 @@ import {
REMOVE_NOTIFICATION,
UPDATE_ALL_WALLETS,
UPDATE_CONNECT_MODAL,
UPDATE_CHAINS
UPDATE_CHAINS,
UPDATE_APP_METADATA
} from './constants.js'

export function addChains(chains: Chain[]): void {
Expand Down Expand Up @@ -438,3 +441,20 @@ export function updateTheme(theme: Theme): void {
const themingObj = returnTheme(theme)
themingObj && handleThemeChange(themingObj)
}

export function updateAppMetadata(
update: AppMetadata| Partial<AppMetadata>
): void {
const error = validateAppMetadataUpdate(update)

if (error) {
throw error
}

const action = {
type: UPDATE_APP_METADATA,
payload: update
}

dispatch(action as UpdateAppMetadataAction)
}
1 change: 1 addition & 0 deletions packages/core/src/store/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export const UPDATE_NOTIFY = 'update_notify'
export const ADD_NOTIFICATION = 'add_notification'
export const REMOVE_NOTIFICATION = 'remove_notification'
export const UPDATE_ALL_WALLETS = 'update_balance'
export const UPDATE_APP_METADATA = 'update_app_metadata'
18 changes: 16 additions & 2 deletions packages/core/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import type {
RemoveNotificationAction,
UpdateAllWalletsAction,
UpdateConnectModalAction,
UpdateChainsAction
UpdateChainsAction,
UpdateAppMetadataAction
} from '../types.js'

import {
Expand All @@ -37,7 +38,8 @@ import {
ADD_NOTIFICATION,
REMOVE_NOTIFICATION,
UPDATE_ALL_WALLETS,
UPDATE_CHAINS
UPDATE_CHAINS,
UPDATE_APP_METADATA
} from './constants.js'

function reducer(state: AppState, action: Action): AppState {
Expand Down Expand Up @@ -218,6 +220,18 @@ function reducer(state: AppState, action: Action): AppState {
}
}

case UPDATE_APP_METADATA: {
const update = payload as UpdateAppMetadataAction['payload']

return {
...state,
appMetadata: {
...state.appMetadata,
...update
}
}
}

case RESET_STORE:
return APP_INITIAL_STATE

Expand Down
11 changes: 9 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ export interface AppState {
locale: Locale
notify: Notify
notifications: Notification[]
connect: ConnectModalOptions
connect: ConnectModalOptions,
appMetadata: AppMetadata
}

export type Configuration = {
Expand Down Expand Up @@ -428,7 +429,8 @@ export type Action =
| AddNotificationAction
| RemoveNotificationAction
| UpdateAllWalletsAction
| UpdateConnectModalAction
| UpdateConnectModalAction
| UpdateAppMetadataAction

export type AddChainsAction = { type: 'add_chains'; payload: Chain[] }
export type UpdateChainsAction = { type: 'update_chains'; payload: Chain }
Expand Down Expand Up @@ -494,6 +496,11 @@ export type UpdateAllWalletsAction = {
payload: WalletState[]
}

export type UpdateAppMetadataAction = {
type: 'update_app_metadata'
payload: AppMetadata | Partial<AppMetadata>
}

// ==== MISC ==== //
export type ChainStyle = {
icon: string
Expand Down
22 changes: 21 additions & 1 deletion packages/core/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
chainNamespaceValidation,
chainIdValidation,
chainValidation,
validate
validate,
AppMetadata
} from '@web3-onboard/common'

import type {
Expand Down Expand Up @@ -128,6 +129,19 @@ const appMetadata = Joi.object({
agreement
})

const appMetadataUpdate = Joi.object({
name: Joi.string(),
description: Joi.string(),
icon: Joi.string(),
logo: Joi.string(),
gettingStartedGuide: Joi.string(),
email: Joi.string(),
appUrl: Joi.string(),
explore: Joi.string(),
recommendedInjectedWallets: Joi.array().items(recommendedWallet),
agreement
})

const walletModule = Joi.object({
label: Joi.string().required(),
getInfo: Joi.function().arity(1).required(),
Expand Down Expand Up @@ -432,3 +446,9 @@ export function validateUpdateBalances(data: WalletState[]): ValidateReturn {
export function validateUpdateTheme(data: Theme): ValidateReturn {
return validate(theme, data)
}

export function validateAppMetadataUpdate(
data: AppMetadata | Partial<AppMetadata>
): ValidateReturn {
return validate(appMetadataUpdate, data)
}
24 changes: 14 additions & 10 deletions packages/core/src/views/account-center/AccountCenterPanel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import DisconnectAllConfirm from './DisconnectAllConfirm.svelte'
import { configuration } from '../../configuration.js'
import SecondaryTokenTable from './SecondaryTokenTable.svelte'
import { shareReplay, startWith } from 'rxjs'

export let expanded: boolean

Expand All @@ -31,7 +32,6 @@
}

const { chains: appChains } = state.get()
const { appMetadata } = configuration
let disconnectConfirmModal = false
let hideWalletRowMenu: () => void

Expand All @@ -52,6 +52,10 @@
connectedChain && connectedChain.id
)

const appMetadata$ = state
.select('appMetadata')
.pipe(startWith(state.get().appMetadata), shareReplay(1))

const { position } = state.get().accountCenter
const { device } = configuration
</script>
Expand Down Expand Up @@ -393,7 +397,7 @@

<!-- app info section -->
<div class="app-info-container">
{#if appMetadata}
{#if $appMetadata$}
<div class="flex items-start app-info-header">
<!-- app icon -->
<div class="relative flex app-icon-name">
Expand All @@ -403,37 +407,37 @@
background="white"
border="black"
radius={8}
icon={(appMetadata && appMetadata.icon) || questionIcon}
icon={($appMetadata$ && $appMetadata$.icon) || questionIcon}
/>
<div class="app-name">
{(appMetadata && appMetadata.name) || 'App Name'}
{($appMetadata$ && $appMetadata$.name) || 'App Name'}
</div>
</div>

<div class="app-description">
{(appMetadata && appMetadata.description) ||
{($appMetadata$ && $appMetadata$.description) ||
'This app has not added a description.'}
</div>
</div>

<!-- app info -->
{#if appMetadata.gettingStartedGuide || appMetadata.explore}
{#if $appMetadata$ && ($appMetadata$.gettingStartedGuide || $appMetadata$.explore)}
<div class="app-info">
<div class="app-info-heading">
{$_('accountCenter.appInfo', {
default: en.accountCenter.appInfo
})}
</div>

{#if appMetadata.gettingStartedGuide}
{#if $appMetadata$.gettingStartedGuide}
<div class="flex justify-between items-center w100">
<div>
{$_('accountCenter.learnMore', {
default: en.accountCenter.learnMore
})}
</div>
<a
href={appMetadata.gettingStartedGuide}
href={$appMetadata$.gettingStartedGuide}
target="_blank"
rel="noreferrer noopener"
>
Expand All @@ -444,15 +448,15 @@
</div>
{/if}

{#if appMetadata.explore}
{#if $appMetadata$.explore}
<div class="flex justify-between items-center w100">
<div>
{$_('accountCenter.smartContracts', {
default: en.accountCenter.smartContracts
})}
</div>
<a
href={appMetadata.explore}
href={$appMetadata$.explore}
target="_blank"
rel="noreferrer noopener"
>
Expand Down
Loading