Skip to content

Commit 32932cf

Browse files
committed
Added fetching token taxes for ShibaSwap + ChewySwap fixes
1 parent 0475e22 commit 32932cf

File tree

10 files changed

+262
-219
lines changed

10 files changed

+262
-219
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"dependencies": {
2626
"@balancer-labs/sdk": "^1.1.5",
2727
"@bancor/carbon-sdk": "^0.0.93-DEV",
28+
"@chewyswap/swap-sdk": "^1.0.1",
2829
"@cosmjs/amino": "^0.32.2",
2930
"@cosmjs/proto-signing": "^0.31.1",
3031
"@cosmjs/stargate": "^0.31.1",
@@ -50,8 +51,7 @@
5051
"@pancakeswap/v3-sdk": "^3.7.0",
5152
"@pangolindex/sdk": "^1.1.0",
5253
"@perp/sdk-curie": "^1.16.0",
53-
"@shibaswap/sdk": "^1.1.17",
54-
"@chewyswap/sdk": "git+https://github.com/PooDoge/chewyswap-sdk#main",
54+
"@shibaswap/sdk": "^1.1.18",
5555
"@sushiswap/sdk": "^5.0.0-canary.116",
5656
"@taquito/rpc": "^17.0.0",
5757
"@taquito/signer": "^17.0.0",

src/chains/shibarium/shibarium.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Ethereumish } from '../../services/common-interfaces';
88
import { ConfigManagerV2 } from '../../services/config-manager-v2';
99
import { EVMController } from '../ethereum/evm.controllers';
1010
import { ShibaswapConfig } from '../../connectors/shibaswap/shibaswap.config';
11+
import { ChewyswapConfig } from '../../connectors/chewyswap/chewyswap.config';
1112

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

