Skip to content

Commit

Permalink
Buffer error and ui
Browse files Browse the repository at this point in the history
  • Loading branch information
schnetzlerjoe committed Mar 17, 2024
1 parent 4a13819 commit 788bfe3
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@metamask/snaps-ui": "^3.1.0",
"buffer": "^6.0.3",
"cosmjs-types": "^0.8.0",
"lodash": "^4.17.21",
"osmojs": "^16.5.1",
"ses": "^0.18.4"
},
Expand Down
3 changes: 3 additions & 0 deletions packages/snap/snap.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const config: SnapConfig = {
stats: {
buffer: false,
},
environment: {
DENO_SERVERLESS_URL: process.env.DENO_SERVERLESS_URL,
},
};

export default config;
7 changes: 4 additions & 3 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"proposedName": "Cosmos Extension",
"repository": {
"type": "git",
"url": "https://github.com/cosmos/snap.git"
"url": "git+https://github.com/cosmos/snap.git"
},
"source": {
"shasum": "70Y16tDS8kL5RMQhR1coQlID5bwKy0TeCmYgqwTIWlk=",
"shasum": "f6e8RsKUURq4joa44SVbSIIwH8SdkhwOiulSa1bWqpA=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down Expand Up @@ -89,7 +89,8 @@
"endowment:rpc": {
"snaps": false,
"dapps": true
}
},
"endowment:page-home": {}
},
"manifestVersion": "0.1"
}
41 changes: 40 additions & 1 deletion packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { OnRpcRequestHandler } from "@metamask/snaps-sdk";
import { OnHomePageHandler, OnRpcRequestHandler, address, image, row } from "@metamask/snaps-sdk";
import { AccountData } from '@cosmjs/amino';
import { panel, text, heading, divider, copyable } from "@metamask/snaps-ui";
import { initializeChains } from "./initialize";
Expand All @@ -14,6 +14,8 @@ import Long from "long";
import { Key } from '@keplr-wallet/types';
import { fromBech32 } from '@cosmjs/encoding';
import { isTxBodyEncodeObject } from "@cosmjs/proto-signing";
import { getBalances } from "./utils";
import _ from "lodash";

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
Expand Down Expand Up @@ -957,3 +959,40 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
throw new Error("Method not found.");
}
};

export const onHomePage: OnHomePageHandler = async () => {
const main: any[] = [
heading('Metamask Extension'),
text('Manage everything across the Cosmos!'),
divider(),
heading('Accounts'),
divider(),
]
const addressesP = ChainState.getChainAddresses();
const chainsP = ChainState.getChains();
const [addresses, chainsInWallet] = await Promise.all([addressesP, chainsP]);
const chains = _.values(_.merge(_.keyBy(addresses, 'chain_id'), _.keyBy(chainsInWallet.chains, 'chain_id')))
const balances = await getBalances(chains);
addresses.forEach((address) => {
const chain = balances.find((balance) => balance.chain_id === address.chain_id);
if (!chain) {
throw new Error(`No chain found for ${address.chain_id}`);
}
main.push(heading(chain ? chain.pretty_name : address.chain_id))
main.push(text("**Balances**"))
chain.balances.forEach((balance) => {
main.push(copyable(`${_.round((Number(balance.amount) / 1_000_000), 2)} ${balance.display}`))
})
main.push(text("**Address**"))
main.push(copyable(address.address))
main.push(divider())
})

// Direct them to the dashboard for more advanced things
main.push(
text('[Manage My Assets](https://metamask.mysticlabs.xyz/)')
);
return {
content: panel(main),
};
};
38 changes: 38 additions & 0 deletions packages/snap/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Coin } from "@cosmjs/amino";
import { Chain } from "./types/chains";

if (!process.env.DENO_SERVERLESS_URL) {
throw new Error("DENO_SERVERLESS_URL not set...");
}
export const denoUrl = process.env.DENO_SERVERLESS_URL;

export interface CoinIBC extends Coin {
ibc: boolean;
ibc_denom?: string;
display: string;
}

export interface ChainBalances extends Chain {
balances: CoinIBC[];
}

export const getBalances = async (chains: Chain[]): Promise<ChainBalances[]> => {
try {
const res = await fetch(`${denoUrl}/balances`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Cache-Control': 'no-cache' },
body: JSON.stringify({ chains: chains })
});

if (!res.ok) {
throw new Error(`HTTP error ${res.status}`);
}

const data = await res.json();

return data.balances;
} catch (error) {
console.error(error);
throw error;
}
};
1 change: 1 addition & 0 deletions packages/snap/src/wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { Secp256k1Wallet } from "@cosmjs/amino";
import { Chain } from "./types/chains";
import { Buffer } from "buffer";

export const getWallet = async (chain: Chain) => {
// get signer info
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2280,6 +2280,7 @@ __metadata:
eslint-plugin-node: ^11.1.0
eslint-plugin-prettier: ^4.2.1
jest: ^29.7.0
lodash: ^4.17.21
node-fetch: 2
osmojs: ^16.5.1
prettier: ^2.2.1
Expand Down

0 comments on commit 788bfe3

Please sign in to comment.