Skip to content

Commit

Permalink
crypto-org-chain#278 - Support amount list for MsgSend
Browse files Browse the repository at this point in the history
  • Loading branch information
cdc-Hitesh committed Jun 16, 2021
1 parent 6eb809f commit 35b4d60
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 32 deletions.
8 changes: 4 additions & 4 deletions lib/e2e/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ describe('e2e test suite', function () {
const msgSend1 = new cro.bank.MsgSend({
fromAddress: address1.account(),
toAddress: randomAddress.account(),
amount: new cro.Coin('100000', Units.BASE),
amount: [new cro.Coin('100000', Units.BASE)],
});

const msgSend2 = new cro.bank.MsgSend({
fromAddress: address2.account(),
toAddress: address1.account(),
amount: new cro.Coin('20000', Units.BASE),
amount: [new cro.Coin('20000', Units.BASE)],
});

const account1 = await client.getAccount(address1.account());
Expand Down Expand Up @@ -146,13 +146,13 @@ describe('e2e test suite', function () {
const msgSend1 = new cro.bank.MsgSend({
fromAddress: address1.account(),
toAddress: randomAddress.account(),
amount: new cro.Coin('100000', Units.BASE),
amount: [new cro.Coin('100000', Units.BASE)],
});

const msgSend2 = new cro.bank.MsgSend({
fromAddress: address2.account(),
toAddress: address1.account(),
amount: new cro.Coin('20000', Units.BASE),
amount: [new cro.Coin('20000', Units.BASE)],
});

const account1 = await client.getAccount(address1.account());
Expand Down
1 change: 0 additions & 1 deletion lib/src/core/cro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const CroSDK = function (configs: InitConfigurations) {
CancelSoftwareUpgradeProposal: cancelSoftwareUpgradeProposal(),
SoftwareUpgradeProposal: softwareUpgradeProposal(),
TextProposal: textProposal(),
// TODO : More type of proposals to be added here
},
},
bank: {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transaction/amino.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Amino JSON sign mode', function () {
const msg = new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair).account(),
toAddress: 'tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50',
amount: cro.Coin.fromBaseUnit('1000'),
amount: [cro.Coin.fromBaseUnit('1000')],
});

const rawTx = new cro.RawTransaction();
Expand Down
14 changes: 7 additions & 7 deletions lib/src/transaction/msg/bank/msgsend.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ describe('Testing MsgSend', function () {
const msgSend = cro.bank.MsgSend.fromCosmosMsgJSON(json, CroNetwork.Testnet);
expect(msgSend.fromAddress).to.eql('tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg');
expect(msgSend.toAddress).to.eql('tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3');
expect(msgSend.amount.toCosmosCoin().amount).to.eql('3478499933290496');
expect(msgSend.amount.toCosmosCoin().denom).to.eql('basetcro');
expect(msgSend.amount[0].toCosmosCoin().amount).to.eql('3478499933290496');
expect(msgSend.amount[0].toCosmosCoin().denom).to.eql('basetcro');
});
});

