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

Governance v3 #1984

Merged
merged 269 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
269 commits
Select commit Hold shift + click to select a range
1ca2ff6
first successful ccip send of createCrossChainPool
dbeal-eth Jun 1, 2023
4b4e718
Merge branch 'main' into xchain-pools
barrasso Jun 3, 2023
7df95a8
fixes to load contract from misc source
dbeal-eth Jun 3, 2023
3e9793d
Add transfer cross chain USD tests (#1634)
barrasso Jun 6, 2023
7accba1
Merge branch 'main' into xchain-pools
barrasso Jun 6, 2023
b4f29ec
fix lint
barrasso Jun 7, 2023
7da2943
Merge branch 'xchain-pools' of https://github.com/Synthetixio/synthet…
dbeal-eth Jun 8, 2023
448d995
Merge remote-tracking branch 'origin/main' into xchain-pools
dbeal-eth Jun 8, 2023
1042046
update transferCrossChain (#1650)
barrasso Jun 13, 2023
52ca63a
Merge branch 'main' into xchain-pools
barrasso Jun 13, 2023
d8e72c7
fix sol lint and revert deps change
barrasso Jun 13, 2023
66cd31a
fixes
dbeal-eth Jun 14, 2023
0533ccf
Merge branch 'xchain-pools' of https://github.com/Synthetixio/synthet…
dbeal-eth Jun 14, 2023
a824f61
Merge branch 'main' into 'xchain-pools'
barrasso Jun 20, 2023
43fd0b2
Add TODOs
barrasso Jun 22, 2023
02cc76b
Merge branch 'main' into xchain-pools
barrasso Jun 22, 2023
fc97fca
Merge branch 'main' into xchain-pools
barrasso Jun 22, 2023
a66e7a9
Merge branch 'xchain-pools' into gov-2.0.0
barrasso Jun 22, 2023
a1c1a89
run yarn
barrasso Jun 23, 2023
28d0165
Merge branch 'xchain-pools' into gov-2.0.0
barrasso Jun 23, 2023
3edc204
Use EVMTokenAmount amount
barrasso Jun 23, 2023
a5031c8
Merge branch 'xchain-pools' into gov-2.0.0
barrasso Jun 23, 2023
8852ee4
Update package.json scripts
barrasso Jun 28, 2023
d6af1b6
wip gov updates
barrasso Jun 28, 2023
f2e3905
remove upgrademodule tests
mjlescano Jul 7, 2023
42d979f
fix governance storage definition
mjlescano Jul 7, 2023
18f7491
add governance tests bootstrap
mjlescano Jul 7, 2023
cf8ad60
Migrate initilization tests
mjlescano Jul 7, 2023
c90d23b
comment unmigrated tests
mjlescano Jul 7, 2023
7d10781
partially migrate ElectionModule tests
mjlescano Jul 7, 2023
09fd67e
update testable image
mjlescano Jul 11, 2023
910b643
update version to 2.0.0-alpha.0
mjlescano Jul 11, 2023
d594097
fix default epoch start date
mjlescano Jul 11, 2023
935396d
refactor DebtShare tests location
mjlescano Jul 14, 2023
f7f2a79
fix first epoch index initialization
mjlescano Jul 14, 2023
1fb4672
fix getting of snapshot id before the array is initialized
mjlescano Jul 14, 2023
0a231fd
update assertEvent usage
mjlescano Jul 14, 2023
8df4672
fix merkle tree getter
mjlescano Jul 14, 2023
1912ba9
fix debtshare initialization
mjlescano Jul 14, 2023
f0e61d7
fix CouncilToken proxy usage
mjlescano Jul 14, 2023
a31edf6
fix CouncilToken proxy usage
mjlescano Jul 14, 2023
c4497cb
fix tests
mjlescano Jul 14, 2023
96b9279
fix settings initialization
mjlescano Jul 15, 2023
c33ecbf
remove unnecessary check
mjlescano Jul 15, 2023
3817130
Merge branch 'main' into gov-2.0.0
barrasso Jul 17, 2023
fcba359
revert xchain-pools changes
barrasso Jul 17, 2023
d3e338c
revert unneeded changes
barrasso Jul 17, 2023
49e4fba
Merge branch 'main' into gov-2.0.0
barrasso Jul 18, 2023
937e273
Fix governance epoch settings storage (#1722)
mjlescano Jul 19, 2023
a68a2be
fix coeverage filter
mjlescano Jul 20, 2023
4851730
refactor proxy deployment on tests
mjlescano Jul 20, 2023
a579085
Merge branch 'main' into gov-2.0.0
barrasso Jul 21, 2023
a403e72
fix core-utils tests
mjlescano Jul 21, 2023
8fa0bce
Fix deps
noisekit Jul 22, 2023
ef7a931
Ignore and work around subgraph AssemblyScrypt in depcheck
noisekit Jul 22, 2023
e429cf0
Fix epoch schedule tweak & next election settings update (#1725)
mjlescano Jul 26, 2023
dd3ba62
Xchain voting (#1728)
barrasso Jul 27, 2023
7ef7b83
add UpgradeProposalModule (#1726)
mjlescano Jul 27, 2023
0ae87e0
Add missing modifiers
barrasso Jul 27, 2023
3919942
disable elections flow tests
mjlescano Jul 31, 2023
b2237b6
bit of a refactor for how ballots are counted (#1734)
dbeal-eth Aug 1, 2023
ceed135
fix cannonfile build
mjlescano Aug 1, 2023
b0d6507
Merge branch 'main' into gov-2.0.0
mjlescano Aug 1, 2023
8d86d30
fix a couple tests
dbeal-eth Aug 2, 2023
a089120
remove unused assert
mjlescano Aug 3, 2023
2b3e111
Merge branch 'main' into gov-2.0.0
mjlescano Aug 8, 2023
4db3c9f
Snapshot vote power tests (#1749)
dbeal-eth Aug 8, 2023
34f9ad1
revert crosschain storage layout. treat the first chain ID in the sup…
barrasso Aug 8, 2023
b19fa66
fix tests generation (#1770)
mjlescano Aug 14, 2023
698ffd2
Add Owner Guardianship (#1732)
mjlescano Aug 15, 2023
7c92934
refactor CrossChain into core-modules (#1756)
barrasso Aug 15, 2023
f46162e
fix OwnerModule compilation
mjlescano Aug 16, 2023
42a56db
Rename lastElectionId -> currentElectionId (#1780)
mjlescano Aug 16, 2023
2f57d56
Remove unnecessary ProxyStorage inheritance
mjlescano Aug 16, 2023
3578625
Governance Initialization Fixes (#1781)
mjlescano Aug 21, 2023
ed0440c
Remove guardianship & delayed upgrade functionalities (#1801)
mjlescano Sep 1, 2023
a55303e
Add satellites NFT distribution (#1787)
mjlescano Sep 1, 2023
47987fa
Merge remote-tracking branch 'origin/main' into gov-2.0.0
mjlescano Sep 1, 2023
bcbf441
Merge remote-tracking branch 'origin/main' into gov-2.0.0
mjlescano Sep 14, 2023
0130611
remove BaseElectionModule in favor of a single ElectionModule (#1826)
mjlescano Sep 14, 2023
9da0826
add CrossChain module to gov cannonfile
mjlescano Sep 18, 2023
e9d06d2
add base ElectionModuleSatellite file
mjlescano Sep 15, 2023
b50b6a5
add election module satellite
mjlescano Sep 20, 2023
1be8b1d
refactor cannonfiles
mjlescano Sep 20, 2023
b7fe0e9
add members dismissal logic
mjlescano Sep 20, 2023
1e7160b
add CCIP epoch resolution
mjlescano Sep 20, 2023
e2ce215
remove unnecessary chain number validator
mjlescano Sep 20, 2023
b10f4af
refactor council members storage
mjlescano Sep 20, 2023
f7299fb
fix usage of council members storage
mjlescano Sep 20, 2023
d9ffd79
add satellite council module initialization
mjlescano Sep 20, 2023
9b68f06
fix AddressSet usage
mjlescano Sep 21, 2023
80c6102
fix cannonfiles dependencies
mjlescano Sep 21, 2023
5a35a4d
fix satellite cannonfile build
mjlescano Sep 21, 2023
f364f6c
added sepolina support
fritzschoff Oct 2, 2023
4a154ca
using getter function
fritzschoff Oct 2, 2023
0f8bfb1
Refactor ccip config to extract core settings (#1853)
mjlescano Oct 3, 2023
bbafd6c
fixed typo
fritzschoff Oct 3, 2023
93227c2
removed unused settings
fritzschoff Oct 3, 2023
d026dc4
make cast func payable
fritzschoff Oct 4, 2023
50fa5fe
support crosschain voting
fritzschoff Oct 9, 2023
fe55748
update gov satellite cannonfile package name
mjlescano Oct 10, 2023
1220df5
refactor cannonfile settings
mjlescano Oct 10, 2023
0e3f828
add cross chain address target setting on initialization
mjlescano Oct 10, 2023
bb52cc8
Merge branch 'main' into gov-2.0.0
mjlescano Oct 10, 2023
0e2be6a
Merge branch 'gov-2.0.0' into add-satellite-election-module
mjlescano Oct 10, 2023
d508fbd
remove depends on cannonfiles
mjlescano Oct 10, 2023
4cc021c
fix test cannonfile version
mjlescano Oct 10, 2023
fc4ca26
fix ccip config
mjlescano Oct 10, 2023
bfeb068
fix msg.sender usage
mjlescano Oct 10, 2023
22c50d2
refactor satellite init
mjlescano Oct 10, 2023
c457aea
add base for cross chain integration tests
mjlescano Oct 11, 2023
ce85cc2
add ccip router
mjlescano Oct 11, 2023
87f4979
add multiple chains connections
mjlescano Oct 12, 2023
7551f92
add ccip router mock to gov tests
mjlescano Oct 12, 2023
a4d7ad4
add cross chain typechain types
mjlescano Oct 12, 2023
6852327
add ccip interaction
mjlescano Oct 14, 2023
571ff65
fix nodes interaction
mjlescano Oct 16, 2023
d2f354a
fix wallet fixture
mjlescano Oct 16, 2023
f1434c6
fix dedupe
mjlescano Oct 16, 2023
2cd9922
Merge pull request #1829 from Synthetixio/add-satellite-election-module
mjlescano Oct 16, 2023
376b9da
add snapshots module to satellite
mjlescano Oct 16, 2023
156b438
Gov intergration test (#1873)
fritzschoff Oct 17, 2023
f26437f
updated the bootstrap function
fritzschoff Oct 17, 2023
a71f2e3
refactor epoch storage
mjlescano Oct 17, 2023
1b43da3
fix tests
mjlescano Oct 17, 2023
209a4ea
add votingPower calculation on satellite networks
mjlescano Oct 17, 2023
28aa791
add epoch initialization on satellites
mjlescano Oct 18, 2023
04dc9ea
add epoch initialization on satellites
mjlescano Oct 18, 2023
d4cc3af
Gov 2 integration (#1876)
fritzschoff Oct 18, 2023
32319af
Merge pull request #1874 from Synthetixio/update-cross-chain-snapshots
mjlescano Oct 18, 2023
91eaa1f
Remove unused supportedNetworkTargets config (#1877)
mjlescano Oct 18, 2023
d35cce5
fix tests
fritzschoff Oct 19, 2023
d1683a4
Merge branch 'main' into gov-2.0.0
mjlescano Oct 20, 2023
eb5df6a
Merge branch 'main' into gov-2.0.0
Rickk137 Oct 23, 2023
16c129c
update cannon file
fritzschoff Oct 23, 2023
ebb2d7d
Fix inifinite amount of votes (#1878)
mjlescano Oct 24, 2023
aba1d3e
Old election test (#1881)
fritzschoff Nov 13, 2023
3e7b212
Merge remote-tracking branch 'origin/main' into gov-2.0.0
mjlescano Nov 13, 2023
2e13f95
fix yarn.lock
mjlescano Nov 13, 2023
bd01c16
add missing @foundry-rs/hardhat-anvil dep
mjlescano Nov 13, 2023
dda4aec
Merge remote-tracking branch 'origin/main' into gov-2.0.0
mjlescano Nov 13, 2023
c8530a0
fix test period helpers
mjlescano Nov 14, 2023
afedd2f
fix cc voting tests in different steps
mjlescano Nov 14, 2023
1e41cee
Fix mothership votes casting (#1916)
mjlescano Nov 14, 2023
35cd091
add epoch update to satellites (#1917)
mjlescano Nov 15, 2023
20ef5e9
added tests
fritzschoff Nov 15, 2023
ac8fdd2
fix payable functions
mjlescano Nov 15, 2023
a10142e
moved validation
fritzschoff Nov 15, 2023
bb1d99c
Fix snapshot contract management (#1920)
mjlescano Nov 16, 2023
931ff9e
ccip event (#1923)
fritzschoff Nov 16, 2023
0d41e01
remove possibility of gov nft transfers (#1924)
mjlescano Nov 16, 2023
63cb756
add epoch index validation on vote casting (#1925)
mjlescano Nov 16, 2023
34af606
add refundLeftoverGas call on broadcast (#1926)
mjlescano Nov 16, 2023
7014727
Fix small issues (#1927)
mjlescano Nov 16, 2023
80dce1e
Iosiro val candidate (#1929)
fritzschoff Nov 16, 2023
0ef9e99
Refactor council tokenid (#1930)
mjlescano Nov 16, 2023
231f73b
add multi chain dev task
mjlescano Nov 16, 2023
6ba1b05
add cross chain message listener
mjlescano Nov 16, 2023
6318c89
fix cross chain signer param
mjlescano Nov 16, 2023
0a5c5c5
updated dev comnand
fritzschoff Dec 11, 2023
b037a31
added candidates to event
fritzschoff Dec 12, 2023
b704c4e
add dummy withdrawVote fn
mjlescano Dec 14, 2023
5c68699
removed only
fritzschoff Jan 5, 2024
0cfec90
remove loop
fritzschoff Jan 5, 2024
ac4999b
add trusted_multicall_forwarder to cannonfile
mjlescano Jan 9, 2024
d62b444
fix setting location
mjlescano Jan 18, 2024
b6bf834
add presets to cannonfiles
mjlescano Jan 18, 2024
323b318
Merge remote-tracking branch 'origin/main' into gov-2.0.0
mjlescano Jan 18, 2024
95d38fb
remove changes to protocol/synthetix
mjlescano Jan 18, 2024
5f824ce
update yarn.lock
mjlescano Jan 18, 2024
001b343
update storage.dump.sol
mjlescano Jan 18, 2024
9af8e7e
fix package.json
mjlescano Jan 18, 2024
06a21bb
update hardhat-cannon to 2.10.8-alpha.1
mjlescano Jan 18, 2024
edecb19
bump to 3.0.0-rc.0
mjlescano Jan 18, 2024
88cadc7
rename satellite package
mjlescano Jan 19, 2024
d887e8e
add cannonfile for snapshot record mock
mjlescano Jan 24, 2024
47eab3a
add create2 and salt to snapshot record
mjlescano Jan 24, 2024
9addcad
bump governance to 3.0.0
mjlescano Jan 24, 2024
b12bf40
Merge remote-tracking branch 'origin/main' into gov-3.0.0
mjlescano Jan 24, 2024
93dfab2
fix snapshot id incorrectly resolved when starting new election (#1986)
dbeal-eth Jan 24, 2024
6739132
update yarn.lock
mjlescano Jan 24, 2024
d7281b7
add highlight to proxies
mjlescano Jan 25, 2024
5aa20db
Merge remote-tracking branch 'origin/main' into gov-3.0.0
mjlescano Jan 25, 2024
772c3f8
remove CouncilToken interface from governance CoreProxy
mjlescano Jan 25, 2024
cb90340
update election module satellite init
fritzschoff Jan 25, 2024
1ef666b
added error
fritzschoff Jan 25, 2024
46dc4f9
bump gov to 3.0.1
mjlescano Jan 25, 2024
f004d98
update comments
mjlescano Jan 25, 2024
11d9f0f
fix test
fritzschoff Jan 25, 2024
2682323
fix tests
mjlescano Jan 25, 2024
8903658
bump gov to 3.0.2
mjlescano Jan 25, 2024
b10b61f
update satellite cannonfile init
mjlescano Jan 25, 2024
3f09e7f
fix satellite tests init
mjlescano Jan 25, 2024
91a6fc4
fix satellite default values
mjlescano Jan 25, 2024
61dd7ea
rename governance's proxy from CoreProxy to GovernanceProxy (#1990)
mjlescano Jan 30, 2024
f5e888f
SIP-360: Wormhole Cross Chain Integration (#2172)
moss-eth Jun 18, 2024
9f91438
reset some files to main
mjlescano Jun 18, 2024
46fca0f
reset yarn.lock
mjlescano Jun 18, 2024
ea55b55
update yarn.lock
mjlescano Jun 18, 2024
bfcabaf
update yarn.lock
mjlescano Jun 18, 2024
564f3bf
Merge remote-tracking branch 'origin/main' into gov-3.0.0-rebased
mjlescano Jun 18, 2024
143a5a3
yarn dedupe
mjlescano Jun 18, 2024
1f5dd96
fix added old contracts
mjlescano Jun 18, 2024
b423016
fix cannon dependencies
mjlescano Jun 20, 2024
3a6f1a9
fix tests to be compatible with latest cannon
mjlescano Jun 20, 2024
74e5740
update cannon to 2.15.2
mjlescano Jun 20, 2024
12d7bcc
fix events parsing
mjlescano Jun 20, 2024
adfd053
fix chainId
mjlescano Jun 20, 2024
75007b1
fixed chain id problems
moss-eth Jun 20, 2024
2413cdd
undo storage dump change
moss-eth Jun 20, 2024
875171e
undo utils hardhat config change
moss-eth Jun 20, 2024
a1ac416
prettier *
moss-eth Jun 20, 2024
8ed2238
prettier .sol
moss-eth Jun 20, 2024
bf7b96a
fix linter
mjlescano Jun 21, 2024
86b9f9c
Fix deps
noisekit Jun 21, 2024
24582b1
fix some lint issues
mjlescano Jun 21, 2024
f9e0730
fixed gov tests
moss-eth Jun 21, 2024
b2ca2df
solhint fixed errors, not warnings
moss-eth Jun 21, 2024
49f0301
fix dev script
mjlescano Jun 24, 2024
ec0f584
Merge branch 'main' into gov-3.0.0
mjlescano Jun 24, 2024
24b51f7
Remove governance ccip (#2182)
mjlescano Jun 24, 2024
b1e9361
fix node export
mjlescano Jun 24, 2024
3c45752
fixed cannonfiles
moss-eth Jun 24, 2024
1e8439d
fixed cannonfiles
moss-eth Jun 24, 2024
581a8a9
fixed tests
moss-eth Jun 24, 2024
c899768
sending cost to relayer
moss-eth Jun 25, 2024
9e78307
remove last ccip references on governance
mjlescano Jun 25, 2024
56d81e1
remove --wipe on dev task
mjlescano Jun 25, 2024
5e7d3d3
refactor mothership chain id usage
mjlescano Jun 25, 2024
0b049b3
remove Election.resolved variable
mjlescano Jun 25, 2024
ede7c62
added some comments
moss-eth Jul 1, 2024
a19421f
some more ccip cleanup
moss-eth Jul 2, 2024
5d24145
receiveWormholeMessages
moss-eth Jul 2, 2024
f28b669
interim changes
moss-eth Jul 2, 2024
1d2cf8a
fixed test relayer addresses
moss-eth Jul 2, 2024
26eef43
fixed js lint error
moss-eth Jul 2, 2024
2c57649
rewrite settings on root cannonfiles
mjlescano Jul 2, 2024
e2af4c6
add temp testnet cannonfiles
mjlescano Jul 2, 2024
827206a
passing 0 for receiver value instead of msg.value when calling transmit
moss-eth Jul 2, 2024
b5888f0
update testnet cannonfiles setup
mjlescano Jul 2, 2024
cc8ebf6
update deployer
mjlescano Jul 2, 2024
de93ef2
update testnet deployment to base sepolia
mjlescano Jul 2, 2024
ac1b574
update testnet salt
mjlescano Jul 2, 2024
f8631bd
Merge branch 'main' into gov-3.0.0
mjlescano Jul 9, 2024
0afc72d
Moss audit fixes (#2190)
moss-eth Aug 14, 2024
1b87a07
Merge branch 'main' into gov-3.0.0
barrasso Aug 14, 2024
1b7c151
fix
moss-eth Aug 14, 2024
3889794
added unit test and deleted cannon files
moss-eth Aug 14, 2024
347ece0
Merge branch 'main' into gov-3.0.0
moss-eth Aug 15, 2024
dfb5f8a
Merge branch 'main' into gov-3.0.0
MrX-SNX Aug 16, 2024
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/case-npm-1.6.3-1bd3a50e38-2fc1df75bb.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
27 changes: 27 additions & 0 deletions auxiliary/RewardsDistributor/cannonfile.snapshot.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name = "synthetix-snapshot-rewards"
version = "<%= package.version %>"
description = "Maintains snapshots of account balances in a pool"

[setting.synthetixPackage]
defaultValue = "synthetix:latest@main"

[setting.servicePoolId]
defaultValue = "0"

[setting.serviceCollateralAddress]
defaultValue = "<%= AddressZero %>"

[setting.snapper]
defaultValue = "<%= AddressZero %>"

[import.synthetix]
source = "<%= settings.synthetixPackage %>"

[contract.RewardsDistributor]
artifact = "SnapshotRewardsDistributor"
args = [
"<%= imports.synthetix.contracts.CoreProxy.address %>",
"<%= settings.servicePoolId %>",
"<%= settings.serviceCollateralAddress %>",
"<%= settings.snapper %>",
]
251 changes: 251 additions & 0 deletions auxiliary/RewardsDistributor/src/SnapshotRewardsDistributor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {ERC2771Context} from "@synthetixio/core-contracts/contracts/utils/ERC2771Context.sol";
import {IERC721} from "@synthetixio/core-contracts/contracts/interfaces/IERC721.sol";
import {IRewardDistributor} from "@synthetixio/main/contracts/interfaces/external/IRewardDistributor.sol";
import {AccessError} from "@synthetixio/core-contracts/contracts/errors/AccessError.sol";
import {ParameterError} from "@synthetixio/core-contracts/contracts/errors/ParameterError.sol";
import {IERC165} from "@synthetixio/core-contracts/contracts/interfaces/IERC165.sol";
import {ISnapshotRecord} from "@synthetixio/governance/contracts/interfaces/external/ISnapshotRecord.sol";
import {IAccountModule} from "@synthetixio/main/contracts/interfaces/IAccountModule.sol";
import {IVaultModule} from "@synthetixio/main/contracts/interfaces/IVaultModule.sol";

contract SnapshotRewardsDistributor is IRewardDistributor, ISnapshotRecord {
error AccountNotFound();

address private rewardsManager;
IERC721 private accountToken;
uint128 public servicePoolId;
address public serviceCollateralType;

struct PeriodBalance {
uint128 amount;
uint128 periodId;
address owner;
}

/**
* Addresses selected by owner which are allowed to call `takeSnapshot`
* `takeSnapshot` is not public because only a small number of snapshots can be retained for a period of time, and so they
* must be controlled to prevent censorship
*/
mapping(address => bool) public authorizedToSnapshot;

/**
* Records a user's balance as it changes from period to period.
* The last item in the array always represents the user's most recent balance
* The intermediate balance is only recorded if
* `currentPeriodId` differs (which would happen upon a call to `setCurrentPeriodId`)
*/
mapping(uint128 => PeriodBalance[]) public balances;

/**
* Records totalSupply as it changes from period to period
* Similar to `balances`, the `totalSupplyOnPeriod` at index `currentPeriodId` matches the current total supply
* Any other period ID would represent its most recent totalSupply before the period ID changed.
*/
mapping(uint256 => uint256) public totalSupplyOnPeriod;

/**
* Records the latest address to which account it is part of
*/
mapping(address => PeriodBalance[]) private accountBalances;

uint128 public currentPeriodId;

uint256 internal constant MAX_PERIOD_ITERATE = 30;

constructor(
address _rewardsManager, // SynthetixCoreProxy
uint128 _servicePoolId,
address _serviceCollateralType,
address snapper
) {
servicePoolId = _servicePoolId;
serviceCollateralType = _serviceCollateralType;
rewardsManager = _rewardsManager;
accountToken = IERC721(IAccountModule(rewardsManager).getAccountTokenAddress());
authorizedToSnapshot[snapper] = true;
}

function onPositionUpdated(
uint128 accountId,
uint128 poolId,
address collateralType,
uint256 // actorSharesD18
) external {
address sender = ERC2771Context._msgSender();
if (sender != rewardsManager) {
revert AccessError.Unauthorized(sender);
}

if (poolId != servicePoolId) {
revert ParameterError.InvalidParameter(
"poolId",
"Pool does not match the rewards pool"
);
}

if (collateralType != serviceCollateralType) {
revert ParameterError.InvalidParameter(
"collateralType",
"Collateral does not match the rewards token"
);
}

// get current account information
uint256 newAmount = IVaultModule(rewardsManager).getPositionCollateral(
accountId,
poolId,
collateralType
);
address account = accountToken.ownerOf(accountId);

// ensure periods for all the values we will be updating are correct
uint256 idIdx = updatePeriod(balances[accountId]);
uint256 oldAccountIdx = updatePeriod(accountBalances[balances[accountId][idIdx].owner]);
uint256 accountIdx = updatePeriod(accountBalances[account]);

uint256 prevBalance = balances[accountId][idIdx].amount;

// subtract balance from previous owner
// solhint-disable-next-line
accountBalances[balances[accountId][idIdx].owner][oldAccountIdx].amount -= uint128(
prevBalance
);

// add balance to new owner
// solhint-disable-next-line
accountBalances[account][accountIdx].amount += uint128(newAmount);

// update account id record
// solhint-disable-next-line
balances[accountId][idIdx].amount = uint128(newAmount);
balances[accountId][idIdx].owner = account;

totalSupplyOnPeriod[currentPeriodId] =
totalSupplyOnPeriod[currentPeriodId] +
newAmount -
prevBalance;
}

function updatePeriod(PeriodBalance[] storage bals) internal returns (uint256) {
uint256 balanceCount = bals.length;
if (balanceCount == 0 || bals[balanceCount - 1].periodId != currentPeriodId) {
// solhint-disable-next-line
bals.push(PeriodBalance(0, uint128(currentPeriodId), address(0)));

if (balanceCount > 0) {
bals[balanceCount].amount = bals[balanceCount - 1].amount;
bals[balanceCount].owner = bals[balanceCount - 1].owner;
}

balanceCount++;
}

return balanceCount - 1;
}

function balanceOfOnPeriod(address account, uint256 periodId) public view returns (uint256) {
uint256 accountPeriodHistoryCount = accountBalances[account].length;
// solhint-disable-next-line
int256 oldestHistoryIterate = int256(
MAX_PERIOD_ITERATE < accountPeriodHistoryCount
? accountPeriodHistoryCount - MAX_PERIOD_ITERATE
: 0
);
int256 i;
// solhint-disable-next-line
for (i = int256(accountPeriodHistoryCount) - 1; i >= oldestHistoryIterate; i--) {
// solhint-disable-next-line
if (accountBalances[account][uint256(i)].periodId <= periodId) {
// solhint-disable-next-line
return uint256(accountBalances[account][uint256(i)].amount);
}
}

if (i >= 0) {
revert AccountNotFound();
}

return 0;
}

function balanceOfOnPeriod(uint128 accountId, uint256 periodId) public view returns (uint256) {
uint256 accountPeriodHistoryCount = balances[accountId].length;
// solhint-disable-next-line
int256 oldestHistoryIterate = int256(
MAX_PERIOD_ITERATE < accountPeriodHistoryCount
? accountPeriodHistoryCount - MAX_PERIOD_ITERATE
: 0
);
int256 i;
// solhint-disable-next-line
for (i = int256(accountPeriodHistoryCount) - 1; i >= oldestHistoryIterate; i--) {
// solhint-disable-next-line
if (balances[accountId][uint256(i)].periodId <= periodId) {
// solhint-disable-next-line
return uint256(balances[accountId][uint256(i)].amount);
}
}

if (i >= 0) {
revert AccountNotFound();
}

return 0;
}

function balanceOf(uint128 accountId) external view returns (uint256) {
return balanceOfOnPeriod(accountId, currentPeriodId);
}

function balanceOf(address user) external view returns (uint256) {
return balanceOfOnPeriod(user, currentPeriodId);
}

function totalSupply() external view returns (uint256) {
return totalSupplyOnPeriod[currentPeriodId];
}

function takeSnapshot(uint128 id) external {
address sender = ERC2771Context._msgSender();
if (!authorizedToSnapshot[sender]) {
revert AccessError.Unauthorized(sender);
}
if (id <= currentPeriodId) {
revert ParameterError.InvalidParameter("id", "period id must always increase");
}
totalSupplyOnPeriod[id] = totalSupplyOnPeriod[currentPeriodId];
currentPeriodId = id;
}

function payout(
uint128, // accountId
uint128, // poolId_
address, // collateralType_
address, // payoutTarget_
uint256 // payoutAmount_
) external pure returns (bool) {
// this is not a rewards distributor that pays out any tokens
return true;
}

function name() public pure override returns (string memory) {
return "snapshot tracker for governance";
}

function token() public pure override returns (address) {}

/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(
bytes4 interfaceId
) public view virtual override(IERC165) returns (bool) {
return
interfaceId == type(IRewardDistributor).interfaceId ||
interfaceId == this.supportsInterface.selector;
}
}
Loading