Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/PRO-2387/update-prime-sdk #147

Merged
merged 13 commits into from
May 22, 2024
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## [0.12.0] - 2024-03-28

### Added Changes
- Added Etherspot Prime SDK `getSupportedAssets` to hook `useEtherspotAssets`
- Added Etherspot Prime SDK `getQuotes` to hook `useEtherspotSwaps`
- Added Etherspot Prime SDK `getTransactionStatus` to hook `useEtherspotHistory`
- Default bridging provider for all above new functions is `Connext`
- Updated `@etherspot/prime-sdk` to version `1.8.1`

## [0.11.0] - 2024-03-28

### Added Changes
Expand Down
116 changes: 90 additions & 26 deletions __mocks__/@etherspot/prime-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { ethers } from 'ethers';

export const defaultAccountAddress = '0x7F30B1960D5556929B03a0339814fE903c55a347';
export const otherFactoryDefaultAccountAddress = '0xe383724e3bDC4753746dEC781809f8CD82010914';
export const otherAccountAddress = '0xAb4C67d8D7B248B2fA6B638C645466065fE8F1F1'
export const otherAccountAddress = '0xAb4C67d8D7B248B2fA6B638C645466065fE8F1F1';

export class PrimeSdk {
sdkChainId;
userOps = [];
nonce = ethers.BigNumber.from(1);
factoryWallet;

constructor (provider, config) {
constructor(provider, config) {
this.sdkChainId = config.chainId;
this.factoryWallet = config.factoryWallet;
}
Expand Down Expand Up @@ -58,9 +58,7 @@ export class PrimeSdk {
throw new Error('Transaction reverted: invalid address');
}
finalGasLimit = finalGasLimit.add(callGasLimit);
if (userOp.data
&& userOp.data !== '0x0'
&& userOp.data !== '0xFFF') {
if (userOp.data && userOp.data !== '0x0' && userOp.data !== '0xFFF') {
finalGasLimit = finalGasLimit.add(callGasLimit);
}
});
Expand All @@ -77,7 +75,7 @@ export class PrimeSdk {
maxPriorityFeePerGas,
paymasterAndData: '0x003',
signature,
}
};
}

