Last active
June 15, 2022 12:08
-
-
Save depresto/6a8f5f7adb848d13a035191d016b6e29 to your computer and use it in GitHub Desktop.
Uniswap V1 interaction for https://playground.ethers.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const uniSwapV1FactoryAddress = "0xc0a47dFe034B400B47bDaD5FecDa2621de6c4d95"; | |
const usdcAddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; | |
const factoryABI = [ | |
{ | |
name: "NewExchange", | |
inputs: [ | |
{ type: "address", name: "token", indexed: true }, | |
{ type: "address", name: "exchange", indexed: true }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "initializeFactory", | |
outputs: [], | |
inputs: [{ type: "address", name: "template" }], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 35725, | |
}, | |
{ | |
name: "createExchange", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [{ type: "address", name: "token" }], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 187911, | |
}, | |
{ | |
name: "getExchange", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [{ type: "address", name: "token" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 715, | |
}, | |
{ | |
name: "getToken", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [{ type: "address", name: "exchange" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 745, | |
}, | |
{ | |
name: "getTokenWithId", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [{ type: "uint256", name: "token_id" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 736, | |
}, | |
{ | |
name: "exchangeTemplate", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 633, | |
}, | |
{ | |
name: "tokenCount", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 663, | |
}, | |
]; | |
const exchangeABI = [ | |
{ | |
name: "TokenPurchase", | |
inputs: [ | |
{ type: "address", name: "buyer", indexed: true }, | |
{ type: "uint256", name: "eth_sold", indexed: true }, | |
{ type: "uint256", name: "tokens_bought", indexed: true }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "EthPurchase", | |
inputs: [ | |
{ type: "address", name: "buyer", indexed: true }, | |
{ type: "uint256", name: "tokens_sold", indexed: true }, | |
{ type: "uint256", name: "eth_bought", indexed: true }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "AddLiquidity", | |
inputs: [ | |
{ type: "address", name: "provider", indexed: true }, | |
{ type: "uint256", name: "eth_amount", indexed: true }, | |
{ type: "uint256", name: "token_amount", indexed: true }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "RemoveLiquidity", | |
inputs: [ | |
{ type: "address", name: "provider", indexed: true }, | |
{ type: "uint256", name: "eth_amount", indexed: true }, | |
{ type: "uint256", name: "token_amount", indexed: true }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "Transfer", | |
inputs: [ | |
{ type: "address", name: "_from", indexed: true }, | |
{ type: "address", name: "_to", indexed: true }, | |
{ type: "uint256", name: "_value", indexed: false }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "Approval", | |
inputs: [ | |
{ type: "address", name: "_owner", indexed: true }, | |
{ type: "address", name: "_spender", indexed: true }, | |
{ type: "uint256", name: "_value", indexed: false }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "setup", | |
outputs: [], | |
inputs: [{ type: "address", name: "token_addr" }], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 175875, | |
}, | |
{ | |
name: "addLiquidity", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "min_liquidity" }, | |
{ type: "uint256", name: "max_tokens" }, | |
{ type: "uint256", name: "deadline" }, | |
], | |
constant: false, | |
payable: true, | |
type: "function", | |
gas: 82605, | |
}, | |
{ | |
name: "removeLiquidity", | |
outputs: [ | |
{ type: "uint256", name: "out" }, | |
{ type: "uint256", name: "out" }, | |
], | |
inputs: [ | |
{ type: "uint256", name: "amount" }, | |
{ type: "uint256", name: "min_eth" }, | |
{ type: "uint256", name: "min_tokens" }, | |
{ type: "uint256", name: "deadline" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 116814, | |
}, | |
{ | |
name: "__default__", | |
outputs: [], | |
inputs: [], | |
constant: false, | |
payable: true, | |
type: "function", | |
}, | |
{ | |
name: "ethToTokenSwapInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "min_tokens" }, | |
{ type: "uint256", name: "deadline" }, | |
], | |
constant: false, | |
payable: true, | |
type: "function", | |
gas: 12757, | |
}, | |
{ | |
name: "ethToTokenTransferInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "min_tokens" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
], | |
constant: false, | |
payable: true, | |
type: "function", | |
gas: 12965, | |
}, | |
{ | |
name: "ethToTokenSwapOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_bought" }, | |
{ type: "uint256", name: "deadline" }, | |
], | |
constant: false, | |
payable: true, | |
type: "function", | |
gas: 50455, | |
}, | |
{ | |
name: "ethToTokenTransferOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_bought" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
], | |
constant: false, | |
payable: true, | |
type: "function", | |
gas: 50663, | |
}, | |
{ | |
name: "tokenToEthSwapInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_sold" }, | |
{ type: "uint256", name: "min_eth" }, | |
{ type: "uint256", name: "deadline" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 47503, | |
}, | |
{ | |
name: "tokenToEthTransferInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_sold" }, | |
{ type: "uint256", name: "min_eth" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 47712, | |
}, | |
{ | |
name: "tokenToEthSwapOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "eth_bought" }, | |
{ type: "uint256", name: "max_tokens" }, | |
{ type: "uint256", name: "deadline" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 50175, | |
}, | |
{ | |
name: "tokenToEthTransferOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "eth_bought" }, | |
{ type: "uint256", name: "max_tokens" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 50384, | |
}, | |
{ | |
name: "tokenToTokenSwapInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_sold" }, | |
{ type: "uint256", name: "min_tokens_bought" }, | |
{ type: "uint256", name: "min_eth_bought" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "token_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 51007, | |
}, | |
{ | |
name: "tokenToTokenTransferInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_sold" }, | |
{ type: "uint256", name: "min_tokens_bought" }, | |
{ type: "uint256", name: "min_eth_bought" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
{ type: "address", name: "token_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 51098, | |
}, | |
{ | |
name: "tokenToTokenSwapOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_bought" }, | |
{ type: "uint256", name: "max_tokens_sold" }, | |
{ type: "uint256", name: "max_eth_sold" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "token_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 54928, | |
}, | |
{ | |
name: "tokenToTokenTransferOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_bought" }, | |
{ type: "uint256", name: "max_tokens_sold" }, | |
{ type: "uint256", name: "max_eth_sold" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
{ type: "address", name: "token_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 55019, | |
}, | |
{ | |
name: "tokenToExchangeSwapInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_sold" }, | |
{ type: "uint256", name: "min_tokens_bought" }, | |
{ type: "uint256", name: "min_eth_bought" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "exchange_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 49342, | |
}, | |
{ | |
name: "tokenToExchangeTransferInput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_sold" }, | |
{ type: "uint256", name: "min_tokens_bought" }, | |
{ type: "uint256", name: "min_eth_bought" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
{ type: "address", name: "exchange_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 49532, | |
}, | |
{ | |
name: "tokenToExchangeSwapOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_bought" }, | |
{ type: "uint256", name: "max_tokens_sold" }, | |
{ type: "uint256", name: "max_eth_sold" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "exchange_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 53233, | |
}, | |
{ | |
name: "tokenToExchangeTransferOutput", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "uint256", name: "tokens_bought" }, | |
{ type: "uint256", name: "max_tokens_sold" }, | |
{ type: "uint256", name: "max_eth_sold" }, | |
{ type: "uint256", name: "deadline" }, | |
{ type: "address", name: "recipient" }, | |
{ type: "address", name: "exchange_addr" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 53423, | |
}, | |
{ | |
name: "getEthToTokenInputPrice", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [{ type: "uint256", name: "eth_sold" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 5542, | |
}, | |
{ | |
name: "getEthToTokenOutputPrice", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [{ type: "uint256", name: "tokens_bought" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 6872, | |
}, | |
{ | |
name: "getTokenToEthInputPrice", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [{ type: "uint256", name: "tokens_sold" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 5637, | |
}, | |
{ | |
name: "getTokenToEthOutputPrice", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [{ type: "uint256", name: "eth_bought" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 6897, | |
}, | |
{ | |
name: "tokenAddress", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1413, | |
}, | |
{ | |
name: "factoryAddress", | |
outputs: [{ type: "address", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1443, | |
}, | |
{ | |
name: "balanceOf", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [{ type: "address", name: "_owner" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1645, | |
}, | |
{ | |
name: "transfer", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_to" }, | |
{ type: "uint256", name: "_value" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 75034, | |
}, | |
{ | |
name: "transferFrom", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_from" }, | |
{ type: "address", name: "_to" }, | |
{ type: "uint256", name: "_value" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 110907, | |
}, | |
{ | |
name: "approve", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_spender" }, | |
{ type: "uint256", name: "_value" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 38769, | |
}, | |
{ | |
name: "allowance", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_owner" }, | |
{ type: "address", name: "_spender" }, | |
], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1925, | |
}, | |
{ | |
name: "name", | |
outputs: [{ type: "bytes32", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1623, | |
}, | |
{ | |
name: "symbol", | |
outputs: [{ type: "bytes32", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1653, | |
}, | |
{ | |
name: "decimals", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1683, | |
}, | |
{ | |
name: "totalSupply", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1713, | |
}, | |
]; | |
const tokenABI = [ | |
{ | |
name: "Transfer", | |
inputs: [ | |
{ type: "address", name: "_from", indexed: true }, | |
{ type: "address", name: "_to", indexed: true }, | |
{ type: "uint256", name: "_value", indexed: false }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "Approval", | |
inputs: [ | |
{ type: "address", name: "_owner", indexed: true }, | |
{ type: "address", name: "_spender", indexed: true }, | |
{ type: "uint256", name: "_value", indexed: false }, | |
], | |
anonymous: false, | |
type: "event", | |
}, | |
{ | |
name: "__init__", | |
outputs: [], | |
inputs: [ | |
{ type: "bytes32", name: "_name" }, | |
{ type: "bytes32", name: "_symbol" }, | |
{ type: "uint256", name: "_decimals" }, | |
{ type: "uint256", name: "_supply" }, | |
], | |
constant: false, | |
payable: false, | |
type: "constructor", | |
}, | |
{ | |
name: "deposit", | |
outputs: [], | |
inputs: [], | |
constant: false, | |
payable: true, | |
type: "function", | |
gas: 74279, | |
}, | |
{ | |
name: "withdraw", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [{ type: "uint256", name: "_value" }], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 108706, | |
}, | |
{ | |
name: "totalSupply", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 543, | |
}, | |
{ | |
name: "balanceOf", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [{ type: "address", name: "_owner" }], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 745, | |
}, | |
{ | |
name: "transfer", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_to" }, | |
{ type: "uint256", name: "_value" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 74698, | |
}, | |
{ | |
name: "transferFrom", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_from" }, | |
{ type: "address", name: "_to" }, | |
{ type: "uint256", name: "_value" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 110600, | |
}, | |
{ | |
name: "approve", | |
outputs: [{ type: "bool", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_spender" }, | |
{ type: "uint256", name: "_value" }, | |
], | |
constant: false, | |
payable: false, | |
type: "function", | |
gas: 37888, | |
}, | |
{ | |
name: "allowance", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [ | |
{ type: "address", name: "_owner" }, | |
{ type: "address", name: "_spender" }, | |
], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 1025, | |
}, | |
{ | |
name: "name", | |
outputs: [{ type: "bytes32", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 723, | |
}, | |
{ | |
name: "symbol", | |
outputs: [{ type: "bytes32", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 753, | |
}, | |
{ | |
name: "decimals", | |
outputs: [{ type: "uint256", name: "out" }], | |
inputs: [], | |
constant: true, | |
payable: false, | |
type: "function", | |
gas: 783, | |
}, | |
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const gasLimit = 100_000 | |
window.ethereum.request({ method: 'eth_requestAccounts' }) | |
const provider = new ethers.providers.Web3Provider(window.ethereum); | |
const signer = provider.getSigner(); | |
const walletAddress = await signer.getAddress(); | |
// Create contract instance for Uniswap V1 factory | |
const factoryContract = new ethers.Contract( | |
uniSwapV1FactoryAddress, | |
factoryABI, | |
provider, | |
{ gasLimit } | |
); | |
// Create contract instance for USDC token | |
const tokenContract = new ethers.Contract(usdcAddress, tokenABI, provider, { gasLimit }); | |
// Create contract address for USDC swap | |
const exchangeAddress = await factoryContract.getExchange(usdcAddress, { gasLimit }); | |
// Create contract instance for Uniswap V1 USDC swap | |
const exchangeContract = new ethers.Contract( | |
exchangeAddress, | |
exchangeABI, | |
provider, | |
{ gasLimit } | |
); | |
const now = Math.floor(new Date().getTime() / 1000) | |
// Swap for 100 USDC | |
await exchangeContract.connect(signer).ethToTokenSwapInput(100, now + 10, { | |
value: ethers.utils.parseEther("0.2"), | |
gasLimit, | |
}); | |
// Get balance for USDC | |
const usdcBalance = await tokenContract.balanceOf(walletAddress, { gasLimit }); | |
const decimals = await tokenContract.decimals({ gasLimit }); | |
const formattedBalance = ethers.utils.formatUnits(usdcBalance, decimals) | |
console.log(`Your USDC balance: ${formattedBalance}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment