Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export balance from utxo to evm with 4337 #8

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@ module.exports = {

parserOptions: {
sourceType: 'module',
},
"rules": {
"@typescript-eslint/no-misused-promises": [2, {
"checksVoidReturn": {
"attributes": false
}
}]
project: ['tsconfig.json'],
},
extends: ['@metamask/eslint-config'],

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-lint-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
key: snap-${{ runner.os }}-${{ github.sha }}
- run: yarn install --immutable
- name: Run e2e tests
run: yarn workspace snap run test
run: yarn workspace qngsnap run test
- name: Require clean working directory
shell: bash
run: |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lint": "yarn lint:eslint && yarn lint:misc --check",
"lint:eslint": "eslint . --cache --ext js,jsx,ts,tsx",
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write",
"lint:misc": "prettier '**/*.json' '**/*.md' '!**/CHANGELOG.md' '**/*.yml' --ignore-path .gitignore",
"lint:misc": "prettier '**/*.md' '!**/CHANGELOG.md' '**/*.yml' --ignore-path .gitignore",
"start": "yarn workspaces foreach --parallel --interlaced --verbose run start",
"test": "yarn workspace snap run test"
},
Expand Down
1 change: 1 addition & 0 deletions packages/site/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {

parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},

overrides: [
Expand Down
120 changes: 117 additions & 3 deletions packages/site/src/components/Card.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-misused-promises */
// import { Buffer } from 'buffer';
// import { hash, hash160 } from 'qitmeer-js';
import { useState } from 'react';
Expand Down Expand Up @@ -103,21 +104,33 @@ export const AACard = () => {
const [address, setAddress] = useState('');
const [balance, setBalance] = useState('');
const [qngAddress, setQngAddress] = useState('');
const [qngBalance, setQngBalance] = useState('');
const [target, setTarget] = useState('');
const [txide, setTxid] = useState('');
const [idx, setIdx] = useState('');
const [fee, setFee] = useState('10000');
const [oneUtxo, setOneUtxo] = useState('');
const [ethAmount, setEthAmount] = useState('');

// const Hash160 = (pubHex: string): string => {
// const h16 = hash.hash160(pubHex);
// return h16.toString('hex');
// };

const handleConnectAAClick = async () => {
const handleConnectAAClick = async (): Promise<void> => {
try {
setEOAAddress((await invokeSnap({ method: 'connect_eoa' })) as string);
setEOABalance((await invokeSnap({ method: 'balance_eoa' })) as string);
setAddress((await invokeSnap({ method: 'connect' })) as string);
setQngAddress((await invokeSnap({ method: 'connect_qng' })) as string);
setBalance((await invokeSnap({ method: 'balance' })) as string);
setQngBalance((await invokeSnap({ method: 'balance_qng' })) as string);
setOneUtxo(
(await invokeSnap({
method: 'getOneUtxo',
params: { utxoFrom: qngAddress },
})) as string,
);
} catch (er) {
console.error(er);
}
Expand All @@ -127,6 +140,13 @@ export const AACard = () => {
try {
setEOABalance((await invokeSnap({ method: 'balance_eoa' })) as string);
setBalance((await invokeSnap({ method: 'balance' })) as string);
setQngBalance((await invokeSnap({ method: 'balance_qng' })) as string);
setOneUtxo(
(await invokeSnap({
method: 'getOneUtxo',
params: { utxoFrom: qngAddress },
})) as string,
);
} catch (er) {
console.error(er);
}
Expand All @@ -140,6 +160,18 @@ export const AACard = () => {
setEthAmount(ev.currentTarget.value);
};

const handleTxidChange = (ev: React.FocusEvent<HTMLInputElement>) => {
setTxid(ev.currentTarget.value);
};

const handleIdxChange = (ev: React.FocusEvent<HTMLInputElement>) => {
setIdx(ev.currentTarget.value);
};

const handleFeeChange = (ev: React.FocusEvent<HTMLInputElement>) => {
setFee(ev.currentTarget.value);
};

const handleTransferFromAAClick = async () => {
if (!target || !ethAmount) {
// eslint-disable-next-line no-alert
Expand Down Expand Up @@ -173,7 +205,7 @@ export const AACard = () => {

try {
const ethValue = `${ethAmount}`;
const rawTx = (await invokeSnap({
const txid = (await invokeSnap({
method: 'utxoTransfer',
params: {
from: qngAddress,
Expand All @@ -183,12 +215,62 @@ export const AACard = () => {
})) as string;
// eslint-disable-next-line no-alert
alert(`tx sign succ`);
console.log(rawTx);
console.log(txid);
} catch (er) {
console.error(er);
}
};

const handleUtxoToEvmClick = async () => {
if (!txide || !idx) {
// eslint-disable-next-line no-alert
alert('enter txid and idx and fee.');
return;
}

const withWallet = false;
try {
const res = (await invokeSnap({
method: 'export',
params: {
txid: txide,
idx,
fee,
withWallet,
},
})) as string;
// eslint-disable-next-line no-alert
alert(`sign succ ${res}`);
console.log(res);
} catch (er) {
console.error(er);
}
};

const handleUtxoToEvmWithWalletClick = async () => {
if (!txide || !idx) {
// eslint-disable-next-line no-alert
alert('enter txid and idx and fee.');
return;
}
const withWallet = true;
try {
const res = (await invokeSnap({
method: 'export',
params: {
txid: txide,
idx,
fee,
withWallet,
},
})) as string;
// eslint-disable-next-line no-alert
alert(`sign succ ${res}`);
console.log(res);
} catch (er) {
console.error(er);
}
};
return (
<>
{!address && (
Expand All @@ -215,6 +297,7 @@ export const AACard = () => {
<CardWrapper fullWidth={true} disabled={false}>
<Title>Your Qng Account 🎉</Title>
<AAText>P2KH Address: {qngAddress}</AAText>
<AAText>Balance: {qngBalance}</AAText>
</CardWrapper>
<CardWrapper fullWidth={true} disabled={false}>
<Button onClick={handleReloadBalancesClick}>Reload Balances</Button>
Expand Down Expand Up @@ -258,6 +341,37 @@ export const AACard = () => {
Transfer from UTXO
</Button>
</CardWrapper>

<CardWrapper fullWidth={true} disabled={false}>
<Title>Transfer from Qng Account</Title>
<div>
<TargetInput
type="text"
placeholder="txid"
onChange={handleTxidChange}
/>
<AmountInput
type="number"
placeholder="idx"
onChange={handleIdxChange}
/>
<AmountInput
type="number"
placeholder="fee"
onChange={handleFeeChange}
/>
</div>
<Title>Available UTXO of the EoaAddress</Title>
<AAText> {oneUtxo} </AAText>
<Spacer />
<Button onClick={handleUtxoToEvmClick}>
Transfer from UTXO to EVM with {eoaAddress}
</Button>
<Spacer />
<Button onClick={handleUtxoToEvmWithWalletClick}>
Transfer from UTXO to EVM with {qngAddress}
</Button>
</CardWrapper>
</>
)}
</>
Expand Down
1 change: 1 addition & 0 deletions packages/snap/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = {

parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},

overrides: [
Expand Down
14 changes: 3 additions & 11 deletions packages/snap/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
# TypeScript Example Snap
# TypeScript Qng Snap

This snap demonstrates how to develop a snap with TypeScript. It is a simple
snap that displays a confirmation dialog when the `hello` JSON-RPC method is
called.

## Testing

The snap comes with some basic tests, to demonstrate how to write tests for
snaps. To test the snap, run `yarn test` in this directory. This will use
[`@metamask/snaps-jest`](https://github.com/MetaMask/snaps/tree/main/packages/snaps-jest)
to run the tests in `src/index.test.ts`.
This qng snap demonstrates how to develop a snap with TypeScript. It is a simple
snap that `qng` JSON-RPC method is called.
9 changes: 9 additions & 0 deletions packages/snap/images/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 7 additions & 7 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{
"name": "snap",
"version": "0.1.0",
"description": "The 'Hello, world!' of MetaMask Snaps, now written in TypeScript.",
"name": "qngsnap",
"version": "0.1.3",
"description": "The Qng of MetaMask Snaps, now written in TypeScript.",
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
"url": "git+https://github.com/Qitmeer/qng-snap.git"
},
"license": "(MIT-0 OR Apache-2.0)",
"main": "./dist/bundle.js",
"files": [
"dist/",
"snap.manifest.json"
"snap.manifest.json",
"images/icon.svg"
],
"scripts": {
"allow-scripts": "yarn workspace root allow-scripts",
Expand All @@ -27,8 +28,7 @@
"test": "jest"
},
"dependencies": {
"@account-abstraction/contracts": "^0.6.0",
"@account-abstraction/sdk": "^0.6.0",
"@metamask/key-tree": "^9.1.2",
"@metamask/snaps-sdk": "^4.0.0",
"@noble/hashes": "^1.4.0",
"@types/base-x": "^3.0.6",
Expand Down
9 changes: 5 additions & 4 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
{
"version": "0.1.0",
"version": "0.1.3",
"description": "An qng example Snap written in TypeScript.",
"proposedName": "QNG Snap",
"repository": {
"type": "git",
"url": "https://github.com/MetaMask/template-snap-monorepo.git"
"url": "git+https://github.com/Qitmeer/qng-snap.git"
},
"source": {
"shasum": "X0E7Z4Xx8kFeadQjs1fMG9MNX51YoJSBtbUo+Ok+VJ8=",
"shasum": "uuJGDar2hF2mIgc3YudQCHflJKtdDy+w3x+JSzynPqg=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
"packageName": "snap",
"packageName": "qngsnap",
"iconPath": "images/icon.svg",
"registry": "https://registry.npmjs.org/"
}
}
Expand Down
58 changes: 58 additions & 0 deletions packages/snap/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { networks } from 'qitmeerts';
import type { NetworkConfig } from 'qitmeerts/dist/src/networks';

export type Config = {
entryPointAddress: string;
factoryAddress: string;
paymasterAddress: string;
proxyUrl: string;
networkConf: NetworkConfig;
meerchangeAddress: string;
};

export const PrivateConfig: Config = {
entryPointAddress: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
factoryAddress: '0x9406cc6185a346906296840746125a0e44976454',
paymasterAddress: '0x0C170fAe5584421092D624425c85156758c190e0',
proxyUrl: 'http://127.0.0.1:8081',
networkConf: networks.privnet,
meerchangeAddress: '0x422f6F90B35D91D7D4F03aC791c6C07b1c14af1f',
};

export const TestConfig: Config = {
entryPointAddress: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
factoryAddress: '0x9406cc6185a346906296840746125a0e44976454',
paymasterAddress: '0x0C170fAe5584421092D624425c85156758c190e0',
proxyUrl: 'https://47.242.255.132',
networkConf: networks.testnet,
meerchangeAddress: '0x422f6F90B35D91D7D4F03aC791c6C07b1c14af1f',
};
export const MixConfig: Config = {
entryPointAddress: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
factoryAddress: '0x9406cc6185a346906296840746125a0e44976454',
paymasterAddress: '0x0C170fAe5584421092D624425c85156758c190e0',
proxyUrl: 'http://127.0.0.1:8081',
networkConf: networks.mixnet,
meerchangeAddress: '0x422f6F90B35D91D7D4F03aC791c6C07b1c14af1f',
};
export const MainConfig: Config = {
entryPointAddress: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
factoryAddress: '0x9406cc6185a346906296840746125a0e44976454',
paymasterAddress: '0x0C170fAe5584421092D624425c85156758c190e0',
proxyUrl: 'http://127.0.0.1:8081',
networkConf: networks.mainnet,
meerchangeAddress: '0x422f6F90B35D91D7D4F03aC791c6C07b1c14af1f',
};

export const getConfig = (chainId: number): Config => {
switch (chainId) {
case 813:
return MainConfig;
case 8131:
return TestConfig;
case 8132:
return MixConfig;
default:
return PrivateConfig;
}
};
Loading
Loading