Skip to content

Commit 385933b

Browse files
committed
Agoric edits
1 parent b1fa8bb commit 385933b

File tree

9 files changed

+152
-109
lines changed

9 files changed

+152
-109
lines changed

Diff for: .github/workflows/build-test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
jobs:
99
build-test:
1010
name: Build, and Test
11-
runs-on: ubuntu-20.04
11+
runs-on: ubuntu-latest
1212
strategy:
1313
matrix:
1414
node-version: [16.x]
@@ -47,7 +47,7 @@ jobs:
4747
# run: yarn auto-changelog validate
4848
all-jobs-pass:
4949
name: All jobs pass
50-
runs-on: ubuntu-20.04
50+
runs-on: ubuntu-latest
5151
needs:
5252
- build-test
5353
steps:

Diff for: CONTRIBUTING.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ We use Github to host code, to track issues and feature requests, as well as acc
1414
## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests
1515
Pull requests are the best way to propose changes to the codebase. We actively welcome your pull requests:
1616

17-
1. Fork the repo and create your branch from `master`.
17+
1. Fork the repo and create your branch from `main`.
1818
2. If you've added code that should be tested, add tests.
1919
3. If you've changed APIs, update the documentation.
2020
4. Ensure the test suite passes.
@@ -24,8 +24,8 @@ Pull requests are the best way to propose changes to the codebase. We actively w
2424
## Any contributions you make will be under the MIT License (LICENSE)
2525
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.
2626

27-
## Report bugs using Github's [issues](https://github.com/briandk/transcriptase-atom/issues)
28-
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!
27+
## Report bugs using Github's [issues](https://github.com/cosmos/snap/issues)
28+
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!
2929

3030
## Write bug reports with detail, background, and sample code
3131
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.

Diff for: packages/snap/src/constants.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const DEFAULT_FEES = {
2+
amount: [],
3+
gas: "200000",
4+
};
5+
6+
export const DEFAULT_SLIP44 = 118;
7+
8+
export const WALLET_URL = "https://wallet.mysticlabs.xyz";
9+
10+
// The multiplier to use to get u{denom} from {denom}
11+
export const U_MULTIPLIER = 1000000;
12+
13+
// This is the default gas in {denom} (note not in u{denom})
14+
export const DEFAULT_AVG_GAS = 0.05;

Diff for: packages/snap/src/index.ts

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { Address } from "./types/address";
66
import { ChainState, AddressState } from "./state";
77
import { Result } from "./types/result";
88
import { submitTransaction } from "./transaction";
9-
import { getAddress } from "./address";
9+
import { DEFAULT_FEES } from "./constants";
1010

