Skip to content

Commit

Permalink
update gas example
Browse files Browse the repository at this point in the history
  • Loading branch information
jtary committed Mar 14, 2022
1 parent db80965 commit 2f8e482
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 79 deletions.
93 changes: 67 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,52 +99,44 @@ const data = QueryDeploymentsResponse.toJSON(response);

## Signed Transactions

For transactions that requiring signing, requests must be passed through the signing client. For creating the message, the appropriate message type can be imported from `akashjs`. Below is an example of creating a CloseDeployment message.
For transactions that requiring signing, requests must be passed through the signing client. For creating the message, the appropriate message type can be imported from `akashjs`. Below is an example of creating and broadcasting a CloseDeployment message.

```ts
import { Secp256k1HdWallet } from "@cosmjs/launchpad";
import { stargate as akashStargate } from "@akashnetwork/akashjs";
import { Registry } from "@cosmjs/proto-signing";
import {
defaultRegistryTypes,
SigningStargateClient,
} from "@cosmjs/stargate";
const mnemonic = "your wallet mnemonic";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "akash" });

const wallet = await Secp256k1HdWallet
.generate(undefined, { prefix: "akash" });
// get first account
const [account] = await wallet.getAccounts();

// Use the encode method for the message to wrap the data
const message = MsgCloseDeployment.encode(
MsgCloseDeployment.fromJSON({
id: {
dseq: "555555",
owner: 'ownerAddress'
}
})
).finish();
const message = MsgCloseDeployment.fromPartial({
id: {
dseq: "555555",
owner: account.address,
}
});

// Set the appropriate typeUrl and attach the encoded message as the value
const msgAny = {
typeUrl: "akash.deployment.v1beta1.Msg/CloseDeployment",
typeUrl: getTypeUrl(MsgCloseDeployment),
value: message
};

// You can use your own RPC node, or get a list of public nodes from akashjs
const rpcEndpoint = "http://my.rpc.node";

const myRegistry = new Registry([
...defaultRegistryTypes,
...akashStargate.registry as any,
]);
const myRegistry = new Registry(
getAkashTypeRegistry()
);

const client = await SigningStargateClient.connectWithSigner(
rpcEndpoint,
wallet,
{ registry: myRegistry }
{
registry: myRegistry
}
);

const [account] = await wallet.getAccounts();

const fee = {
amount: [
{
Expand All @@ -161,4 +153,53 @@ const signedMessage = await client.signAndBroadcast(
fee,
"take down deployment"
);
```

## Estimating Gas

When sending transactions, it can be useful to get an estimate of the gas required for a given message. This can be done using the `simulate` method of the signing client which will send the passed in message to an RPC node which will return the estimated gas for that transaction. Before is an example of doing this for the same transaction as shown above.

```ts
const mnemonic = "your wallet mnemonic";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "akash" });

// get first account
const [account] = await wallet.getAccounts();

// Use the encode method for the message to wrap the data
const message = MsgCloseDeployment.fromPartial({
id: {
dseq: "555555",
owner: account.address,
}
});

// Set the appropriate typeUrl and attach the encoded message as the value
const msgAny = {
typeUrl: getTypeUrl(MsgCloseDeployment),
value: message
};

// You can use your own RPC node, or get a list of public nodes from akashjs
const rpcEndpoint = "http://my.rpc.node";

const myRegistry = new Registry(
getAkashTypeRegistry()
);

const client = await SigningStargateClient.connectWithSigner(
rpcEndpoint,
wallet,
{
registry: myRegistry
}
);

const gas = await client.simulate(
account.address,
[msgAny],
"take down deployment"
);

console.log(gas);
```
4 changes: 1 addition & 3 deletions curl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ Since broadcasting is a mutating action, it must be submitted via a POST request

## Estimate Gas Fees

The REST API does not expose a general method for estimating the gas for a transaction. However, a message with the `simulate` flag can be used to request an estimation from a node.