totalGasEstimated({ callGasLimit, verificationGasLimit, preVerificationGas }) {
Expand Down Expand Up @@ -105,7 +103,66 @@ export class PrimeSdk {
}

export class DataUtils {
constructor () {}
constructor() { }

getQuotes({ fromAddress, toAddress, fromChainId, toChainId, fromToken, fromAmount, slippage }) {
if (!fromAddress || !toAddress || !fromChainId || !toChainId || !fromToken || !fromAmount || !slippage) {
return 'getQuotes: missing required props';
}

if (fromAddress === '0x111' && toAddress === '0x222' && fromChainId === 1 && toChainId === 56 && fromToken == '0x123456' && fromAmount === '0x10000000000' && slippage === 1) {
return {
transactions: [
{
data: "0x111222333",
to: "0x123",
value: "0x00"
},
{
value: "0xb2eed2c27ce6",
data: "0x444555666",
to: "0x456",
chainId: 1
}
]
}
}

return { transactions: [] };
}

getSupportedAssets({ chainId, provider: bridgingProvider }) {
const allSupportedAssets = [{
address: "0x123",
chainId: 1,
name: "USDC",
symbol: "USDC",
},
{
address: "0x456",
chainId: 1,
name: "USDC",
symbol: "USDC",
},
{
address: "0x789",
chainId: 137,
name: "USDC",
symbol: "USDC",
},
];

if (!chainId) {
return { tokens: allSupportedAssets }
}

if (allSupportedAssets.some(asset => asset.chainId === chainId)) {
return { tokens: allSupportedAssets.filter((asset) => asset.chainId === chainId) }
}

return { tokens: [] }

}

getAccountBalances({ chainId, account }) {
console.log('getAccountBalances', chainId, account);
Expand Down Expand Up @@ -150,6 +207,24 @@ export class DataUtils {

return { transactions: [] };
}

getTransactionStatus({ fromChainId, toChainId, transactionHash, provider }) {
if (!fromChainId || !toChainId || !transactionHash) {
return 'getTransactionStatus: missing required props'
}

if (fromChainId === 1 && toChainId === 137 && transactionHash === '0x123') {
return {
connextscanUrl: 'https://connextscan.io/tx/0x123',
status: 'completed',
transactionHash: '0x123',
transferId: 'abc123',
};
}

return {};
}

getTransaction({ hash, chainId }) {
if (hash !== '0x42' || chainId !== 1) return;
return { hash: '0x42', value: '690000000000000' };
Expand Down Expand Up @@ -181,9 +256,7 @@ export class DataUtils {
const token2 = { address: '0x2', chainId, name: 'tk2', symbol: 'TK2', decimals: 18, logoURI: '' };
const token3 = { address: '0x3', chainId, name: 'tk3', symbol: 'TK3', decimals: 18, logoURI: '' };

return chainId === 1
? [token1, token2, token3]
: [token1];
return chainId === 1 ? [token1, token2, token3] : [token1];
}

getExchangeOffers({ fromTokenAddress, toTokenAddress, fromChainId }) {
Expand All @@ -195,28 +268,19 @@ export class DataUtils {
provider: 'abc-swap',
receiveAmount: ethers.utils.parseEther('0.1'),
transactions: ['0x1', '0x2'],
}
};

const offer2 = {
provider: 'def-swap',
receiveAmount: ethers.utils.parseEther('0.11'),
transactions: ['0x1'],
}
};

return [offer1, offer2];
}

getAdvanceRoutesLiFi({
fromAmount,
fromChainId,
toChainId,
fromTokenAddress,
toTokenAddress,
}) {
if (fromChainId !== 1
|| toChainId !== 137
|| fromTokenAddress !== '0x111'
|| toTokenAddress !== '0x222') {
getAdvanceRoutesLiFi({ fromAmount, fromChainId, toChainId, fromTokenAddress, toTokenAddress }) {
if (fromChainId !== 1 || toChainId !== 137 || fromTokenAddress !== '0x111' || toTokenAddress !== '0x222') {
return { items: [] };
}

Expand All @@ -227,7 +291,7 @@ export class DataUtils {
fromAmount,
toAmount: ethers.utils.parseEther('0.1'),
steps: ['0x1', '0x2'],
}
};

const offer2 = {
id: 'abc-bridge-offer-2',
Expand All @@ -236,7 +300,7 @@ export class DataUtils {
fromAmount,
toAmount: ethers.utils.parseEther('0.12'),
steps: ['0x1', '0x2'],
}
};

return { items: [offer1, offer2] };
}
Expand Down Expand Up @@ -269,7 +333,7 @@ export class DataUtils {
usd: 1800 * (1 + index * 0.1),
}));

return { items: prices }
return { items: prices };
}
}

Expand Down
2 changes: 1 addition & 1 deletion __tests__/components/EtherspotApprovalTransaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from 'ethers';

import { useEtherspotTransactions, EtherspotTransactionKit, EtherspotBatches, EtherspotBatch, EtherspotApprovalTransaction } from '../../src';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'sepolia'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);

describe('EtherspotApprovalTransaction', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/components/EtherspotContractTransaction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from 'ethers';

import { useEtherspotTransactions, EtherspotTransactionKit, EtherspotBatches, EtherspotBatch, EtherspotContractTransaction } from '../../src';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'sepolia'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);

describe('EtherspotContractTransaction', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ethers } from 'ethers';

import { useEtherspotTransactions, EtherspotTransactionKit, EtherspotBatches, EtherspotBatch, EtherspotTokenTransferTransaction } from '../../src';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'sepolia'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);

