Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Luphia1984 committed Nov 23, 2021
2 parents c4da304 + 45a5360 commit 03b6698
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ npm run build
// check version
const version = Lunar.version;

// list all blockchain
const blockchains = Lunar.listBlockchain();

// list support blockchain(Mainnet)
const blockchains = Lunar.listBlockchain({ testnet: false });

Expand All @@ -31,6 +34,9 @@ const blockchains = Lunar.listBlockchain({ testnet: true });
// initial
const lunar = new Lunar();

// get current blockchain
const blockchain = lunar.blockchain;

// regist notification
lunar.on((event, data) => {
// ready, connected, disconnected
Expand Down
2 changes: 1 addition & 1 deletion build/lunar.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cafeca/lunar",
"version": "0.2.12",
"version": "0.3.0",
"description": "Blockchain Connect Module",
"main": "./build/lunar.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/connectors/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Connector {
get type() {
return this._type;
}
get chainId() {
return this.blockchain.chainId;
}
get wallet() {}

async init() {}
Expand Down
2 changes: 1 addition & 1 deletion src/connectors/connectorFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ConnectorFactory {
}

static create(wallet = '') {
let connector;
let connector
switch(wallet) {
case Wallets.TideWallet:
connector = new TideWallet();
Expand Down
21 changes: 17 additions & 4 deletions src/connectors/metamask.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import Wallets from '../constants/wallets.js';
import Connector from './connector.js'
import SmartContract from '../libs/smartcontract.js'
import Connector from './connector.js';
import SmartContract from '../libs/smartcontract.js';
import BigNumber from '../libs/bignumber.js';
import Blockchains from '../constants/blockchain.js';

class Metamask extends Connector {
_type = Wallets.Metamask;

constructor() {
super();
try {
const chainId = ethereum.chainId;
this._blockchain = Blockchains.findByChainId(chainId);
}
catch(e) {
console.trace(e);
}
}

async connect({ blockchain }) {
return this._connect({ blockchain });
}
Expand All @@ -22,12 +34,13 @@ class Metamask extends Connector {
to,
value,
data,
chainId: this._chainId
chainId: this.chainId
}
const requestData = {
method: 'eth_sendTransaction',
params: [ transactionParameters ],
};
console.log(requestData)
const txHash = await ethereum.request(requestData);
return txHash;
}
Expand Down Expand Up @@ -194,7 +207,7 @@ class Metamask extends Connector {
method: 'wallet_switchEthereumChain',
params:[ { chainId: blockchain.chainId } ]
};
return ethereum.request(requestData);
return ethereum.request(requestData)
})
}
async _addBlockchain({ blockchain }) {
Expand Down
2 changes: 1 addition & 1 deletion src/constants/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Blockchains {
static list({ testnet }) {
const result = this.keys
.map((v) => Blockchains[v])
.filter((v) => !(v.isTestnet ^ testnet))
.filter((v) => testnet == undefined || !(v.isTestnet ^ testnet))

return result;
}
Expand Down
11 changes: 8 additions & 3 deletions src/lunar.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ class Lunar {
static listBlockchain({ testnet } = {}) {
return Blockchains.list({ testnet });
}
static findBlockchain({ chainId } = {}) {
return Blockchains.findByChainId(chainId);
}
static version = `v${version}`;

_connector;
_connectors = [];
_blockchain;
_eventEmitter;

constructor() {
Expand Down Expand Up @@ -43,7 +45,10 @@ class Lunar {
}

get blockchain() {
return this._blockchain;
if(!this._connector.blockchain) {
throw new Error('connection error with Lunar');
}
return this._connector.blockchain;
}

on(event, callback) {
Expand All @@ -69,8 +74,8 @@ class Lunar {
const newConnector = ConnectorFactory.create(walletType);
this._connectors.push(newConnector);
this._connector = newConnector;
await this._connector.connect({ blockchain });
}
this._blockchain = await this._connector.connect({ blockchain });
return this.address;
}

Expand Down

0 comments on commit 03b6698

Please sign in to comment.