Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: zenrock integration #8731

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 60 additions & 16 deletions libs/coin-modules/coin-cosmos/src/buildTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,40 @@ import { PubKey } from "@keplr-wallet/proto-types/cosmos/crypto/secp256k1/keys";
import { AuthInfo, Fee } from "@keplr-wallet/proto-types/cosmos/tx/v1beta1/tx";
import type { Account } from "@ledgerhq/types-live";
import { Transaction } from "./types";
import { AminoMsg, Coin } from "@cosmjs/amino";

type ProtoMsg = {
typeUrl: string;
value: Uint8Array;
};

type AminoMsg = {
readonly type: string;
readonly value: any;
};
export interface AminoZenrockMsgDelegate extends AminoMsg {
readonly type: "zrchain/MsgDelegate";
readonly value: {
readonly delegator_address: string;
readonly validator_address: string;
readonly amount: Coin;
};
}
qperrot marked this conversation as resolved.
Show resolved Hide resolved

export interface AminoZenrockMsgBeginRedelegate extends AminoMsg {
readonly type: "zrchain/MsgBeginRedelegate";
readonly value: {
readonly delegator_address: string;
readonly validator_src_address: string;
readonly validator_dst_address: string;
readonly amount: Coin;
};
}

export interface AminoZenrockMsgUndelegate extends AminoMsg {
readonly type: "zrchain/MsgUndelegate";
readonly value: {
readonly delegator_address: string;
readonly validator_address: string;
readonly amount: Coin;
};
}

export const txToMessages = (
account: Account,
Expand Down Expand Up @@ -73,8 +97,9 @@ export const txToMessages = (
if (transaction.validators && transaction.validators.length > 0) {
const validator = transaction.validators[0];
if (validator && validator.address && transaction.amount.gt(0)) {
const aminoMsg: AminoMsgDelegate = {
type: "cosmos-sdk/MsgDelegate",
const aminoMsg: AminoMsgDelegate | AminoZenrockMsgDelegate = {
type:
account.currency.id === "zenrock" ? "zrchain/MsgDelegate" : "cosmos-sdk/MsgDelegate",
value: {
delegator_address: account.freshAddress,
validator_address: validator.address,
Expand All @@ -88,7 +113,10 @@ export const txToMessages = (

// PROTO MESSAGE
protoMsgs.push({
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
typeUrl:
account.currency.id === "zenrock"
? "/zrchain.validation.MsgDelegate"
: "/cosmos.staking.v1beta1.MsgDelegate",
value: MsgDelegate.encode({
delegatorAddress: account.freshAddress,
validatorAddress: validator.address,
Expand All @@ -111,8 +139,11 @@ export const txToMessages = (
transaction.validators[0].amount.gt(0)
) {
const validator = transaction.validators[0];
const aminoMsg: AminoMsgBeginRedelegate = {
type: "cosmos-sdk/MsgBeginRedelegate",
const aminoMsg: AminoMsgBeginRedelegate | AminoZenrockMsgBeginRedelegate = {
type:
account.currency.id === "zenrock"
? "zrchain/MsgBeginRedelegate"
: "cosmos-sdk/MsgBeginRedelegate",
value: {
delegator_address: account.freshAddress,
validator_src_address: transaction.sourceValidator,
Expand All @@ -127,7 +158,10 @@ export const txToMessages = (

// PROTO MESSAGE
protoMsgs.push({
typeUrl: "/cosmos.staking.v1beta1.MsgBeginRedelegate",
typeUrl:
account.currency.id === "zenrock"
? "/zrchain.validation.MsgBeginRedelegate"
: "/cosmos.staking.v1beta1.MsgBeginRedelegate",
value: MsgBeginRedelegate.encode({
delegatorAddress: account.freshAddress,
validatorSrcAddress: transaction.sourceValidator,
Expand All @@ -145,8 +179,11 @@ export const txToMessages = (
if (transaction.validators && transaction.validators.length > 0) {
const validator = transaction.validators[0];
if (validator && validator.address && validator.amount.gt(0)) {
const aminoMsg: AminoMsgUndelegate = {
type: "cosmos-sdk/MsgUndelegate",
const aminoMsg: AminoMsgUndelegate | AminoZenrockMsgUndelegate = {
type:
account.currency.id === "zenrock"
? "zrchain/MsgUndelegate"
: "cosmos-sdk/MsgUndelegate",
value: {
delegator_address: account.freshAddress,
validator_address: validator.address,
Expand All @@ -160,7 +197,10 @@ export const txToMessages = (

// PROTO MESSAGE
protoMsgs.push({
typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate",
typeUrl:
account.currency.id === "zenrock"
? "/zrchain.validation.MsgUndelegate"
: "/cosmos.staking.v1beta1.MsgUndelegate",
value: MsgUndelegate.encode({
delegatorAddress: account.freshAddress,
validatorAddress: validator.address,
Expand Down Expand Up @@ -215,8 +255,9 @@ export const txToMessages = (
validator_address: validator.address,
},
};
const aminoDelegateMsg: AminoMsgDelegate = {
type: "cosmos-sdk/MsgDelegate",
const aminoDelegateMsg: AminoMsgDelegate | AminoZenrockMsgDelegate = {
type:
account.currency.id === "zenrock" ? "zrchain/MsgDelegate" : "cosmos-sdk/MsgDelegate",
value: {
delegator_address: account.freshAddress,
validator_address: validator.address,
Expand All @@ -237,7 +278,10 @@ export const txToMessages = (
}).finish(),
});
protoMsgs.push({
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
typeUrl:
account.currency.id === "zenrock"
? "/zrchain.validation.MsgDelegate"
: "/cosmos.staking.v1beta1.MsgDelegate",
value: MsgDelegate.encode({
delegatorAddress: account.freshAddress,
validatorAddress: validator.address,
Expand Down
Loading