From 11417971d3043362e9d093f4cd4a839a5b42ef05 Mon Sep 17 00:00:00 2001 From: Manthankumar Satani Date: Mon, 8 Mar 2021 13:07:33 +0530 Subject: [PATCH 1/2] new: DefiDollar readonly adapters --- .../defidollar/DefiDollarProtocolAdapter.sol | 40 ++++++++++++++ .../defidollar/DefiDollarTokenAdapter.sol | 54 +++++++++++++++++++ test/DefiDollarTokenAdapter.js | 33 ++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 contracts/adapters/defidollar/DefiDollarProtocolAdapter.sol create mode 100644 contracts/adapters/defidollar/DefiDollarTokenAdapter.sol create mode 100644 test/DefiDollarTokenAdapter.js diff --git a/contracts/adapters/defidollar/DefiDollarProtocolAdapter.sol b/contracts/adapters/defidollar/DefiDollarProtocolAdapter.sol new file mode 100644 index 00000000..14fbc16c --- /dev/null +++ b/contracts/adapters/defidollar/DefiDollarProtocolAdapter.sol @@ -0,0 +1,40 @@ +// Copyright (C) 2020 Zerion Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +pragma solidity 0.6.5; +pragma experimental ABIEncoderV2; + +import { ERC20 } from "../../ERC20.sol"; +import { ProtocolAdapter } from "../ProtocolAdapter.sol"; + +/** + * @title Adapter for DefiDollar protocol. + * @dev Implementation of ProtocolAdapter interface. + * @author Manthankumar Satani + */ +contract DefiDollarProtocolAdapter is ProtocolAdapter { + string public constant override adapterType = "Asset"; + + string public constant override tokenType = "ERC20"; + + /** + * @return Amount of DefiDollar tokens held by the given account. + * @param token Address of the DefiDollar Protocol tokens. + * @dev Implementation of ProtocolAdapter interface function. + */ + function getBalance(address token, address account) external view override returns (uint256) { + return ERC20(token).balanceOf(account); + } +} diff --git a/contracts/adapters/defidollar/DefiDollarTokenAdapter.sol b/contracts/adapters/defidollar/DefiDollarTokenAdapter.sol new file mode 100644 index 00000000..eee10354 --- /dev/null +++ b/contracts/adapters/defidollar/DefiDollarTokenAdapter.sol @@ -0,0 +1,54 @@ +// Copyright (C) 2020 Zerion Inc. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +pragma solidity 0.6.5; +pragma experimental ABIEncoderV2; + +import { ERC20 } from "../../ERC20.sol"; +import { TokenMetadata, Component } from "../../Structs.sol"; +import { TokenAdapter } from "../TokenAdapter.sol"; + +/** + * @title Token adapter for DefiDollar protocol. + * @dev Implementation of TokenAdapter interface. + * @author Manthankumar Satani + */ +contract DefiDollarTokenAdapter is TokenAdapter { + /** + * @return TokenMetadata struct with ERC20-style token info. + * @dev Implementation of TokenAdapter interface function. + */ + function getMetadata(address token) external view override returns (TokenMetadata memory) { + return + TokenMetadata({ + token: token, + name: ERC20(token).name(), + symbol: ERC20(token).symbol(), + decimals: ERC20(token).decimals() + }); + } + + /** + * @return Array of Component structs with underlying tokens rates for the given token. + * @dev Implementation of TokenAdapter interface function. + */ + function getComponents(address token) external view override returns (Component[] memory) { + Component[] memory underlyingTokens = new Component[](1); + + underlyingTokens[0] = Component({ token: token, tokenType: "ERC20", rate: uint256(1e18) }); + + return underlyingTokens; + } +} diff --git a/test/DefiDollarTokenAdapter.js b/test/DefiDollarTokenAdapter.js new file mode 100644 index 00000000..eef37723 --- /dev/null +++ b/test/DefiDollarTokenAdapter.js @@ -0,0 +1,33 @@ +const TokenAdapter = artifacts.require("DefiDollarTokenAdapter"); + +contract("DefiDollarTokenAdapter", () => { + const tokenAddress = "0x5bc25f649fc4e26069ddf4cf4010f9f706c23831"; + + let accounts; + let tokenAdapter; + const DUSD = [tokenAddress, "DefiDollar", "DUSD", "18"]; + + beforeEach(async () => { + accounts = await web3.eth.getAccounts(); + + await TokenAdapter.new({ from: accounts[0] }).then((result) => { + tokenAdapter = result.contract; + }); + }); + + it("should return correct components", async () => { + await tokenAdapter.methods["getComponents(address)"](tokenAddress) + .call() + .then((result) => { + console.dir(result, { depth: null }); + }); + }); + + it("should return correct metadata", async () => { + await tokenAdapter.methods["getMetadata(address)"](tokenAddress) + .call() + .then((result) => { + assert.deepEqual(result, DUSD); + }); + }); +}); From d123ca161097086b192ab0c33cba87366f520f91 Mon Sep 17 00:00:00 2001 From: Manthankumar Satani Date: Sat, 24 Apr 2021 14:29:23 +0530 Subject: [PATCH 2/2] fix: linting in defidollar test --- test/DefiDollarTokenAdapter.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/DefiDollarTokenAdapter.js b/test/DefiDollarTokenAdapter.js index eef37723..28cff368 100644 --- a/test/DefiDollarTokenAdapter.js +++ b/test/DefiDollarTokenAdapter.js @@ -1,11 +1,11 @@ -const TokenAdapter = artifacts.require("DefiDollarTokenAdapter"); +const TokenAdapter = artifacts.require('DefiDollarTokenAdapter'); -contract("DefiDollarTokenAdapter", () => { - const tokenAddress = "0x5bc25f649fc4e26069ddf4cf4010f9f706c23831"; +contract('DefiDollarTokenAdapter', () => { + const tokenAddress = '0x5bc25f649fc4e26069ddf4cf4010f9f706c23831'; let accounts; let tokenAdapter; - const DUSD = [tokenAddress, "DefiDollar", "DUSD", "18"]; + const DUSD = [tokenAddress, 'DefiDollar', 'DUSD', '18']; beforeEach(async () => { accounts = await web3.eth.getAccounts(); @@ -15,16 +15,16 @@ contract("DefiDollarTokenAdapter", () => { }); }); - it("should return correct components", async () => { - await tokenAdapter.methods["getComponents(address)"](tokenAddress) + it('should return correct components', async () => { + await tokenAdapter.methods['getComponents(address)'](tokenAddress) .call() .then((result) => { console.dir(result, { depth: null }); }); }); - it("should return correct metadata", async () => { - await tokenAdapter.methods["getMetadata(address)"](tokenAddress) + it('should return correct metadata', async () => { + await tokenAdapter.methods['getMetadata(address)'](tokenAddress) .call() .then((result) => { assert.deepEqual(result, DUSD);