Skip to content

Instantly share code, notes, and snippets.

@webmodularity
Created April 29, 2022 01:27
Show Gist options
  • Save webmodularity/02267805cd2d272f1679175d18249a6f to your computer and use it in GitHub Desktop.
Save webmodularity/02267805cd2d272f1679175d18249a6f to your computer and use it in GitHub Desktop.
DustSweeper V2 taker sweepDust implementation
const { ethers } = require("ethers");
const axios = require('axios').default;
require('dotenv').config();
/////////////////////////
// EDIT THESE VALUES //
/////////////////////////
const makers = [
"0xFA62E6eFEc39b4Dc5E3f37D676F18B560f7dD458"
];
const tokenAddresses = [
"0xDe30da39c46104798bB5aA3fe8B9e0e1F348163F"
];
const providerUrl = `https://mainnet.infura.io/v3/${process.env.INFURA_ID}`; // Replace with your provider info
const privateKey = process.env.PK;// Replace with your private key
/////////////////////////
// END EDIT VALUES //
/////////////////////////
const priceApiUrl = "https://api.paymagic.xyz/v1/utils/fetchTrustedPrices";
const dustSweeperAddress = "0x78106f7db3EbCEe3D2CFAC647f0E4c9b06683B39";
async function main() {
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const wallet = new ethers.Wallet(privateKey, provider);
const dustSweeperContract = new ethers.Contract(dustSweeperAddress, getDustSweeperAbi(), wallet);
let apiResponse;
try {
apiResponse = await axios.post(priceApiUrl, {"tokenAddresses": [...new Set(tokenAddresses)]});
} catch (err) {
throw new Error(`Fetch price API fatal error: ${err.response.data.error}`);
}
const prices = apiResponse.data.data;
const packet = apiResponse.data.packet;
// Calculate ETH send amount
let totalEthSend = ethers.BigNumber.from(0);
const protocolPercent = await dustSweeperContract.protocolFee();
const powPercent = ethers.BigNumber.from("10").pow(4);
for (let x = 0;x < makers.length;x++) {
const tokenContract = new ethers.Contract(tokenAddresses[x], getErc20Abi(), provider);
const decimals = await tokenContract.decimals();
const allowance = await tokenContract.allowance(makers[x], dustSweeperContract.address);
const balance = await tokenContract.balanceOf(makers[x]);
const amount = balance < allowance ? balance : allowance;
const powDecimals = ethers.BigNumber.from("10").pow(decimals);
const quotePrice = ethers.BigNumber.from(prices.pricesWei[prices.tokenArray.indexOf(tokenAddresses[x])]);
const totalPrice = quotePrice.mul(ethers.BigNumber.from(amount)).div(powDecimals);
const takerDiscountTier = await dustSweeperContract.getTokenTakerDiscountTier(tokenAddresses[x]);
const takerPercent = await dustSweeperContract.takerDiscountTiers(takerDiscountTier);
const discountedPrice = totalPrice.mul(powPercent.sub(takerPercent)).div(powPercent);
const protocolTotal = totalPrice.mul(protocolPercent).div(powPercent);
totalEthSend = totalEthSend.add(discountedPrice).add(protocolTotal);
}
const sweepTx = await dustSweeperContract.sweepDust(
makers,
tokenAddresses,
packet,
{
value: totalEthSend
}
);
const sweepReceipt = await sweepTx.wait();
console.log("dustSweep txHash: " + sweepReceipt.transactionHash);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
function getDustSweeperAbi() {
return [
{
"inputs": [
{
"internalType": "address payable",
"name": "_protocolWallet",
"type": "address"
},
{
"internalType": "address payable",
"name": "_governorWallet",
"type": "address"
},
{
"internalType": "uint256[]",
"name": "_takerDiscountTiers",
"type": "uint256[]"
},
{
"internalType": "uint256",
"name": "_protocolFeePercent",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_protocolPayoutSplitPercent",
"type": "uint256"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "sendAmount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "remainingBalance",
"type": "uint256"
}
],
"name": "InsufficientNative",
"type": "error"
},
{
"inputs": [],
"name": "NoBalance",
"type": "error"
},
{
"inputs": [],
"name": "NoSweepableOrders",
"type": "error"
},
{
"inputs": [
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
}
],
"name": "NoTokenPrice",
"type": "error"
},
{
"inputs": [],
"name": "NotContract",
"type": "error"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "param",
"type": "uint256"
}
],
"name": "OutOfRange",
"type": "error"
},
{
"inputs": [],
"name": "Trustus__InvalidPacket",
"type": "error"
},
{
"inputs": [],
"name": "ZeroAddress",
"type": "error"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "previousOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnershipTransferred",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "protocolSplit",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "governorSplit",
"type": "uint256"
}
],
"name": "ProtocolPayout",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "makerAddress",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "tokenAddress",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "tokenAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "ethAmount",
"type": "uint256"
}
],
"name": "Sweep",
"type": "event"
},
{
"stateMutability": "payable",
"type": "fallback"
},
{
"inputs": [],
"name": "DOMAIN_SEPARATOR",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MAX_PROTOCOL_FEE_PCT",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MAX_PROTOCOL_PAYOUT_SPLIT_PCT",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MAX_SWEEP_ORDER_SIZE",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MAX_TAKER_DISCOUNT_PCT",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "MIN_OVERAGE_RETURN_WEI",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "TRUSTUS_REQUEST_VALUE",
"outputs": [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "destinations",
"outputs": [
{
"internalType": "address payable",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_trustedProviderAddress",
"type": "address"
}
],
"name": "getIsTrusted",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenAddress",
"type": "address"
}
],
"name": "getTokenDecimals",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenAddress",
"type": "address"
}
],
"name": "getTokenTakerDiscountTier",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "governorWallet",
"outputs": [
{
"internalType": "address payable",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "owner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "payoutProtocolFees",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "protocolFee",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "protocolPayoutSplit",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "protocolWallet",
"outputs": [
{
"internalType": "address payable",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "renounceOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_destinationAddress",
"type": "address"
}
],
"name": "setDestinationAddress",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address payable",
"name": "_governorWallet",
"type": "address"
}
],
"name": "setGovernorWallet",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_protocolFeePercent",
"type": "uint256"
}
],
"name": "setProtocolFeePercent",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_protocolPayoutSplitPercent",
"type": "uint256"
}
],
"name": "setProtocolPayoutSplit",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address payable",
"name": "_protocolWallet",
"type": "address"
}
],
"name": "setProtocolWallet",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_takerDiscountPercent",
"type": "uint256"
},
{
"internalType": "uint8",
"name": "_tier",
"type": "uint8"
}
],
"name": "setTakerDiscountPercent",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenAddress",
"type": "address"
},
{
"internalType": "uint8",
"name": "_decimals",
"type": "uint8"
}
],
"name": "setTokenDecimals",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenAddress",
"type": "address"
},
{
"internalType": "uint8",
"name": "_tier",
"type": "uint8"
}
],
"name": "setTokenTakerDiscountTier",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenAddress",
"type": "address"
}
],
"name": "setupToken",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "makers",
"type": "address[]"
},
{
"internalType": "address[]",
"name": "tokenAddresses",
"type": "address[]"
},
{
"components": [
{
"internalType": "uint8",
"name": "v",
"type": "uint8"
},
{
"internalType": "bytes32",
"name": "r",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "s",
"type": "bytes32"
},
{
"internalType": "bytes32",
"name": "request",
"type": "bytes32"
},
{
"internalType": "uint256",
"name": "deadline",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "payload",
"type": "bytes"
}
],
"internalType": "struct Trustus.TrustusPacket",
"name": "packet",
"type": "tuple"
}
],
"name": "sweepDust",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "sweepWhitelist",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "sweepWhitelistOn",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"name": "takerDiscountTiers",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_trustedProviderAddress",
"type": "address"
}
],
"name": "toggleIsTrusted",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "toggleSweepWhitelist",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_whitelistAddress",
"type": "address"
}
],
"name": "toggleSweepWhitelistAddress",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "transferOwnership",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_tokenAddress",
"type": "address"
}
],
"name": "withdrawToken",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"stateMutability": "payable",
"type": "receive"
}
];
}
function getErc20Abi() {
return [
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_spender",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "approve",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_from",
"type": "address"
},
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transferFrom",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
}
],
"name": "balanceOf",
"outputs": [
{
"name": "balance",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "_owner",
"type": "address"
},
{
"name": "_spender",
"type": "address"
}
],
"name": "allowance",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"payable": true,
"stateMutability": "payable",
"type": "fallback"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "owner",
"type": "address"
},
{
"indexed": true,
"name": "spender",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Approval",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"name": "from",
"type": "address"
},
{
"indexed": true,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "value",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment