Skip to content
This repository was archived by the owner on Jun 19, 2024. It is now read-only.

Commit ad9cf54

Browse files
committed
chore: coverage on perform swap txns
1 parent c19486d commit ad9cf54

File tree

1 file changed

+130
-36
lines changed

1 file changed

+130
-36
lines changed

src/utils/api/swaps/createPerformSwapTxns.test.ts

Lines changed: 130 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,49 @@ import { dummyContract } from './__utils__/testUtils';
1111
import { SwapConfiguration, SwapType } from '@/models/Swap';
1212
import { GET_INCENTIVE_FEE, INCENTIVE_WALLET } from '@/common/constants';
1313

14+
export function expectIncentivePayment(
15+
txn: TransactionToSign,
16+
from: string,
17+
version: string,
18+
) {
19+
expect(txn.signer).toBe(undefined);
20+
expect(txn.type).toBe(TransactionToSignType.UserFeeTransaction);
21+
expect(txn.transaction.type).toBe(`pay`);
22+
expect(encodeAddress(txn.transaction.to.publicKey)).toBe(INCENTIVE_WALLET);
23+
expect(encodeAddress(txn.transaction.from.publicKey)).toBe(from);
24+
expect(txn.transaction.amount).toBe(GET_INCENTIVE_FEE(version));
25+
}
26+
function expectOfferedAsaXferTxn(
27+
txn: TransactionToSign,
28+
logicSig: LogicSigAccount,
29+
to: string,
30+
amount: number,
31+
assetIndex: number,
32+
) {
33+
expect(txn.signer).toBe(logicSig);
34+
expect(txn.type).toBe(TransactionToSignType.LsigTransaction);
35+
expect(encodeAddress(txn.transaction.from.publicKey)).toBe(
36+
logicSig.address(),
37+
);
38+
expect(encodeAddress(txn.transaction.to.publicKey)).toBe(to);
39+
expect(txn.transaction.assetIndex).toBe(assetIndex);
40+
expect(txn.transaction.amount).toBe(amount);
41+
}
42+
function expectRequestedTxn(
43+
txn: TransactionToSign,
44+
from: string,
45+
to: string,
46+
assetIndex?: number,
47+
) {
48+
expect(txn.signer).toBe(undefined);
49+
expect(txn.type).toBe(TransactionToSignType.UserTransaction);
50+
expect(encodeAddress(txn.transaction.from.publicKey)).toBe(from);
51+
expect(encodeAddress(txn.transaction.to.publicKey)).toBe(to);
52+
if (typeof assetIndex !== `undefined`) {
53+
expect(txn.transaction.assetIndex).toBe(assetIndex);
54+
}
55+
}
56+
1457
describe(`createPerformSwapTxns`, () => {
1558
it(`generates asa to asa txns correctly`, async () => {
1659
const dummyAccount = generateAccount();
@@ -46,51 +89,102 @@ describe(`createPerformSwapTxns`, () => {
4689
dummyLogicSig,
4790
dummySwapConfiguration,
4891
);
92+
// Ensure length is correct
93+
expect(txns.length).toBe(3);
4994

50-
// First txn
51-
expect((txns[0] as TransactionToSign).signer).toBe(dummyLogicSig);
52-
expect((txns[0] as TransactionToSign).type).toBe(
53-
TransactionToSignType.LsigTransaction,
95+
// First txn - Offered ASA
96+
expectOfferedAsaXferTxn(
97+
txns[0],
98+
dummyLogicSig,
99+
dummyAccount.addr,
100+
0,
101+
expectedAssetIndex,
54102
);
55-
expect(
56-
encodeAddress((txns[0] as TransactionToSign).transaction.from.publicKey),
57-
).toBe(dummyLogicSig.address());
58-
expect(
59-
encodeAddress((txns[0] as TransactionToSign).transaction.to.publicKey),
60-
).toBe(dummyAccount.addr);
61-
expect((txns[0] as TransactionToSign).transaction.assetIndex).toBe(
103+
104+
// Second txn - Requested ASA
105+
expectRequestedTxn(
106+
txns[1],
107+
dummyAccount.addr,
108+
dummyAccount.addr,
62109
expectedAssetIndex,
63110
);
64-
expect((txns[0] as TransactionToSign).transaction.amount).toBe(0);
65111

66-
// Second txn
67-
expect((txns[1] as TransactionToSign).signer).toBe(undefined);
68-
expect((txns[1] as TransactionToSign).type).toBe(
69-
TransactionToSignType.UserTransaction,
112+
// Third txn - Incentive payment
113+
expectIncentivePayment(txns[2], dummyAccount.addr, dummySwapVersion);
114+
});
115+
it(`generates Multi ASA to ALGO txns correctly`, async () => {
116+
const dummyAccount = generateAccount();
117+
const dummyLogicSig = getLogicSign(dummyContract) as LogicSigAccount;
118+
const expectedAssetIndex = 123;
119+
const expectedSecondAssetIndex = 1234;
120+
const dummyOfferingAsset = {
121+
index: expectedAssetIndex,
122+
creator: `test`,
123+
name: `test`,
124+
imageUrl: `test`,
125+
decimals: 6,
126+
unitName: `test`,
127+
amount: 1,
128+
frozen: false,
129+
offeringAmount: 0,
130+
requestingAmount: 0,
131+
} as Asset;
132+
const secondDummyOfferingAsset = {
133+
index: expectedSecondAssetIndex,
134+
creator: `test`,
135+
name: `test`,
136+
imageUrl: `test`,
137+
decimals: 6,
138+
unitName: `test`,
139+
amount: 10,
140+
frozen: false,
141+
offeringAmount: 0,
142+
requestingAmount: 0,
143+
} as Asset;
144+
const dummySwapVersion = `0.0.2`;
145+
const dummySwapConfiguration = {
146+
version: dummySwapVersion,
147+
type: SwapType.MULTI_ASA_TO_ALGO,
148+
offering: [dummyOfferingAsset, secondDummyOfferingAsset],
149+
requesting: [dummyOfferingAsset],
150+
creator: dummyAccount.addr,
151+
proxy: dummyLogicSig.address(),
152+
escrow: dummyLogicSig.address(),
153+
contract: dummyContract,
154+
} as SwapConfiguration;
155+
156+
const txns = await createPerformSwapTxns(
157+
ChainType.TestNet,
158+
dummyAccount.addr,
159+
dummyLogicSig,
160+
dummySwapConfiguration,
70161
);
71-
expect(
72-
encodeAddress((txns[1] as TransactionToSign).transaction.from.publicKey),
73-
).toBe(dummyAccount.addr);
74-
expect(
75-
encodeAddress((txns[1] as TransactionToSign).transaction.to.publicKey),
76-
).toBe(dummyAccount.addr);
77-
expect((txns[1] as TransactionToSign).transaction.assetIndex).toBe(
162+
163+
// Ensure length is correct
164+
expect(txns.length).toBe(4);
165+
166+
// First txn - Incentive Fee
167+
expectIncentivePayment(txns[0], dummyAccount.addr, dummySwapVersion);
168+
169+
// Second txn - Request Algo Payment
170+
expectRequestedTxn(txns[1], dummyAccount.addr, dummyAccount.addr);
171+
172+
// Third txn - Assets Transfer
173+
expectOfferedAsaXferTxn(
174+
txns[2],
175+
dummyLogicSig,
176+
dummyAccount.addr,
177+
0,
78178
expectedAssetIndex,
79179
);
80180

81-
// Third txn
82-
expect((txns[2] as TransactionToSign).signer).toBe(undefined);
83-
expect((txns[2] as TransactionToSign).type).toBe(
84-
TransactionToSignType.UserFeeTransaction,
85-
);
86-
expect(
87-
encodeAddress((txns[2] as TransactionToSign).transaction.from.publicKey),
88-
).toBe(dummyAccount.addr);
89-
expect(
90-
encodeAddress((txns[2] as TransactionToSign).transaction.to.publicKey),
91-
).toBe(INCENTIVE_WALLET);
92-
expect((txns[2] as TransactionToSign).transaction.amount).toBe(
93-
GET_INCENTIVE_FEE(dummySwapVersion),
181+
// Fourth txn - Assets Transfer
182+
expectOfferedAsaXferTxn(
183+
txns[3],
184+
dummyLogicSig,
185+
dummyAccount.addr,
186+
0,
187+
expectedSecondAssetIndex,
94188
);
95189
});
96190
});

0 commit comments

Comments
 (0)