Skip to content

Commit 788bfe3

Browse files
committed
Buffer error and ui
1 parent 4a13819 commit 788bfe3

File tree

7 files changed

+88
-4
lines changed

7 files changed

+88
-4
lines changed

packages/snap/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"@metamask/snaps-ui": "^3.1.0",
5555
"buffer": "^6.0.3",
5656
"cosmjs-types": "^0.8.0",
57+
"lodash": "^4.17.21",
5758
"osmojs": "^16.5.1",
5859
"ses": "^0.18.4"
5960
},

packages/snap/snap.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const config: SnapConfig = {
99
stats: {
1010
buffer: false,
1111
},
12+
environment: {
13+
DENO_SERVERLESS_URL: process.env.DENO_SERVERLESS_URL,
14+
},
1215
};
1316

1417
export default config;

packages/snap/snap.manifest.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"proposedName": "Cosmos Extension",
55
"repository": {
66
"type": "git",
7-
"url": "https://github.com/cosmos/snap.git"
7+
"url": "git+https://github.com/cosmos/snap.git"
88
},
99
"source": {
10-
"shasum": "70Y16tDS8kL5RMQhR1coQlID5bwKy0TeCmYgqwTIWlk=",
10+
"shasum": "f6e8RsKUURq4joa44SVbSIIwH8SdkhwOiulSa1bWqpA=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",
@@ -89,7 +89,8 @@
8989
"endowment:rpc": {
9090
"snaps": false,
9191
"dapps": true
92-
}
92+
},
93+
"endowment:page-home": {}
9394
},
9495
"manifestVersion": "0.1"
9596
}

packages/snap/src/index.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { OnRpcRequestHandler } from "@metamask/snaps-sdk";
1+
import { OnHomePageHandler, OnRpcRequestHandler, address, image, row } from "@metamask/snaps-sdk";
22
import { AccountData } from '@cosmjs/amino';
33
import { panel, text, heading, divider, copyable } from "@metamask/snaps-ui";
44
import { initializeChains } from "./initialize";
@@ -14,6 +14,8 @@ import Long from "long";
1414
import { Key } from '@keplr-wallet/types';
1515
import { fromBech32 } from '@cosmjs/encoding';
1616
import { isTxBodyEncodeObject } from "@cosmjs/proto-signing";
17+
import { getBalances } from "./utils";
18+
import _ from "lodash";
1719

1820
/**
1921
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
@@ -957,3 +959,40 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
957959
throw new Error("Method not found.");
958960
}
959961
};
962+
963+
export const onHomePage: OnHomePageHandler = async () => {
964+
const main: any[] = [
965+
heading('Metamask Extension'),
966+
text('Manage everything across the Cosmos!'),
967+
divider(),
968+
heading('Accounts'),
969+
divider(),
970+
]
971+
const addressesP = ChainState.getChainAddresses();
972+
const chainsP = ChainState.getChains();
973+
const [addresses, chainsInWallet] = await Promise.all([addressesP, chainsP]);
974+
const chains = _.values(_.merge(_.keyBy(addresses, 'chain_id'), _.keyBy(chainsInWallet.chains, 'chain_id')))
975+
const balances = await getBalances(chains);
976+
addresses.forEach((address) => {
977+
const chain = balances.find((balance) => balance.chain_id === address.chain_id);
978+
if (!chain) {
979+
throw new Error(`No chain found for ${address.chain_id}`);
980+
}
981+
main.push(heading(chain ? chain.pretty_name : address.chain_id))
982+
main.push(text("**Balances**"))
983+
chain.balances.forEach((balance) => {
984+
main.push(copyable(`${_.round((Number(balance.amount) / 1_000_000), 2)} ${balance.display}`))
985+
})
986+
main.push(text("**Address**"))
987+
main.push(copyable(address.address))
988+
main.push(divider())
989+
})
990+
991+
// Direct them to the dashboard for more advanced things
992+
main.push(
993+
text('[Manage My Assets](https://metamask.mysticlabs.xyz/)')
994+
);
995+
return {
996+
content: panel(main),
997+
};
998+
};

packages/snap/src/utils.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Coin } from "@cosmjs/amino";
2+
import { Chain } from "./types/chains";
3+
4+
if (!process.env.DENO_SERVERLESS_URL) {
5+
throw new Error("DENO_SERVERLESS_URL not set...");
6+
}
7+
export const denoUrl = process.env.DENO_SERVERLESS_URL;
8+
9+
export interface CoinIBC extends Coin {
10+
ibc: boolean;
11+
ibc_denom?: string;
12+
display: string;
13+
}
14+
15+
export interface ChainBalances extends Chain {
16+
balances: CoinIBC[];
17+
}
18+
19+
export const getBalances = async (chains: Chain[]): Promise<ChainBalances[]> => {
20+
try {
21+
const res = await fetch(`${denoUrl}/balances`, {
22+
method: 'POST',
23+
headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' },
24+
body: JSON.stringify({ chains: chains })
25+
});
26+
27+
if (!res.ok) {
28+
throw new Error(`HTTP error ${res.status}`);
29+
}
30+
31+
const data = await res.json();
32+
33+
return data.balances;
34+
} catch (error) {
35+
console.error(error);
36+
throw error;
37+
}
38+
};

packages/snap/src/wallet.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
22
import { Secp256k1Wallet } from "@cosmjs/amino";
33
import { Chain } from "./types/chains";
4+
import { Buffer } from "buffer";
45

56
export const getWallet = async (chain: Chain) => {
67
// get signer info

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,7 @@ __metadata:
22802280
eslint-plugin-node: ^11.1.0
22812281
eslint-plugin-prettier: ^4.2.1
22822282
jest: ^29.7.0
2283+
lodash: ^4.17.21
22832284
node-fetch: 2
22842285
osmojs: ^16.5.1
22852286
prettier: ^2.2.1

0 commit comments

Comments
 (0)