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

V2.0.2 #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 25 additions & 1 deletion contracts/PriceOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,19 @@ contract PriceOracle is Ownable {
* @dev Emitted when the price is updated.
*/
event PriceUpdate(uint256 price);
/**
* @dev Emitted when the rate is updated.
*/
event ExchangeRateUpdate(uint256 rate);

// current price in wei per GB/month
uint256 public price;
constructor(uint256 _price) {
// current rate
uint256 public exchangeRate;

constructor(uint256 _price, uint256 _exchangeRate) {
price = _price;
exchangeRate = _exchangeRate;
}

/**
Expand All @@ -122,6 +130,13 @@ contract PriceOracle is Ownable {
return price;
}

/**
* @notice Returns the rate of price
*/
function getExchangeRate() external view returns (uint256) {
return exchangeRate;
}

/**
* @notice Update the price. Can only be called by the owner.
* @param newPrice the new price
Expand All @@ -130,4 +145,13 @@ contract PriceOracle is Ownable {
price = newPrice;
emit PriceUpdate(price);
}

/**
* @notice Update the rate. Can only be called by the owner.
* @param newRate the new rate
*/
function updateExchangeRate(uint256 newRate) external onlyOwner {
exchangeRate = newRate;
emit ExchangeRateUpdate(newRate);
}
}
13 changes: 11 additions & 2 deletions contracts/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ contract Vault {
uint callerPayout
);
event ChequeBounced();
event Withdraw(uint amount);
event Withdraw(address indexed from, uint amount);
event Deposit(address indexed from, uint amount);

struct EIP712Domain {
string name;
Expand Down Expand Up @@ -166,7 +167,15 @@ contract Vault {
/* ensure we don't take anything from the hard deposit */
require(amount <= totalbalance(), "totalbalance not sufficient");
require(token.transfer(issuer, amount), "transfer failed");
emit Withdraw(amount);
emit Withdraw(issuer, amount);
}

/*
* deposit wbtt to address(this), befrore it, must approve to address(this)
*/
function deposit(uint amount) public {
require(token.transferFrom(msg.sender, address(this), amount), "deposit failed");
emit Deposit(msg.sender, amount);
}

function chequeHash(address vault, address beneficiary, uint cumulativePayout)
Expand Down
17 changes: 13 additions & 4 deletions contracts/VaultFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@ import "@openzeppelin/contracts/proxy/Clones.sol";
*/
contract VaultFactory {

/* event fired on every new Vault deployment */
event VaultDeployed(address issuer,address contractAddress);
/**
@notice event fired after new Vault deployed
@param issuer the issuer of the new vault contract
@param contractAddress the address of the new deployed contract
@param id the peerID of the btfs node
*/
event VaultDeployed(address issuer,address contractAddress,string id);

/* mapping to keep track of which contracts were deployed by this factory */
mapping (address => bool) public deployedContracts;
/* mapping between btfs node's peerID and its vault address */
mapping (string => address) public peerVaultAddress;

/* address of the TRC20-token, to be used by the to-be-deployed vaults */
address public TokenAddress;
Expand All @@ -33,13 +40,15 @@ contract VaultFactory {
@notice creates a clone of the master Vault contract
@param issuer the issuer of cheques for the new vault
@param salt salt to include in create2 to enable the same address to deploy multiple Vaults
@param id the peerID of the btfs node
*/
function deployVault(address issuer, bytes32 salt)
function deployVault(address issuer, bytes32 salt, string memory id)
public returns (address) {
address contractAddress = Clones.cloneDeterministic(master, keccak256(abi.encode(msg.sender, salt)));
Vault(contractAddress).init(issuer, TokenAddress);
deployedContracts[contractAddress] = true;
emit VaultDeployed(issuer,contractAddress);
peerVaultAddress[id] = contractAddress;
emit VaultDeployed(issuer,contractAddress,id);
return contractAddress;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"scripts": {
"compile": "hardhat compile",
"test": "hardhat test",
"test": "hardhat test --show-stack-traces",
"abigen": "./abigen/gen.sh Vault.sol VaultFactory ",
"lint": "solhint contracts/Vault.sol contracts/VaultFactory.sol",
"coverage": "hardhat coverage --solcoverjs ./.solcover.js"
Expand Down
3 changes: 2 additions & 1 deletion test/Vault.should.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ const { expect } = require('chai');

function shouldDeploy(issuer, value) {

const adminPeerID = "0000000000000000000000000000000000000000000000000000a"
const salt = "0x000000000000000000000000000000000000000000000000000000000000abcd"

beforeEach(async function() {
this.TestToken = await TestToken.new({from: issuer})
await this.TestToken.mint(issuer, 1000000000, {from: issuer});
this.vaultFactory = await VaultFactory.new(this.TestToken.address)
let { logs } = await this.vaultFactory.deployVault(issuer, salt)
let { logs } = await this.vaultFactory.deployVault(issuer, salt, adminPeerID)
this.VaultAddress = logs[0].args.contractAddress
this.Vault = await Vault.at(this.VaultAddress)
if(value != 0) {
Expand Down
30 changes: 22 additions & 8 deletions test/VaultFactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,25 @@ const VaultFactory = artifacts.require('VaultFactory')
const Vault = artifacts.require('Vault')
const TestToken = artifacts.require("TestToken")

contract('VaultFactory', function([issuer, other]) {
contract('VaultFactory', function([issuer, other1, other2]) {

const salt = "0x000000000000000000000000000000000000000000000000000000000000abcd"
const adminPeerID = "0000000000000000000000000000000000000000000000000000a"
const userPeerID1 = "0000000000000000000000000000000000000000000000000000b"
const userPeerID2 = "0000000000000000000000000000000000000000000000000000c"
function shouldDeployVault(issuer, value) {

beforeEach(async function() {
this.TestToken = await TestToken.new({from: issuer})
this.vaultFactory = await VaultFactory.new(this.TestToken.address)
let { logs } = await this.vaultFactory.deployVault(issuer, salt)
let { logs } = await this.vaultFactory.deployVault(issuer, salt, adminPeerID)
this.VaultAddress = logs[0].args.contractAddress
this.Vault = await Vault.at(this.VaultAddress)
if(value != 0) {
await this.TestToken.mint(issuer, value) // mint tokens
await this.TestToken.transfer(this.Vault.address, value, {from: issuer}); // deposit those tokens in vault
}
})

it('should allow other addresses to deploy with same salt', async function() {
await this.vaultFactory.deployVault(issuer, salt, { from: other })
})

it('should deploy with the right issuer', async function() {
expect(await this.Vault.issuer()).to.be.equal(issuer)
Expand All @@ -48,6 +47,21 @@ contract('VaultFactory', function([issuer, other]) {
it('should have set the ERC20 address correctly', async function() {
expect(await this.Vault.token()).to.be.equal(this.TestToken.address)
})

it('should allow other addresses to deploy with same salt', async function() {
let { logs } = await this.vaultFactory.deployVault(issuer, salt, userPeerID1, { from: other1 })
const vaultAddr1 = logs[0].args.contractAddress
expect(await this.vaultFactory.deployedContracts(vaultAddr1)).to.be.true
})

it("should record the relationship between peerID and it's vault address", async function() {
await this.vaultFactory.deployVault(issuer, salt, userPeerID1, { from: other1 })
await this.vaultFactory.deployVault(issuer, salt, userPeerID2, { from: other2 })
const vaultAddr1 = await this.vaultFactory.peerVaultAddress(userPeerID1)
const vaultAddr2 = await this.vaultFactory.peerVaultAddress(userPeerID2)
expect(vaultAddr1).to.be.not.equal(vaultAddr2)
})

}

describe('when we deploy Vault', function() {
Expand All @@ -61,12 +75,12 @@ contract('VaultFactory', function([issuer, other]) {
shouldDeployVault(issuer, new BN(10))
})

describe("when we deposit while issuer 0", function() {
describe("when we deposit with zero issuer", function() {
this.timeout(100000);
it('should fail', async function() {
this.TestToken = await TestToken.new({from: issuer})
this.vaultFactory = await VaultFactory.new(this.TestToken.address)
await expectRevert(this.vaultFactory.deployVault(constants.ZERO_ADDRESS, salt), 'invalid issuer')
await expectRevert(this.vaultFactory.deployVault(constants.ZERO_ADDRESS, salt, adminPeerID), 'invalid issuer')
})
})
})
Expand Down