-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
734a896
commit a837279
Showing
1 changed file
with
133 additions
and
104 deletions.
There are no files selected for viewing
237 changes: 133 additions & 104 deletions
237
lib/src/transaction/msg/distribution/MsgFundCommunityPool.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,139 @@ | ||
import ow from 'ow'; | ||
import { CosmosMsg } from '../cosmosMsg'; | ||
import { Msg } from '../../../cosmos/v1beta1/types/msg'; | ||
import { InitConfigurations, CroSDK } from '../../../core/cro'; | ||
import { AddressType, validateAddress } from '../../../utils/address'; | ||
import { owMsgFundCommunityPoolOptions } from '../ow.types'; | ||
import { COSMOS_MSG_TYPEURL } from '../../common/constants/typeurl'; | ||
/* eslint-disable */ | ||
import { expect } from 'chai'; | ||
import Big from 'big.js'; | ||
import { fuzzyDescribe } from '../../../test/mocha-fuzzy/suite'; | ||
import { CroSDK, CroNetwork } from '../../../core/cro'; | ||
import { Secp256k1KeyPair } from '../../../keypair/secp256k1'; | ||
import { Bytes } from '../../../utils/bytes/bytes'; | ||
import * as legacyAmino from '../../../cosmos/amino'; | ||
import { ICoin } from '../../../coin/coin'; | ||
import { Network } from '../../../network/network'; | ||
|
||
export const msgFundCommunityPool = function (config: InitConfigurations) { | ||
return class MsgFundCommunityPool implements CosmosMsg { | ||
// Normal user addresses with (t)cro prefix | ||
public readonly depositor: string; | ||
|
||
public amount: ICoin; | ||
|
||
/** | ||
* Constructor to create a new MsgFundCommunityPool | ||
* @param {MsgFundCommunityPoolOptions} options | ||
* @returns {MsgFundCommunityPool} | ||
* @throws {Error} when options is invalid | ||
*/ | ||
constructor(options: MsgFundCommunityPoolOptions) { | ||
ow(options, 'communityPoolOptions', owMsgFundCommunityPoolOptions); | ||
|
||
this.depositor = options.depositor; | ||
this.amount = options.amount; | ||
|
||
this.validateAddresses(); | ||
} | ||
|
||
// eslint-disable-next-line class-methods-use-this | ||
toRawAminoMsg(): legacyAmino.Msg { | ||
return { | ||
import { Units } from '../../../coin/coin'; | ||
|
||
const cro = CroSDK({ | ||
network: { | ||
defaultNodeUrl: '', | ||
chainId: 'testnet-croeseid-1', | ||
addressPrefix: 'tcro', | ||
validatorAddressPrefix: 'tcrocncl', | ||
validatorPubKeyPrefix: 'tcrocnclconspub', | ||
coin: { | ||
baseDenom: 'basetcro', | ||
croDenom: 'tcro', | ||
}, | ||
bip44Path: { | ||
coinType: 1, | ||
account: 0, | ||
}, | ||
rpcUrl: '', | ||
}, | ||
}); | ||
let amount = new cro.Coin('1000', Units.BASE) | ||
|
||
describe('Testing MsgFundCommunityPool', function () { | ||
fuzzyDescribe('should throw Error when options is invalid', function (fuzzy) { | ||
const anyValidOptions = { | ||
depositor: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', | ||
amount, | ||
}; | ||
const testRunner = fuzzy(fuzzy.ObjArg(anyValidOptions)); | ||
|
||
testRunner(function (options) { | ||
if (options.valid) { | ||
return; | ||
} | ||
expect(() => new cro.distribution.MsgFundCommunityPool(options.value)).to.throw( | ||
'Expected `communityPoolOptions` to be of type `object`', | ||
); | ||
}); | ||
}); | ||
|
||
it('Test appending MsgFundCommunityPool and signing it', function () { | ||
const anyKeyPair = Secp256k1KeyPair.fromPrivKey( | ||
Bytes.fromHexString('66633d18513bec30dd11a209f1ceb1787aa9e2069d5d47e590174dc9665102b3'), | ||
); | ||
|
||
const MsgFundCommunityPool = new cro.distribution.MsgFundCommunityPool({ | ||
depositor: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', | ||
amount, | ||
}); | ||
|
||
const anySigner = { | ||
publicKey: anyKeyPair.getPubKey(), | ||
accountNumber: new Big(0), | ||
accountSequence: new Big(10), | ||
}; | ||
|
||
const rawTx = new cro.RawTransaction(); | ||
|
||
const signableTx = rawTx.appendMessage(MsgFundCommunityPool).addSigner(anySigner).toSignable(); | ||
|
||
const signedTx = signableTx.setSignature(0, anyKeyPair.sign(signableTx.toSignDocumentHash(0))).toSigned(); | ||
|
||
const signedTxHex = signedTx.encode().toHexString(); | ||
expect(signedTxHex).to.be.eql( | ||
'0a760a740a312f636f736d6f732e646973747269627574696f6e2e763162657461312e4d736746756e64436f6d6d756e697479506f6f6c123f0a100a08626173657463726f120431303030122b7463726f313635747a63726832796c3833673871657178756567326735677a6775353779336665336b633312580a500a460a1f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657912230a2103fd0d560b6c4aa1ca16721d039a192867c3457e19dad553edb98e7ba88b159c2712040a020801180a120410c09a0c1a400c93f8e74991dde45638e3d4366f7607fd9711272d0602f1e2e797d9180d25c30031955522e7ddc380d7ac4435e7f6d01fbea5db685655ba0b04d43b4135bf3d', | ||
); | ||
}); | ||
|
||
describe('Testing MsgFundCommunityPool json', function () { | ||
it('Test MsgFundCommunityPool conversion for amino json', function () { | ||
const MsgWithdrawDelegatatorReward = new cro.distribution.MsgFundCommunityPool({ | ||
depositor: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', | ||
amount, | ||
}); | ||
|
||
const rawMsg: legacyAmino.Msg = { | ||
type: 'cosmos-sdk/MsgFundCommunityPool', | ||
value: { | ||
depositor: this.depositor, | ||
amount: this.amount.toCosmosCoins(), | ||
}, | ||
} as legacyAmino.MsgFundCommunityPool; | ||
} | ||
|
||
/** | ||
* Returns the raw Msg representation of MsgFundCommunityPool | ||
* @returns {Msg} | ||
*/ | ||
toRawMsg(): Msg { | ||
return { | ||
typeUrl: COSMOS_MSG_TYPEURL.distribution.MsgFundCommunityPool, | ||
value: { | ||
depositor: this.depositor, | ||
amount: this.amount.toCosmosCoins(), | ||
depositor: 'tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3', | ||
amount: amount.toCosmosCoins(), | ||
}, | ||
}; | ||
} | ||
|
||
/** | ||
* Returns an instance of MsgFundCommunityPool | ||
* @param {string} msgJsonStr | ||
* @param {Network} network | ||
* @returns {MsgFundCommunityPool} | ||
*/ | ||
public static fromCosmosMsgJSON(msgJsonStr: string, network: Network): MsgFundCommunityPool { | ||
const parsedMsg = JSON.parse(msgJsonStr) as MsgFundCommunityPoolRaw; | ||
const cro = CroSDK({ network }); | ||
if (parsedMsg['@type'] !== COSMOS_MSG_TYPEURL.distribution.MsgFundCommunityPool) { | ||
throw new Error( | ||
`Expected ${COSMOS_MSG_TYPEURL.distribution.MsgFundCommunityPool} but got ${parsedMsg['@type']}`, | ||
); | ||
} | ||
if (!parsedMsg.amount || parsedMsg.amount.length !== 1) { | ||
throw new Error('Invalid amount in the Msg.'); | ||
} | ||
|
||
return new MsgFundCommunityPool({ | ||
depositor: parsedMsg.depositor, | ||
amount: cro.Coin.fromCustomAmountDenom(parsedMsg.amount[0].amount, parsedMsg.amount[0].denom), | ||
}); | ||
} | ||
|
||
validateAddresses() { | ||
if ( | ||
!validateAddress({ | ||
address: this.depositor, | ||
network: config.network, | ||
type: AddressType.USER, | ||
}) | ||
) { | ||
throw new TypeError('Provided `depositor` address doesnt match network selected'); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
export type MsgFundCommunityPoolOptions = { | ||
depositor: string; | ||
amount: ICoin; | ||
}; | ||
interface MsgFundCommunityPoolRaw { | ||
'@type': string; | ||
amount: Amount[]; | ||
depositor: string; | ||
} | ||
|
||
interface Amount { | ||
denom: string; | ||
amount: string; | ||
} | ||
expect(MsgWithdrawDelegatatorReward.toRawAminoMsg()).to.eqls(rawMsg); | ||
}); | ||
}); | ||
|
||
describe('Testing throw scenarios', function () { | ||
it('Should throw on invalid depositor', function () { | ||
expect(() => { | ||
new cro.distribution.MsgFundCommunityPool({ | ||
depositor: 'cro1xh3dqgljnydpwelzqf265edryrqrq7wzacx2nr', | ||
amount, | ||
}); | ||
}).to.throw('Provided `depositor` address doesnt match network selected'); | ||
}); | ||
}); | ||
describe('fromCosmosJSON', function () { | ||
it('should throw Error if the JSON is not a MsgFundCommunityPool', function () { | ||
const json = | ||
'{ "@type": "/cosmos.bank.v1beta1.MsgCreateValidator", "amount": [{ "denom": "basetcro", "amount": "3478499933290496" }], "from_address": "tcro1x07kkkepfj2hl8etlcuqhej7jj6myqrp48y4hg", "to_address": "tcro184lta2lsyu47vwyp2e8zmtca3k5yq85p6c4vp3" }'; | ||
expect(() => cro.distribution.MsgFundCommunityPool.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw( | ||
'Expected /cosmos.distribution.v1beta1.MsgFundCommunityPool but got /cosmos.bank.v1beta1.MsgCreateValidator', | ||
); | ||
}); | ||
|
||
it('should throw Error when the `depositor` field is missing', function () { | ||
const json = | ||
'{"@type":"/cosmos.distribution.v1beta1.MsgFundCommunityPool","amount":[{ "denom": "basetcro", "amount": "3478499933290496" }]}'; | ||
expect(() => cro.distribution.MsgFundCommunityPool.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw( | ||
'Expected property `depositor` to be of type `string` but received type `undefined` in object `communityPoolOptions`', | ||
); | ||
}); | ||
it('should throw Error when the amount field is missing', function () { | ||
const json = | ||
'{"@type":"/cosmos.distribution.v1beta1.MsgFundCommunityPool","depositor":"tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3"}'; | ||
expect(() => cro.distribution.MsgFundCommunityPool.fromCosmosMsgJSON(json, CroNetwork.Testnet)).to.throw( | ||
'Invalid amount in the Msg.', | ||
); | ||
}); | ||
it('should return the `MsgFundCommunityPool` corresponding to the JSON', function () { | ||
const json = | ||
'{"@type":"/cosmos.distribution.v1beta1.MsgFundCommunityPool","amount":[{ "denom": "basetcro", "amount": "3478499933290496" }],"depositor":"tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3"}'; | ||
|
||
const msgFundCommPool = cro.distribution.MsgFundCommunityPool.fromCosmosMsgJSON(json, CroNetwork.Testnet); | ||
expect(msgFundCommPool.depositor).to.eql('tcro165tzcrh2yl83g8qeqxueg2g5gzgu57y3fe3kc3'); | ||
expect(msgFundCommPool.amount.toCosmosCoin().amount).to.eql('3478499933290496'); | ||
expect(msgFundCommPool.amount.toCosmosCoin().denom).to.eql('basetcro'); | ||
}); | ||
}); | ||
}); |