6970
getSpender(reqSpender: string): string {
7071
let spender: string;
71-
if (['shibaswap', 'chewyswap'].includes(reqSpender)) {
72+
if (reqSpender === 'chewyswap') {
73+
spender = ChewyswapConfig.config.routerAddress('shibarium', this._chain);
74+
} else if (reqSpender === 'shibaswap') {
7275
spender = ShibaswapConfig.config.routerAddress('shibarium', this._chain);
7376
} else {
7477
spender = reqSpender;

src/chains/shibarium/shibarium.validators.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export const invalidSpenderError: string =
2020
export const validateSpender: Validator = mkValidator(
2121
'spender',
2222
invalidSpenderError,
23-
(val) => typeof val === 'string' && (val === 'shibaswap' || isAddress(val)),
23+
(val) =>
24+
typeof val === 'string' &&
25+
(val === 'shibaswap' || val === 'chewyswap' || isAddress(val)),
2426
);
2527

2628
export const validateApproveRequest: RequestValidator = mkRequestValidator([

src/connectors/chewyswap/chewyswap.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import {
1212
Trade,
1313
Pair,
1414
SwapParameters,
15-
TokenAmount,
16-
} from '@chewyswap/sdk';
15+
} from '@chewyswap/swap-sdk';
1716
import IUniswapV2Pair from '@uniswap/v2-core/build/IUniswapV2Pair.json';
1817
import { ExpectedTrade, Uniswapish } from '../../services/common-interfaces';
1918
import { Shibarium } from '../../chains/shibarium/shibarium';
@@ -156,8 +155,8 @@ export class Chewyswap implements Uniswapish {
156155
? [reserves0, reserves1]
157156
: [reserves1, reserves0];
158157
const pair = new Pair(
159-
new TokenAmount(baseToken, balances[0]),
160-
new TokenAmount(quoteToken, balances[1]),
158+
CurrencyAmount.fromRawAmount(baseToken, balances[0]),
159+
CurrencyAmount.fromRawAmount(quoteToken, balances[1]),
161160
);
162161
return pair;
163162
}
@@ -178,7 +177,7 @@ export class Chewyswap implements Uniswapish {
178177
quoteToken: Token,
179178
amount: BigNumber,
180179
): Promise<ExpectedTrade> {
181-
const nativeTokenAmount: CurrencyAmount = new TokenAmount(
180+
const nativeTokenAmount = CurrencyAmount.fromRawAmount(
182181
baseToken,
183182
amount.toString(),
184183
);
@@ -189,7 +188,7 @@ export class Chewyswap implements Uniswapish {
189188

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

192-
const trades: Trade[] = Trade.bestTradeExactIn(
191+
const trades = Trade.bestTradeExactIn(
193192
[pair],
194193
nativeTokenAmount,
195194
quoteToken,
@@ -218,14 +217,14 @@ export class Chewyswap implements Uniswapish {
218217
baseToken: Token,
219218
amount: BigNumber,
220219
): Promise<ExpectedTrade> {
221-
const nativeTokenAmount: CurrencyAmount = new TokenAmount(
220+
const nativeTokenAmount = CurrencyAmount.fromRawAmount(
222221
baseToken,
223222
amount.toString(),
224223
);
225224

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

228-
const trades: Trade[] = Trade.bestTradeExactOut(
227+
const trades = Trade.bestTradeExactOut(
229228
[pair],
230229
quoteToken,
231230
nativeTokenAmount,
@@ -267,7 +266,7 @@ export class Chewyswap implements Uniswapish {
267266

268267
async executeTrade(
269268
wallet: Wallet,
270-
trade: Trade,
269+
trade: any,
271270
gasPrice: number,
272271
sushswapRouter: string,
273272
ttl: number,

src/connectors/shibaswap/shibaswap.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Transaction,
2525
Contract,
2626
ContractTransaction,
27+
ethers,
2728
} from 'ethers';
2829
import { percentRegexp } from '../../services/config-manager-v2';
2930
import { logger } from '../../services/logger';
@@ -216,6 +217,7 @@ export class Shibaswap implements Uniswapish {
216217

217218
return { trade: trades[0], expectedAmount };
218219
}
220+
219221
async estimateBuyTrade(
220222
quoteToken: Token,
221223
baseToken: Token,
@@ -253,39 +255,67 @@ export class Shibaswap implements Uniswapish {
253255
return { trade: trades[0], expectedAmount };
254256
}
255257

258+
async isFeeOnTransfer(trade: Trade, wallet: Wallet): Promise<boolean> {
259+
const token: any = trade.inputAmount.currency;
260+
261+
// We need request taxes info from the token contract and if the token has a transfer tax, we return true
262+
const TOKEN_ABI = [
263+
'function taxes() view returns (uint16 buy, uint16 sell, address feeReceiver)',
264+
];
265+
266+
try {
267+
const tokenContract = new ethers.Contract(
268+
token.address,
269+
TOKEN_ABI,
270+
wallet,
271+
);
272+
const { buy, sell, feeReceiver } = await tokenContract.taxes();
273+
274+
logger.warn(`Token taxes: Buy ${buy / 100}%, Sell ${sell / 100}%`);
275+
logger.warn(`Fee receiver: ${feeReceiver}`);
276+
277+
return sell > 0 || buy > 0;
278+
} catch (error) {
279+
// Ignore errors and return false
280+
}
281+
282+
return false;
283+
}
284+
256285
/**
257286
* Given a wallet and a Uniswap trade, try to execute it on blockchain.
258287
*
259288
* @param wallet Wallet
260289
* @param trade Expected trade
261290
* @param gasPrice Base gas price, for pre-EIP1559 transactions
262-
* @param sushswapRouter Router smart contract address
291+
* @param routerAddress Router smart contract address
263292
* @param ttl How long the swap is valid before expiry, in seconds
264293
* @param abi Router contract ABI
265294
* @param gasLimit Gas limit
266295
* @param nonce (Optional) EVM transaction nonce
267296
* @param maxFeePerGas (Optional) Maximum total fee per gas you want to pay
268297
* @param maxPriorityFeePerGas (Optional) Maximum tip per gas you want to pay
269298
*/
270-
271299
async executeTrade(
272300
wallet: Wallet,
273301
trade: Trade,
274302
gasPrice: number,
275-
sushswapRouter: string,
303+
routerAddress: string,
276304
ttl: number,
277305
abi: ContractInterface,
278306
gasLimit: number,
279307
nonce?: number,
280308
maxFeePerGas?: BigNumber,
281309
maxPriorityFeePerGas?: BigNumber,
282310
): Promise<Transaction> {
311+
const feeOnTransfer = await this.isFeeOnTransfer(trade, wallet);
283312
const result: SwapParameters = Router.swapCallParameters(trade, {
313+
feeOnTransfer,
284314
ttl,
285315
recipient: wallet.address,
286316
allowedSlippage: this.getSlippagePercentage(),
287317
});
288-
const contract: Contract = new Contract(sushswapRouter, abi, wallet);
318+
const contract: Contract = new Contract(routerAddress, abi, wallet);
289319
return this.chain.nonceManager.provideNonce(
290320
nonce,
291321
wallet.address,
@@ -308,7 +338,6 @@ export class Shibaswap implements Uniswapish {
308338
});
309339
}
310340

311-
logger.info(JSON.stringify(tx));
312341
return tx;
313342
},
314343
);

src/services/common-interfaces.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ import {
4141
import {
4242
Trade as ChewyswapTrade,
4343
Token as ChewyswapToken,
44+
TradeType as ChewyswapTradeType,
45+
Currency as ChewyswapCurrency,
4446
CurrencyAmount as ChewyswapCurrencyAmount,
4547
Fraction as ChewyswapFraction,
46-
} from '@chewyswap/sdk';
48+
} from '@chewyswap/swap-sdk';
4749
import {
4850
Trade as ShibaswapTrade,
4951
Token as ShibaswapToken,
@@ -162,7 +164,7 @@ export type UniswapishTrade =
162164
| TradeQuickswap
163165
| TradeTraderjoe
164166
| ShibaswapTrade
165-
| ChewyswapTrade
167+
| ChewyswapTrade<ChewyswapCurrency, ChewyswapCurrency, ChewyswapTradeType>
166168
| SushiswapTrade<SushiToken, SushiToken, SushiTradeType>
167169
| TradeUniswap
168170
| PancakeSwapTrade<
@@ -199,7 +201,7 @@ export type UniswapishAmount =
199201
| UniswapCoreCurrencyAmount<Currency>
200202
| CurrencyAmountTraderjoe
201203
| ShibaswapCurrencyAmount
202-
| ChewyswapCurrencyAmount
204+
| ChewyswapCurrencyAmount<ChewyswapCurrency | ChewyswapToken>
203205
| SushiCurrencyAmount<SushiCurrency | SushiToken>
204206
| PancakeSwapCurrencyAmount<PancakeSwapCurrency>
205207
| CurrencyAmountMMF

src/templates/lists/shibarium_tokens_mainnet.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@
295295
"symbol": "DAMN",
296296
"chainId": 109
297297
},
298+
{
299+
"decimals": 18,
300+
"address": "0xc76f4c819d820369fb2d7c1531ab3bb18e6fe8d8",
301+
"name": "Shibarium Wrapped BONE",
302+
"symbol": "BONE",
303+
"chainId": 109
304+
},
298305
{
299306
"decimals": 18,
300307
"address": "0xc76f4c819d820369fb2d7c1531ab3bb18e6fe8d8",

src/templates/shibarium.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ networks:
22
mainnet:
33
chainID: 109
44
nodeURL: https://www.shibrpc.com
5-
tokenListType: 'FILE'
6-
tokenListSource: 'conf/lists/shibarium_tokens_mainnet.json'
7-
nativeCurrencySymbol: 'BONE'
5+
tokenListType: FILE
6+
tokenListSource: conf/lists/shibarium_tokens_mainnet.json
7+
nativeCurrencySymbol: BONE
88
puppynet:
99
chainID: 157
1010
nodeURL: https://puppynet.shibrpc.com
11-
tokenListType: 'FILE'
12-
tokenListSource: 'conf/lists/shibarium_tokens_puppynet.json'
13-
nativeCurrencySymbol: 'BONE'
14-
11+
tokenListType: FILE
12+
tokenListSource: conf/lists/shibarium_tokens_puppynet.json
13+
nativeCurrencySymbol: BONE
1514
manualGasPrice: 1
16-
gasLimitTransaction: 200000
15+
gasLimitTransaction: 300000

src/templates/shibaswap.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# how much the execution price is allowed to move unfavorably from the trade
22
# execution price. It uses a rational number for precision.
3-
allowedSlippage: '1/100'
3+
allowedSlippage: '5/100'
44

55
# the maximum gas used to estimate cost of a traderjoe trade.
66
gasLimitEstimate: 300000
@@ -13,6 +13,7 @@ contractAddresses:
1313
ethereum:
1414
mainnet:
1515
routerAddress: '0x03f7724180AA6b939894B5Ca4314783B0b36b329'
16+
1617
shibarium:
1718
mainnet:
1819
routerAddress: '0xEF83bbB63E8A7442E3a4a5d28d9bBf32D7c813c8'

0 commit comments

Comments
 (0)