Skip to content

Commit

Permalink
feat: add coinbase pay w3m
Browse files Browse the repository at this point in the history
  • Loading branch information
enesozturk committed Nov 21, 2023
1 parent 30f4bd4 commit 678138c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/scaffold/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"lint": "eslint . --ext .js,.jsx,.ts,.tsx"
},
"dependencies": {
"@coinbase/cbpay-js": "^1.9.0",
"@web3modal/core": "3.3.2",
"@web3modal/ui": "3.3.2",
"lit": "3.0.0"
Expand Down
52 changes: 51 additions & 1 deletion packages/scaffold/src/views/w3m-account-view/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ import { state } from 'lit/decorators.js'
import { ifDefined } from 'lit/directives/if-defined.js'
import styles from './styles.js'

import { initOnRamp } from '@coinbase/cbpay-js'
import type { CBPayInstanceType } from '@coinbase/cbpay-js'

const coinbaseAppID = process.env['NEXT_PUBLIC_COINBASE_APP_ID']!

Check failure on line 21 in packages/scaffold/src/views/w3m-account-view/index.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Forbidden non-null assertion
console.log({ coinbaseAppID })

Check failure on line 22 in packages/scaffold/src/views/w3m-account-view/index.ts

View workflow job for this annotation

GitHub Actions / code_style (lint)

Unexpected console statement
if (!coinbaseAppID) {
throw new Error('NEXT_PUBLIC_COINBASE_APP_ID is not set')
}

@customElement('w3m-account-view')
export class W3mAccountView extends LitElement {
public static override styles = styles
Expand All @@ -37,6 +46,8 @@ export class W3mAccountView extends LitElement {

@state() private network = NetworkController.state.caipNetwork

@state() private onrampInstance: CBPayInstanceType | null = null

@state() private disconecting = false

public constructor() {
Expand Down Expand Up @@ -67,6 +78,33 @@ export class W3mAccountView extends LitElement {
this.usubscribe.forEach(unsubscribe => unsubscribe())
}

public override firstUpdated() {
initOnRamp(
{
appId: coinbaseAppID,
widgetParameters: {
destinationWallets: [
{
address: '0xf3ea39310011333095CFCcCc7c4Ad74034CABA63',
blockchains: ['ethereum']
}
]
},
experienceLoggedIn: 'popup',
experienceLoggedOut: 'popup',
closeOnExit: true,
closeOnSuccess: true
},
(_, instance) => {
this.onrampInstance = instance
}
)

return () => {
this.onrampInstance?.destroy()
}
}

// -- Render -------------------------------------------- //
public override render() {
if (!this.address) {
Expand All @@ -87,7 +125,6 @@ export class W3mAccountView extends LitElement {
address=${this.address}
imageSrc=${ifDefined(this.profileImage)}
></wui-avatar>
<wui-flex flexDirection="column" alignItems="center">
<wui-flex gap="3xs" alignItems="center" justifyContent="center">
<wui-text variant="large-600" color="fg-100">
Expand Down Expand Up @@ -135,6 +172,15 @@ export class W3mAccountView extends LitElement {
${this.network?.name ?? 'Unknown'}
</wui-text>
</wui-list-item>
<wui-list-item
iconVariant="blue"
icon="swapHorizontalBold"
iconSize="sm"
?chevron=${true}
@click=${this.handleClickPay.bind(this)}
>
<wui-text variant="paragraph-500" color="fg-100">Pay with Coinbase</wui-text>
</wui-list-item>
<wui-list-item
iconVariant="blue"
icon="swapHorizontalBold"
Expand All @@ -159,6 +205,10 @@ export class W3mAccountView extends LitElement {
}

// -- Private ------------------------------------------- //
private handleClickPay() {
this.onrampInstance?.open()
}

private explorerBtnTemplate() {
const { addressExplorerUrl } = AccountController.state

Expand Down

0 comments on commit 678138c

Please sign in to comment.