Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added min residual token transfers for A & B tokens #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions contracts/Zap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ contract Zap is Ownable, ReentrancyGuard {
@param swapTokenB Data for swap tx pool's token B - amounts, path & DEX
@param receiver LP token receiver address
@param affiliate Affiliate address
@param transferResidual Set false to save gas by donating the residual remaining after a ZapTx
@param transferResidualMinTokenA Set mimimum amount of token A transfer back any residual amount, otherwise donated to contract
@param transferResidualMinTokenB Set mimimum amount of token B transfer back any residual amount, otherwise donated to contract
@return lpBought Amount of LP tokens transferred to receiver
@return lpToken LP token address
*/
Expand All @@ -137,7 +138,8 @@ contract Zap is Ownable, ReentrancyGuard {
SwapTx calldata swapTokenB,
address receiver,
address affiliate,
bool transferResidual
uint256 transferResidualMinTokenA,
uint256 transferResidualMinTokenB
) external payable nonReentrant stopInEmergency returns (uint256 lpBought, address lpToken) {
// check if start token is the same for both paths
if (swapTokenA.path[0] != swapTokenB.path[0]) revert InvalidStartPath();
Expand All @@ -149,7 +151,8 @@ contract Zap is Ownable, ReentrancyGuard {
swapTokenA,
swapTokenB,
zap,
transferResidual
transferResidualMinTokenA,
transferResidualMinTokenB
);

if (lpBought < zap.amountLPMin) revert InsufficientMinAmount();
Expand Down Expand Up @@ -332,7 +335,8 @@ contract Zap is Ownable, ReentrancyGuard {
SwapTx calldata swapTokenA,
SwapTx calldata swapTokenB,
ZapInTx calldata zap,
bool transferResidual
uint256 transferResidualMinTokenA,
uint256 transferResidualMinTokenB
) internal returns (uint256 liquidity, address lpToken) {
// check if dex address is valid and supported
(address router, address factory) = getSupportedDEX(zap.dexIndex);
Expand All @@ -357,7 +361,8 @@ contract Zap is Ownable, ReentrancyGuard {
swapTokenA.amountMin,
swapTokenB.amountMin,
router,
transferResidual
transferResidualMinTokenA,
transferResidualMinTokenB
);
}

Expand Down Expand Up @@ -586,7 +591,8 @@ contract Zap is Ownable, ReentrancyGuard {
@param amountAMin The minimum amount of token A to receive
@param amountBMin The minimum amount of token A to receive
@param router The address of platform's router
@param transferResidual Set false to save gas by donating the residual remaining after a ZapTx
@param transferResidualMinTokenA Set mimimum amount of token A transfer back any residual amount, otherwise donated to contract
@param transferResidualMinTokenB Set mimimum amount of token B transfer back any residual amount, otherwise donated to contract
@return amountA Token A amount added to LP
@return amountB Token B amount added to LP
@return liquidity LP tokens minted
Expand All @@ -599,7 +605,8 @@ contract Zap is Ownable, ReentrancyGuard {
uint256 amountAMin,
uint256 amountBMin,
address router,
bool transferResidual
uint256 transferResidualMinTokenA,
uint256 transferResidualMinTokenB
) internal returns (uint256 amountA, uint256 amountB, uint256 liquidity) {
_approveTokenIfNeeded(tokenA, amountADesired, router);
_approveTokenIfNeeded(tokenB, amountBDesired, router);
Expand All @@ -615,16 +622,16 @@ contract Zap is Ownable, ReentrancyGuard {
deadline
);

if (transferResidual) {
// returning residue in tokenA, if any
if (amountADesired - amountA > 0) {
TransferHelper.safeTransfer(tokenA, msg.sender, (amountADesired - amountA));
}
uint256 residualTokenA = amountADesired - amountA;
// returning residue in tokenA, if any
if (residualTokenA > transferResidualMinTokenA) {
TransferHelper.safeTransfer(tokenA, msg.sender, residualTokenA);
}

// returning residue in tokenB, if any
if (amountBDesired - amountB > 0) {
TransferHelper.safeTransfer(tokenB, msg.sender, (amountBDesired - amountB));
}
uint256 residualTokenB = amountBDesired - amountB;
// returning residue in tokenB, if any
if (residualTokenB > transferResidualMinTokenB) {
TransferHelper.safeTransfer(tokenB, msg.sender, residualTokenB);
}
}

Expand Down
145 changes: 126 additions & 19 deletions test/Zap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ describe.only("Zap", function () {
{amount: zeroBN, amountMin: zeroBN, path: [WXDAI.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true
zeroBN,
zeroBN
)
).to.be.revertedWith("InvalidInputAmount()")

Expand All @@ -143,7 +144,8 @@ describe.only("Zap", function () {
{amount: zeroBN, amountMin: zeroBN, path: [AddressZero, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)
).to.be.revertedWith("InvalidInputAmount()")
Expand All @@ -155,7 +157,8 @@ describe.only("Zap", function () {
{amount: amountIn, amountMin: zeroBN, path: [WXDAI.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true
zeroBN,
zeroBN
)
).to.be.revertedWith("InvalidStartPath()")

Expand Down Expand Up @@ -293,7 +296,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [DXD.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
randomSigner.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand All @@ -319,7 +323,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [DXD.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down Expand Up @@ -350,7 +355,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path:[DXD.address] , dexIndex: dexIndex1},
impersonated.address,
randomSigner.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down Expand Up @@ -379,7 +385,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [DXD.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand All @@ -406,7 +413,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [DXD.address, WXDAI.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand All @@ -433,7 +441,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [WXDAI.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand All @@ -448,7 +457,7 @@ describe.only("Zap", function () {
.withArgs(impersonated.address, impersonated.address,WXDAI.address, totalAmount, cowWeth.address, lpBought)
})

it("zap in native currency (xdai) token to cow/weth", async function () {
it("zap in native currency (xdai) token to cow/weth with residual transfer", async function () {
const totalAmount = ethers.utils.parseEther("1")
const nativeCurrencyBalanceInit = await impersonated.getBalance()
expect(nativeCurrencyBalanceInit).to.be.above(0)
Expand All @@ -463,7 +472,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [AddressZero, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: totalAmount, gasLimit: 9999999}
)

Expand All @@ -474,12 +484,102 @@ describe.only("Zap", function () {

expect(lpBought).to.be.above(0)
expect(nativeCurrencyBalanceInit).to.be.above(nativeCurrencyBalance)


await expect(txZapIn).to.emit(zap, "ZapIn")
.withArgs(impersonated.address, impersonated.address,AddressZero, totalAmount, cowWeth.address, lpBought)

// Get transfer event data
const receipt = await ethers.provider.getTransactionReceipt(txZapIn.hash)
const interfaceEvent = new ethers.utils.Interface(["event Transfer(address indexed from, address indexed to, uint256 amount)"])

// Search WETH Transfer event
let wethTransferIndex = -1;
for (let i = 0; i < receipt.logs.length; i++) {
if (receipt.logs[i].address.toLowerCase() == WETH.address.toLowerCase()) {
// Test we got a transfer back from token A residuals
let data = receipt.logs[i].data
let topics = receipt.logs[i].topics
let event;
try {
event = interfaceEvent.decodeEventLog("Transfer", data, topics)
} catch {
continue
}

if (event.from.toLowerCase() == zap.address.toLowerCase()
&& event.to.toLowerCase() == impersonated.address.toLowerCase()) {
wethTransferIndex = i;
break;
}
}
}

if (wethTransferIndex === -1) {
expect("").to.be.equal("Missing residual token transfer")
}
})

it("zap in native currency (xdai) token to cow/weth without residual transfer", async function () {
const totalAmount = ethers.utils.parseEther("1")
const nativeCurrencyBalanceInit = await impersonated.getBalance()
expect(nativeCurrencyBalanceInit).to.be.above(0)

const lpBalanceInit = await cowWeth.balanceOf(impersonated.address)
const tokenInBalanceInit = await WXDAI.balanceOf(impersonated.address)

await WXDAI.connect(impersonated).approve(zap.address, totalAmount)
const txZapIn = await zap.connect(impersonated).zapIn(
{amountAMin: zeroBN, amountBMin: zeroBN, amountLPMin: zeroBN, dexIndex: dexIndex1},
{amount: totalAmount.div(2), amountMin: zeroBN, path:[AddressZero, WETH.address, COW.address] , dexIndex: dexIndex1},
{amount: totalAmount.div(2), amountMin: zeroBN, path: [AddressZero, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
zeroBN,
BigNumber.from("9999999999999999999999999"),
{value: totalAmount, gasLimit: 9999999}
)

const tokenInBalance = await WXDAI.balanceOf(impersonated.address)
const lpBalance = await cowWeth.balanceOf(impersonated.address)
const lpBought = lpBalance.sub(lpBalanceInit)
const nativeCurrencyBalance = await impersonated.getBalance()

expect(lpBought).to.be.above(0)
expect(nativeCurrencyBalanceInit).to.be.above(nativeCurrencyBalance)

await expect(txZapIn).to.emit(zap, "ZapIn")
.withArgs(impersonated.address, impersonated.address,AddressZero, totalAmount, cowWeth.address, lpBought)

// Get transfer event data
const receipt = await ethers.provider.getTransactionReceipt(txZapIn.hash)
const interfaceEvent = new ethers.utils.Interface(["event Transfer(address indexed from, address indexed to, uint256 amount)"])

// Search WETH Transfer event
let wethTransferIndex = -1;
for (let i = 0; i < receipt.logs.length; i++) {
if (receipt.logs[i].address.toLowerCase() == WETH.address.toLowerCase()) {
// Test we got a transfer back from token A residuals
let data = receipt.logs[i].data
let topics = receipt.logs[i].topics
let event;
try {
event = interfaceEvent.decodeEventLog("Transfer", data, topics)
} catch {
continue
}

if (event.from.toLowerCase() == zap.address.toLowerCase()
&& event.to.toLowerCase() == impersonated.address.toLowerCase()) {
wethTransferIndex = i;
break;
}
}
}

if (wethTransferIndex !== -1) {
expect("").to.be.equal("Residual token transfer done")
}
})
})

describe("Zap Out", function () {
Expand All @@ -495,7 +595,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path:[DXD.address] , dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down Expand Up @@ -545,7 +646,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [DXD.address, WXDAI.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down Expand Up @@ -593,7 +695,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [WXDAI.address, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down Expand Up @@ -644,7 +747,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [AddressZero, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: totalAmount, gasLimit: 9999999}
)

Expand Down Expand Up @@ -696,7 +800,8 @@ describe.only("Zap", function () {
{amount: totalAmount.div(2), amountMin: zeroBN, path: [AddressZero, WETH.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: totalAmount, gasLimit: 9999999}
)

Expand Down Expand Up @@ -754,7 +859,8 @@ describe.only("Zap", function () {
{amount: amountB, amountMin: zeroBN, path: [WXDAI.address, GNO.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down Expand Up @@ -786,7 +892,8 @@ describe.only("Zap", function () {
{amount: amountB, amountMin: zeroBN, path: [WXDAI.address, GNO.address], dexIndex: dexIndex1},
impersonated.address,
impersonated.address,
true,
zeroBN,
zeroBN,
{value: zeroBN, gasLimit: 9999999}
)

Expand Down