Skip to content

Commit

Permalink
Added fetching token taxes for ShibaSwap + ChewySwap fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
evseevnn committed Nov 6, 2024
1 parent 0475e22 commit 32932cf
Show file tree
Hide file tree
Showing 10 changed files with 262 additions and 219 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"dependencies": {
"@balancer-labs/sdk": "^1.1.5",
"@bancor/carbon-sdk": "^0.0.93-DEV",
"@chewyswap/swap-sdk": "^1.0.1",
"@cosmjs/amino": "^0.32.2",
"@cosmjs/proto-signing": "^0.31.1",
"@cosmjs/stargate": "^0.31.1",
Expand All @@ -50,8 +51,7 @@
"@pancakeswap/v3-sdk": "^3.7.0",
"@pangolindex/sdk": "^1.1.0",
"@perp/sdk-curie": "^1.16.0",
"@shibaswap/sdk": "^1.1.17",
"@chewyswap/sdk": "git+https://github.com/PooDoge/chewyswap-sdk#main",
"@shibaswap/sdk": "^1.1.18",
"@sushiswap/sdk": "^5.0.0-canary.116",
"@taquito/rpc": "^17.0.0",
"@taquito/signer": "^17.0.0",
Expand Down
5 changes: 4 additions & 1 deletion src/chains/shibarium/shibarium.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Ethereumish } from '../../services/common-interfaces';
import { ConfigManagerV2 } from '../../services/config-manager-v2';
import { EVMController } from '../ethereum/evm.controllers';
import { ShibaswapConfig } from '../../connectors/shibaswap/shibaswap.config';
import { ChewyswapConfig } from '../../connectors/chewyswap/chewyswap.config';

