Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
added getSupportedWallets method and added adapter properties
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Aug 8, 2023
1 parent 40b97b2 commit 5e14fc1
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"namespace": "multiplechain",
"name": "@multiplechain/solana",
"version": "0.1.8",
"version": "0.1.9",
"description": "Solana has ready-made methods to connect to wallets and perform many transactions.",
"scripts": {
"serve": "parcel test.html --no-cache --dist-dir test",
Expand Down
9 changes: 7 additions & 2 deletions src/adapters/phantom.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ module.exports = phantom = (provider) => {
return {
key: 'phantom',
name: 'Phantom',
type: 'browser',
supports: [
'browser',
'mobile'
],
wallet,
download: 'https://phantom.app/'
download: 'https://phantom.app/download',
deepLink: 'https://phantom.app/ul/browse/{siteUrl}?ref={siteUrl}',
detected: Boolean(window.phantom?.solana?.isPhantom)
}
}
8 changes: 6 additions & 2 deletions src/adapters/slope.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ module.exports = slope = (provider) => {
return {
key: 'slope',
name: 'Slope',
type: 'browser',
wallet
supports: [
'browser'
],
wallet,
download: 'https://www.slope.finance/',
detected: Boolean(window.Slope)
}
}
9 changes: 8 additions & 1 deletion src/adapters/solflare.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ module.exports = solflare = (provider) => {
key: 'solflare',
name: 'Solflare',
type: 'browser',
wallet
supports: [
'browser',
'mobile'
],
wallet,
download: 'https://solflare.com/download#extension',
deepLink: 'https://solflare.com/ul/v1/browse/{siteUrl}?ref={siteUrl}',
detected: Boolean(window.solflare?.isSolflare)
}
}
28 changes: 25 additions & 3 deletions src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,17 @@ class Provider {
* @param {Object} options
*/
constructor(options) {
this.wsUrl = options.customWs;
this.testnet = options.testnet;

this.network = this.networks[this.testnet ? 'devnet' : 'mainnet'];
if (!this.testnet && options.customRpc) {
this.network.host = options.customRpc;
}

if (!this.testnet && options.customWs) {
this.wsUrl = options.customWs;
}

if (this.wsUrl) {
this.qrPayments = true;
}
Expand Down Expand Up @@ -241,12 +244,31 @@ class Provider {
}

/**
* @param {Array} filter
* @param {Array|null} filter
* @returns {Array}
*/
getSupportedWallets(filter) {

const Wallet = require('./wallet');

const wallets = {
phantom: new Wallet('phantom', this),
solflare: new Wallet('solflare', this),
slope: new Wallet('slope', this),
};

return Object.fromEntries(Object.entries(wallets).filter(([key]) => {
return !filter ? true : filter.includes(key);
}));
}

/**
* @param {Array|null} filter
* @returns {Array}
*/
getDetectedWallets(filter) {
return Object.fromEntries(Object.entries(this.detectedWallets).filter(([key]) => {
return filter.includes(key);
return !filter ? true : filter.includes(key);
}));
}

Expand Down
22 changes: 9 additions & 13 deletions src/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class Wallet {
/**
* @returns {String}
*/
getType() {
return this.adapter.type;
getSupports() {
return this.adapter.supports;
}

/**
Expand All @@ -89,6 +89,13 @@ class Wallet {
return this.adapter.download;
}

/**
* @returns {Boolean}
*/
isDetected() {
return this.adapter.detected;
}

getConnectedPublicKey() {
return this.solWalletAdapter.publicKey.value || this.solWalletAdapter.publicKey;
}
Expand All @@ -101,23 +108,12 @@ class Wallet {
this.wallet.addListener('error', (error) => {
utils.rejectMessage(error, reject);
});

let time = 0;
let timeout = 15;
let timer = setInterval(async () => {
time += 1;
if (time > timeout) {
clearInterval(timer);
reject('timeout');
}
}, 1000);

await this.solWalletAdapter.connect(this.wallet.name);

this.provider.setConnectedWallet(this);
this.connectedAccount = this.getConnectedAccount();

clearInterval(timer);
resolve(this.connectedAccount);
} catch (error) {
utils.rejectMessage(error, reject);
Expand Down

0 comments on commit 5e14fc1

Please sign in to comment.