1111
/**
1212
* Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`.
1313
*
1414
* @param args - The request handler args as object.
15-
* @param args.request - A validated JSON-RPC request object.
15+
* @param args.request - A JSON-RPC request object that will be validated.
1616
* @returns A result object.
1717
* @throws If the request method is not valid for this snap.
1818
*/
@@ -38,9 +38,8 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
3838
if (!confirmation) {
3939
throw new Error("Initialize Cosmos chain support was denied.");
4040
}
41-
let chains = new Chains([]);
4241
let chainList = await initializeChains();
43-
chains = new Chains(chainList);
42+
let chains = new Chains(chainList);
4443
// Initialize with initial state
4544
await snap.request({
4645
method: "snap_manageState",
@@ -84,10 +83,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
8483
}
8584

8685
//Calculate fees for transaction
87-
let fees: Fees = {
88-
amount: [],
89-
gas: "200000",
90-
};
86+
let fees: Fees = DEFAULT_FEES;
9187

9288
if (request.params.fees) {
9389
if (typeof request.params.fees == "string") {
@@ -116,7 +112,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
116112
text(`${request.params.chain_id}`),
117113
divider(),
118114
heading("Transaction"),
119-
text(`${messages}`),
115+
text(JSON.stringify(messages, null, 2)),
120116
heading("Fees Amount"),
121117
text(`${fees}`),
122118
]),
@@ -225,7 +221,7 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
225221

226222
// Ensure chain id doesn't already exist
227223
let get_chain = await ChainState.getChain(new_chain.chain_id);
228-
224+
229225
if (get_chain != null) {
230226
await snap.request({
231227
method: "snap_dialog",

Diff for: packages/snap/src/state.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Chains, Chain, CosmosAddress } from "./types/chains";
22
import { Addresses, Address } from "./types/address";
33
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
4+
import { DEFAULT_SLIP44, WALLET_URL } from "./constants";
45

56
/**
67
* ChainState is the class to manage all Chain state within Metamask.
@@ -40,7 +41,8 @@ export class ChainState {
4041
let node = await snap.request({
4142
method: "snap_getBip44Entropy",
4243
params: {
43-
coinType: typeof chain.slip44 == "number" ? chain.slip44 : 118,
44+
coinType:
45+
typeof chain.slip44 == "number" ? chain.slip44 : DEFAULT_SLIP44,
4446
},
4547
});
4648

@@ -78,7 +80,7 @@ export class ChainState {
7880
method: "snap_manageState",
7981
params: { operation: "get" },
8082
});
81-
if (data?.chains == undefined || data?.chains == null) {
83+
if (data?.chains == null) {
8284
throw new Error("Snap has not been initialized. Please initialize snap.");
8385
}
8486
return new Chains(JSON.parse(data?.chains?.toString()!));
@@ -94,7 +96,7 @@ export class ChainState {
9496
method: "snap_manageState",
9597
params: { operation: "get" },
9698
});
97-
if (data?.chains == undefined || data?.chains == null) {
99+
if (data?.chains == null) {
98100
throw new Error("Snap has not been initialized. Please initialize snap.");
99101
}
100102
let chains = new Chains(JSON.parse(data?.chains?.toString()!));
@@ -144,7 +146,7 @@ export class ChainState {
144146
* @returns Returns the current state of Chains.
145147
* @throws If an error occurs.
146148
*/
147-
public static async addChains(chains: Chains): Promise<Chains> {
149+
public static async replaceAllChains(chains: Chains): Promise<Chains> {
148150
// get current state
149151
const data = await snap.request({
150152
method: "snap_manageState",
@@ -166,7 +168,7 @@ export class ChainState {
166168
method: "snap_manageState",
167169
params: { operation: "get" },
168170
});
169-
if (data?.chains == undefined || data?.chains == null) {
171+
if (data?.chains == null) {
170172
throw new Error("Snap has not been initialized. Please initialize snap.");
171173
}
172174
// remember we keep chain stores as a json string so convert into a Chains object
@@ -206,7 +208,7 @@ export class AddressState {
206208
params: { operation: "get" },
207209
});
208210

209-
if (data?.addresses == undefined || data?.addresses == null) {
211+
if (data?.addresses == null) {
210212
throw new Error(
211213
"Address book was not found. Add an address to address book to initialize."
212214
);
@@ -235,7 +237,7 @@ export class AddressState {
235237
params: { operation: "get" },
236238
});
237239

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

257259
if (addressList.length == 0) {
258260
throw new Error(
259-
`${chain_id} is not found. Add the address to your address book at https://wallet.mysticlabs.xyz`
261+
`${chain_id} is not found. Add the address to your address book at ${WALLET_URL}`
260262
);
261263
}
262264

@@ -310,7 +312,9 @@ export class AddressState {
310312
* @returns Boolean indicating success or not.
311313
* @throws If an error occurs.
312314
*/
313-
public static async addAddresses(addresses: Addresses): Promise<Boolean> {
315+
public static async replaceAllAddresses(
316+
addresses: Addresses
317+
): Promise<Boolean> {
314318
// get current state
315319
const data = await snap.request({
316320
method: "snap_manageState",
@@ -342,7 +346,7 @@ export class AddressState {
342346
params: { operation: "get" },
343347
});
344348

345-
if (data?.addresses == undefined || data?.addresses == null) {
349+
if (data?.addresses == null) {
346350
throw new Error("Snap has not been initialized. Please initialize snap.");
347351
}
348352

Diff for: packages/snap/src/transaction.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { DeliverTxResponse, SigningStargateClient } from "@cosmjs/stargate";
22
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
3-
import { CosmosAddress, Fees } from "./types/chains";
3+
import { Fees } from "./types/chains";
44
import { ChainState } from "./state";
55
import { heading, panel, text } from "@metamask/snaps-ui";
6+
import {
7+
WALLET_URL,
8+
DEFAULT_FEES,
9+
U_MULTIPLIER,
10+
DEFAULT_AVG_GAS,
11+
} from "./constants";
612

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

2536
// if fees are not specified then just use default fees + gas
26-
let ugas = chain.fees.fee_tokens[0].average_gas_price * 1000000;
37+
let avg_gas_price = chain.fees.fee_tokens[0].average_gas_price
38+
? chain.fees.fee_tokens[0].average_gas_price
39+
: DEFAULT_AVG_GAS;
40+
let ugas = avg_gas_price * U_MULTIPLIER;
2741
if (fees == null) {
2842
fees = {
29-
amount: [{ denom: chain.fees.fee_tokens[0].denom, amount: "500" }],
43+
amount: [
44+
{ denom: chain.fees.fee_tokens[0].denom, amount: DEFAULT_FEES.gas },
45+
],
3046
gas: ugas.toString(),
3147
};
3248
}

Diff for: packages/snap/tests/address-book-test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class PassingAddressStateTests {
122122
let new_address_book = new Addresses([new_address]);
123123

124124
//Add new address to Address Book
125-
await AddressState.addAddresses(new_address_book);
125+
await AddressState.replaceAllAddresses(new_address_book);
126126

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

227227
await t.throwsAsync(
228228
async () => {
229-
await AddressState.addAddresses(new_address_book);
229+
await AddressState.replaceAllAddresses(new_address_book);
230230
},
231231
{ instanceOf: Error, message: message }
232232
);

Diff for: packages/snap/tests/chains-test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class PassingChainStateTests {
8888
let new_chains = new Chains([new_chain]);
8989

9090
//Add new chain to Chains
91-
await ChainState.addChains(new_chains);
91+
await ChainState.replaceAllChains(new_chains);
9292

9393
//Get updated Chains
9494
const result = await ChainState.getChains();

0 commit comments

Comments
 (0)