Skip to content

Commit

Permalink
Added missing Prime SDK mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmKio committed May 21, 2024
1 parent db2333d commit bd88702
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 73 deletions.
8 changes: 8 additions & 0 deletions __mocks__/@etherspot/prime-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ export class PrimeSdk {
export class DataUtils {
constructor() {}

getQuotes({ fromAccountAddress, toAddress, fromChainId, toChainId, fromToken, fromAmount, slippage, provider }) {
return [1, 2];
}

getSupportedAssets({ chainId, provider: bridgingProvider }) {
return [1, 2, 3, 4];
}

getAccountBalances({ chainId, account }) {
console.log('getAccountBalances', chainId, account);
const tokenBalance = ethers.utils.parseEther('420');
Expand Down
17 changes: 5 additions & 12 deletions __tests__/hooks/useEtherspotAssets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ describe('useEtherspotAssets()', () => {
describe('getAssets()', () => {
it('returns assets', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
<EtherspotTransactionKit provider={provider}>{children}</EtherspotTransactionKit>
);

const { result, rerender } = renderHook(({ chainId }) => useEtherspotAssets(chainId), {
Expand All @@ -26,15 +24,15 @@ describe('useEtherspotAssets()', () => {
const assetsMainnet = await result.current.getAssets();
expect(assetsMainnet.length).toEqual(3);

console.log('log1', result)
console.log('log1', result);

// rerender with different chain ID 137
rerender({ chainId: 137 });

const assetsPolygon = await result.current.getAssets();
expect(assetsPolygon.length).toEqual(1);
});
})
});

describe('getSupportedAssets()', () => {
it('returns all supported assets', async () => {
Expand All @@ -53,12 +51,8 @@ describe('useEtherspotAssets()', () => {
await waitFor(() => expect(result.current).not.toBeNull());

const assetsMainnet = await result.current.getSupportedAssets();
console.log('log', assetsMainnet);
expect(assetsMainnet.length).toBeGreaterThan(0);




// console.log('log1', result)
// console.log('THEPROVIDER', provider)
// // wait for assets to be fetched for chain ID 1
Expand All @@ -74,7 +68,7 @@ describe('useEtherspotAssets()', () => {
// // const assetsPolygon = await result.current.getSupportedAssets();
// // expect(assetsPolygon.length).toEqual(1);
});
})
});
// describe('getSupportedAssets()', () => {
// it('returns all supported assets for chain ID 1', async () => {
// const wrapper = ({ children }) => (
Expand All @@ -99,5 +93,4 @@ describe('useEtherspotAssets()', () => {

// });
// });

});
});
50 changes: 14 additions & 36 deletions __tests__/hooks/useEtherspotSwaps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { renderHook, waitFor } from '@testing-library/react';
import { BigNumber, ethers } from 'ethers';

// hooks
import { useEtherspotSwaps, EtherspotTransactionKit } from '../../src';
import { EtherspotTransactionKit, useEtherspotSwaps } from '../../src';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);
Expand All @@ -11,9 +11,7 @@ describe('useEtherspotSwaps()', () => {
describe('getOffers()', () => {
it('returns offers for same chain swaps', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
<EtherspotTransactionKit provider={provider}>{children}</EtherspotTransactionKit>
);

const { result, rerender } = renderHook(({ chainId }) => useEtherspotSwaps(chainId), {
Expand All @@ -24,11 +22,7 @@ describe('useEtherspotSwaps()', () => {
// wait for hook to load
await waitFor(() => expect(result.current).not.toBeNull());

const offersMainnet = await result.current.getOffers(
ethers.utils.parseEther('1'),
'0x111',
'0x222',
);
const offersMainnet = await result.current.getOffers(ethers.utils.parseEther('1'), '0x111', '0x222');
expect(offersMainnet).not.toBe(undefined);
expect(offersMainnet.offers.length).toEqual(2);
expect(offersMainnet.offers[0].provider).toEqual('abc-swap');
Expand All @@ -39,20 +33,14 @@ describe('useEtherspotSwaps()', () => {
// rerender with different chain ID 137
rerender({ chainId: 137 });

const offersPolygon = await result.current.getOffers(
ethers.utils.parseEther('1'),
'0x111',
'0x222',
);
const offersPolygon = await result.current.getOffers(ethers.utils.parseEther('1'), '0x111', '0x222');
expect(offersPolygon).not.toBe(undefined);
expect(offersPolygon.offers?.length).toEqual(0);
});

it('returns offers for cross chain swaps', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
<EtherspotTransactionKit provider={provider}>{children}</EtherspotTransactionKit>
);

const { result, rerender } = renderHook(({ chainId }) => useEtherspotSwaps(chainId), {
Expand Down Expand Up @@ -82,22 +70,15 @@ describe('useEtherspotSwaps()', () => {
// rerender with different chain ID 137
rerender({ chainId: 137 });

const offersPolygonToMainnet = await result.current.getOffers(
ethers.utils.parseEther('1'),
'0x111',
'0x222',
1
);
const offersPolygonToMainnet = await result.current.getOffers(ethers.utils.parseEther('1'), '0x111', '0x222', 1);
expect(offersPolygonToMainnet).not.toBe(undefined);
expect(offersPolygonToMainnet.offers?.length).toEqual(0);
});
});
describe('prepareCrossChainOfferTransactions()', () => {
it('returns parsed transactions for cross chain offer', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
<EtherspotTransactionKit provider={provider}>{children}</EtherspotTransactionKit>
);

const { result } = renderHook(({ chainId }) => useEtherspotSwaps(chainId), {
Expand All @@ -118,7 +99,7 @@ describe('useEtherspotSwaps()', () => {
symbol: 'ABC',
decimals: 18,
chainId: 1,
name: 'Token ABC'
name: 'Token ABC',
},
toChainId: 137,
toAmount: ethers.utils.parseEther('0.1').toString(),
Expand All @@ -128,10 +109,10 @@ describe('useEtherspotSwaps()', () => {
symbol: 'DEF',
decimals: 18,
chainId: 137,
name: 'Token DEF'
name: 'Token DEF',
},
steps: ['0x1', '0x2'],
}
};

const parsedCrossChainOfferTransactions = await result.current.prepareCrossChainOfferTransactions(offer);
expect(parsedCrossChainOfferTransactions).not.toBe(undefined);
Expand All @@ -145,9 +126,7 @@ describe('useEtherspotSwaps()', () => {
describe('getQuotes()', () => {
it('returns quotes cross chain', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
<EtherspotTransactionKit provider={provider}>{children}</EtherspotTransactionKit>
);

const { result } = renderHook(({ chainId }) => useEtherspotSwaps(chainId), {
Expand All @@ -164,13 +143,12 @@ describe('useEtherspotSwaps()', () => {
fromChainId: 1,
toChainId: 56,
fromToken: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
fromAmount: BigNumber.from("1000000000000000000"),
fromAmount: BigNumber.from('1000000000000000000'),
slippage: 1,
}
};

const allQuotes = await result.current.getQuotes(quote1);
console.log(allQuotes)
expect(allQuotes.length).toBe(2);
});
});
})
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"rollup:watch": "rimraf ./node_modules/react ./node_modules/react-dom && rollup -c -w",
"test": "jest __tests__ --silent --detectOpenHandles",
"test:watch": "jest --watch",
"test:only": "jest useEtherspotHistory.test.js --detectOpenHandles --watch"
"test:only": "jest useEtherspotAssets.test.js --detectOpenHandles --watch"
},
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useEtherspotHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ const useEtherspotHistory = (chainId?: number): IEtherspotHistoryHook => {
): Promise<TransactionStatus | undefined> => {
try {
const dataService = getDataService();
console.log('dataservice getTransactionStatus', dataService.getTransactionStatus);
return dataService.getTransactionStatus({ fromChainId, toChainId, transactionHash: hash, provider });
} catch (e) {
console.error(e);
Expand Down
49 changes: 26 additions & 23 deletions src/hooks/useEtherspotSwaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface IEtherspotSwapsHook {
fromAmount: BigNumber,
slippage: number,
provider?: BridgingProvider
) => Promise<Quote[] | undefined>
) => Promise<Quote[] | undefined>;
}

/**
Expand All @@ -54,7 +54,7 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
): Promise<StepTransaction[] | undefined> => {
const sdkForChainId = await getSdk(swapsChainId);

const forAccount = accountAddress ?? await sdkForChainId.getCounterFactualAddress();
const forAccount = accountAddress ?? (await sdkForChainId.getCounterFactualAddress());
if (!forAccount) {
console.warn(`No account address provided!`);
return [];
Expand All @@ -69,23 +69,22 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
return items;
} catch (e) {
console.warn(
`Sorry, an error occurred whilst trying to fetch cross-chain offer transactions.`
+ ` Please try again. Error:`,
e,
`Sorry, an error occurred whilst trying to fetch cross-chain offer transactions.` + ` Please try again. Error:`,
e
);
}
}
};

const getOffers = async (
fromAmount: BigNumber,
fromTokenAddress: string,
toTokenAddress: string,
toChainId?: number,
fromAccountAddress?: string,
fromAccountAddress?: string
): Promise<ISameChainSwapOffers | ICrossChainSwapOffers | undefined> => {
const sdkForChainId = await getSdk(swapsChainId);

const fromAccount = fromAccountAddress ?? await sdkForChainId.getCounterFactualAddress();
const fromAccount = fromAccountAddress ?? (await sdkForChainId.getCounterFactualAddress());
if (!fromAccount) {
console.warn(`No account address provided!`);
return;
Expand All @@ -106,9 +105,8 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
return { type: 'cross-chain', offers };
} catch (e) {
console.warn(
`Sorry, an error occurred whilst trying to fetch cross-chain offers.`
+ ` Please try again. Error:`,
e,
`Sorry, an error occurred whilst trying to fetch cross-chain offers.` + ` Please try again. Error:`,
e
);
}
return;
Expand All @@ -126,14 +124,12 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
return { type: 'same-chain', offers };
} catch (e) {
console.warn(
`Sorry, an error occurred whilst trying to fetch same-chain offers.`
+ ` Please try again. Error:`,
e,
`Sorry, an error occurred whilst trying to fetch same-chain offers.` + ` Please try again. Error:`,
e
);
}
};


const getQuotes = async (
fromAccountAddress: string,
toAddress: string,
Expand All @@ -146,7 +142,7 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
): Promise<Quote[] | undefined> => {
const sdkForChainId = await getSdk(swapsChainId);

const fromAccount = fromAccountAddress ?? await sdkForChainId.getCounterFactualAddress();
const fromAccount = fromAccountAddress ?? (await sdkForChainId.getCounterFactualAddress());
if (!fromAccount) {
console.warn(`No account address provided!`);
return;
Expand All @@ -155,24 +151,31 @@ const useEtherspotSwaps = (chainId?: number): IEtherspotSwapsHook => {
const dataService = getDataService();

try {
const quotes = await dataService.getQuotes({ fromAddress: fromAccount, toAddress, fromChainId, toChainId, fromToken, fromAmount, slippage, provider })
const quotes = await dataService.getQuotes({
fromAddress: fromAccount,
toAddress,
fromChainId,
toChainId,
fromToken,
fromAmount,
slippage,
provider,
});

return quotes;
} catch (e) {
console.warn(
`Sorry, an error occurred whilst trying to fetch cross-chain quotes.`
+ ` Please try again. Error:`,
e,
`Sorry, an error occurred whilst trying to fetch cross-chain quotes.` + ` Please try again. Error:`,
e
);
}
};


return ({
return {
getOffers,
prepareCrossChainOfferTransactions,
getQuotes,
});
};
};

export default useEtherspotSwaps;

0 comments on commit bd88702

Please sign in to comment.