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

Commit

Permalink
chore: Coverage on createPerformSwapTxns.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
PhearZero committed Aug 10, 2023
1 parent c19486d commit a2de6a3
Showing 1 changed file with 130 additions and 36 deletions.
166 changes: 130 additions & 36 deletions src/utils/api/swaps/createPerformSwapTxns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,49 @@ import { dummyContract } from './__utils__/testUtils';
import { SwapConfiguration, SwapType } from '@/models/Swap';
import { GET_INCENTIVE_FEE, INCENTIVE_WALLET } from '@/common/constants';

export function expectIncentivePayment(
txn: TransactionToSign,
from: string,
version: string,
) {
expect(txn.signer).toBe(undefined);
expect(txn.type).toBe(TransactionToSignType.UserFeeTransaction);
expect(txn.transaction.type).toBe(`pay`);
expect(encodeAddress(txn.transaction.to.publicKey)).toBe(INCENTIVE_WALLET);
expect(encodeAddress(txn.transaction.from.publicKey)).toBe(from);
expect(txn.transaction.amount).toBe(GET_INCENTIVE_FEE(version));
}
function expectOfferedAsaXferTxn(
txn: TransactionToSign,
logicSig: LogicSigAccount,
to: string,
amount: number,
assetIndex: number,
) {
expect(txn.signer).toBe(logicSig);
expect(txn.type).toBe(TransactionToSignType.LsigTransaction);
expect(encodeAddress(txn.transaction.from.publicKey)).toBe(
logicSig.address(),
);
expect(encodeAddress(txn.transaction.to.publicKey)).toBe(to);
expect(txn.transaction.assetIndex).toBe(assetIndex);
expect(txn.transaction.amount).toBe(amount);
}
function expectRequestedTxn(
txn: TransactionToSign,
from: string,
to: string,
assetIndex?: number,
) {
expect(txn.signer).toBe(undefined);
expect(txn.type).toBe(TransactionToSignType.UserTransaction);
expect(encodeAddress(txn.transaction.from.publicKey)).toBe(from);
expect(encodeAddress(txn.transaction.to.publicKey)).toBe(to);
if (typeof assetIndex !== `undefined`) {
expect(txn.transaction.assetIndex).toBe(assetIndex);
}
}

describe(`createPerformSwapTxns`, () => {
it(`generates asa to asa txns correctly`, async () => {
const dummyAccount = generateAccount();
Expand Down Expand Up @@ -46,51 +89,102 @@ describe(`createPerformSwapTxns`, () => {
dummyLogicSig,
dummySwapConfiguration,
);
// Ensure length is correct
expect(txns.length).toBe(3);

// First txn
expect((txns[0] as TransactionToSign).signer).toBe(dummyLogicSig);
expect((txns[0] as TransactionToSign).type).toBe(
TransactionToSignType.LsigTransaction,
// First txn - Offered ASA
expectOfferedAsaXferTxn(
txns[0],
dummyLogicSig,
dummyAccount.addr,
0,
expectedAssetIndex,
);
expect(
encodeAddress((txns[0] as TransactionToSign).transaction.from.publicKey),
).toBe(dummyLogicSig.address());
expect(
encodeAddress((txns[0] as TransactionToSign).transaction.to.publicKey),
).toBe(dummyAccount.addr);
expect((txns[0] as TransactionToSign).transaction.assetIndex).toBe(

// Second txn - Requested ASA
expectRequestedTxn(
txns[1],
dummyAccount.addr,
dummyAccount.addr,
expectedAssetIndex,
);
expect((txns[0] as TransactionToSign).transaction.amount).toBe(0);

// Second txn
expect((txns[1] as TransactionToSign).signer).toBe(undefined);
expect((txns[1] as TransactionToSign).type).toBe(
TransactionToSignType.UserTransaction,
// Third txn - Incentive payment
expectIncentivePayment(txns[2], dummyAccount.addr, dummySwapVersion);
});
it(`generates Multi ASA to ALGO txns correctly`, async () => {
const dummyAccount = generateAccount();
const dummyLogicSig = getLogicSign(dummyContract) as LogicSigAccount;
const expectedAssetIndex = 123;
const expectedSecondAssetIndex = 1234;
const dummyOfferingAsset = {
index: expectedAssetIndex,
creator: `test`,
name: `test`,
imageUrl: `test`,
decimals: 6,
unitName: `test`,
amount: 1,
frozen: false,
offeringAmount: 0,
requestingAmount: 0,
} as Asset;
const secondDummyOfferingAsset = {
index: expectedSecondAssetIndex,
creator: `test`,
name: `test`,
imageUrl: `test`,
decimals: 6,
unitName: `test`,
amount: 10,
frozen: false,
offeringAmount: 0,
requestingAmount: 0,
} as Asset;
const dummySwapVersion = `0.0.2`;
const dummySwapConfiguration = {
version: dummySwapVersion,
type: SwapType.MULTI_ASA_TO_ALGO,
offering: [dummyOfferingAsset, secondDummyOfferingAsset],
requesting: [dummyOfferingAsset],
creator: dummyAccount.addr,
proxy: dummyLogicSig.address(),
escrow: dummyLogicSig.address(),
contract: dummyContract,
} as SwapConfiguration;

const txns = await createPerformSwapTxns(
ChainType.TestNet,
dummyAccount.addr,
dummyLogicSig,
dummySwapConfiguration,
);
expect(
encodeAddress((txns[1] as TransactionToSign).transaction.from.publicKey),
).toBe(dummyAccount.addr);
expect(
encodeAddress((txns[1] as TransactionToSign).transaction.to.publicKey),
).toBe(dummyAccount.addr);
expect((txns[1] as TransactionToSign).transaction.assetIndex).toBe(

// Ensure length is correct
expect(txns.length).toBe(4);

// First txn - Incentive Fee
expectIncentivePayment(txns[0], dummyAccount.addr, dummySwapVersion);

// Second txn - Request Algo Payment
expectRequestedTxn(txns[1], dummyAccount.addr, dummyAccount.addr);

// Third txn - Assets Transfer
expectOfferedAsaXferTxn(
txns[2],
dummyLogicSig,
dummyAccount.addr,
0,
expectedAssetIndex,
);

// Third txn
expect((txns[2] as TransactionToSign).signer).toBe(undefined);
expect((txns[2] as TransactionToSign).type).toBe(
TransactionToSignType.UserFeeTransaction,
);
expect(
encodeAddress((txns[2] as TransactionToSign).transaction.from.publicKey),
).toBe(dummyAccount.addr);
expect(
encodeAddress((txns[2] as TransactionToSign).transaction.to.publicKey),
).toBe(INCENTIVE_WALLET);
expect((txns[2] as TransactionToSign).transaction.amount).toBe(
GET_INCENTIVE_FEE(dummySwapVersion),
// Fourth txn - Assets Transfer
expectOfferedAsaXferTxn(
txns[3],
dummyLogicSig,
dummyAccount.addr,
0,
expectedSecondAssetIndex,
);
});
});

0 comments on commit a2de6a3

Please sign in to comment.