Skip to content

Commit d9b8eea

Browse files
authored
Merge pull request #6567 from BitGo/COIN-4990-generic-tests-cosmos-module
feat(sdk-coin-cosmos): generic configuration driven test cases for sh…
2 parents ef4872d + b181c59 commit d9b8eea

File tree

12 files changed

+1365
-1
lines changed

12 files changed

+1365
-1
lines changed

modules/sdk-coin-cosmos/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,15 @@
4141
},
4242
"dependencies": {
4343
"@bitgo/abstract-cosmos": "^11.10.0",
44+
"@bitgo/sdk-api": "^1.65.2",
4445
"@bitgo/sdk-core": "^35.9.0",
4546
"@bitgo/statics": "^55.3.0",
4647
"@cosmjs/amino": "^0.29.5",
48+
"@cosmjs/encoding": "^0.29.5",
4749
"@cosmjs/stargate": "^0.29.5",
4850
"bignumber.js": "^9.1.1"
4951
},
50-
"devDependencies": {}
52+
"devDependencies": {
53+
"@bitgo/sdk-test": "^8.0.98"
54+
}
5155
}
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
/**
2+
* Data generation functions for Cosmos SDK test data
3+
* This file contains functions to generate test data for Cosmos SDK-based coins
4+
*/
5+
6+
import {
7+
ChainConfig,
8+
DefaultValues,
9+
TestTransaction,
10+
TestAddresses,
11+
TestBlockHashes,
12+
TestTxIds,
13+
TestCoinAmounts,
14+
GasBudget,
15+
TransactionMessage,
16+
CoinTestData,
17+
} from './types';
18+
19+
/**
20+
* Generate addresses for testing
21+
* @param {DefaultValues} defaults - Default values containing base addresses
22+
* @returns {TestAddresses} Object containing various test addresses
23+
*/
24+
export const generateAddresses = (defaults: DefaultValues): TestAddresses => {
25+
return {
26+
address1: defaults.senderAddress,
27+
address2: defaults.recipientAddress1,
28+
address3: defaults.recipientAddress2 || '',
29+
address4: defaults.senderAddress.slice(0, -1), // remove last character to make invalid
30+
address5: defaults.senderAddress + 'x', // add random character to make invalid
31+
address6: defaults.senderAddress.replace(/[a-z]/, '.'), // add dot to make invalid
32+
validatorAddress1: defaults.validatorAddress1,
33+
validatorAddress2: defaults.validatorAddress2,
34+
validatorAddress3: defaults.validatorAddress1 + 'sd', // extra characters to make invalid
35+
validatorAddress4: defaults.validatorAddress1.replace('1', 'x'), // change character to make invalid
36+
noMemoIdAddress: defaults.recipientAddress1,
37+
validMemoIdAddress: defaults.recipientAddress1 + '?memoId=2',
38+
invalidMemoIdAddress: defaults.recipientAddress1 + '?memoId=1.23',
39+
multipleMemoIdAddress: defaults.recipientAddress1 + '?memoId=3&memoId=12',
40+
};
41+
};
42+
43+
/**
44+
* Generate transaction IDs from test transactions
45+
* @param {Object} testTxs - Object containing test transactions
46+
* @returns {TestTxIds} Object containing transaction IDs
47+
*/
48+
export const generateTxIds = (testTxs: { [key: string]: TestTransaction }): TestTxIds => {
49+
return {
50+
hash1: testTxs.TEST_SEND_TX.hash,
51+
hash2: testTxs.TEST_SEND_TX2.hash,
52+
hash3: testTxs.TEST_SEND_MANY_TX.hash,
53+
};
54+
};
55+
56+
/**
57+
* Generate coin amounts for testing
58+
* @param {string} baseDenom - Base denomination of the coin
59+
* @returns {TestCoinAmounts} Object containing various test amounts
60+
*/
61+
export const generateCoinAmounts = (baseDenom: string): TestCoinAmounts => {
62+
return {
63+
amount1: { amount: '1', denom: baseDenom },
64+
amount2: { amount: '10', denom: baseDenom },
65+
amount3: { amount: '100', denom: baseDenom },
66+
amount4: { amount: '-1', denom: baseDenom },
67+
amount5: { amount: '100', denom: 'm' + baseDenom },
68+
};
69+
};
70+
71+
/**
72+
* Generate a standard transaction message for sending tokens
73+
* @param {string} fromAddress - The sender address
74+
* @param {string} toAddress - The recipient address
75+
* @param {string} denom - The token denomination
76+
* @param {string} amount - The amount to send
77+
* @returns {TransactionMessage} A transaction message
78+
*/
79+
export const generateSendMessage = (
80+
fromAddress: string,
81+
toAddress: string,
82+
denom: string,
83+
amount: string
84+
): TransactionMessage => {
85+
return {
86+
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
87+
value: {
88+
amount: [
89+
{
90+
denom,
91+
amount,
92+
},
93+
],
94+
fromAddress,
95+
toAddress,
96+
},
97+
};
98+
};
99+
100+
/**
101+
* Generate a standard gas budget
102+
* @param {string} denom - The token denomination
103+
* @param {string} amount - The gas amount
104+
* @param {number} gasLimit - The gas limit
105+
* @returns {GasBudget} A gas budget
106+
*/
107+
export const generateGasBudget = (denom: string, amount: string, gasLimit = 500000): GasBudget => {
108+
return {
109+
amount: [{ denom, amount }],
110+
gasLimit,
111+
};
112+
};
113+
114+
/**
115+
* Generate complete coin test data
116+
* @param {ChainConfig} chainConfig - Chain configuration
117+
* @param {DefaultValues} defaults - Default values
118+
* @param {TestBlockHashes} blockHashes - Block hashes for testing
119+
* @param {Object} testTxs - Test transactions
120+
* @returns {CoinTestData} Complete coin test data
121+
*/
122+
export const generateCoinData = (
123+
chainConfig: ChainConfig,
124+
defaults: DefaultValues,
125+
blockHashes: TestBlockHashes,
126+
testTxs: { [key: string]: TestTransaction }
127+
): CoinTestData => {
128+
const addresses = generateAddresses(defaults);
129+
const txIds = generateTxIds(testTxs);
130+
const coinAmounts = generateCoinAmounts(chainConfig.baseDenom);
131+
132+
return {
133+
mainnetName: chainConfig.mainnetName,
134+
mainnetCoin: chainConfig.mainnetCoin,
135+
testnetCoin: chainConfig.testnetCoin,
136+
testnetName: chainConfig.testnetName,
137+
family: chainConfig.family,
138+
decimalPlaces: chainConfig.decimalPlaces,
139+
baseDenom: chainConfig.baseDenom,
140+
chainId: chainConfig.chainId,
141+
senderAddress: defaults.senderAddress,
142+
pubKey: defaults.pubKey,
143+
privateKey: defaults.privateKey,
144+
validatorPrefix: chainConfig.validatorPrefix,
145+
addressPrefix: chainConfig.addressPrefix,
146+
addresses,
147+
blockHashes,
148+
txIds,
149+
coinAmounts,
150+
testSendTx: {
151+
...testTxs.TEST_SEND_TX,
152+
sender: testTxs.TEST_SEND_TX.sender || defaults.senderAddress,
153+
recipient: testTxs.TEST_SEND_TX.recipient || defaults.recipientAddress1,
154+
chainId: chainConfig.chainId,
155+
sendAmount: testTxs.TEST_SEND_TX.sendAmount || defaults.sendAmount,
156+
feeAmount: testTxs.TEST_SEND_TX.feeAmount || defaults.feeAmount,
157+
privateKey: testTxs.TEST_SEND_TX.privateKey || defaults.privateKey,
158+
pubKey: testTxs.TEST_SEND_TX.pubKey || defaults.pubKey,
159+
gasBudget: generateGasBudget(
160+
chainConfig.baseDenom,
161+
testTxs.TEST_SEND_TX.feeAmount || defaults.feeAmount,
162+
defaults.gasLimit
163+
),
164+
sendMessage: generateSendMessage(
165+
testTxs.TEST_SEND_TX.sender || defaults.senderAddress,
166+
testTxs.TEST_SEND_TX.recipient || defaults.recipientAddress1,
167+
chainConfig.baseDenom,
168+
testTxs.TEST_SEND_TX.sendAmount || defaults.sendAmount
169+
),
170+
},
171+
testSendTx2: {
172+
...testTxs.TEST_SEND_TX2,
173+
sender: testTxs.TEST_SEND_TX2.sender || defaults.senderAddress,
174+
recipient: testTxs.TEST_SEND_TX2.recipient || defaults.recipientAddress1,
175+
chainId: chainConfig.chainId,
176+
sendAmount: testTxs.TEST_SEND_TX2.sendAmount || defaults.sendAmount,
177+
feeAmount: testTxs.TEST_SEND_TX2.feeAmount || defaults.feeAmount,
178+
privateKey: testTxs.TEST_SEND_TX2.privateKey || defaults.privateKey,
179+
pubKey: testTxs.TEST_SEND_TX2.pubKey || defaults.pubKey,
180+
gasBudget: generateGasBudget(
181+
chainConfig.baseDenom,
182+
testTxs.TEST_SEND_TX2.feeAmount || defaults.feeAmount,
183+
defaults.gasLimit
184+
),
185+
sendMessage: generateSendMessage(
186+
testTxs.TEST_SEND_TX2.sender || defaults.senderAddress,
187+
testTxs.TEST_SEND_TX2.recipient || defaults.recipientAddress1,
188+
chainConfig.baseDenom,
189+
testTxs.TEST_SEND_TX2.sendAmount || defaults.sendAmount
190+
),
191+
},
192+
testSendManyTx: {
193+
...testTxs.TEST_SEND_MANY_TX,
194+
sender: testTxs.TEST_SEND_MANY_TX.sender || defaults.senderAddress,
195+
chainId: chainConfig.chainId,
196+
sendAmount: testTxs.TEST_SEND_MANY_TX.sendAmount || defaults.sendAmount,
197+
sendAmount2: testTxs.TEST_SEND_MANY_TX.sendAmount2 || defaults.sendAmount2,
198+
feeAmount: testTxs.TEST_SEND_MANY_TX.feeAmount || defaults.feeAmount,
199+
pubKey: testTxs.TEST_SEND_MANY_TX.pubKey || defaults.pubKey,
200+
privateKey: testTxs.TEST_SEND_MANY_TX.privateKey || defaults.privateKey,
201+
memo: '',
202+
gasBudget: generateGasBudget(
203+
chainConfig.baseDenom,
204+
testTxs.TEST_SEND_MANY_TX.feeAmount || defaults.feeAmount,
205+
defaults.gasLimit
206+
),
207+
sendMessages: [
208+
generateSendMessage(
209+
testTxs.TEST_SEND_MANY_TX.sender || defaults.senderAddress,
210+
testTxs.TEST_SEND_MANY_TX.recipient || defaults.recipientAddress1,
211+
chainConfig.baseDenom,
212+
testTxs.TEST_SEND_MANY_TX.sendAmount || defaults.sendAmount
213+
),
214+
generateSendMessage(
215+
testTxs.TEST_SEND_MANY_TX.sender || defaults.senderAddress,
216+
testTxs.TEST_SEND_MANY_TX.recipient2 || defaults.recipientAddress1,
217+
chainConfig.baseDenom,
218+
testTxs.TEST_SEND_MANY_TX.sendAmount2 || defaults.sendAmount
219+
),
220+
],
221+
},
222+
testTxWithMemo: {
223+
...testTxs.TEST_TX_WITH_MEMO,
224+
sender: testTxs.TEST_TX_WITH_MEMO.sender || defaults.senderAddress,
225+
recipient: testTxs.TEST_TX_WITH_MEMO.recipient || defaults.recipientAddress1,
226+
chainId: chainConfig.chainId,
227+
sendAmount: testTxs.TEST_TX_WITH_MEMO.sendAmount || defaults.sendAmount,
228+
feeAmount: testTxs.TEST_TX_WITH_MEMO.feeAmount || defaults.feeAmount,
229+
pubKey: testTxs.TEST_TX_WITH_MEMO.pubKey || defaults.pubKey,
230+
privateKey: testTxs.TEST_TX_WITH_MEMO.privateKey || defaults.privateKey,
231+
gasBudget: generateGasBudget(
232+
chainConfig.baseDenom,
233+
testTxs.TEST_TX_WITH_MEMO.feeAmount || defaults.feeAmount,
234+
defaults.gasLimit
235+
),
236+
sendMessage: generateSendMessage(
237+
testTxs.TEST_TX_WITH_MEMO.sender || defaults.senderAddress,
238+
testTxs.TEST_TX_WITH_MEMO.recipient || defaults.recipientAddress1,
239+
chainConfig.baseDenom,
240+
testTxs.TEST_TX_WITH_MEMO.sendAmount || defaults.sendAmount
241+
),
242+
},
243+
};
244+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Main exports for Cosmos SDK test utilities
3+
* This file re-exports all utilities from the testUtils directory
4+
*/
5+
6+
// Re-export everything from the other files
7+
export * from './types';
8+
export * from './generators';
9+
export * from './utils';

0 commit comments

Comments
 (0)