Skip to content

Commit

Permalink
Merge pull request bitrocks#41 from nervosnetwork/eth-wallet-mode
Browse files Browse the repository at this point in the history
Eth wallet mode
  • Loading branch information
classicalliu authored Aug 9, 2021
2 parents 2940599 + 2599611 commit 26b6639
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ yarn run build:godwoken
yarn run start
```

Normal mode: http://your-url/

Eth wallet mode: http://your-url/eth-wallet (for wallet like metamask, please connect to this url)

## Web3 RPC Modules

### net
Expand Down
14 changes: 10 additions & 4 deletions packages/api-server/middlewares/jayson.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
var jayson = require('jayson');
var methods = require('../lib/methods/index');
const jayson = require('jayson');
const { methods, ethWalletMethods } = require('../lib/methods/index');

var server = jayson.server(methods);
const server = jayson.server(methods);
const ethWalletServer = jayson.server(ethWalletMethods);

module.exports = server.middleware()
module.exports = function (req, res, next) {
if (req.url.endsWith("/eth-wallet") && req.body.method.startsWith("eth_")) {
return ethWalletServer.middleware()(req, res, next)
}
return server.middleware()(req, res, next)
}
37 changes: 36 additions & 1 deletion packages/api-server/src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,41 @@ function getMethods() {
return methods;
}

// TODO: maybe can merge to `getMethods`
// only `eth` module, `poly` module will conflict with leveldb lock.
function getEthWalletMethods() {
const methods: any = {};

const modName = "Eth";
const mod = new (modules as any)[modName](true);
getMethodNames((modules as any)[modName])
.filter((methodName: string) => methodName !== "constructor")
.forEach((methodName: string) => {
const concatedMethodName = `${modName.toLowerCase()}_${methodName}`;
methods[concatedMethodName] = async (args: any[], cb: Callback) => {
try {
const result = await mod[methodName].bind(mod)(args);
cb(null, result);
} catch (err) {
if (err.name === "RpcError") {
return cb({
code: err.code,
message: err.message,
});
}
throw err;
}
};
});

console.log(methods);
return methods;
}

const methods = getMethods();
const ethWalletMethods = getEthWalletMethods();

module.exports = methods;
module.exports = {
methods,
ethWalletMethods,
};
16 changes: 15 additions & 1 deletion packages/api-server/src/methods/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ const EMPTY_ADDRESS = "0x" + "00".repeat(20);
export class Eth {
private query: Query;
private rpc: GodwokenClient;
private ethWallet: boolean;

constructor() {
constructor(ethWallet: boolean = false) {
this.ethWallet = ethWallet;
this.query = new Query(envConfig.databaseUrl);
this.rpc = new GodwokenClient(envConfig.godwokenJsonRpc);

Expand Down Expand Up @@ -290,6 +292,12 @@ export class Eth {
+CKB_SUDT_ID,
blockNumber
);

if (this.ethWallet) {
const balanceHex = new Uint128(balance * 10n ** 10n).toHex();
return balanceHex;
}

const balanceHex = new Uint128(balance).toHex();
return balanceHex;
} catch (error) {
Expand Down Expand Up @@ -375,6 +383,9 @@ export class Eth {
}

async call(args: [TransactionCallObject, string]): Promise<HexString> {
if (this.ethWallet) {
throw new MethodNotSupportError("eth_call is not supported!");
}
try {
const blockParameter = args[1];
const blockNumber: U64 | undefined = await this.parseBlockParameter(
Expand All @@ -393,6 +404,9 @@ export class Eth {
}

async estimateGas(args: [TransactionCallObject]): Promise<HexNumber> {
if (this.ethWallet) {
throw new MethodNotSupportError("eth_estimateGas is not supported!");
}
try {
const rawL2Transaction = await buildEthCallTx(args[0], this.rpc);
const runResult = await this.rpc.executeRawL2Transaction(
Expand Down

0 comments on commit 26b6639

Please sign in to comment.