Creating a message like this can be generated using the same method as linked to above.
The REST API does not expose a general method for estimating the gas for a transaction. If an estimation of gas fees is required, the messages can be simulated using the RPC API available though akashjs. Please see [this document](https://github.com/ovrclk/akashjs-doc/blob/main/README.md) for details.

## Getting Transaction Status

Expand Down
49 changes: 22 additions & 27 deletions examples/take_down_deployment.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
import { Secp256k1HdWallet } from "@cosmjs/launchpad";
import { stargate as akashStargate } from "@akashnetwork/akashjs";
import { Registry } from "@cosmjs/proto-signing";
import {
defaultRegistryTypes,
SigningStargateClient,
} from "@cosmjs/stargate";
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";

// import the required message type from akashjs
import { MsgCloseDeployment } from "@akashnetwork/akashjs/build/protobuf/akash/deployment/v1beta1/deployment";
import { getAkashTypeRegistry, getTypeUrl } from "@akashnetwork/akashjs/build/src/stargate/index";
import { MsgCloseDeployment } from "@akashnetwork/akashjs/build/src/protobuf/akash/deployment/v1beta1/deployment";

async function main() {
const mnemonic = "your wallet mnemonic";
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "akash" });

const wallet = await Secp256k1HdWallet
.generate(undefined, { prefix: "akash" });
// get first account
const [account] = await wallet.getAccounts();

// Use the encode method for the message to wrap the data
const message = MsgCloseDeployment.encode(
MsgCloseDeployment.fromJSON({
id: {
dseq: "555555",
owner: 'ownerAddress'
}
})
).finish();
const message = MsgCloseDeployment.fromPartial({
id: {
dseq: "555555",
owner: account.address,
}
});

// Set the appropriate typeUrl and attach the encoded message as the value
const msgAny = {
typeUrl: "akash.deployment.v1beta1.Msg/CloseDeployment",
typeUrl: getTypeUrl(MsgCloseDeployment),
value: message
};

// You can use your own RPC node, or get a list of public nodes from akashjs
const rpcEndpoint = "http://my.rpc.node";

const myRegistry = new Registry([
...defaultRegistryTypes,
...akashStargate.registry as any,
]);
const myRegistry = new Registry(
getAkashTypeRegistry()
);

const client = await SigningStargateClient.connectWithSigner(
rpcEndpoint,
wallet,
{ registry: myRegistry }
{
registry: myRegistry
}
);

const [account] = await wallet.getAccounts();

