Skip to content
This repository has been archived by the owner on Apr 15, 2022. It is now read-only.

Commit

Permalink
support Juno network. added wasm protobuf to proto.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Booyoun-Kim committed Jan 26, 2022
1 parent d204348 commit 5ad902c
Show file tree
Hide file tree
Showing 4 changed files with 81,954 additions and 68,738 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,29 @@ const signedTxBytes = cosmos.sign(txBody, authInfo, data.account.account_number,
cosmos.broadcast(signedTxBytes).then(response => console.log(response));
```

Now it is time to transfer CW20 tokens via Cosmosjs! Copy the CW20 contract into the variable of cw20Contract. You can instantiate and execute cosmwasm contracts on JUNO. It is possible to create your own CW20 token [here](https://junomint.ezstaking.io/).
```js
cosmos.getAccounts(address).then(data => {
// signDoc = (1)txBody + (2)authInfo
// ---------------------------------- (1)txBody ----------------------------------
let cw20Contract = "juno10rktvmllvgctcmhl5vv8kl3mdksukyqf2tdveh8drpn0sppugwwqjzz30z";
let transferBytes = new Buffer('{"transfer":{"amount":"1","recipient":"juno1cx4nq77x3unvl2xsa9fmm9drxkexzkjnzwt2y7"}}');
const msgExecuteContract = new message.cosmwasm.wasm.v1.MsgExecuteContract({
sender: address,
contract: cw20Contract,
msg: transferBytes,
funds: []
});

const msgExecuteContractAny = new message.google.protobuf.Any({
type_url: "/cosmwasm.wasm.v1.MsgExecuteContract",
value: message.cosmwasm.wasm.v1.MsgExecuteContract.encode(msgExecuteContract).finish()
});

const txBody = new message.cosmos.tx.v1beta1.TxBody({ messages: [msgExecuteContractAny], memo: "" });
});
```

Official LCD url([https://api.cosmos.network](https://api.cosmos.network/node_info)).
- This rest server URL may be disabled at any time. In order to maintain stable blockchain service, it is recommended to prepare your rest server.
- Setting up the rest server: (https://hub.cosmos.network/main/gaia-tutorials/join-mainnet.html#enable-the-rest-api)
Expand Down
63 changes: 41 additions & 22 deletions example/stargate-final.js → example/juno.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import message from "../src/messages/proto";

// [WARNING] This mnemonic is just for the demo purpose. DO NOT USE THIS MNEMONIC for your own wallet.
const mnemonic = "swear buyer security impulse public stereo peasant correct cross tornado bid discover anchor float venture deal patch property cool wreck eight dwarf december surface";
const chainId = "stargate-final";
// This rest server URL may be disabled at any time. In order to maintain stable blockchain service, it is recommended to prepare your rest server.
// (https://hub.cosmos.network/master/resources/service-providers.html#setting-up-the-rest-server)
const lcdUrl = "https://lcd-office.cosmostation.io/stargate-final";

const cosmos = new Cosmos(lcdUrl, chainId);
cosmos.setBech32MainPrefix("cosmos");
const chainId = "juno-1";
// In order to maintain stable blockchain service, it is recommended to create your rest server.
// (https://docs.cosmos.network/master/core/grpc_rest.html#rest-server)
const cosmos = new Cosmos("http://YOUR_REST_SERVER_URL:1317", chainId);
cosmos.setBech32MainPrefix("juno");
cosmos.setPath("m/44'/118'/0'/0/0");
const address = cosmos.getAddress(mnemonic);
const privKey = cosmos.getECPairPriv(mnemonic);
Expand All @@ -18,23 +16,21 @@ const pubKeyAny = cosmos.getPubKeyAny(privKey);
cosmos.getAccounts(address).then(data => {
// signDoc = (1)txBody + (2)authInfo
// ---------------------------------- (1)txBody ----------------------------------
const msgSend = new message.cosmos.bank.v1beta1.MsgSend({
from_address: address,
to_address: "cosmos1jf874x5vr6wkza6ahvamck4sy4w76aq4w9c4s5",
amount: [{ denom: "umuon", amount: String(100000) }] // 6 decimal places (1000000 uatom = 1 ATOM)
let cw20Contract = "juno10rktvmllvgctcmhl5vv8kl3mdksukyqf2tdveh8drpn0sppugwwqjzz30z";
let transferBytes = new Buffer('{"transfer":{"amount":"1","recipient":"juno1cx4nq77x3unvl2xsa9fmm9drxkexzkjnzwt2y7"}}');
const msgExecuteContract = new message.cosmwasm.wasm.v1.MsgExecuteContract({
sender: address,
contract: cw20Contract,
msg: transferBytes,
funds: []
});

const msgSendAny = new message.google.protobuf.Any({
type_url: "/cosmos.bank.v1beta1.MsgSend",
value: message.cosmos.bank.v1beta1.MsgSend.encode(msgSend).finish()
const msgExecuteContractAny = new message.google.protobuf.Any({
type_url: "/cosmwasm.wasm.v1.MsgExecuteContract",
value: message.cosmwasm.wasm.v1.MsgExecuteContract.encode(msgExecuteContract).finish()
});

console.log("msgSendAny: ", msgSendAny);

const txBody = new message.cosmos.tx.v1beta1.TxBody({ messages: [msgSendAny], memo: "" });

console.log("txBody: ", txBody);
return;
const txBody = new message.cosmos.tx.v1beta1.TxBody({ messages: [msgExecuteContractAny], memo: "" });

// --------------------------------- (2)authInfo ---------------------------------
const signerInfo = new message.cosmos.tx.v1beta1.SignerInfo({
Expand All @@ -44,14 +40,37 @@ cosmos.getAccounts(address).then(data => {
});

const feeValue = new message.cosmos.tx.v1beta1.Fee({
amount: [{ denom: "umuon", amount: String(5000) }],
amount: [{ denom: "ujuno", amount: String(5000) }],
gas_limit: 200000
});

const authInfo = new message.cosmos.tx.v1beta1.AuthInfo({ signer_infos: [signerInfo], fee: feeValue });

// -------------------------------- sign --------------------------------
const signedTxBytes = cosmos.sign(txBody, authInfo, data.account.account_number, privKey);

cosmos.broadcast(signedTxBytes).then(response => console.log(response));
});
























7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cosmostation/cosmosjs",
"version": "0.10.9",
"version": "0.11.0",
"description": "A JavasSript Open Source Library for Cosmos(ATOM).",
"main": "src/index.js",
"type": "module",
Expand All @@ -22,10 +22,11 @@
"akash",
"akt",
"stargate",
"cryptoorg"
"cryptoorg",
"juno"
],
"scripts": {
"start": "node --es-module-specifier-resolution=node example/cosmoshub.js",
"start": "node --es-module-specifier-resolution=node example/juno.js",
"dev": "node --es-module-specifier-resolution=node example/stargate-final.js",
"test": "mocha --es-module-specifier-resolution=node"
},
Expand Down
Loading

0 comments on commit 5ad902c

Please sign in to comment.