Skip to content

Commit

Permalink
bug fix on approve
Browse files Browse the repository at this point in the history
  • Loading branch information
dennohpeter committed Oct 17, 2021
1 parent 7f49139 commit 50dbccf
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export const config = {
SELL: 2
},
SLIPPAGE: 0.5,
GAS_LIMIT: process.env.GAS_LIMIT!,
EXPLORER: process.env.EXPLORER || 'https://etherscan.io/',

PRICE_CHECK_INTERVAL_IN_SECONDS: process.env.PRICE_CHECK_INTERVAL_IN_SECONDS || 45,
ETH_IN_AMOUNT: parseFloat(process.env.ETH_IN_AMOUNT!),
DB_URL: process.env.DB_URL!
Expand Down
13 changes: 7 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const Main = async () => {
console.log(`Buy Tx Data:`, txData);

// send a buy Tx
nonce++;
nonce += 1;
oneInch.sendTx({
data: txData.tx,
nonce
Expand All @@ -156,13 +156,13 @@ const Main = async () => {
* Approve Token if it has not been approved before and save it to db
*/
// approve if token has not been approved
const token_is_approved = await Approve.exists({ token: token, by: process.env.PUBLIC_KEY!.toLowerCase() })
const token_is_approved = await Approve.exists({ token: token })
if (!token_is_approved) {
// approve if not approved
message = `Approving ${token.name}...`
sendMessage(users, message)
let txData = await oneInch.approve(token.address)
nonce++;
nonce += 1;
await oneInch.sendTx({
data: txData.tx,
nonce
Expand All @@ -182,7 +182,7 @@ const Main = async () => {
let tries = 0
let tokenBalance = '0'
while (tries < 2000) {
tokenBalance = await oneInch.balanceOf(tx.toToken.address)
tokenBalance = await oneInch.balanceOf(token.address)
if (parseInt(tokenBalance) > parseInt(new BigNumber(token_amount).multipliedBy(0.5).toString())) {
break
}
Expand All @@ -206,10 +206,11 @@ const Main = async () => {
console.log(`Sell Tx Data:`, txData);

// send the sell Tx
nonce++;
nonce += 1;
oneInch.sendTx({
data: txData.tx,
nonce
nonce,
gasLimit: config.GAS_LIMIT
}).then(async (tx: any) => {
if (tx.hash) {
console.log(`Tx for Sell:`, tx.hash)
Expand Down
7 changes: 4 additions & 3 deletions src/lib/1inch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ export class OneInch extends Aggr {
method: "GET",
url: `${this.API_URL}${config.NETWORK.ID}/approve/calldata?tokenAddress=${tokenAddress}`
})
delete data.tx.gasPrice; //ethersjs will find the gasPrice needed
delete data.tx.gas;
console.log(data)
delete data.gasPrice; //ethersjs will find the gasPrice needed
delete data.gas;

data.tx["value"] = toHex(parseInt(data.tx["value"]))
data["value"] = toHex(parseInt(data["value"]))
return data
} catch (error: any) {
throw new Error(error);
Expand Down
11 changes: 7 additions & 4 deletions src/lib/aggr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ export abstract class Aggr {
* @param nonce - wallet current nonce
* @returns Tx hash if successful else error message
*/
sendTx = async (params: { data: any, nonce: number }) => {
const { data, nonce } = params
sendTx = async (params: { data: any, gasLimit?: string, nonce: number }) => {
const { data, gasLimit, nonce } = params
try {

if (!isNaN(nonce)) {
data.nonce = nonce + 1
// if (!isNaN(nonce)) {
// data.nonce = nonce + 1
// }
if (gasLimit) {
data.gasLimit = gasLimit
}

const tx = await this.account.sendTransaction(data)
Expand Down

0 comments on commit 50dbccf

Please sign in to comment.