Frankencoin-ZCHF / FrankenCoin

Frankencoin Smart Contracts
MIT License
12 stars 6 forks source link

Proof of Concept for FlashLoan #27

Closed samclassix closed 3 months ago

samclassix commented 3 months ago

Development Branch for Flashloan's

This is just a "Proof of Concept" branch and further developments can/should be made before merging.

The FlashLoan contract

The FlashLoan contract in the Frankencoin ecosystem provides a mechanism for users to borrow and repay ZCHF tokens within the same transaction without collateral. This kind of mechanism could be needed in implementing a Roll-Over feature for Frankencoin positions.

Impact for FPS holders

FPS holder would benefit, due to

For a Position Roll-Over, the FlashLoan contract would allow users to:

This process enables users to seamlessly(!) transition from one position to another without needing to have additional funds on hand (if possible). It's particularly useful for adjusting position parameters or extending the position's duration without closing and reopening.

The FlashLoan contract's ability to execute multiple calls in a single transaction (via the takeLoan function) makes it well-suited for this complex operation, ensuring atomicity and reducing the risk of partial execution.

For testing "delegatecall" is used

To play around with rolling position with a "FlashLoan", is is used to test. This is not safe, because it can overwrite the storage of the flashloan contract.

samclassix commented 3 months ago

Tested

  1. Addr: FlashLoan, Calldata: repayLoan(0.5 ZCHF)
  2. Addr: FlashLoan, Calldata: repayLoan(0.5 ZCHF)

Logs

  1. "zchf.event": "Transfer" // mint 1 zchf --> sender
  2. "flashloan.event": "LoanTaken",
  3. "zchf.event": "Transfer", // burn 1st
  4. "zchf.event": "Transfer" // fee paid 1st
  5. "flashloan.event": "Repaid", // repaid 1st ... ... ...

Raw Logs

status  0x1 Transaction mined and execution succeed
transaction hash    0x3bcb3517c289908a970c76d25f7e8c755ffcb224bd2adab850456511faa89422
block hash  0x478544440337a2603c575fc2cc38fa7569ac083a59483b6ba446434195dfd5bf
block number    68
from    0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
to  FlashLoan.takeLoan(uint256,address[],bytes[]) 0x3d33C01bCC36ac6A8f872599A9c9351c11Ef07E7
gas 202356 gas
transaction cost    170361 gas 
execution cost  149289 gas 
input   0x97a...00000
decoded input   {
    "uint256 amount": "1000000000000000000",
    "address[] targets": [
        "0x3d33C01bCC36ac6A8f872599A9c9351c11Ef07E7",
        "0x3d33C01bCC36ac6A8f872599A9c9351c11Ef07E7"
    ],
    "bytes[] calldatas": [
        "0xab7b1c8900000000000000000000000000000000000000000000000006f05b59d3b20000",
        "0xab7b1c8900000000000000000000000000000000000000000000000006f05b59d3b20000"
    ]
}
decoded output  {}
logs    [
    {
        "from": "0xFbCA1d693A100774A2443cc1D41bD1a3D5295112",
        "topic": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "event": "Transfer",
        "args": {
            "0": "0x0000000000000000000000000000000000000000",
            "1": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "2": "1000000000000000000",
            "from": "0x0000000000000000000000000000000000000000",
            "to": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "value": "1000000000000000000"
        }
    },
    {
        "from": "0x3d33C01bCC36ac6A8f872599A9c9351c11Ef07E7",
        "topic": "0x006b9bb4766cf5ab74b890df959e4fad0e54b1c3885e49c9541d36253a6e39e6",
        "event": "LoanTaken",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "1000000000000000000",
            "2": "1000000000000000000",
            "to": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "amount": "1000000000000000000",
            "totalMint": "1000000000000000000"
        }
    },
    {
        "from": "0xFbCA1d693A100774A2443cc1D41bD1a3D5295112",
        "topic": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "event": "Transfer",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "0x0000000000000000000000000000000000000000",
            "2": "500000000000000000",
            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "to": "0x0000000000000000000000000000000000000000",
            "value": "500000000000000000"
        }
    },
    {
        "from": "0xFbCA1d693A100774A2443cc1D41bD1a3D5295112",
        "topic": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "event": "Transfer",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "0x9C4e667fD64FfB8eA86dacA9BC78bfE976D0d726",
            "2": "500000000000000",
            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "to": "0x9C4e667fD64FfB8eA86dacA9BC78bfE976D0d726",
            "value": "500000000000000"
        }
    },
    {
        "from": "0x3d33C01bCC36ac6A8f872599A9c9351c11Ef07E7",
        "topic": "0x07178c166b5aef4d8409e6bbfd0e7b1ad42c92eb65903a5e5482166f3f4e62f0",
        "event": "Repaid",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "500500000000000000",
            "2": "500000000000000000",
            "3": "500000000000000",
            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "total": "500500000000000000",
            "repay": "500000000000000000",
            "fee": "500000000000000"
        }
    },
    {
        "from": "0xFbCA1d693A100774A2443cc1D41bD1a3D5295112",
        "topic": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "event": "Transfer",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "0x0000000000000000000000000000000000000000",
            "2": "500000000000000000",
            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "to": "0x0000000000000000000000000000000000000000",
            "value": "500000000000000000"
        }
    },
    {
        "from": "0xFbCA1d693A100774A2443cc1D41bD1a3D5295112",
        "topic": "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
        "event": "Transfer",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "0x9C4e667fD64FfB8eA86dacA9BC78bfE976D0d726",
            "2": "500000000000000",
            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "to": "0x9C4e667fD64FfB8eA86dacA9BC78bfE976D0d726",
            "value": "500000000000000"
        }
    },
    {
        "from": "0x3d33C01bCC36ac6A8f872599A9c9351c11Ef07E7",
        "topic": "0x07178c166b5aef4d8409e6bbfd0e7b1ad42c92eb65903a5e5482166f3f4e62f0",
        "event": "Repaid",
        "args": {
            "0": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "1": "500500000000000000",
            "2": "500000000000000000",
            "3": "500000000000000",
            "from": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4",
            "total": "500500000000000000",
            "repay": "500000000000000000",
            "fee": "500000000000000"
        }
    }
]