describe('EtherspotTokenTransferTransaction', () => {
Expand Down Expand Up @@ -41,8 +41,8 @@ describe('EtherspotTokenTransferTransaction', () => {
})
}).rejects.toThrow(
'Failed to build transaction data, please check data/method formatting: invalid address'
+ ' (argument="address", value="0xtransfer", code=INVALID_ARGUMENT, version=address/5.7.0)'
+ ' (argument=\"to\", value="0xtransfer", code=INVALID_ARGUMENT, version=abi/5.7.0)'
+ ' (argument="address", value="0xtransfer", code=INVALID_ARGUMENT, version=address/5.7.0)'
+ ' (argument=\"to\", value="0xtransfer", code=INVALID_ARGUMENT, version=abi/5.7.0)'
);
});

Expand Down
91 changes: 71 additions & 20 deletions __tests__/hooks/useEtherspotAssets.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,82 @@ import { ethers } from 'ethers';
// hooks
import { EtherspotTransactionKit, useEtherspotAssets } from '../../src';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'sepolia'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);

describe('useEtherspotAssets()', () => {
it('returns assets', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
);

const { result, rerender } = renderHook(({ chainId }) => useEtherspotAssets(chainId), {
initialProps: { chainId: 1 },
wrapper,
describe('getAssets()', () => {
it('returns assets', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>{children}</EtherspotTransactionKit>
);

const { result, rerender } = renderHook(({ chainId }) => useEtherspotAssets(chainId), {
initialProps: { chainId: 1 },
wrapper,
});

// wait for assets to be fetched for chain ID 1
await waitFor(() => expect(result.current).not.toBeNull());
const assetsMainnet = await result.current.getAssets();
expect(assetsMainnet.length).toEqual(3);

// 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 by Etherspot', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
);

const { result } = renderHook(() => useEtherspotAssets(), {
wrapper,
});

// wait for assets to be fetched
await waitFor(() => expect(result.current).not.toBeNull());

const allSupportedAssets = await result.current.getSupportedAssets();
expect(allSupportedAssets).not.toBeUndefined();
expect(allSupportedAssets.tokens.length).toEqual(3);
});

// wait for assets to be fetched for chain ID 1
await waitFor(() => expect(result.current).not.toBeNull());
const assetsMainnet = await result.current.getAssets();
expect(assetsMainnet.length).toEqual(3);
it('returns all supported assets by Etherspot and by Chain ID', async () => {
const wrapper = ({ children }) => (
<EtherspotTransactionKit provider={provider}>
{children}
</EtherspotTransactionKit>
);

const { result } = renderHook(() => useEtherspotAssets(), {
wrapper,
});

// rerender with different chain ID 137
rerender({ chainId: 137 });
// wait for assets to be fetched
await waitFor(() => expect(result.current).not.toBeNull());

const assetsPolygon = await result.current.getAssets();
expect(assetsPolygon.length).toEqual(1);
// chain ID 1
const allSupportedAssetsMainnet = await result.current.getSupportedAssets(1);
expect(allSupportedAssetsMainnet).not.toBeUndefined();
expect(allSupportedAssetsMainnet.tokens.length).toEqual(2);

// chain ID 137
const allSupportedAssetsPolygon = await result.current.getSupportedAssets(137);
expect(allSupportedAssetsPolygon).not.toBeUndefined();
expect(allSupportedAssetsPolygon.tokens.length).toEqual(1);

// chain ID 56
const allSupportedAssetsBinance = await result.current.getSupportedAssets(56);
expect(allSupportedAssetsBinance).not.toBeUndefined();
expect(allSupportedAssetsBinance.tokens.length).toEqual(0);
});
});
})
});
2 changes: 1 addition & 1 deletion __tests__/hooks/useEtherspotBalances.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ethers } from 'ethers';
// hooks
import { useEtherspotBalances, EtherspotTransactionKit } from '../../src';

const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'goerli'); // replace with your node's RPC URL
const ethersProvider = new ethers.providers.JsonRpcProvider('http://localhost:8545', 'sepolia'); // replace with your node's RPC URL
const provider = new ethers.Wallet.createRandom().connect(ethersProvider);

describe('useEtherspotBalances()', () => {
Expand Down
Loading
Loading