Skip to content

Instantly share code, notes, and snippets.

@AustinWoetzel
Last active February 16, 2022 08:19
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save AustinWoetzel/76cd331678c56e90643798379d965215 to your computer and use it in GitHub Desktop.
Save AustinWoetzel/76cd331678c56e90643798379d965215 to your computer and use it in GitHub Desktop.
SecretJS + Keplr Staking Function
//Built using VueJS
const {
SigningCosmWasmClient,
// Secp256k1Pen,
// makeSignBytes,
} = require("secretjs");
if (!window.getOfflineSigner || !window.keplr) {
alert(
"Keplr Wallet not detected, please install extension. Learn more at https://keplr.xyz/"
);
} else {
await window.keplr.enable(process.env.VUE_APP_SECRET_CHAIN_ID);
this.keplrOfflineSigner = await window.getOfflineSigner(
process.env.VUE_APP_SECRET_CHAIN_ID
);
const accounts = await this.keplrOfflineSigner.getAccounts();
// import address
this.walletAddress = accounts[0].address;
this.cosmJS = new SigningCosmWasmClient(
process.env.VUE_APP_SECRET_REST_URL,
this.walletAddress,
this.keplrOfflineSigner,
window.getEnigmaUtils(process.env.VUE_APP_SECRET_CHAIN_ID),
{
init: {
amount: [{ amount: "300000", denom: "uscrt" }],
gas: "300000",
},
exec: {
amount: [{ amount: "300000", denom: "uscrt" }],
gas: "300000",
},
}
);
this.account = await this.cosmJS.getAccount();
this.balance = this.getScrt(this.account);
}
stake(amount) {
amount = parseFloat(amount);
amount *= 1000000;
amount = Math.floor(amount);
// send delegation tx
(async () => {
// define memo (not required)
const memo = "INSERT MEMO HERE";
// define msg
const sendMsg = {
type: "cosmos-sdk/MsgDelegate",
value: {
delegator_address: this.walletAddress,
validator_address: this.validatorAddress,
amount: {
denom: "uscrt",
amount: amount.toString(),
},
},
};
// // define fees, may be adjusted later via keplr popup
const fee = {
amount: [
{
amount: "50000",
denom: "uscrt",
},
],
gas: "270000",
};
// sign transaction
const { accountNumber, sequence } = await this.cosmJS
.getNonce(this.walletAddress)
.catch((err) => {
throw new Error(`Could not get nonce: ${err}`);
});
const signedTx = await this.cosmJS
.signAdapter(
[sendMsg],
fee,
process.env.VUE_APP_SECRET_CHAIN_ID,
memo,
accountNumber,
sequence
)
.catch((err) => {
throw new Error(`Could not sign Tx: ${err}`);
});
// broadcast TX
const result = await this.cosmJS.postTx(signedTx).catch((err) => {
throw new Error(`Could not post tx: ${err}`);
});
if (result.code !== undefined && result.code !== 0) {
alert("Failed to send tx: " + result.log || result.rawLog);
} else {
this.transactionHashLink =
"https://secretnodes.com/secret/chains/secret-2/transactions/" +
result.transactionHash;
//refresh display for account balance
this.account = await this.cosmJS.getAccount();
this.balance = this.getScrt(this.account);
}
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment