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

Initial Commit #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build/npm-module
build
node_modules
yarn-error.log
npm-debug.log
addresses.json
/.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 singnet

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,39 @@
# snet-cf-gateway
# SNET-CF-Gateway Contracts

[![CircleCI](https://circleci.com/gh/singnet/platform-contracts.svg?style=svg)](https://circleci.com/gh/singnet/SNET-CF-Gateway-Contracts)

Includes SingularityNET Crypto to Fiat contracts, migrations, tests

## Contracts

### CryptoToFiat
* Contract that maintains a Crypto to fiat conversion for Service Providers and AI Developers. Consumers can use this service to convert the AGI received as part if their services utilization.

## Requirements
* [Node.js](https://github.com/nodejs/node) (8+)
* [Npm](https://www.npmjs.com/package/npm)

## Install

### Dependencies
```bash
npm install
```

### Compile
```bash
truffle compile
```

### Test
```bash
truffle test
ksridharbabuus marked this conversation as resolved.
Show resolved Hide resolved
```

## Package
```bash
npm run package-npm
```

## Release
Contract build artifacts are published to NPM: COMING SOON
143 changes: 143 additions & 0 deletions contracts/CryptoToFiat.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
pragma solidity ^0.4.24;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-solidity/contracts/math/SafeMath.sol";

contract CryptoToFiat {

using SafeMath for uint256;

// Owner of the contract
address public owner;

// Configurable Parameters
string public AGIPrice; // Price JSON
uint256 public minBalance;
uint256 public conversionUpperLimit; // Upper limit on the AGI conversion
uint256 public txnLimitInBlocks; // Number of blocks to be wait between Transactions
vsbogd marked this conversation as resolved.
Show resolved Hide resolved
address public authorizer; // Authorizer address singing the conversion process

// To store the last txn Block Number
mapping(address => uint256) public lastTxnBlocks;

// To store the total conversion amount
mapping(address => uint256) public totalConvertedAmt;

//Tokens which have been deposit into the contract
mapping (address => uint256) public balances;

// Address of token contract
ERC20 public token;

// Events
event PriceUpdated(string price);
event NewOwner(address owner);
event ConfigurationUpdate(uint256 minBalance, uint256 conversionUpperLimit, uint256 txnLimitInBlocks);
event DepositFunds(address indexed sender, uint256 amount);
event WithdrawFunds(address indexed sender, uint256 amount);
event TransferFunds(address indexed sender, address indexed receiver, uint256 amount);
event ConvertFunds(address indexed sender, address indexed owner, uint256 amount);

// Modifiers
modifier onlyOwner() {
require(
msg.sender == owner,
"Only owner can call this function."
);
_;
}

constructor (address _token)
public
{
token = ERC20(_token);
owner = msg.sender;
}

function deposit(uint256 value)
public
returns(bool)
{
require(token.transferFrom(msg.sender, this, value), "Unable to transfer token to the contract.");
balances[msg.sender] = balances[msg.sender].add(value);
emit DepositFunds(msg.sender, value);
return true;
}

function withdraw(uint256 value)
public
returns(bool)
{
require(balances[msg.sender] >= value, "Insufficient balance in the contract.");
require(token.transfer(msg.sender, value), "Unable to transfer token from the contract.");
balances[msg.sender] = balances[msg.sender].sub(value);
emit WithdrawFunds(msg.sender, value);
return true;
}

function transfer(address receiver, uint256 value)
public
returns(bool)
{
require(balances[msg.sender] >= value, "Insufficient balance in the contract");
balances[msg.sender] = balances[msg.sender].sub(value);
balances[receiver] = balances[receiver].add(value);

emit TransferFunds(msg.sender, receiver, value);
return true;
}

function initiateConversion(uint256 value, uint256 totalClaim, uint8 v, bytes32 r, bytes32 s)
public
returns(bool)
{
require(balances[msg.sender] >= value, "Insufficient balance in the contract");
require(balances[msg.sender] >= value.add(minBalance), "Minimum balance to be maintained in the contract");
require(value <= conversionUpperLimit, "Exceeding the conversion limit");
require(lastTxnBlocks[msg.sender] <= block.number.sub(txnLimitInBlocks), "Exceeding the number of transactions in given time");
require(totalConvertedAmt[msg.sender].add(value) <= totalClaim, "Exceeding the claims made so far");
ksridharbabuus marked this conversation as resolved.
Show resolved Hide resolved

//compose the message which was signed
bytes32 message = prefixed(keccak256(abi.encodePacked("__Conversion", this, msg.sender, totalClaim, lastTxnBlocks[msg.sender])));

// check that the signature is from the authorizer
address authAddress = ecrecover(message, v, r, s);
require(authAddress == authorizer, "Invalid signature");

balances[msg.sender] = balances[msg.sender].sub(value);
balances[owner] = balances[owner].add(value);

lastTxnBlocks[msg.sender] = block.number;
totalConvertedAmt[msg.sender] = totalConvertedAmt[msg.sender].add(value);

// This event is monitored to initiate the fiat transfer based on AGIPrice field
emit ConvertFunds(msg.sender, owner, value);
return true;
}


function updateOwner(address _owner) public onlyOwner {
owner = _owner;
emit NewOwner(_owner);
}

function updatePrice(string memory _sPrice) public onlyOwner {
AGIPrice = _sPrice;
emit PriceUpdated(_sPrice);
}

function setConfigurations(address _authorizer, uint256 _minBalance, uint256 _conversionUpperLimit, uint256 _txnLimitInBlocks) public onlyOwner {
authorizer = _authorizer;
minBalance = _minBalance;
conversionUpperLimit = _conversionUpperLimit;
txnLimitInBlocks = _txnLimitInBlocks;
emit ConfigurationUpdate(_minBalance, _conversionUpperLimit, _txnLimitInBlocks);
}

/// builds a prefixed hash to mimic the behavior of ethSign.
function prefixed(bytes32 hash) internal pure returns (bytes32)
{
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
}

}
23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity ^0.4.23;

contract Migrations {
address public owner;
uint public last_completed_migration;

constructor() public {
owner = msg.sender;
}

modifier restricted() {
if (msg.sender == owner) _;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}

function upgrade(address new_address) public restricted {
Migrations upgraded = Migrations(new_address);
upgraded.setCompleted(last_completed_migration);
}
}
5 changes: 5 additions & 0 deletions migrations/1_initial_migration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
deployer.deploy(Migrations);
};
12 changes: 12 additions & 0 deletions migrations/2_CryptoToFiat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let CryptoToFiat = artifacts.require("./CryptoToFiat.sol");
let Contract = require("truffle-contract");
let TokenAbi = require("singularitynet-token-contracts/abi/SingularityNetToken.json");
let TokenNetworks = require("singularitynet-token-contracts/networks/SingularityNetToken.json");
let TokenBytecode = require("singularitynet-token-contracts/bytecode/SingularityNetToken.json");
let Token = Contract({contractName: "SingularityNetToken", abi: TokenAbi, networks: TokenNetworks, bytecode: TokenBytecode});

module.exports = function(deployer, network, accounts) {
Token.setProvider(web3.currentProvider)
Token.defaults({from: accounts[0], gas: 4000000});
deployer.deploy(Token, {overwrite: false}).then((TokenInstance) => deployer.deploy(CryptoToFiat, TokenInstance.address));
};
Loading