add slope wallet

This commit is contained in:
Maximilian Schneider 2021-09-21 12:08:13 +02:00
parent 53e3f42ba1
commit 5da4887736
5 changed files with 142 additions and 0 deletions

View File

@ -5,6 +5,7 @@ import useMangoStore from '../stores/useMangoStore'
import { notify } from '../utils/notifications'
import {
PhantomWalletAdapter,
SlopeWalletAdapter,
SolletExtensionAdapter,
} from '../utils/wallet-adapters'
import { WalletAdapter } from '../@types/types'
@ -32,6 +33,12 @@ export const WALLET_PROVIDERS = [
icon: `${ASSET_URL}/sollet.svg`,
adapter: SolletExtensionAdapter as any,
},
{
name: 'Slope',
url: 'https://www.slope.finance/#/wallet',
icon: 'data:image/svg+xml;base64,PHN2ZyBmaWxsPSJub25lIiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDEyOCAxMjgiIHdpZHRoPSIxMjgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGNpcmNsZSBjeD0iNjQiIGN5PSI2NCIgZmlsbD0iIzZlNjZmYSIgcj0iNjQiLz48ZyBmaWxsPSIjZmZmIj48cGF0aCBkPSJtMzUuMTk2MyA1NC4zOTk4aDE5LjJ2MTkuMmgtMTkuMnoiLz48cGF0aCBkPSJtNzMuNTk3IDU0LjM5OTgtMTkuMiAxOS4ydi0xOS4ybDE5LjItMTkuMnoiIGZpbGwtb3BhY2l0eT0iLjQiLz48cGF0aCBkPSJtNzMuNTk3IDczLjU5OTgtMTkuMiAxOS4ydi0xOS4ybDE5LjItMTkuMnoiIGZpbGwtb3BhY2l0eT0iLjc1Ii8+PHBhdGggZD0ibTczLjYwNCA1NC4zOTk4aDE5LjJ2MTkuMmgtMTkuMnoiLz48cGF0aCBkPSJtNTQuMzk2OCAzNS4yIDE5LjItMTkuMnYxOS4ybC0xOS4yIDE5LjJoLTE5LjJ6IiBmaWxsLW9wYWNpdHk9Ii43NSIvPjxwYXRoIGQ9Im03My41OTE1IDkyLjgtMTkuMiAxOS4ydi0xOS4ybDE5LjItMTkuMmgxOS4yeiIgZmlsbC1vcGFjaXR5PSIuNCIvPjwvZz48L3N2Zz4=',
adapter: SlopeWalletAdapter,
},
]
export const PROVIDER_LOCAL_STORAGE_KEY = 'walletProvider-0.1'
export const DEFAULT_PROVIDER = WALLET_PROVIDERS[0]

View File

@ -1,2 +1,3 @@
export * from './phantom'
export * from './sollet-extension'
export * from './slope'

View File

@ -82,6 +82,7 @@ export class PhantomWalletAdapter
window.open('https://phantom.app/', '_blank')
notify({
title: 'Connection Error',
type: 'error',
description: 'Please install Phantom wallet and then reload this page.',
})
return

View File

@ -0,0 +1,132 @@
import { PublicKey, Transaction } from '@solana/web3.js'
import bs58 from 'bs58'
import EventEmitter from 'events'
import { WalletAdapter } from '../../@types/types'
import { notify } from '../notifications'
interface SlopeWallet {
connect(): Promise<{
msg: string
data: {
publicKey?: string
}
}>
disconnect(): Promise<{ msg: string }>
signTransaction(message: string): Promise<{
msg: string
data: {
publicKey?: string
signature?: string
}
}>
signAllTransactions(messages: string[]): Promise<{
msg: string
data: {
publicKey?: string
signatures?: string[]
}
}>
}
interface SlopeWindow extends Window {
Slope?: {
new (): SlopeWallet
}
}
declare const window: SlopeWindow
export class SlopeWalletAdapter extends EventEmitter implements WalletAdapter {
private _connecting: boolean
private _wallet: SlopeWallet | null
private _publicKey: PublicKey | null
constructor() {
super()
this._connecting = false
this._wallet = null
this._publicKey = null
}
get publicKey(): PublicKey | null {
return this._publicKey
}
get ready(): boolean {
return typeof window !== 'undefined' && !!window.Slope
}
get connecting(): boolean {
return this._connecting
}
get connected(): boolean {
return !!this._publicKey
}
get autoApprove() {
return true
}
async connect(): Promise<void> {
try {
if (this.connected || this.connecting) return
this._connecting = true
const wallet = new window.Slope()
const { data } = await wallet.connect()
this._wallet = wallet
this._publicKey = new PublicKey(data.publicKey)
this.emit('connect')
} catch (error: any) {
notify({
title: 'Connection Error',
type: 'error',
description: 'Please install Slope wallet and then reload this page.',
})
} finally {
this._connecting = false
}
}
async disconnect(): Promise<void> {
const wallet = this._wallet
if (wallet) {
this._wallet = null
this._publicKey = null
await wallet.disconnect()
this.emit('disconnect')
}
}
async signTransaction(transaction: Transaction): Promise<Transaction> {
const wallet = this._wallet
const message = bs58.encode(transaction.serializeMessage())
const { data } = await wallet.signTransaction(message)
const publicKey = new PublicKey(data.publicKey)
const signature = bs58.decode(data.signature)
transaction.addSignature(publicKey, signature)
return transaction
}
async signAllTransactions(
transactions: Transaction[]
): Promise<Transaction[]> {
const wallet = this._wallet
const messages = transactions.map((transaction) =>
bs58.encode(transaction.serializeMessage())
)
const { data } = await wallet.signAllTransactions(messages)
const length = transactions.length
const publicKey = new PublicKey(data.publicKey)
for (let i = 0; i < length; i++) {
transactions[i].addSignature(publicKey, bs58.decode(data.signatures[i]))
}
return transactions
}
}

View File

@ -12,6 +12,7 @@ export function SolletExtensionAdapter(_, network) {
connect: () => {
notify({
title: 'Sollet Extension Error',
type: 'error',
description:
'Please install the Sollet Extension for Chrome and then reload this page.',
})