-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaoDistributionCalculator.sol
71 lines (55 loc) · 2.38 KB
/
DaoDistributionCalculator.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { AxelarExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol';
import { IAxelarGateway } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol';
import 'hardhat/console.sol';
contract DaoDistributionCalculator is AxelarExecutable {
string public layerOneChain;
string public layerOneContractAddress;
address public immutable owner;
event SentDistributions(string destinationChain, string contractAddress, bytes payload);
constructor(address gateway_) AxelarExecutable(gateway_) {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, 'Not authorized');
_;
}
modifier validConfig() {
require(bytes(layerOneChain).length > 0, 'L1 chain not configured');
require(bytes(layerOneContractAddress).length > 0, 'L1 chain not configured');
_;
}
function configureLayerOne(
string memory layerOneChain_,
string memory layerOneContractAddress_
) external onlyOwner {
layerOneChain = layerOneChain_;
layerOneContractAddress = layerOneContractAddress_;
}
function _execute(
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
) internal override validConfig {
console.log(" -- dist: initiate _execute ");
(address[] memory addresses, uint256 tokenSupply) = abi.decode(payload, (address[], uint256));
// Do the distribution calculation
uint256[] memory amounts = calculateDistribution(addresses, tokenSupply, msg.sender);
// send the results back to the layer one
bytes memory destPayload = abi.encode(addresses, amounts);
gateway.callContract(sourceChain, sourceAddress, destPayload);
emit SentDistributions(sourceChain, sourceAddress, destPayload);
}
function calculateDistribution(
address[] memory addresses,
uint256 tokenSupply,
address sender
) internal view returns (uint256[] memory) {
uint256[] memory amounts = new uint256[](addresses.length);
for (uint i = 0; i < addresses.length; i++) {
amounts[i] = uint(keccak256(abi.encodePacked(block.timestamp, sender, addresses[i]))) % tokenSupply + 1;
}
return amounts;
}
}