mirror of
https://github.com/Instadapp/dsa-polygon-migration.git
synced 2024-07-29 22:27:58 +00:00
Fix stack too deep error
This commit is contained in:
parent
48fe904e09
commit
ab84d537ec
|
@ -1,4 +1,5 @@
|
||||||
pragma solidity >=0.7.0;
|
pragma solidity >=0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
import { DSMath } from "../../common/math.sol";
|
import { DSMath } from "../../common/math.sol";
|
||||||
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
|
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
|
||||||
|
@ -17,6 +18,25 @@ import {
|
||||||
abstract contract Helpers is Stores, DSMath, Variables {
|
abstract contract Helpers is Stores, DSMath, Variables {
|
||||||
using SafeERC20 for IERC20;
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
|
struct TransferHelperData {
|
||||||
|
address token;
|
||||||
|
address atoken;
|
||||||
|
uint atokenBal;
|
||||||
|
uint supplyAmt;
|
||||||
|
uint flashAmt;
|
||||||
|
uint tokenLiq;
|
||||||
|
bool isFlash;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SpellHelperData {
|
||||||
|
address token;
|
||||||
|
address atoken;
|
||||||
|
uint tokenLiq;
|
||||||
|
uint borrowAmt;
|
||||||
|
uint flashAmt;
|
||||||
|
bool isFlash;
|
||||||
|
}
|
||||||
|
|
||||||
function remapTokens(AaveData memory data) internal view returns (AaveData memory) {
|
function remapTokens(AaveData memory data) internal view returns (AaveData memory) {
|
||||||
for (uint i = 0; i < data.supplyTokens.length; i++) {
|
for (uint i = 0; i < data.supplyTokens.length; i++) {
|
||||||
data.supplyTokens[i] = tokenMapping.getMapping(data.supplyTokens[i]);
|
data.supplyTokens[i] = tokenMapping.getMapping(data.supplyTokens[i]);
|
||||||
|
@ -39,69 +59,67 @@ abstract contract Helpers is Stores, DSMath, Variables {
|
||||||
|
|
||||||
function transferAtokens(AaveInterface aave, address dsa, address[] memory supplyTokens, uint[] memory supplyAmts) internal {
|
function transferAtokens(AaveInterface aave, address dsa, address[] memory supplyTokens, uint[] memory supplyAmts) internal {
|
||||||
for (uint i = 0; i < supplyTokens.length; i++) {
|
for (uint i = 0; i < supplyTokens.length; i++) {
|
||||||
address _token = supplyTokens[i] == maticAddr ? wmaticAddr : supplyTokens[i];
|
TransferHelperData memory data;
|
||||||
(address _aToken, ,) = aaveData.getReserveTokensAddresses(_token);
|
data.token = supplyTokens[i] == maticAddr ? wmaticAddr : supplyTokens[i];
|
||||||
IERC20 _atokenContract = IERC20(_aToken);
|
(data.atoken, ,) = aaveData.getReserveTokensAddresses(data.token);
|
||||||
uint _atokenBal = _atokenContract.balanceOf(address(this));
|
IERC20 _atokenContract = IERC20(data.atoken);
|
||||||
uint _supplyAmt = supplyAmts[i];
|
data.atokenBal = _atokenContract.balanceOf(address(this));
|
||||||
bool isFlash;
|
data.supplyAmt = supplyAmts[i];
|
||||||
uint _flashAmt;
|
|
||||||
|
|
||||||
(uint tokenLiq,,,,,,,,,) = aaveData.getReserveData(_token);
|
(data.tokenLiq,,,,,,,,,) = aaveData.getReserveData(data.token);
|
||||||
|
|
||||||
if (_atokenBal < _supplyAmt) {
|
if (data.atokenBal < data.supplyAmt) {
|
||||||
uint _reqAmt = _supplyAmt - _atokenBal;
|
uint _reqAmt = data.supplyAmt - data.atokenBal;
|
||||||
if (tokenLiq < _reqAmt) {
|
if (data.tokenLiq < _reqAmt) {
|
||||||
_flashAmt = flashAmts[_token];
|
data.flashAmt = flashAmts[data.token];
|
||||||
if (_flashAmt > 0) {
|
if (data.flashAmt > 0) {
|
||||||
aave.deposit(_token, _flashAmt, address(this), 3288); // TODO: what is our ID on Polygon?
|
aave.deposit(data.token, data.flashAmt, address(this), 3288); // TODO: what is our ID on Polygon?
|
||||||
tokenLiq += _flashAmt;
|
data.tokenLiq += data.flashAmt;
|
||||||
isFlash = true;
|
data.isFlash = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint num = _reqAmt/tokenLiq + 1; // TODO: Is this right
|
uint num = _reqAmt/data.tokenLiq + 1; // TODO: Is this right
|
||||||
uint splitAmt = _reqAmt/num; // TODO: Check decimal
|
uint splitAmt = _reqAmt/num; // TODO: Check decimal
|
||||||
uint finalSplit = _reqAmt - (splitAmt * (num - 1)); // TODO: to resolve upper decimal error
|
uint finalSplit = _reqAmt - (splitAmt * (num - 1)); // TODO: to resolve upper decimal error
|
||||||
|
|
||||||
for (uint j = 0; j < num; j++) {
|
for (uint j = 0; j < num; j++) {
|
||||||
if (j < num - 1) {
|
if (j < num - 1) {
|
||||||
aave.borrow(_token, splitAmt, 2, 3288, address(this));
|
aave.borrow(data.token, splitAmt, 2, 3288, address(this));
|
||||||
aave.deposit(_token, splitAmt, address(this), 3288);
|
aave.deposit(data.token, splitAmt, address(this), 3288);
|
||||||
} else {
|
} else {
|
||||||
aave.borrow(_token, finalSplit, 2, 3288, address(this));
|
aave.borrow(data.token, finalSplit, 2, 3288, address(this));
|
||||||
aave.deposit(_token, finalSplit, address(this), 3288);
|
aave.deposit(data.token, finalSplit, address(this), 3288);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFlash) {
|
if (data.isFlash) {
|
||||||
aave.withdraw(_token, _flashAmt, address(this));
|
aave.withdraw(data.token, data.flashAmt, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
_atokenContract.safeTransfer(dsa, _supplyAmt);
|
_atokenContract.safeTransfer(dsa, data.supplyAmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function borrowAndTransferSpells(AaveInterface aave, address dsa, address[] memory borrowTokens, uint[] memory borrowAmts) internal {
|
function borrowAndTransferSpells(AaveInterface aave, address dsa, address[] memory borrowTokens, uint[] memory borrowAmts) internal {
|
||||||
for (uint i = 0; i < borrowTokens.length; i++) {
|
for (uint i = 0; i < borrowTokens.length; i++) {
|
||||||
address _token = borrowTokens[i] == maticAddr ? wmaticAddr : borrowTokens[i];
|
SpellHelperData memory data;
|
||||||
(address _atoken, ,) = aaveData.getReserveTokensAddresses(_token);
|
data.token = borrowTokens[i] == maticAddr ? wmaticAddr : borrowTokens[i];
|
||||||
(uint tokenLiq,,,,,,,,,) = aaveData.getReserveData(_token);
|
(data.atoken, ,) = aaveData.getReserveTokensAddresses(data.token);
|
||||||
uint _borrowAmt = borrowAmts[i];
|
(data.tokenLiq,,,,,,,,,) = aaveData.getReserveData(data.token);
|
||||||
|
data.borrowAmt = borrowAmts[i];
|
||||||
|
|
||||||
uint _flashAmt;
|
if (data.tokenLiq < data.borrowAmt) {
|
||||||
bool isFlash;
|
data.flashAmt = flashAmts[data.token];
|
||||||
if (tokenLiq < _borrowAmt) {
|
aave.deposit(data.token, data.flashAmt, address(this), 3288); // TODO: what is our ID on Polygon?
|
||||||
_flashAmt = flashAmts[_token];
|
data.isFlash = true;
|
||||||
aave.deposit(_token, _flashAmt, address(this), 3288); // TODO: what is our ID on Polygon?
|
data.tokenLiq += data.flashAmt;
|
||||||
isFlash = true;
|
|
||||||
tokenLiq += _flashAmt;
|
|
||||||
}
|
}
|
||||||
// TODO: Check number of loops needed. Borrow and supply on user's account.
|
// TODO: Check number of loops needed. Borrow and supply on user's account.
|
||||||
uint num = _borrowAmt/tokenLiq + 1; // TODO: Is this right
|
uint num = data.borrowAmt/data.tokenLiq + 1; // TODO: Is this right
|
||||||
uint splitAmt = _borrowAmt/num; // TODO: Check decimal
|
uint splitAmt = data.borrowAmt/num; // TODO: Check decimal
|
||||||
uint finalSplit = _borrowAmt - (splitAmt * (num - 1)); // TODO: to resolve upper decimal error
|
uint finalSplit = data.borrowAmt - (splitAmt * (num - 1)); // TODO: to resolve upper decimal error
|
||||||
|
|
||||||
uint spellsAmt = (2 * num) + 1;
|
uint spellsAmt = (2 * num) + 1;
|
||||||
string[] memory targets = new string[](spellsAmt);
|
string[] memory targets = new string[](spellsAmt);
|
||||||
|
@ -110,23 +128,23 @@ abstract contract Helpers is Stores, DSMath, Variables {
|
||||||
uint k = j * 2;
|
uint k = j * 2;
|
||||||
if (i < num - 1) {
|
if (i < num - 1) {
|
||||||
targets[k] = "AAVE-V2-A";
|
targets[k] = "AAVE-V2-A";
|
||||||
castData[k] = abi.encodeWithSignature("borrow(address,uint256,uint256,uint256,uint256)", _token, splitAmt, 2, 0, 0);
|
castData[k] = abi.encodeWithSignature("borrow(address,uint256,uint256,uint256,uint256)", data.token, splitAmt, 2, 0, 0);
|
||||||
targets[k+1] = "AAVE-V2-A";
|
targets[k+1] = "AAVE-V2-A";
|
||||||
castData[k+1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256)", _token, splitAmt, 0, 0);
|
castData[k+1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256)", data.token, splitAmt, 0, 0);
|
||||||
} else {
|
} else {
|
||||||
targets[k] = "AAVE-V2-A";
|
targets[k] = "AAVE-V2-A";
|
||||||
castData[k] = abi.encodeWithSignature("borrow(address,uint256,uint256,uint256,uint256)", _token, finalSplit, 2, 0, 0);
|
castData[k] = abi.encodeWithSignature("borrow(address,uint256,uint256,uint256,uint256)", data.token, finalSplit, 2, 0, 0);
|
||||||
targets[k+1] = "AAVE-V2-A";
|
targets[k+1] = "AAVE-V2-A";
|
||||||
castData[k+1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256)", _token, finalSplit, 0, 0);
|
castData[k+1] = abi.encodeWithSignature("deposit(address,uint256,uint256,uint256)", data.token, finalSplit, 0, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFlash) {
|
if (data.isFlash) {
|
||||||
aave.withdraw(_token, _flashAmt, address(this));
|
aave.withdraw(data.token, data.flashAmt, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
targets[spellsAmt] = "BASIC-A"; // TODO: right spell?
|
targets[spellsAmt] = "BASIC-A"; // TODO: right spell?
|
||||||
castData[spellsAmt] = abi.encode("withdraw(address,uint256,address,uint256,uint256)", _atoken, _borrowAmt, address(this), 0, 0); // encode the data of atoken withdrawal
|
castData[spellsAmt] = abi.encode("withdraw(address,uint256,address,uint256,uint256)", data.atoken, data.borrowAmt, address(this), 0, 0); // encode the data of atoken withdrawal
|
||||||
AccountInterface(dsa).castMigrate(targets, castData, address(this));
|
AccountInterface(dsa).castMigrate(targets, castData, address(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1183
package-lock.json
generated
1183
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -1,14 +0,0 @@
|
||||||
const { expect } = require("chai");
|
|
||||||
|
|
||||||
describe("Greeter", function() {
|
|
||||||
it("Should return the new greeting once it's changed", async function() {
|
|
||||||
const Greeter = await ethers.getContractFactory("Greeter");
|
|
||||||
const greeter = await Greeter.deploy("Hello, world!");
|
|
||||||
|
|
||||||
await greeter.deployed();
|
|
||||||
expect(await greeter.greet()).to.equal("Hello, world!");
|
|
||||||
|
|
||||||
await greeter.setGreeting("Hola, mundo!");
|
|
||||||
expect(await greeter.greet()).to.equal("Hola, mundo!");
|
|
||||||
});
|
|
||||||
});
|
|
Loading…
Reference in New Issue
Block a user