Skip to content

Commit

Permalink
Merge pull request #14 from cosmos/commercial
Browse files Browse the repository at this point in the history
Agoric edits
  • Loading branch information
schnetzlerjoe authored Aug 13, 2023
2 parents b1fa8bb + 385933b commit b209753
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 109 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
jobs:
build-test:
name: Build, and Test
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
Expand Down Expand Up @@ -47,7 +47,7 @@ jobs:
# run: yarn auto-changelog validate
all-jobs-pass:
name: All jobs pass
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
needs:
- build-test
steps:
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ We use Github to host code, to track issues and feature requests, as well as acc
## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests
Pull requests are the best way to propose changes to the codebase. We actively welcome your pull requests:

1. Fork the repo and create your branch from `master`.
1. Fork the repo and create your branch from `main`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
4. Ensure the test suite passes.
Expand All @@ -24,8 +24,8 @@ Pull requests are the best way to propose changes to the codebase. We actively w
## Any contributions you make will be under the MIT License (LICENSE)
In short, when you submit code changes, your submissions are understood to be under the same MIT License (LICENSE) that covers the project. Feel free to contact the maintainers if that's a concern.

## Report bugs using Github's [issues](https://github.com/briandk/transcriptase-atom/issues)
We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/briandk/transcriptase-atom/issues); it's that easy!
## Report bugs using Github's [issues](https://github.com/cosmos/snap/issues)
We use GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/cosmos/snap/issues); it's that easy!

## Write bug reports with detail, background, and sample code
Here's [an example](http://stackoverflow.com/q/12488905/180626) of a bug report. The more information you can provide, the easier it is for us to understand and solve the problem.
Expand Down
14 changes: 14 additions & 0 deletions packages/snap/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const DEFAULT_FEES = {
amount: [],
gas: "200000",
};

export const DEFAULT_SLIP44 = 118;

export const WALLET_URL = "https://wallet.mysticlabs.xyz";

// The multiplier to use to get u{denom} from {denom}
export const U_MULTIPLIER = 1000000;

// This is the default gas in {denom} (note not in u{denom})
export const DEFAULT_AVG_GAS = 0.05;
16 changes: 6 additions & 10 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { Address } from "./types/address";
import { ChainState, AddressState } from "./state";
import { Result } from "./types/result";
import { submitTransaction } from "./transaction";
import { getAddress } from "./address";
import { DEFAULT_FEES } from "./constants";

