Skip to content

Commit

Permalink
Merge pull request #377 from crypto-matto/add-governance-amino-support
Browse files Browse the repository at this point in the history
Problem: Add amino support for MsgDeposit & MsgSubmitProposal
  • Loading branch information
cdc-Hitesh authored Jul 21, 2022
2 parents e9cdc5c + 8eaa391 commit e9beb8c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
22 changes: 22 additions & 0 deletions lib/src/transaction/msg/v2/gov/v2.MsgDeposit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CroSDK } from '../../../../core/cro';
import { Msg } from '../../../../cosmos/v1beta1/types/msg';
import { Secp256k1KeyPair } from '../../../../keypair/secp256k1';
import { HDKey } from '../../../../hdkey/hdkey';
import * as legacyAmino from '../../../../cosmos/amino';

const cro = CroSDK({
network: {
Expand Down Expand Up @@ -106,6 +107,27 @@ describe('Testing MsgDeposit', function () {
'0a730a710a1e2f636f736d6f732e676f762e763162657461312e4d73674465706f736974124f08e0f64b122b7463726f3138346c7461326c7379753437767779703265387a6d746361336b3579713835703663347670331a1c0a08626173657463726f12103132303030353030303030303030303012580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a21030bf28c5f92c336db4703791691fa650fee408690b0a22c5ee4afb7e2508d32a712040a0208011800120410c09a0c1a40ba8c80028a85015ac737ca56603bef0a82e0fbd83f701ccbba02a4f381e5ee4a3d83af13cd02f1e9c1e8b386995d8468c2db1db73952c30fac6114004fe269c0',
);
});
describe('Testing MsgDeposit Json', function () {
it('Test MsgDeposit conversion for amino json', function () {
const coin = new cro.Coin('12000500', Units.CRO);

const msgDeposit = new cro.v2.gov.MsgDepositV2({
proposalId: Big(1244000),
depositor: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: [coin],
});

const rawMsg: legacyAmino.Msg = {
type: 'cosmos-sdk/MsgDeposit',
value: {
proposal_id: Long.fromNumber(1244000, true),
depositor: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: [coin.toCosmosCoin()],
},
};
expect(msgDeposit.toRawAminoMsg()).to.eqls(rawMsg);
});
});
describe('fromCosmosJSON', function () {
it('should throw Error if the JSON is not a MsgDeposit', function () {
const json =
Expand Down
10 changes: 9 additions & 1 deletion lib/src/transaction/msg/v2/gov/v2.MsgDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ export const msgDepositV2 = function (config: InitConfigurations) {

// eslint-disable-next-line class-methods-use-this
toRawAminoMsg(): legacyAmino.Msg {
throw new Error('Method not implemented.');
const proposal = Long.fromNumber(this.proposalId.toNumber(), true);
return {
type: 'cosmos-sdk/MsgDeposit',
value: {
proposal_id: proposal,
depositor: this.depositor,
amount: this.amount.map((coin) => coin.toCosmosCoin()),
},
};
}

/**
Expand Down
34 changes: 33 additions & 1 deletion lib/src/transaction/msg/v2/gov/v2.MsgSubmitProposal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CroSDK } from '../../../../core/cro';
import { HDKey } from '../../../../hdkey/hdkey';
import { Secp256k1KeyPair } from '../../../../keypair/secp256k1';
import { Network } from '../../../../network/network';
import * as legacyAmino from '../../../../cosmos/amino';

const PystaportTestNet: Network = {
defaultNodeUrl: '',
Expand Down Expand Up @@ -184,7 +185,38 @@ describe('Testing MsgSubmitProposalV2 and its content types', function () {
'0abc020ab9020a252f636f736d6f732e676f762e763162657461312e4d73675375626d697450726f706f73616c128f020ac6010a2e2f636f736d6f732e706172616d732e763162657461312e506172616d657465724368616e676550726f706f73616c1293010a2a4368616e6765206120706172616d20746f20736f6d657468696e67206d6f7265206f7074696d697a656412474c6f72656d20497073756d202e2e2e2054686520706172616d2073686f756c64206265206368616e67656420746f20736f6d657468696e67206d6f7265206f7074696d697a65641a1c0a077374616b696e67120d4d617856616c696461746f72731a02313212170a08626173657463726f120b31323030303030303030301a2b7463726f31347368343930776b3739646c7465613475646b39356b376d773430776d7666373770306c356112580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a210280c5e37a2bc3e68cc7c4aac78eac8c769cf58ce269ecd4307427aa16c2ba05a412040a0208011800120410c09a0c1a4072bd47137d440036995ea6b5c4754b4f15609df2fdd17496d6c39f47d6663d0e51d171bcae92fc6078496cf657e2a705cd59b0d882cf0356463e57b26e285941',
);
});

describe('Testing MsgSubmitProposal Json', function () {
it('Test MsgSubmitProposal conversion for amino json', function () {
const coin = new cro.Coin('120', Units.CRO);

const communityPoolSpentContent = new cro.gov.proposal.ParamChangeProposal({
title: 'Change a param to something more optimized',
description: 'Lorem Ipsum ... The param should be changed to something more optimized',
paramChanges: [
{
subspace: 'staking',
key: 'MaxValidators',
value: '12',
},
],
});
const MsgSubmitProposalChangeParam = new cro.v2.gov.MsgSubmitProposalV2({
proposer: 'tcro14sh490wk79dltea4udk95k7mw40wmvf77p0l5a',
initialDeposit: [coin],
content: communityPoolSpentContent,
});

const rawMsg: legacyAmino.Msg = {
type: 'cosmos-sdk/MsgSubmitProposal',
value: {
proposer: 'tcro14sh490wk79dltea4udk95k7mw40wmvf77p0l5a',
content: communityPoolSpentContent.getEncoded(),
initial_deposit: [coin.toCosmosCoin()],
},
};
expect(MsgSubmitProposalChangeParam.toRawAminoMsg()).to.eqls(rawMsg);
});
});
describe('fromCosmosJSON', function () {
it('should throw Error if the JSON is not a MsgSubmitProposal', function () {
const json =
Expand Down
9 changes: 8 additions & 1 deletion lib/src/transaction/msg/v2/gov/v2.MsgSubmitProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ export const msgSubmitProposalV2 = function (config: InitConfigurations) {

// eslint-disable-next-line class-methods-use-this
toRawAminoMsg(): legacyAmino.Msg {
throw new Error('Method not implemented.');
return {
type: 'cosmos-sdk/MsgSubmitProposal',
value: {
proposer: this.proposer,
content: this.content.getEncoded(),
initial_deposit: this.initialDeposit.map((coin) => coin.toCosmosCoin()),
},
};
}

/**
Expand Down

0 comments on commit e9beb8c

Please sign in to comment.