const fee = {
amount: [
{
Expand All @@ -64,4 +59,4 @@ async function main() {
);
}

main();
main();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Joseph Tary <[email protected]>",
"license": "MIT",
"dependencies": {
"@akashnetwork/akashjs": "^0.0.14",
"@akashnetwork/akashjs": "^0.0.20",
"@cosmjs/launchpad": "^0.27.1",
"@cosmjs/proto-signing": "^0.27.1",
"@cosmjs/stargate": "^0.27.1",
Expand Down
44 changes: 22 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# yarn lockfile v1


"@akashnetwork/akashjs@^0.0.14":
version "0.0.14"
resolved "https://registry.yarnpkg.com/@akashnetwork/akashjs/-/akashjs-0.0.14.tgz#89e5e83de49cba12fa12835bace5d65f7aa8be2a"
integrity sha512-O7K1x+WDiH4/X4w8qiNXVZRZB4mTI5W91ypcRXhF6BqVT5GTHCqs336GiJzYNoBLdKl+ZO2eI6zXaG/zaCgWJQ==
"@akashnetwork/akashjs@^0.0.20":
version "0.0.20"
resolved "https://registry.yarnpkg.com/@akashnetwork/akashjs/-/akashjs-0.0.20.tgz#b1967379382e68d43aced798a55967a74b25c83c"
integrity sha512-x7BV81U4F5ljJJ2eePVUgA1CHg60C++jiqqfkrpMXwU4l16KH4CvoQTx1uEEeI/zbnBZrX23el/v8k/k/u7M+A==
dependencies:
"@cosmjs/launchpad" "^0.25.5"
"@cosmjs/proto-signing" "^0.25.5"
Expand All @@ -31,7 +31,7 @@

"@cosmjs/[email protected]":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/amino/-/amino-0.27.1.tgz#0910256b5aecd794420bb5f7319d98fc63252fa1"
resolved "https://registry.npmjs.org/@cosmjs/amino/-/amino-0.27.1.tgz"
integrity sha512-w56ar/nK9+qlvWDpBPRmD0Blk2wfkkLqRi1COs1x7Ll1LF0AtkIBUjbRKplENLbNovK0T3h+w8bHiFm+GBGQOA==
dependencies:
"@cosmjs/crypto" "0.27.1"
Expand All @@ -51,7 +51,7 @@

"@cosmjs/[email protected]":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/crypto/-/crypto-0.27.1.tgz#271c853089a3baf3acd6cf0b2122fd49f8815743"
resolved "https://registry.npmjs.org/@cosmjs/crypto/-/crypto-0.27.1.tgz"
integrity sha512-vbcxwSt99tIYJg8Spp00wc3zx72qx+pY3ozGuBN8gAvySnagK9dQ/jHwtWQWdammmdD6oW+75WfIHZ+gNa+Ybg==
dependencies:
"@cosmjs/encoding" "0.27.1"
Expand Down Expand Up @@ -83,7 +83,7 @@

"@cosmjs/[email protected]":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/encoding/-/encoding-0.27.1.tgz#3cd5bc0af743485eb2578cdb08cfa84c86d610e1"
resolved "https://registry.npmjs.org/@cosmjs/encoding/-/encoding-0.27.1.tgz"
integrity sha512-rayLsA0ojHeniaRfWWcqSsrE/T1rl1gl0OXVNtXlPwLJifKBeLEefGbOUiAQaT0wgJ8VNGBazVtAZBpJidfDhw==
dependencies:
base64-js "^1.3.0"
Expand Down Expand Up @@ -130,7 +130,7 @@

"@cosmjs/launchpad@^0.27.1":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/launchpad/-/launchpad-0.27.1.tgz#b6f1995748be96560f5f01e84d3ff907477dda77"
resolved "https://registry.npmjs.org/@cosmjs/launchpad/-/launchpad-0.27.1.tgz"
integrity sha512-DcFwGD/z5PK8CzO2sojDxa+Be9EIEtRZb2YawgVnw2Ht/p5FlNv+OVo8qlishpBdalXEN7FvQ1dVeDFEe9TuJw==
dependencies:
"@cosmjs/amino" "0.27.1"
Expand All @@ -143,7 +143,7 @@

"@cosmjs/[email protected]":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/math/-/math-0.27.1.tgz#be78857b008ffc6b1ed6fecaa1c4cd5bc38c07d7"
resolved "https://registry.npmjs.org/@cosmjs/math/-/math-0.27.1.tgz"
integrity sha512-cHWVjmfIjtRc7f80n7x+J5k8pe+vTVTQ0lA82tIxUgqUvgS6rogPP/TmGtTiZ4+NxWxd11DUISY6gVpr18/VNQ==
dependencies:
bn.js "^5.2.0"
Expand All @@ -157,7 +157,7 @@

"@cosmjs/[email protected]", "@cosmjs/proto-signing@^0.27.1":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/proto-signing/-/proto-signing-0.27.1.tgz#1f8f662550aab012d957d02f43c77d914c2ae0db"
resolved "https://registry.npmjs.org/@cosmjs/proto-signing/-/proto-signing-0.27.1.tgz"
integrity sha512-t7/VvQivMdM1KgKWai/9ZCEcGFXJtr9Xo0hGcPLTn9wGkh9tmOsUXINYVMsf5D/jWIm1MDPbGCYfdb9V1Od4hw==
dependencies:
"@cosmjs/amino" "0.27.1"
Expand Down Expand Up @@ -276,7 +276,7 @@

"@cosmjs/[email protected]":
version "0.27.1"
resolved "https://registry.yarnpkg.com/@cosmjs/utils/-/utils-0.27.1.tgz#1c8efde17256346ef142a3bd15158ee4055470e2"
resolved "https://registry.npmjs.org/@cosmjs/utils/-/utils-0.27.1.tgz"
integrity sha512-VG7QPDiMUzVPxRdJahDV8PXxVdnuAHiIuG56hldV4yPnOz/si/DLNd7VAUUA5923b6jS1Hhev0Hr6AhEkcxBMg==

"@cosmjs/utils@^0.25.6":
Expand Down Expand Up @@ -354,7 +354,7 @@

"@types/node@>=13.7.0":
version "17.0.21"
resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.21.tgz#864b987c0c68d07b4345845c3e63b75edd143644"
resolved "https://registry.npmjs.org/@types/node/-/node-17.0.21.tgz"
integrity sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ==

"@types/node@^13.7.0":
Expand Down Expand Up @@ -399,7 +399,7 @@ atob@^2.1.2:

axios@^0.21.1, axios@^0.21.2:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
resolved "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
dependencies:
follow-redirects "^1.14.0"
Expand Down Expand Up @@ -447,7 +447,7 @@ bn.js@^4.11.8, bn.js@^4.11.9:

bn.js@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.0.tgz#358860674396c6997771a9d051fcc1b57d4ae002"
resolved "https://registry.npmjs.org/bn.js/-/bn.js-5.2.0.tgz"
integrity sha512-D7iWRBvnZE8ecXiLj/9wbxH7Tk79fAh8IHaTNq1RWRixsS02W+5qS+iE9yq6RYl0asXx5tw0bLhmT5pIfbSquw==

brorand@^1.1.0:
Expand Down Expand Up @@ -503,7 +503,7 @@ core-util-is@~1.0.0:

cosmjs-types@^0.4.0:
version "0.4.1"
resolved "https://registry.yarnpkg.com/cosmjs-types/-/cosmjs-types-0.4.1.tgz#3b2a53ba60d33159dd075596ce8267cfa7027063"
resolved "https://registry.npmjs.org/cosmjs-types/-/cosmjs-types-0.4.1.tgz"
integrity sha512-I7E/cHkIgoJzMNQdFF0YVqPlaTqrqKHrskuSTIqlEyxfB5Lf3WKCajSXVK2yHOfOFfSux/RxEdpMzw/eO4DIog==
dependencies:
long "^4.0.0"
Expand Down Expand Up @@ -839,13 +839,13 @@ pbkdf2@^3.0.9:
sha.js "^2.4.8"

pkijs@^2.1.95:
version "2.2.2"
resolved "https://registry.yarnpkg.com/pkijs/-/pkijs-2.2.2.tgz#d0780379a2fb80c892c3ead4bcfb42675b67a982"
integrity sha512-xRTEW9LgUeHBe5hRCC4EHvLbO6o3L/t8uEk8cTaSMVEjEcy2G5qJ/bY2ndvrGgZlKDThnTrM4Xlwu8Qpyr6mOg==
version "2.3.0"
resolved "https://registry.yarnpkg.com/pkijs/-/pkijs-2.3.0.tgz#4956813de77c16a3a7ec0cf15aa6b8bd368c15df"
integrity sha512-MYg9B0TTOCNDQPIMfTHdPEbldL+tt34u9/XYBwxnhTrbX2g4vfO2l5pNAAVnfXWLAsAbDUzEVsYncCYLwkr8OQ==
dependencies:
asn1js "^2.1.1"
bytestreamjs "^1.0.29"
pvutils "^1.0.17"
pvutils "^1.1.3"

prebuild-install@^7.0.1:
version "7.0.1"
Expand Down Expand Up @@ -878,7 +878,7 @@ process@^0.11.10:

protobufjs@^6.8.8, protobufjs@~6.11.2:
version "6.11.2"
resolved "https://registry.yarnpkg.com/protobufjs/-/protobufjs-6.11.2.tgz#de39fabd4ed32beaa08e9bb1e30d08544c1edf8b"
resolved "https://registry.npmjs.org/protobufjs/-/protobufjs-6.11.2.tgz"
integrity sha512-4BQJoPooKJl2G9j3XftkIXjoC9C0Av2NOrWmbLWT1vH32GcSUHjM0Arra6UfTsVyfMAuFzaLucXn1sadxJydAw==
dependencies:
"@protobufjs/aspromise" "^1.1.2"
Expand Down Expand Up @@ -922,7 +922,7 @@ pump@^3.0.0:
end-of-stream "^1.1.0"
once "^1.3.1"

pvutils@^1.0.17, pvutils@latest:
pvutils@^1.0.17, pvutils@^1.1.3, pvutils@latest:
version "1.1.3"
resolved "https://registry.yarnpkg.com/pvutils/-/pvutils-1.1.3.tgz#f35fc1d27e7cd3dfbd39c0826d173e806a03f5a3"
integrity sha512-pMpnA0qRdFp32b1sJl1wOJNxZLQ2cbQx+k6tjNtZ8CpvVhNqEPRgivZ2WOUev2YMajecdH7ctUPDvEe87nariQ==
Expand Down Expand Up @@ -1124,7 +1124,7 @@ tunnel-agent@^0.6.0:

typescript@^4.6.2:
version "4.6.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.6.2.tgz#fe12d2727b708f4eef40f51598b3398baa9611d4"
resolved "https://registry.npmjs.org/typescript/-/typescript-4.6.2.tgz"
integrity sha512-HM/hFigTBHZhLXshn9sN37H085+hQGeJHJ/X7LpBWLID/fbc2acUMfU+lGD98X81sKP+pFa9f0DZmCwB9GnbAg==

util-deprecate@^1.0.1, util-deprecate@~1.0.1:
Expand Down

0 comments on commit 2f8e482

Please sign in to comment.