export class Shibarium extends EthereumBase implements Ethereumish {
private static _instances: { [name: string]: Shibarium };
Expand Down Expand Up @@ -68,7 +69,9 @@ export class Shibarium extends EthereumBase implements Ethereumish {

getSpender(reqSpender: string): string {
let spender: string;
if (['shibaswap', 'chewyswap'].includes(reqSpender)) {
if (reqSpender === 'chewyswap') {
spender = ChewyswapConfig.config.routerAddress('shibarium', this._chain);
} else if (reqSpender === 'shibaswap') {
spender = ShibaswapConfig.config.routerAddress('shibarium', this._chain);
} else {
spender = reqSpender;
Expand Down
4 changes: 3 additions & 1 deletion src/chains/shibarium/shibarium.validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export const invalidSpenderError: string =
export const validateSpender: Validator = mkValidator(
'spender',
invalidSpenderError,
(val) => typeof val === 'string' && (val === 'shibaswap' || isAddress(val)),
(val) =>
typeof val === 'string' &&
(val === 'shibaswap' || val === 'chewyswap' || isAddress(val)),
);

export const validateApproveRequest: RequestValidator = mkRequestValidator([
Expand Down
17 changes: 8 additions & 9 deletions src/connectors/chewyswap/chewyswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
Trade,
Pair,
SwapParameters,
TokenAmount,
} from '@chewyswap/sdk';
} from '@chewyswap/swap-sdk';
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json';
import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces';
import { Shibarium } from '../../chains/shibarium/shibarium';
Expand Down Expand Up @@ -156,8 +155,8 @@ export class Chewyswap implements Uniswapish {
? [reserves0, reserves1]
: [reserves1, reserves0];
const pair = new Pair(
new TokenAmount(baseToken, balances[0]),
new TokenAmount(quoteToken, balances[1]),
CurrencyAmount.fromRawAmount(baseToken, balances[0]),
CurrencyAmount.fromRawAmount(quoteToken, balances[1]),
);
return pair;
}
Expand All @@ -178,7 +177,7 @@ export class Chewyswap implements Uniswapish {
quoteToken: Token,
amount: BigNumber,
): Promise<ExpectedTrade> {
const nativeTokenAmount: CurrencyAmount = new TokenAmount(
const nativeTokenAmount = CurrencyAmount.fromRawAmount(
baseToken,
amount.toString(),
);
Expand All @@ -189,7 +188,7 @@ export class Chewyswap implements Uniswapish {

const pair: Pair = await this.fetchData(baseToken, quoteToken);

const trades: Trade[] = Trade.bestTradeExactIn(
const trades = Trade.bestTradeExactIn(
[pair],
nativeTokenAmount,
quoteToken,
Expand Down Expand Up @@ -218,14 +217,14 @@ export class Chewyswap implements Uniswapish {
baseToken: Token,
amount: BigNumber,
): Promise<ExpectedTrade> {
const nativeTokenAmount: CurrencyAmount = new TokenAmount(
const nativeTokenAmount = CurrencyAmount.fromRawAmount(
baseToken,
amount.toString(),
);

const pair: Pair = await this.fetchData(quoteToken, baseToken);

const trades: Trade[] = Trade.bestTradeExactOut(
const trades = Trade.bestTradeExactOut(
[pair],
quoteToken,
nativeTokenAmount,
Expand Down Expand Up @@ -267,7 +266,7 @@ export class Chewyswap implements Uniswapish {

async executeTrade(
wallet: Wallet,
trade: Trade,
trade: any,
gasPrice: number,
sushswapRouter: string,
ttl: number,
Expand Down
39 changes: 34 additions & 5 deletions src/connectors/shibaswap/shibaswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Transaction,
Contract,
ContractTransaction,
ethers,
} from 'ethers';
import { percentRegexp } from '../../services/config-manager-v2';
import { logger } from '../../services/logger';
Expand Down Expand Up @@ -216,6 +217,7 @@ export class Shibaswap implements Uniswapish {

return { trade: trades[0], expectedAmount };
}

async estimateBuyTrade(
quoteToken: Token,
baseToken: Token,
Expand Down Expand Up @@ -253,39 +255,67 @@ export class Shibaswap implements Uniswapish {
return { trade: trades[0], expectedAmount };
}

async isFeeOnTransfer(trade: Trade, wallet: Wallet): Promise<boolean> {
const token: any = trade.inputAmount.currency;

// We need request taxes info from the token contract and if the token has a transfer tax, we return true
const TOKEN_ABI = [
'function taxes() view returns (uint16 buy, uint16 sell, address feeReceiver)',
];

try {
const tokenContract = new ethers.Contract(
token.address,
TOKEN_ABI,
wallet,
);
const { buy, sell, feeReceiver } = await tokenContract.taxes();

logger.warn(`Token taxes: Buy ${buy / 100}%, Sell ${sell / 100}%`);
logger.warn(`Fee receiver: ${feeReceiver}`);

return sell > 0 || buy > 0;
} catch (error) {
// Ignore errors and return false
}

return false;
}

/**
* Given a wallet and a Uniswap trade, try to execute it on blockchain.
*
* @param wallet Wallet
* @param trade Expected trade
* @param gasPrice Base gas price, for pre-EIP1559 transactions
* @param sushswapRouter Router smart contract address
* @param routerAddress Router smart contract address
* @param ttl How long the swap is valid before expiry, in seconds
* @param abi Router contract ABI
* @param gasLimit Gas limit
* @param nonce (Optional) EVM transaction nonce
* @param maxFeePerGas (Optional) Maximum total fee per gas you want to pay
* @param maxPriorityFeePerGas (Optional) Maximum tip per gas you want to pay
*/

async executeTrade(
wallet: Wallet,
trade: Trade,
gasPrice: number,
sushswapRouter: string,
routerAddress: string,
ttl: number,
abi: ContractInterface,
gasLimit: number,
nonce?: number,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber,
): Promise<Transaction> {
const feeOnTransfer = await this.isFeeOnTransfer(trade, wallet);
const result: SwapParameters = Router.swapCallParameters(trade, {
feeOnTransfer,
ttl,
recipient: wallet.address,
allowedSlippage: this.getSlippagePercentage(),
});
const contract: Contract = new Contract(sushswapRouter, abi, wallet);
const contract: Contract = new Contract(routerAddress, abi, wallet);
return this.chain.nonceManager.provideNonce(
nonce,
wallet.address,
Expand All @@ -308,7 +338,6 @@ export class Shibaswap implements Uniswapish {
});
}

logger.info(JSON.stringify(tx));
return tx;
},
);
Expand Down
8 changes: 5 additions & 3 deletions src/services/common-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ import {
import {
Trade as ChewyswapTrade,
Token as ChewyswapToken,
TradeType as ChewyswapTradeType,
Currency as ChewyswapCurrency,
CurrencyAmount as ChewyswapCurrencyAmount,
Fraction as ChewyswapFraction,
} from '@chewyswap/sdk';
} from '@chewyswap/swap-sdk';
import {
Trade as ShibaswapTrade,
Token as ShibaswapToken,
Expand Down Expand Up @@ -162,7 +164,7 @@ export type UniswapishTrade =
| TradeQuickswap
| TradeTraderjoe
| ShibaswapTrade
| ChewyswapTrade
| ChewyswapTrade<ChewyswapCurrency, ChewyswapCurrency, ChewyswapTradeType>
| SushiswapTrade<SushiToken, SushiToken, SushiTradeType>
| TradeUniswap
| PancakeSwapTrade<
Expand Down Expand Up @@ -199,7 +201,7 @@ export type UniswapishAmount =
| UniswapCoreCurrencyAmount<Currency>
| CurrencyAmountTraderjoe
| ShibaswapCurrencyAmount
| ChewyswapCurrencyAmount
| ChewyswapCurrencyAmount<ChewyswapCurrency | ChewyswapToken>
| SushiCurrencyAmount<SushiCurrency | SushiToken>
| PancakeSwapCurrencyAmount<PancakeSwapCurrency>
| CurrencyAmountMMF
Expand Down
7 changes: 7 additions & 0 deletions src/templates/lists/shibarium_tokens_mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@
"symbol": "DAMN",
"chainId": 109
},
{
"decimals": 18,
"address": "0xc76f4c819d820369fb2d7c1531ab3bb18e6fe8d8",
"name": "Shibarium Wrapped BONE",
"symbol": "BONE",
"chainId": 109
},
{
"decimals": 18,
"address": "0xc76f4c819d820369fb2d7c1531ab3bb18e6fe8d8",
Expand Down
15 changes: 7 additions & 8 deletions src/templates/shibarium.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ networks:
mainnet:
chainID: 109
nodeURL: https://www.shibrpc.com
tokenListType: 'FILE'
tokenListSource: 'conf/lists/shibarium_tokens_mainnet.json'
nativeCurrencySymbol: 'BONE'
tokenListType: FILE
tokenListSource: conf/lists/shibarium_tokens_mainnet.json
nativeCurrencySymbol: BONE
puppynet:
chainID: 157
nodeURL: https://puppynet.shibrpc.com
tokenListType: 'FILE'
tokenListSource: 'conf/lists/shibarium_tokens_puppynet.json'
nativeCurrencySymbol: 'BONE'

tokenListType: FILE
tokenListSource: conf/lists/shibarium_tokens_puppynet.json
nativeCurrencySymbol: BONE
manualGasPrice: 1
gasLimitTransaction: 200000
gasLimitTransaction: 300000
3 changes: 2 additions & 1 deletion src/templates/shibaswap.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# how much the execution price is allowed to move unfavorably from the trade
# execution price. It uses a rational number for precision.
allowedSlippage: '1/100'
allowedSlippage: '5/100'

# the maximum gas used to estimate cost of a traderjoe trade.
gasLimitEstimate: 300000
Expand All @@ -13,6 +13,7 @@ contractAddresses:
ethereum:
mainnet:
routerAddress: '0x03f7724180AA6b939894B5Ca4314783B0b36b329'

shibarium:
mainnet:
routerAddress: '0xEF83bbB63E8A7442E3a4a5d28d9bBf32D7c813c8'
Expand Down
Loading

0 comments on commit 32932cf

Please sign in to comment.