Expand All @@ -107,7 +107,7 @@ describe('Testing MsgSend', function () {
const msgSend = new cro.bank.MsgSend({
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: coin,
amount: [coin],
});

const rawMsg: Msg = {
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('Testing MsgSend', function () {
const msgSend = new cro.bank.MsgSend({
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: coin,
amount: [coin],
});

const anySigner = {
Expand All @@ -163,19 +163,19 @@ describe('Testing MsgSend', function () {
const params1 = {
fromAddress: 'cro1pndm4ywdf4qtmupa0fqe75krmqed2znjyj6x8f',
toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: coin,
amount: [coin],
};

const params2 = {
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
toAddress: 'cro1pndm4ywdf4qtmupa0fqe75krmqed2znjyj6x8f',
amount: coin,
amount: [coin],
};

const params3 = {
fromAddress: 'tcro1pndm4ywdf4qtmupa0fqe75krmqed2znjyj6x8fzqa',
toAddress: 'cro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3',
amount: coin,
amount: [coin],
};

expect(() => new cro.bank.MsgSend(params1)).to.throw('Provided `fromAddress` does not match network selected');
Expand Down
14 changes: 6 additions & 8 deletions lib/src/transaction/msg/bank/msgsend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const msgSend = function (config: InitConfigurations) {

public readonly toAddress: string;

public amount: ICoin;
public amount: ICoin[];

/**
* Constructor to create a new MsgSend
Expand Down Expand Up @@ -58,15 +58,14 @@ export const msgSend = function (config: InitConfigurations) {
if (parsedMsg['@type'] !== COSMOS_MSG_TYPEURL.MsgSend) {
throw new Error(`Expected ${COSMOS_MSG_TYPEURL.MsgSend} but got ${parsedMsg['@type']}`);
}
if (!parsedMsg.amount || parsedMsg.amount.length !== 1) {
if (!parsedMsg.amount || parsedMsg.amount.length < 1) {
throw new Error('Invalid amount in the Msg.');
}

return new MsgSend({
fromAddress: parsedMsg.from_address,
toAddress: parsedMsg.to_address,
// TODO: Handle the complete list
amount: cro.Coin.fromCustomAmountDenom(parsedMsg.amount[0].amount, parsedMsg.amount[0].denom),
amount: parsedMsg.amount.map(coin => cro.Coin.fromCustomAmountDenom(coin.amount, coin.denom)),
});
}

Expand All @@ -80,7 +79,7 @@ export const msgSend = function (config: InitConfigurations) {
value: {
fromAddress: this.fromAddress,
toAddress: this.toAddress,
amount: this.amount.toCosmosCoins(),
amount: this.amount.map(coin => coin.toCosmosCoin()),
},
};
}
Expand All @@ -92,7 +91,7 @@ export const msgSend = function (config: InitConfigurations) {
value: {
from_address: this.fromAddress,
to_address: this.toAddress,
amount: this.amount.toCosmosCoins(),
amount: this.amount.map(coin => coin.toCosmosCoin()),
},
} as legacyAmino.MsgSend;
}
Expand Down Expand Up @@ -124,6 +123,5 @@ export const msgSend = function (config: InitConfigurations) {
export type MsgSendOptions = {
fromAddress: string;
toAddress: string;
// Todo: It should be ICoin[]
amount: ICoin;
amount: ICoin[];
};
2 changes: 1 addition & 1 deletion lib/src/transaction/msg/ow.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const owVoteOption = () => ow.number.validate(voteOptionValidator);
export const owMsgSendOptions = owStrictObject().exactShape({
fromAddress: ow.string,
toAddress: ow.string,
amount: owCoin(),
amount: ow.array.ofType(owCoin()),
});

const proposalContentValidatorFn = (val: object) => ({
Expand Down
1 change: 0 additions & 1 deletion lib/src/transaction/signable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ export class SignableTransaction {
const { memo } = body;
const timeoutHeight = body.timeout_height;

// TODO: If extension_options and non_critical_extension_options length > 0, then throw
if (
(body.non_critical_extension_options && body.non_critical_extension_options.length > 0) ||
(body.extension_options && body.extension_options.length > 0)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/transaction/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const CosmosMsgSuiteFactory = new Factory<MessageSuite>()
new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair.getPubKey()).account(),
toAddress,
amount: cro.Coin.fromBaseUnit(chance.integer({ min: 0 }).toString()),
amount: [cro.Coin.fromBaseUnit(chance.integer({ min: 0 }).toString())],
}),
);

Expand Down
10 changes: 5 additions & 5 deletions lib/src/transaction/tx-custom-props.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('Testing Tx signing with custom parameters', function () {
const msg = new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair).account(),
toAddress: 'tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50',
amount: cro.Coin.fromBaseUnit('1000'),
amount: [cro.Coin.fromBaseUnit('1000')],
});

const rawTx = new cro.RawTransaction();
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('Testing Tx signing with custom parameters', function () {
const msg = new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair).account(),
toAddress: 'tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50',
amount: cro.Coin.fromBaseUnit('1000'),
amount: [cro.Coin.fromBaseUnit('1000')],
});

const rawTx = new cro.RawTransaction();
Expand Down Expand Up @@ -150,7 +150,7 @@ describe('Testing Tx signing with custom parameters', function () {
const msg = new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair).account(),
toAddress: 'tcro1fzcrza3j4f2677jfuxulkg33z6852qsqs8hx50',
amount: cro.Coin.fromBaseUnit('1000'),
amount: [cro.Coin.fromBaseUnit('1000')],
});

const rawTx = new cro.RawTransaction();
Expand Down Expand Up @@ -204,7 +204,7 @@ describe('Testing Tx signing with custom parameters', function () {
const msgSend1 = new cro.bank.MsgSend({
fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
toAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3',
amount: new cro.Coin('1210', Units.BASE),
amount: [new cro.Coin('1210', Units.BASE)],
});

const signableTx = rawTx
Expand Down Expand Up @@ -234,7 +234,7 @@ describe('Testing Tx signing with custom parameters', function () {
const msg = new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair).account(),
toAddress: 'tcro18tk89ddr4lg32e58sp5kfrm0egldlcfu40ww80',
amount: cro.Coin.fromBaseUnit('990227306075'),
amount: [cro.Coin.fromBaseUnit('990227306075')],
});

const rawTx = new cro.RawTransaction();
Expand Down
6 changes: 3 additions & 3 deletions lib/src/transaction/tx-edgecase.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Testing edge case Txs with 0 account numbers or 0 sequence', function
const msgSend1 = new cro.bank.MsgSend({
fromAddress: 'tcro15rsn69ze9r7g52tk0u6cyhu4edep88dxgtzm65',
toAddress: 'tcro1l4gxejy8qxl3vxcxv7vpk4cqu8qhrz2nfxxr2p',
amount: new cro.Coin('1210', Units.BASE),
amount: [new cro.Coin('1210', Units.BASE)],
});

const signableTx = rawTx
Expand Down Expand Up @@ -92,7 +92,7 @@ describe('Testing edge case Txs with 0 account numbers or 0 sequence', function
const msgSend1 = new cro.bank.MsgSend({
fromAddress: 'tcro1l4gxejy8qxl3vxcxv7vpk4cqu8qhrz2nfxxr2p',
toAddress: 'tcro12z3awt3kkh0u58hmw85lhtdg67d44pwu62x8sa',
amount: new cro.Coin('2210', Units.BASE),
amount: [new cro.Coin('2210', Units.BASE)],
});

const signableTx = rawTx
Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Testing edge case Txs with 0 account numbers or 0 sequence', function
const msg = new cro.bank.MsgSend({
fromAddress: new cro.Address(keyPair).account(),
toAddress: 'tcro1ca066afeuj52k3r29je25q0auyr32k4plkh33r',
amount: cro.Coin.fromBaseUnit('1000'),
amount: [cro.Coin.fromBaseUnit('1000')],
});

const rawTx = new cro.RawTransaction();
Expand Down

0 comments on commit 35b4d60

Please sign in to comment.