Class: ERC20LiquidityPool
- Inherits:
-
Contract
- Object
- Contract
- ERC20LiquidityPool
- Defined in:
- lib/rubysol/contracts/erc20_liquidity_pool.rb
Instance Method Summary collapse
- #addLiquidity(token0Amount:, token1Amount:) ⇒ Object
- #calculateOutputAmount(inputToken:, outputToken:, inputAmount:) ⇒ Object
- #constructor(token0:, token1:) ⇒ Object
- #reserves ⇒ Object
- #swap(inputToken:, outputToken:, inputAmount:) ⇒ Object
Instance Method Details
#addLiquidity(token0Amount:, token1Amount:) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rubysol/contracts/erc20_liquidity_pool.rb', line 14 def addLiquidity( token0Amount:, token1Amount: ) ERC20(@token0).transferFrom( from: msg.sender, to: address( this ), ## note: add support for this (alias for self ??) amount: token0Amount ) ERC20(@token1).transferFrom( from: msg.sender, to: address(this), amount: token1Amount ) end |
#calculateOutputAmount(inputToken:, outputToken:, inputAmount:) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/rubysol/contracts/erc20_liquidity_pool.rb', line 41 def calculateOutputAmount( inputToken:, outputToken:, inputAmount: ) inputReserve = ERC20(inputToken).balanceOf(address(this)) outputReserve = ERC20(outputToken).balanceOf(address(this)) ((inputAmount * outputReserve) / (inputReserve + inputAmount)).to_i end |
#constructor(token0:, token1:) ⇒ Object
7 8 9 10 11 |
# File 'lib/rubysol/contracts/erc20_liquidity_pool.rb', line 7 def constructor( token0:, token1: ) @token0 = token0 @token1 = token1 end |
#reserves ⇒ Object
31 32 33 34 35 36 |
# File 'lib/rubysol/contracts/erc20_liquidity_pool.rb', line 31 def reserves { token0: ERC20(@token0).balanceOf(address(this)), token1: ERC20(@token1).balanceOf(address(this)) } end |
#swap(inputToken:, outputToken:, inputAmount:) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rubysol/contracts/erc20_liquidity_pool.rb', line 52 def swap( inputToken:, outputToken:, inputAmount: ) assert [@token0, @token1].include?(inputToken), "Invalid input token" assert [@token0, @token1].include?(outputToken), "Invalid output token" assert inputToken != outputToken, "Input and output tokens can't be the same" outputAmount = calculateOutputAmount( inputToken: inputToken, outputToken: outputToken, inputAmount: inputAmount ) outputReserve = ERC20(outputToken).balanceOf( address(this)) assert outputAmount <= outputReserve, "Insufficient output reserve" ERC20(inputToken).transferFrom( from: msg.sender, to: dumbContractId(this), amount: inputAmount ) ERC20(outputToken).transfer( msg.sender, outputAmount ) outputAmount end |