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(mento-v2): Integrate mento-v2 #42

Merged
merged 19 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ build/
dist/
node_modules/
types/
**/cache
**/out
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = v1.5.2
97 changes: 97 additions & 0 deletions contracts/swappa/PairMentoV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.6.8;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./ISwappaPairV1.sol";

interface IBroker {
function swapIn(
address exchangeProvider,
bytes32 exchangeId,
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256 amountOutMin
) external returns (uint256 amountOut);

function getAmountOut(
address exchangeProvider,
bytes32 exchangeId,
address tokenIn,
address tokenOut,
uint256 amountIn
) external view returns (uint256 amountOut);
}

contract PairMentoV2 is ISwappaPairV1 {
function swap(
address input,
address output,
address to,
bytes calldata data
) external override {
(
address _broker,
address exchangeProvider,
bytes32 exchangeId
) = parseData(data);
uint inputAmount = ERC20(input).balanceOf(address(this));
require(
ERC20(input).approve(_broker, inputAmount),
"PairMentoV2: failed!"
);
IBroker broker = IBroker(_broker);
uint256 amountOut = broker.swapIn(
exchangeProvider,
exchangeId,
input,
output,
inputAmount,
0
);
require(
IERC20(output).transfer(to, amountOut),
"PairMentoV2: transfer failed!"
);

}

function parseData(
bytes memory data
)
public
pure
returns (address broker, address exchangeProvider, bytes32 exchangeId)
{
require(data.length == 72, "PairMentoV2: invalid data!");
assembly {
broker := mload(add(data, 20))
exchangeProvider := mload(add(data, 40))
exchangeId := mload(add(data, 72))
}
}

function getOutputAmount(
address input,
address output,
uint256 amountIn,
bytes calldata data
) external view override returns (uint amountOut) {
(
address _broker,
address exchangeProvider,
bytes32 exchangeId
) = parseData(data);
IBroker broker = IBroker(_broker);
return
broker.getAmountOut(
exchangeProvider,
exchangeId,
input,
output,
amountIn
);
}

receive() external payable {}
}
9 changes: 9 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[profile.default]
src = 'contracts'
out = 'out'
test = 'test'
libs = [
'lib',
'node_modules',
]
# See more config options https://github.com/gakonst/foundry/tree/master/config
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 2b58ec
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
"dependencies": {
"@celo/connect": "^1.2.4",
"@celo/contractkit": "^1.2.4",
"@mento-protocol/mento-core-ts": "^0.1.0",
"@mento-protocol/mento-sdk": "^0.1.4",
"@terminal-fi/savingscelo": "^3.4.0",
"@ubeswap/default-token-list": "^4.0.11",
"axios": "^1.2.3",
"bignumber.js": "^9.0.1",
"ethers": "5.7.2",
"web3": "^1.3.4",
"web3-utils": "^1.3.0"
},
Expand Down
15 changes: 14 additions & 1 deletion src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
mainnetRegistryCeloDex, mainnetRegistrySymmetric, mainnetRegistryMisc, mainnetRegistryCurve,
} from '../registry-cfg';
import { RegistryMento } from '../registries/mento';
import { RegistryMentoV2 } from '../registries/mento-v2';
import { Registry } from '../registry';

const program = commander.program
Expand All @@ -29,6 +30,7 @@ const program = commander.program
.option("--max-slippage <max-slippage>", "Maximum allowed slippage.", "0.9999")
.option("--no-precheck", "If provided, will skip expected output precheck.")
.option("--benchmark <iterations>", "If non-zero, benchmark the route finding for N iterations.", "0")
.option("--privateKey <private-key>", "Private key for the from", undefined)
.parse(process.argv)

process.on('unhandledRejection', (reason: any, _promise: any) => {
Expand All @@ -39,6 +41,7 @@ process.on('unhandledRejection', (reason: any, _promise: any) => {

const registriesByName: {[name: string]: (kit: ContractKit) => Registry} = {
"mento": (kit: ContractKit) => new RegistryMento(kit),
"mentov2": (kit: ContractKit) => new RegistryMentoV2(kit),
"ubeswap": mainnetRegistryUbeswap,
"sushiswap": mainnetRegistrySushiswap,
"mobius": mainnetRegistryMobius,
Expand Down Expand Up @@ -72,6 +75,9 @@ async function main() {
const opts = program.opts()
const kit = await newKit(opts.network)
chainId = await kit.web3.eth.getChainId()
if (opts.privateKey) {
kit.connection.addAccount(opts.privateKey);
}

const celoTokenListURI = "https://celo-org.github.io/celo-token-list/celo.tokenlist.json"
const celoTokenList = (await axios.get<{tokens: Token[]}>(celoTokenListURI)).data
Expand All @@ -83,7 +89,14 @@ async function main() {
address: "0x617f3112bf5397D0467D315cC709EF968D9ba546",
symbol: "USDTxWormhole",
decimals: 6,
}
},
{ // this is a Mock USDC on baklava created by the mento team
chainId: 62320,
address: "0x4c6B046750F9aBF6F0f3B511217438451bc6Aa02",
symbol: "mockBridgedUSDC",
decimals: 18,
},
// add AxelarUSDC for mainnet that will be used
]

const inputToken = tokenByAddrOrSymbol(opts.input)
Expand Down
Loading