Skip to content

Commit

Permalink
Feature/prime sdk based service hooks (#60)
Browse files Browse the repository at this point in the history
* removed via prop, removed etherspot provider type, default provider etherspot prime

* removed gas token, added prime sdk estimations

* optimized sdk connection, fixed tests, improved existing service hooks, fixed tests accordingly, updated example dapp

* fixed EtherspotTokenTransferTransaction test

* updated prime sdk version to support prices and assets hooks

* updated changelog, types chore

* removed session instance
  • Loading branch information
poocart authored Oct 24, 2023
1 parent 4a89c33 commit f964273
Show file tree
Hide file tree
Showing 38 changed files with 749 additions and 2,123 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# Changelog

## [0.6.1] - 2023-10-24

### Added Changes
- Updated `example` React code to latest changes
- Updated `@etherspot/prime-sdk` to version `1.3.4`

### Breaking Changes
- Removed `etherspot` dependency
- Removed `@etherspot/react-etherspot` dependency
- Updated `provider` prop as required for `<EtherspotTransactionKit />` component
- Removed `getSdkForChainId` from `useEtherspot` hook, replaced with `getSdk` that returns `Prime SDK` instance as `Promise`
- Removed `connect` from `useEtherspot` therefore it's no longer required to start SDK session
- Removed `accountAddress` and `providerWalletAddress` from `useEtherspot` hook, please use `useWalletAddress` hook
- Removed `sdk` from `useEtherspot` hook, please use `getSdk` on `useEtherspot` hook
- Removed `etherspotSessionStorage` from `<EtherspotTransactionKit />` component, session is now handled internally

### Pending Changes
- Method `getOffers` for same chain swaps on `useEtherspotSwaps` hook is not working on this release
- Methods `getAccountTransactions` and `getAccountTransaction` on `useEtherspotHistory` hook are not working on this release

## [0.6.0] - 2023-10-18

### Breaking Changes
Expand Down
175 changes: 173 additions & 2 deletions __mocks__/@etherspot/prime-sdk.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,181 @@
import * as EtherspotPrime from '@etherspot/prime-sdk';
import { ethers } from 'ethers';

const defaultAccountAddress = '0x7F30B1960D5556929B03a0339814fE903c55a347';
const otherAccountAddress = '0xAb4C67d8D7B248B2fA6B638C645466065fE8F1F1'

export class PrimeSdk {
constructor () {}
sdkChainId;

constructor (provider, config) {
this.sdkChainId = config.chainId;
}

getCounterFactualAddress() {
return '0x07ff85757f5209534EB601E1CA60d72807ECE0bC';
return defaultAccountAddress;
}

getAccountBalances({ chainId, account }) {
const tokenBalance = ethers.utils.parseEther('420');
const nativeAssetBalance = ethers.utils.parseEther('0');

const token = { token: '0x', balance: tokenBalance, superBalance: tokenBalance };
const nativeAsset = { token: null, balance: nativeAssetBalance, superBalance: nativeAssetBalance };

if (chainId !== 1) {
return { items: [nativeAsset] };
}

if (account === defaultAccountAddress) {
return { items: [nativeAsset, token] };
}

if (account === otherAccountAddress) {
return { items: [nativeAsset, { ...token, balance: ethers.utils.parseEther('69') }] };
}

return { items: [] };
}

getTransactions({ account }) {
const accountTransactions = [
{ hash: '0x1', value: '100000000000000' },
{ hash: '0x2', value: '420000000000000' },
];

if (this.sdkChainId !== 1) {
return { items: [] };
}

if (account === defaultAccountAddress) {
return { items: accountTransactions };
}

if (account === otherAccountAddress) {
return { items: [{ hash: '0x69', value: '0' }] };
}

return { items: [] };
}
getTransaction({ hash }) {
if (hash !== '0x42' || this.sdkChainId !== 1) return;
return { hash: '0x42', value: '690000000000000' };
}

getNftList({ account }) {
const accountNfts = [
{ contractName: 'Collection Alpha', contractAddress: '0x2', items: [{ tokenId: 420 }] },
{ contractName: 'Collection Beta', contractAddress: '0x1', items: [{ tokenId: 6 }, { tokenId: 9 }] },
];

if (this.sdkChainId !== 1) {
return { items: [] };
}

if (account === defaultAccountAddress) {
return { items: accountNfts };
}

if (account === otherAccountAddress) {
return { items: [{ ...accountNfts[0], contractName: 'Collection Gama' }] };
}

return { items: [] };
}
getTokenListTokens() {
const token1 = { address: '0x1', chainId: this.sdkChainId, name: 'tk1', symbol: 'TK1', decimals: 18, logoURI: '' };
const token2 = { address: '0x2', chainId: this.sdkChainId, name: 'tk2', symbol: 'TK2', decimals: 18, logoURI: '' };
const token3 = { address: '0x3', chainId: this.sdkChainId, name: 'tk3', symbol: 'TK3', decimals: 18, logoURI: '' };

return this.sdkChainId === 1
? [token1, token2, token3]
: [token1];
}

getExchangeOffers({ fromTokenAddress, toTokenAddress }) {
if (this.sdkChainId !== 1 || fromTokenAddress !== '0x111' || toTokenAddress !== '0x222') {
return [];
}

const offer1 = {
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') {
return { items: [] };
}

const offer1 = {
id: 'abc-bridge-offer-1',
fromChainId,
toChainId,
fromAmount,
toAmount: ethers.utils.parseEther('0.1'),
steps: ['0x1', '0x2'],
}

const offer2 = {
id: 'abc-bridge-offer-2',
fromChainId,
toChainId,
fromAmount,
toAmount: ethers.utils.parseEther('0.12'),
steps: ['0x1', '0x2'],
}

return { items: [offer1, offer2] };
}

getStepTransaction({ route: { id } }) {
if (id !== 'abc-bridge-offer-1') {
return { items: [] };
}

const transactions = [
{ to: '0x111', data: '0x2', value: undefined },
{ to: '0x222', data: '0x3', value: '100000000000000' },
];

return { items: transactions };
}

fetchExchangeRates({ chainId, tokens }) {
if (chainId !== 1) {
return { items: [] };
}

if (tokens.includes('some_wrongAddressFormat')) {
return { items: [], errored: true, error: 'Wrong address provided!' };
}

const prices = tokens.map((token, index) => ({
address: token,
eth: 1 + index * 0.1,
usd: 1800 * (1 + index * 0.1),
}));

return { items: prices }
}
}

Expand Down
183 changes: 0 additions & 183 deletions __mocks__/@etherspot/react-etherspot.js

This file was deleted.

Loading

0 comments on commit f964273

Please sign in to comment.