|
| 1 | +import 'mocha'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import Big from 'big.js'; |
| 4 | + |
| 5 | +import Long from 'long'; |
| 6 | +import { fuzzyDescribe } from '../../../test/mocha-fuzzy/suite'; |
| 7 | +import { Msg } from '../../../cosmos/v1beta1/types/msg'; |
| 8 | +import { Secp256k1KeyPair } from '../../../keypair/secp256k1'; |
| 9 | +import { Bytes } from '../../../utils/bytes/bytes'; |
| 10 | +import { Units } from '../../../coin/coin'; |
| 11 | +import { CroSDK } from '../../../core/cro'; |
| 12 | + |
| 13 | +const cro = CroSDK({ |
| 14 | + network: { |
| 15 | + defaultNodeUrl: '', |
| 16 | + chainId: 'testnet-croeseid-1', |
| 17 | + addressPrefix: 'tcro', |
| 18 | + validatorAddressPrefix: 'tcrocncl', |
| 19 | + validatorPubKeyPrefix: 'tcrocnclconspub', |
| 20 | + coin: { |
| 21 | + baseDenom: 'basetcro', |
| 22 | + croDenom: 'tcro', |
| 23 | + }, |
| 24 | + bip44Path: { |
| 25 | + coinType: 1, |
| 26 | + account: 0, |
| 27 | + }, |
| 28 | + rpcUrl: '', |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +describe('Testing DelayedVestingAccount', function () { |
| 33 | + describe('fromCosmosJSON', function () { |
| 34 | + it('should throw Error if the JSON is not a DelayedVestingAccount', function () { |
| 35 | + const json = |
| 36 | + '{ "@type": "/cosmos.bank.v1beta1.MsgCreateValidator", "amount": { "denom": "basetcro", "amount": "3478499933290496" }, "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }'; |
| 37 | + expect(() => cro.accounts.DelayedVestingAccount.fromCosmosMsgJSON(json)).to.throw( |
| 38 | + 'Expected /cosmos.vesting.v1beta1.DelayedVestingAccount but got /cosmos.bank.v1beta1.MsgCreateValidator', |
| 39 | + ); |
| 40 | + }); |
| 41 | + it('should return the DelayedVestingAccount corresponding to the JSON', function () { |
| 42 | + const json = `{"@type":"/cosmos.vesting.v1beta1.DelayedVestingAccount","base_vesting_account":{"base_account":{"address":"cro18n9xzc576np866k2ze3x5fwp8wvelf52avtwdf","pub_key":{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Argwi2l2NcgtYU2NSPknoeciqOBDWZgv96O7Q3iOK1ei"},"account_number":"67","sequence":"8"},"original_vesting":[{"denom":"basecro","amount":"606902735563"}],"delegated_free":[{"denom":"basecro","amount":"2647264437"}],"delegated_vesting":[{"denom":"basecro","amount":"606902735563"}],"end_time":"1648170000"}}`; |
| 43 | + const DelayedVestingAccount = cro.accounts.DelayedVestingAccount.fromCosmosMsgJSON(json); |
| 44 | + |
| 45 | + expect(DelayedVestingAccount.endTime.toString()).to.eql('1648170000'); |
| 46 | + |
| 47 | + expect(DelayedVestingAccount.delegatedVesting[0].toCosmosCoin().amount).to.eql('606902735563'); |
| 48 | + expect(DelayedVestingAccount.delegatedVesting[0].toCosmosCoin().denom).to.eql('basecro'); |
| 49 | + |
| 50 | + expect(DelayedVestingAccount.delegatedFree[0].toCosmosCoin().amount).to.eql('2647264437'); |
| 51 | + expect(DelayedVestingAccount.delegatedFree[0].toCosmosCoin().denom).to.eql('basecro'); |
| 52 | + |
| 53 | + expect(DelayedVestingAccount.originalVesting[0].toCosmosCoin().amount).to.eql('606902735563'); |
| 54 | + expect(DelayedVestingAccount.originalVesting[0].toCosmosCoin().denom).to.eql('basecro'); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + fuzzyDescribe('should throw Error when options is invalid', function (fuzzy) { |
| 59 | + const anyValidOptions = { |
| 60 | + fromAddress: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', |
| 61 | + toAddress: 'tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3', |
| 62 | + amount: new cro.Coin('1000', Units.BASE), |
| 63 | + }; |
| 64 | + const testRunner = fuzzy(fuzzy.ObjArg(anyValidOptions)); |
| 65 | + |
| 66 | + testRunner(function (options) { |
| 67 | + if (options.valid) { |
| 68 | + return; |
| 69 | + } |
| 70 | + expect(() => new cro.accounts.DelayedVestingAccount(options.value)).to.throw( |
| 71 | + 'Expected `options` to be of type `object`', |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('Test DelayedVestingAccount conversion', function () { |
| 77 | + const DelayedVestingAccount = new cro.accounts.DelayedVestingAccount({ |
| 78 | + baseAccount: { |
| 79 | + address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', |
| 80 | + pubKey: null, |
| 81 | + accountNumber: Long.fromString('10'), |
| 82 | + sequence: Long.fromString('10'), |
| 83 | + }, |
| 84 | + originalVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')], |
| 85 | + delegatedFree: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')], |
| 86 | + delegatedVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')], |
| 87 | + endTime: Long.fromString('1648170000'), |
| 88 | + }); |
| 89 | + |
| 90 | + const rawMsg: Msg = { |
| 91 | + typeUrl: '/cosmos.vesting.v1beta1.DelayedVestingAccount', |
| 92 | + value: { |
| 93 | + baseVestingAccount: { |
| 94 | + baseAccount: { |
| 95 | + accountNumber: Long.fromString('10'), |
| 96 | + address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', |
| 97 | + sequence: Long.fromString('10'), |
| 98 | + }, |
| 99 | + delegatedFree: [ |
| 100 | + { |
| 101 | + amount: '111111', |
| 102 | + denom: 'basetcro', |
| 103 | + }, |
| 104 | + ], |
| 105 | + delegatedVesting: [ |
| 106 | + { |
| 107 | + amount: '111111', |
| 108 | + denom: 'basetcro', |
| 109 | + }, |
| 110 | + ], |
| 111 | + endTime: Long.fromString('1648170000'), |
| 112 | + originalVesting: [ |
| 113 | + { |
| 114 | + amount: '111111', |
| 115 | + denom: 'basetcro', |
| 116 | + }, |
| 117 | + ], |
| 118 | + }, |
| 119 | + }, |
| 120 | + }; |
| 121 | + |
| 122 | + expect(DelayedVestingAccount.toRawMsg()).to.eqls(rawMsg); |
| 123 | + }); |
| 124 | + |
| 125 | + it('Test appendTxBody DelayedVestingAccount Tx signing', function () { |
| 126 | + const anyKeyPair = Secp256k1KeyPair.fromPrivKey( |
| 127 | + Bytes.fromHexString('66633d18513bec30dd11a209f1ceb1787aa9e2069d5d47e590174dc9665102b3'), |
| 128 | + ); |
| 129 | + |
| 130 | + const DelayedVestingAccount = new cro.accounts.DelayedVestingAccount({ |
| 131 | + baseAccount: { |
| 132 | + address: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', |
| 133 | + pubKey: null, |
| 134 | + accountNumber: Long.fromString('10'), |
| 135 | + sequence: Long.fromString('10'), |
| 136 | + }, |
| 137 | + originalVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')], |
| 138 | + delegatedFree: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')], |
| 139 | + delegatedVesting: [cro.v2.CoinV2.fromCustomAmountDenom('111111', 'basetcro')], |
| 140 | + endTime: Long.fromString('1648170000'), |
| 141 | + }); |
| 142 | + |
| 143 | + const anySigner = { |
| 144 | + publicKey: anyKeyPair.getPubKey(), |
| 145 | + accountNumber: new Big(0), |
| 146 | + accountSequence: new Big(2), |
| 147 | + }; |
| 148 | + |
| 149 | + const rawTx = new cro.RawTransaction(); |
| 150 | + |
| 151 | + const signableTx = rawTx.appendMessage(DelayedVestingAccount).addSigner(anySigner).toSignable(); |
| 152 | + |
| 153 | + const signedTx = signableTx.setSignature(0, anyKeyPair.sign(signableTx.toSignDocumentHash(0))).toSigned(); |
| 154 | + |
| 155 | + const signedTxHex = signedTx.encode().toHexString(); |
| 156 | + expect(signedTxHex).to.be.eql( |
| 157 | + '0aab010aa8010a2d2f636f736d6f732e76657374696e672e763162657461312e44656c6179656456657374696e674163636f756e7412770a750a310a2b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b6333180a200a12120a08626173657463726f12063131313131311a120a08626173657463726f120631313131313122120a08626173657463726f12063131313131312890a8f4910612580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103fd0d560b6c4aa1ca16721d039a192867c3457e19dad553edb98e7ba88b159c2712040a0208011802120410c09a0c1a409e4105e3d17d411f722f69afff864e6d22c2d4cc5a66b4b2ec77b6fb512401f67ba8615e61f16b6db243e33e376ce7252190d5ee202346f06b189523fb6c05d6', |
| 158 | + ); |
| 159 | + }); |
| 160 | +}); |
0 commit comments