-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Synaps3Protocol/distributor/randomweight
Distributor/randomweight
- Loading branch information
Showing
6 changed files
with
245 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
pragma solidity 0.8.26; | ||
|
||
import "forge-std/Test.sol"; | ||
import { IERC165 } from "@openzeppelin/contracts/interfaces/IERC165.sol"; | ||
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol"; | ||
import { IDistributor } from "contracts/interfaces/syndication/IDistributor.sol"; | ||
import { IBalanceVerifiable } from "contracts/interfaces/IBalanceVerifiable.sol"; | ||
import { IBalanceWithdrawable } from "contracts/interfaces/IBalanceWithdrawable.sol"; | ||
import { BaseTest } from "test/BaseTest.t.sol"; | ||
|
||
contract DistributorImplTest is BaseTest { | ||
address token; | ||
|
||
function setUp() public { | ||
token = deployToken(); | ||
} | ||
|
||
function test_Create_ValidDistributor() public { | ||
address distributor = deployDistributor("test.com"); | ||
assertEq(IERC165(distributor).supportsInterface(type(IDistributor).interfaceId), true); | ||
} | ||
|
||
function test_GetOwner_ExpectedDeployer() public { | ||
vm.prank(admin); | ||
address distributor = deployDistributor("test2.com"); | ||
assertEq(IDistributor(distributor).getManager(), admin); | ||
} | ||
|
||
function test_GetEndpoint_ExpectedEndpoint() public { | ||
address distributor = deployDistributor("test3.com"); | ||
assertEq(IDistributor(distributor).getEndpoint(), "test3.com"); | ||
} | ||
|
||
function test_SetEndpoint_ValidEndpoint() public { | ||
// created with an initial endpoint | ||
address distributor = deployDistributor("1.1.1.1"); | ||
// changed to a dns domain | ||
vm.prank(admin); // only owner can do this | ||
IDistributor(distributor).setEndpoint("mynew.com"); | ||
assertEq(IDistributor(distributor).getEndpoint(), "mynew.com"); | ||
} | ||
|
||
function test_SetEndpoint_RevertWhen_InvalidOwner() public { | ||
// created with an initial endpoint | ||
address distributor = deployDistributor("1.1.1.1"); | ||
vm.prank(user); | ||
vm.expectRevert(abi.encodeWithSignature("OwnableUnauthorizedAccount(address)", user)); | ||
IDistributor(distributor).setEndpoint("mynew.com"); | ||
} | ||
|
||
function test_GetBalance_ValidBalance() public { | ||
// created with an initial endpoint | ||
address distributor = deployDistributor("1.1.1.1"); | ||
uint256 expected = 100 * 1e18; | ||
// admin acting as reward system to transfer funds | ||
// here the expected is that rewards system do it. | ||
vm.startPrank(admin); // only owner can get balance by default deployer | ||
IERC20(token).transfer(distributor, expected); | ||
assertEq(IBalanceVerifiable(distributor).getBalance(token), expected); | ||
vm.stopPrank(); | ||
} | ||
|
||
function test_Withdraw_ValidFundsWitdrawn() public { | ||
// created with an initial endpoint | ||
uint256 expected = 100 * 1e18; | ||
address distributor = deployDistributor("1.1.1.1"); | ||
|
||
vm.startPrank(admin); // only owner can get balance by default deployer | ||
IERC20(token).transfer(distributor, expected); | ||
// only owner can withdraw funds by default deployer | ||
IBalanceWithdrawable(distributor).withdraw(user, expected, token); | ||
vm.stopPrank(); | ||
|
||
assertEq(IERC20(token).balanceOf(user), expected); | ||
|
||
} | ||
|
||
function test_Withdraw_EmitFundsWithdrawn() public { | ||
// created with an initial endpoint | ||
uint256 expected = 100 * 1e18; | ||
address distributor = deployDistributor("1.1.1.1"); | ||
|
||
vm.startPrank(admin); // only owner can get balance by default deployer | ||
IERC20(token).transfer(distributor, expected); | ||
// only owner can withdraw funds by default deployer | ||
vm.expectEmit(true, true, false, true, address(distributor)); | ||
emit IBalanceWithdrawable.FundsWithdrawn(user, expected, token); | ||
IBalanceWithdrawable(distributor).withdraw(user, expected, token); | ||
} | ||
|
||
function test_Withdraw_RevertWhen_NoBalance() public { | ||
// created with an initial endpoint | ||
uint256 expected = 100 * 1e18; | ||
address distributor = deployDistributor("1.1.1.1"); | ||
|
||
vm.startPrank(admin); // only owner can get balance by default deployer | ||
vm.expectRevert(abi.encodeWithSignature("NoFundsToWithdraw()")); | ||
IBalanceWithdrawable(distributor).withdraw(user, expected, token); | ||
} | ||
|
||
function test_Withdraw_RevertWhen_InvalidOwner() public { | ||
// created with an initial endpoint | ||
uint256 expected = 100 * 1e18; | ||
address distributor = deployDistributor("1.1.1.1"); | ||
|
||
vm.prank(user); | ||
vm.expectRevert(abi.encodeWithSignature("OwnableUnauthorizedAccount(address)", user)); | ||
IBalanceWithdrawable(distributor).withdraw(user, expected, token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import hre from 'hardhat' | ||
import { expect } from 'chai' | ||
import { switcher, getAccounts } from './helpers/CommonHelper' | ||
import { ContentVault, Ownership, Referendum } from '@/typechain-types' | ||
import { | ||
deployInitializedContentVault | ||
} from './helpers/ContentVaultHelper' | ||
|
||
|
||
|
||
describe('ContentVault', function () { | ||
|
||
const tests: { id: number, name: string, data: string }[] = [ | ||
// example storing LIT in format: chain.ciphertext.dataToEncryptHash | ||
// This data is needed in decryption process and obtained during encryption. | ||
// https://developer.litprotocol.com/sdk/access-control/quick-start#encryption | ||
{ id: 10, name: 'LIT', data: "ethereum.dGVzdF9zZWN1cmVMb25nQ29udGVudF93YXRjaGl0X3Byb3RvY29sX3ZhdWx0c3RvcmFnZQ==.4Kw2DRwEmqK25fOi4bnJ6KLgs8O5gpmxu25VfAUBXXQ=" }, | ||
// RSA | ||
{ id: 20, name: 'RSA', data: "8c7e50310a8fc4be1bbadfcd8e9359c8b304dbd96dbe1f5b8ee5b8a249b19fc8403e80aeb2a4b9ebec50fabe98b6e632858571aeb4bde8de2a6471d9a41b1b7c5082d2f2" }, | ||
// chachapoly | ||
{ id: 30, name: 'ChaChaPoly', data: "e4bfc4a68d3017c1c50cbb65.f579fc8e4f8e917127cd6d10a85ccbf2.976f64c6011a0a94b9495c1bcf5e7e4ecff4c4e1e9f5293d59b19670f7e945a8a3f1b5045fbe7255ec3d41b5" }, | ||
// AES | ||
{ id: 40, name: 'AES', data: "2b2e31fc7c0e47818e18d12b.724f1e73e41f6c5e8bc14a7c6f4d481d.3c4a42b0fcd3e4d5856f8e55b5e8f7e45ac8e58ed8bde3b7d1c58bd74eb3d407ab59b252de04a8c390bbd5" } | ||
]; | ||
|
||
let referendum: Referendum; | ||
let contentVault: ContentVault; | ||
let ownership: Ownership; | ||
|
||
let owner: hre.ethers.HardhatEthersSigner; | ||
let governor: hre.ethers.HardhatEthersSigner; | ||
|
||
before(async () => { | ||
[ | ||
contentVault, | ||
ownership, | ||
referendum | ||
] = await switcher(deployInitializedContentVault); | ||
|
||
[owner, governor] = await getAccounts() | ||
// we need set a governor fake account | ||
await (await referendum.setGovernance(governor)).wait() | ||
// then as "governor account" set a verified role | ||
// to bypass register only approved content.. | ||
await (await referendum.connect(governor).grantVerifiedRole(owner)).wait(); | ||
}) | ||
|
||
tests.forEach((test) => { | ||
|
||
it(`Should store and retrieve ${test.name} successfully.`, async function () { | ||
// first needed to register content and prove ownership to set secure data | ||
await (await ownership.registerContent(owner, test.id)).wait(); | ||
const encoder = hre.ethers.AbiCoder.defaultAbiCoder() | ||
const cypherBytes = encoder.encode(["string"], [test.data]) | ||
await contentVault.setContent(test.id, cypherBytes); | ||
|
||
const expected = await contentVault.getContent(test.id) | ||
const decoded = encoder.decode(["string"], expected)[0]; | ||
expect(decoded).to.be.equal(test.data) | ||
}) | ||
}) | ||
|
||
}) |