Skip to content

Commit

Permalink
add example for msg send
Browse files Browse the repository at this point in the history
  • Loading branch information
jtary committed Mar 15, 2022
1 parent 69c3084 commit 92cb5a8
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,64 @@ const signedMessage = await client.signAndBroadcast(
);
```

## Signing MsgSend

Message types that are not part of the Akash specifically may not have specific encoders, as used in the above example. For these, the messages can be created directly as long as they conform to the expected schema for the message. Below is an example of doing this for the cosmos MsgSend message.

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

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

// Setup a send message manually. See the appropriate repo (cosmjs in this case)
// for the specific shape of the message.
const message = {
fromAddress: account.address,
toAddress: "akash123...",
amount: coins(10, "akt"),
};

// Set the appropriate typeUrl and attach the encoded message as the value
const msgAny = {
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: message
};

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

const myRegistry = new Registry(
defaultRegistryTypes
);

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

const fee = {
amount: [
{
denom: "uakt",
amount: "5000",
},
],
gas: "800000",
};

const msg = await client.sign(
account.address,
[msgAny],
fee,
"send some tokens"
);
```

## 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.
Expand Down
54 changes: 54 additions & 0 deletions examples/estimate_gas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { SigningStargateClient } from "@cosmjs/stargate";

// import the required message type from akashjs
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" });

// 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);
}

main();
60 changes: 60 additions & 0 deletions examples/signed_msg_send.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { coins, DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing";
import { defaultRegistryTypes, SigningStargateClient } from "@cosmjs/stargate";

async function main() {

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

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

// Setup a send message
const message = {
fromAddress: account.address,
toAddress: "akash123...",
amount: coins(10, "akt"),
};

// Set the appropriate typeUrl and attach the encoded message as the value
const msgAny = {
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: message
};

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

const myRegistry = new Registry(
defaultRegistryTypes
);

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

const fee = {
amount: [
{
denom: "uakt",
amount: "5000",
},
],
gas: "800000",
};

const msg = await client.sign(
account.address,
[msgAny],
fee,
"take down deployment"
);

console.log(msg);
}

main();

0 comments on commit 92cb5a8

Please sign in to comment.