Skip to content

Commit

Permalink
uniswap-v2-flash-swap çevirisi tamamlandı
Browse files Browse the repository at this point in the history
  • Loading branch information
eminmtas committed Dec 2, 2023
1 parent 4fc35e8 commit 4726115
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
16 changes: 8 additions & 8 deletions src/pages/defi/uniswap-v2-flash-swap/UniswapV2FlashSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ contract UniswapV2FlashSwap is IUniswapV2Callee {

IUniswapV2Pair private immutable pair;

// For this example, store the amount to repay
// Bu örnek için, ödenecek miktarı kaydedin
uint public amountToRepay;

constructor() {
pair = IUniswapV2Pair(factory.getPair(DAI, WETH));
}

function flashSwap(uint wethAmount) external {
// Need to pass some data to trigger uniswapV2Call
// uniswapV2Call fonksiyonunu tetiklemek için birkaç veri göndermek gerekiyor
bytes memory data = abi.encode(WETH, msg.sender);

// amount0Out is DAI, amount1Out is WETH
// amount0Out DAI'yi temsil ediyor, amount1Out ise WETH'yi
pair.swap(0, wethAmount, address(this), data);
}

// This function is called by the DAI/WETH pair contract
// Bu fonksiyon DAI/WETH kontratı tarafından çağırılıyor
function uniswapV2Call(
address sender,
uint amount0,
Expand All @@ -50,17 +50,17 @@ contract UniswapV2FlashSwap is IUniswapV2Callee {

(address tokenBorrow, address caller) = abi.decode(data, (address, address));

// Your custom code would go here. For example, code to arbitrage.
// Sizin yazacağınız kod buraya yazılacak. Örneğin, arbitraj kodu.
require(tokenBorrow == WETH, "token borrow != WETH");

// about 0.3% fee, +1 to round up
// Yaklaşık yüzde 0.3 fee, +1 kısmını yumarlamak için ekliyoruz
uint fee = (amount1 * 3) / 997 + 1;
amountToRepay = amount1 + fee;

// Transfer flash swap fee from caller
// Kullanıcıdan flash swap feeyi burada transfer ediyoruz
weth.transferFrom(caller, address(this), fee);

// Repay
// Geri ödeme kısmı
weth.transfer(address(pair), amountToRepay);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract UniswapV2FlashSwapTest is Test {

function testFlashSwap() public {
weth.deposit{value: 1e18}();
// Approve flash swap fee
// Flash swap fee için harcama izni ver
weth.approve(address(uni), 1e18);

uint amountToBorrow = 10 * 1e18;
Expand Down
28 changes: 14 additions & 14 deletions src/pages/defi/uniswap-v2-flash-swap/index.html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const codes = [
},
]

const html = `<h3>Uniswap V2 Flash Swap Example</h3>
const html = `<h3>Uniswap V2 Flash Swap Örneği</h3>
<pre><code class="language-solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>
Expand All @@ -42,22 +42,22 @@ const html = `<h3>Uniswap V2 Flash Swap Example</h3>
IUniswapV2Pair <span class="hljs-keyword">private</span> <span class="hljs-keyword">immutable</span> pair;
<span class="hljs-comment">// For this example, store the amount to repay</span>
<span class="hljs-comment">// Bu örnek için, ödenecek miktarı kaydedin</span>
<span class="hljs-keyword">uint</span> <span class="hljs-keyword">public</span> amountToRepay;
<span class="hljs-function"><span class="hljs-keyword">constructor</span>(<span class="hljs-params"></span>) </span>{
pair <span class="hljs-operator">=</span> IUniswapV2Pair(factory.getPair(DAI, WETH));
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">flashSwap</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> wethAmount</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span> </span>{
<span class="hljs-comment">// Need to pass some data to trigger uniswapV2Call</span>
<span class="hljs-comment">// uniswapV2Call fonksiyonunu tetiklemek için birkaç veri göndermek gerekiyor</span>
<span class="hljs-keyword">bytes</span> <span class="hljs-keyword">memory</span> data <span class="hljs-operator">=</span> <span class="hljs-built_in">abi</span>.<span class="hljs-built_in">encode</span>(WETH, <span class="hljs-built_in">msg</span>.<span class="hljs-built_in">sender</span>);
<span class="hljs-comment">// amount0Out is DAI, amount1Out is WETH</span>
<span class="hljs-comment">// amount0Out DAI'yi temsil ediyor, amount1Out ise WETH'yi</span>
pair.swap(<span class="hljs-number">0</span>, wethAmount, <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>), data);
}
<span class="hljs-comment">// This function is called by the DAI/WETH pair contract</span>
<span class="hljs-comment">// Bu fonksiyon DAI/WETH kontratı tarafından çağırılıyor</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">uniswapV2Call</span>(<span class="hljs-params">
<span class="hljs-keyword">address</span> sender,
<span class="hljs-keyword">uint</span> amount0,
Expand All @@ -69,17 +69,17 @@ const html = `<h3>Uniswap V2 Flash Swap Example</h3>
(<span class="hljs-keyword">address</span> tokenBorrow, <span class="hljs-keyword">address</span> caller) <span class="hljs-operator">=</span> <span class="hljs-built_in">abi</span>.<span class="hljs-built_in">decode</span>(data, (<span class="hljs-keyword">address</span>, <span class="hljs-keyword">address</span>));
<span class="hljs-comment">// Your custom code would go here. For example, code to arbitrage.</span>
<span class="hljs-comment">// Sizin yazacağınız kod buraya yazılacak. Örneğin, arbitraj kodu.</span>
<span class="hljs-built_in">require</span>(tokenBorrow <span class="hljs-operator">=</span><span class="hljs-operator">=</span> WETH, <span class="hljs-string">"token borrow != WETH"</span>);
<span class="hljs-comment">// about 0.3% fee, +1 to round up</span>
<span class="hljs-comment">// Yaklaşık yüzde 0.3 fee, +1 kısmını yumarlamak için ekliyoruz</span>
<span class="hljs-keyword">uint</span> fee <span class="hljs-operator">=</span> (amount1 <span class="hljs-operator">*</span> <span class="hljs-number">3</span>) <span class="hljs-operator">/</span> <span class="hljs-number">997</span> <span class="hljs-operator">+</span> <span class="hljs-number">1</span>;
amountToRepay <span class="hljs-operator">=</span> amount1 <span class="hljs-operator">+</span> fee;
<span class="hljs-comment">// Transfer flash swap fee from caller</span>
<span class="hljs-comment">// Kullanıcıdan flash swap feeyi burada transfer ediyoruz</span>
weth.transferFrom(caller, <span class="hljs-keyword">address</span>(<span class="hljs-built_in">this</span>), fee);
<span class="hljs-comment">// Repay</span>
<span class="hljs-comment">// Geri ödeme kısmı</span>
weth.<span class="hljs-built_in">transfer</span>(<span class="hljs-keyword">address</span>(pair), amountToRepay);
}
}
Expand Down Expand Up @@ -126,9 +126,9 @@ const html = `<h3>Uniswap V2 Flash Swap Example</h3>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">withdraw</span>(<span class="hljs-params"><span class="hljs-keyword">uint</span> amount</span>) <span class="hljs-title"><span class="hljs-keyword">external</span></span></span>;
}
</code></pre><h3>Test with Foundry</h3>
</code></pre><h3>Foundry ile Test Et</h3>
<ol>
<li>Copy and paste this into <code>test</code> folder in your foundry project</li>
<li>Bu kodu kopyalayıp projenizin <code>test</code> klasörüne kaydedin</li>
</ol>
<pre><code class="language-solidity"><span class="hljs-comment">// SPDX-License-Identifier: MIT</span>
<span class="hljs-meta"><span class="hljs-keyword">pragma</span> <span class="hljs-keyword">solidity</span> ^0.8.20;</span>
Expand All @@ -151,7 +151,7 @@ const html = `<h3>Uniswap V2 Flash Swap Example</h3>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">testFlashSwap</span>(<span class="hljs-params"></span>) <span class="hljs-title"><span class="hljs-keyword">public</span></span> </span>{
weth.deposit{<span class="hljs-built_in">value</span>: <span class="hljs-number">1e18</span>}();
<span class="hljs-comment">// Approve flash swap fee</span>
<span class="hljs-comment">// Flash swap fee için harcama izni ver</span>
weth.approve(<span class="hljs-keyword">address</span>(uni), <span class="hljs-number">1e18</span>);
<span class="hljs-keyword">uint</span> amountToBorrow <span class="hljs-operator">=</span> <span class="hljs-number">10</span> <span class="hljs-operator">*</span> <span class="hljs-number">1e18</span>;
Expand All @@ -161,13 +161,13 @@ const html = `<h3>Uniswap V2 Flash Swap Example</h3>
}
}
</code></pre><ol start="2">
<li>Execute the following commands to run the test</li>
<li>Testi başlatmak için aşağıdaki komutları çalıştırın</li>
</ol>
<pre><code class="language-shell">FORK_URL=https://eth-mainnet.g.alchemy.com/v2/613t3mfjTevdrCwDl28CVvuk6wSIxRPi
forge test -vv --gas-report --fork-url $FORK_URL --match-path test/UniswapV2FlashSwap.test.sol
</code></pre><h3>Links</h3>
<p><a href="https://github.com/foundry-rs/foundry" target="__blank">Foundry</a></p>
<p><a href="https://github.com/t4sk/defi-notes" target="__blank">Uniswap V3 Foundry example</a></p>
<p><a href="https://github.com/t4sk/defi-notes" target="__blank">Uniswap V3 Foundry örneği</a></p>
`

export default html
10 changes: 5 additions & 5 deletions src/pages/defi/uniswap-v2-flash-swap/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@ description: Uniswap V2 flash swap
keywords: [defi, uniswap, v2, flash, swap, amm]
---

### Uniswap V2 Flash Swap Example
### Uniswap V2 Flash Swap Örneği

```solidity
{{{UniswapV2FlashSwap}}}
```

### Test with Foundry
### Foundry ile Test Et

1. Copy and paste this into `test` folder in your foundry project
1. Bu kodu kopyalayıp projenizin `test` klasörüne kaydedin

```solidity
{{{UniswapV2FlashSwapTest}}}
```

2. Execute the following commands to run the test
2. Testi başlatmak için aşağıdaki komutları çalıştırın

```shell
FORK_URL=https://eth-mainnet.g.alchemy.com/v2/613t3mfjTevdrCwDl28CVvuk6wSIxRPi
Expand All @@ -30,4 +30,4 @@ forge test -vv --gas-report --fork-url $FORK_URL --match-path test/UniswapV2Flas

<a href="https://github.com/foundry-rs/foundry" target="__blank">Foundry</a>

<a href="https://github.com/t4sk/defi-notes" target="__blank">Uniswap V3 Foundry example</a>
<a href="https://github.com/t4sk/defi-notes" target="__blank">Uniswap V3 Foundry örneği</a>

0 comments on commit 4726115

Please sign in to comment.