/**
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
*
* @param args - The request handler args as object.
* @param args.request - A validated JSON-RPC request object.
* @param args.request - A JSON-RPC request object that will be validated.
* @returns A result object.
* @throws If the request method is not valid for this snap.
*/
Expand All @@ -38,9 +38,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
if (!confirmation) {
throw new Error("Initialize Cosmos chain support was denied.");
}
let chains = new Chains([]);
let chainList = await initializeChains();
chains = new Chains(chainList);
let chains = new Chains(chainList);
// Initialize with initial state
await snap.request({
method: "snap_manageState",
Expand Down Expand Up @@ -84,10 +83,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
}

//Calculate fees for transaction
let fees: Fees = {
amount: [],
gas: "200000",
};
let fees: Fees = DEFAULT_FEES;

if (request.params.fees) {
if (typeof request.params.fees == "string") {
Expand Down Expand Up @@ -116,7 +112,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
text(`${request.params.chain_id}`),
divider(),
heading("Transaction"),
text(`${messages}`),
text(JSON.stringify(messages, null, 2)),
heading("Fees Amount"),
text(`${fees}`),
]),
Expand Down Expand Up @@ -225,7 +221,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({

// Ensure chain id doesn't already exist
let get_chain = await ChainState.getChain(new_chain.chain_id);

if (get_chain != null) {
await snap.request({
method: "snap_dialog",
Expand Down
24 changes: 14 additions & 10 deletions packages/snap/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Chains, Chain, CosmosAddress } from "./types/chains";
import { Addresses, Address } from "./types/address";
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { DEFAULT_SLIP44, WALLET_URL } from "./constants";

/**
* ChainState is the class to manage all Chain state within Metamask.
Expand Down Expand Up @@ -40,7 +41,8 @@ export class ChainState {
let node = await snap.request({
method: "snap_getBip44Entropy",
params: {
coinType: typeof chain.slip44 == "number" ? chain.slip44 : 118,
coinType:
typeof chain.slip44 == "number" ? chain.slip44 : DEFAULT_SLIP44,
},
});

Expand Down Expand Up @@ -78,7 +80,7 @@ export class ChainState {
method: "snap_manageState",
params: { operation: "get" },
});
if (data?.chains == undefined || data?.chains == null) {
if (data?.chains == null) {
throw new Error("Snap has not been initialized. Please initialize snap.");
}
return new Chains(JSON.parse(data?.chains?.toString()!));
Expand All @@ -94,7 +96,7 @@ export class ChainState {
method: "snap_manageState",
params: { operation: "get" },
});
if (data?.chains == undefined || data?.chains == null) {
if (data?.chains == null) {
throw new Error("Snap has not been initialized. Please initialize snap.");
}
let chains = new Chains(JSON.parse(data?.chains?.toString()!));
Expand Down Expand Up @@ -144,7 +146,7 @@ export class ChainState {
* @returns Returns the current state of Chains.
* @throws If an error occurs.
*/
public static async addChains(chains: Chains): Promise<Chains> {
public static async replaceAllChains(chains: Chains): Promise<Chains> {
// get current state
const data = await snap.request({
method: "snap_manageState",
Expand All @@ -166,7 +168,7 @@ export class ChainState {
method: "snap_manageState",
params: { operation: "get" },
});
if (data?.chains == undefined || data?.chains == null) {
if (data?.chains == null) {
throw new Error("Snap has not been initialized. Please initialize snap.");
}
// remember we keep chain stores as a json string so convert into a Chains object
Expand Down Expand Up @@ -206,7 +208,7 @@ export class AddressState {
params: { operation: "get" },
});

if (data?.addresses == undefined || data?.addresses == null) {
if (data?.addresses == null) {
throw new Error(
"Address book was not found. Add an address to address book to initialize."
);
Expand Down Expand Up @@ -235,7 +237,7 @@ export class AddressState {
params: { operation: "get" },
});

if (data?.addresses == undefined || data?.addresses == null) {
if (data?.addresses == null) {
throw new Error(
"Address book was not found. Add an address to address book to initialize."
);
Expand All @@ -256,7 +258,7 @@ export class AddressState {

if (addressList.length == 0) {
throw new Error(
`${chain_id} is not found. Add the address to your address book at https://wallet.mysticlabs.xyz`
`${chain_id} is not found. Add the address to your address book at ${WALLET_URL}`
);
}

Expand Down Expand Up @@ -310,7 +312,9 @@ export class AddressState {
* @returns Boolean indicating success or not.
* @throws If an error occurs.
*/
public static async addAddresses(addresses: Addresses): Promise<Boolean> {
public static async replaceAllAddresses(
addresses: Addresses
): Promise<Boolean> {
// get current state
const data = await snap.request({
method: "snap_manageState",
Expand Down Expand Up @@ -342,7 +346,7 @@ export class AddressState {
params: { operation: "get" },
});

if (data?.addresses == undefined || data?.addresses == null) {
if (data?.addresses == null) {
throw new Error("Snap has not been initialized. Please initialize snap.");
}

Expand Down
22 changes: 19 additions & 3 deletions packages/snap/src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { DeliverTxResponse, SigningStargateClient } from "@cosmjs/stargate";
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
import { CosmosAddress, Fees } from "./types/chains";
import { Fees } from "./types/chains";
import { ChainState } from "./state";
import { heading, panel, text } from "@metamask/snaps-ui";
import {
WALLET_URL,
DEFAULT_FEES,
U_MULTIPLIER,
DEFAULT_AVG_GAS,
} from "./constants";

/**
* submitTransaction Submits a transaction to the chain specified.
Expand All @@ -21,12 +27,22 @@ export const submitTransaction = async (
try {
// get the chain from state
let chain = await ChainState.getChain(chain_id);
if (chain == null) {
throw new Error(
`Chain ${chain_id} not found. Please go to ${WALLET_URL} to add it!`
);
}

// if fees are not specified then just use default fees + gas
let ugas = chain.fees.fee_tokens[0].average_gas_price * 1000000;
let avg_gas_price = chain.fees.fee_tokens[0].average_gas_price
? chain.fees.fee_tokens[0].average_gas_price
: DEFAULT_AVG_GAS;
let ugas = avg_gas_price * U_MULTIPLIER;
if (fees == null) {
fees = {
amount: [{ denom: chain.fees.fee_tokens[0].denom, amount: "500" }],
amount: [
{ denom: chain.fees.fee_tokens[0].denom, amount: DEFAULT_FEES.gas },
],
gas: ugas.toString(),
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/tests/address-book-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class PassingAddressStateTests {
let new_address_book = new Addresses([new_address]);

//Add new address to Address Book
await AddressState.addAddresses(new_address_book);
await AddressState.replaceAllAddresses(new_address_book);

//Get updated Address Book
const result = await AddressState.getAddressBook();
Expand Down Expand Up @@ -226,7 +226,7 @@ class FailingAddressStateTests {

await t.throwsAsync(
async () => {
await AddressState.addAddresses(new_address_book);
await AddressState.replaceAllAddresses(new_address_book);
},
{ instanceOf: Error, message: message }
);
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/tests/chains-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PassingChainStateTests {
let new_chains = new Chains([new_chain]);

//Add new chain to Chains
await ChainState.addChains(new_chains);
await ChainState.replaceAllChains(new_chains);

//Get updated Chains
const result = await ChainState.getChains();
Expand Down
Loading

0 comments on commit b209753

Please sign in to comment.