@snapshot-labs/snapshot.js 0.12.52 → 0.12.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2827,6 +2827,21 @@ var networks = {
2827
2827
  },
2828
2828
  start: 3525964,
2829
2829
  logo: "ipfs://bafkreidqkleori7pmilesz4t52iebebaqf3oflzmoz646qfuaznanb3sgm"
2830
+ },
2831
+ "4162": {
2832
+ key: "4162",
2833
+ name: "SX Rollup",
2834
+ shortName: "mainnet",
2835
+ chainId: 4162,
2836
+ network: "mainnet",
2837
+ multicall: "0x46997eb05F6A6A0bD5C140ABed09Cb02CA36b98c",
2838
+ rpc: [
2839
+ ],
2840
+ explorer: {
2841
+ url: "https://explorerl2.sx.technology"
2842
+ },
2843
+ start: 1847483,
2844
+ logo: "ipfs://bafkreibjkjx7evbw74e3sx65dtmcdr4g2rrbcorb2ivd6jz6yh6on6ozw4"
2830
2845
  },
2831
2846
  "4200": {
2832
2847
  key: "4200",
@@ -3976,36 +3991,46 @@ class CopelandVoting {
3976
3991
  const validVotes = this.getValidVotes();
3977
3992
  const choicesCount = this.proposal.choices.length;
3978
3993
  const pairwiseComparisons = Array.from({ length: choicesCount }, () => Array(choicesCount).fill(0));
3994
+ const totalVotingPower = this.getScoresTotal();
3979
3995
  // Calculate pairwise comparisons
3980
3996
  for (const vote of validVotes) {
3981
- for (let i = 0; i < vote.choice.length; i++) {
3982
- for (let j = i + 1; j < vote.choice.length; j++) {
3983
- const winner = vote.choice[i] - 1;
3984
- const loser = vote.choice[j] - 1;
3985
- pairwiseComparisons[winner][loser] += vote.balance;
3986
- pairwiseComparisons[loser][winner] -= vote.balance;
3997
+ for (let currentRank = 0; currentRank < vote.choice.length; currentRank++) {
3998
+ for (let nextRank = currentRank + 1; nextRank < vote.choice.length; nextRank++) {
3999
+ const preferredChoice = vote.choice[currentRank] - 1;
4000
+ const lowerChoice = vote.choice[nextRank] - 1;
4001
+ pairwiseComparisons[preferredChoice][lowerChoice] += vote.balance;
4002
+ pairwiseComparisons[lowerChoice][preferredChoice] -= vote.balance;
3987
4003
  }
3988
4004
  }
3989
4005
  }
3990
4006
  // Calculate Copeland scores
3991
4007
  const scores = Array(choicesCount).fill(0);
3992
- for (let i = 0; i < choicesCount; i++) {
3993
- for (let j = 0; j < choicesCount; j++) {
3994
- if (i !== j) {
3995
- if (pairwiseComparisons[i][j] > 0) {
3996
- scores[i]++;
4008
+ let totalCopelandScore = 0;
4009
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4010
+ for (let opponentIndex = 0; opponentIndex < choicesCount; opponentIndex++) {
4011
+ if (choiceIndex !== opponentIndex) {
4012
+ const comparison = pairwiseComparisons[choiceIndex][opponentIndex];
4013
+ if (comparison > 0) {
4014
+ scores[choiceIndex]++;
3997
4015
  }
3998
- else if (pairwiseComparisons[i][j] < 0) {
3999
- scores[j]++;
4016
+ else if (comparison < 0) {
4017
+ scores[opponentIndex]++;
4000
4018
  }
4001
4019
  else {
4002
- scores[i] += 0.5;
4003
- scores[j] += 0.5;
4020
+ scores[choiceIndex] += 0.5;
4021
+ scores[opponentIndex] += 0.5;
4004
4022
  }
4005
4023
  }
4006
4024
  }
4007
4025
  }
4008
- return scores;
4026
+ // Calculate total Copeland score for normalization
4027
+ totalCopelandScore = scores.reduce((sum, score) => sum + score, 0);
4028
+ // Normalize scores to distribute voting power
4029
+ if (totalCopelandScore > 0) {
4030
+ return scores.map((score) => (score / totalCopelandScore) * totalVotingPower);
4031
+ }
4032
+ // If no clear winners, distribute power equally
4033
+ return scores.map(() => totalVotingPower / choicesCount);
4009
4034
  }
4010
4035
  // Calculates the Copeland scores for each choice, broken down by strategy
4011
4036
  getScoresByStrategy() {
@@ -4013,40 +4038,74 @@ class CopelandVoting {
4013
4038
  const choicesCount = this.proposal.choices.length;
4014
4039
  const strategiesCount = this.strategies.length;
4015
4040
  const pairwiseComparisons = Array.from({ length: choicesCount }, () => Array.from({ length: choicesCount }, () => Array(strategiesCount).fill(0)));
4041
+ // Calculate total voting power per strategy
4042
+ const strategyTotals = Array(strategiesCount).fill(0);
4043
+ for (const vote of validVotes) {
4044
+ for (let i = 0; i < strategiesCount; i++) {
4045
+ strategyTotals[i] += vote.scores[i];
4046
+ }
4047
+ }
4016
4048
  // Calculate pairwise comparisons for each strategy
4017
4049
  for (const vote of validVotes) {
4018
- for (let i = 0; i < vote.choice.length; i++) {
4019
- for (let j = i + 1; j < vote.choice.length; j++) {
4020
- const winner = vote.choice[i] - 1;
4021
- const loser = vote.choice[j] - 1;
4022
- for (let s = 0; s < strategiesCount; s++) {
4023
- pairwiseComparisons[winner][loser][s] += vote.scores[s];
4024
- pairwiseComparisons[loser][winner][s] -= vote.scores[s];
4050
+ for (let currentRank = 0; currentRank < vote.choice.length; currentRank++) {
4051
+ for (let nextRank = currentRank + 1; nextRank < vote.choice.length; nextRank++) {
4052
+ const preferredChoice = vote.choice[currentRank] - 1;
4053
+ const lowerChoice = vote.choice[nextRank] - 1;
4054
+ for (let strategyIndex = 0; strategyIndex < strategiesCount; strategyIndex++) {
4055
+ pairwiseComparisons[preferredChoice][lowerChoice][strategyIndex] +=
4056
+ vote.scores[strategyIndex];
4057
+ pairwiseComparisons[lowerChoice][preferredChoice][strategyIndex] -=
4058
+ vote.scores[strategyIndex];
4025
4059
  }
4026
4060
  }
4027
4061
  }
4028
4062
  }
4029
4063
  // Calculate Copeland scores for each strategy
4030
4064
  const scores = Array.from({ length: choicesCount }, () => Array(strategiesCount).fill(0));
4031
- for (let i = 0; i < choicesCount; i++) {
4032
- for (let j = 0; j < choicesCount; j++) {
4033
- if (i !== j) {
4034
- for (let s = 0; s < strategiesCount; s++) {
4035
- if (pairwiseComparisons[i][j][s] > 0) {
4036
- scores[i][s]++;
4065
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4066
+ for (let opponentIndex = 0; opponentIndex < choicesCount; opponentIndex++) {
4067
+ if (choiceIndex !== opponentIndex) {
4068
+ for (let strategyIndex = 0; strategyIndex < strategiesCount; strategyIndex++) {
4069
+ const comparison = pairwiseComparisons[choiceIndex][opponentIndex][strategyIndex];
4070
+ if (comparison > 0) {
4071
+ scores[choiceIndex][strategyIndex]++;
4037
4072
  }
4038
- else if (pairwiseComparisons[i][j][s] < 0) {
4039
- scores[j][s]++;
4073
+ else if (comparison < 0) {
4074
+ scores[opponentIndex][strategyIndex]++;
4040
4075
  }
4041
4076
  else {
4042
- scores[i][s] += 0.5;
4043
- scores[j][s] += 0.5;
4077
+ scores[choiceIndex][strategyIndex] += 0.5;
4078
+ scores[opponentIndex][strategyIndex] += 0.5;
4044
4079
  }
4045
4080
  }
4046
4081
  }
4047
4082
  }
4048
4083
  }
4049
- return scores;
4084
+ // Normalize scores by strategy to distribute voting power
4085
+ const normalizedScores = Array.from({ length: choicesCount }, () => Array(strategiesCount).fill(0));
4086
+ for (let strategyIndex = 0; strategyIndex < strategiesCount; strategyIndex++) {
4087
+ // Calculate total Copeland score for this strategy
4088
+ let totalCopelandScore = 0;
4089
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4090
+ totalCopelandScore += scores[choiceIndex][strategyIndex];
4091
+ }
4092
+ // Normalize scores to distribute voting power for this strategy
4093
+ if (totalCopelandScore > 0) {
4094
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4095
+ normalizedScores[choiceIndex][strategyIndex] =
4096
+ (scores[choiceIndex][strategyIndex] / totalCopelandScore) *
4097
+ strategyTotals[strategyIndex];
4098
+ }
4099
+ }
4100
+ else if (strategyTotals[strategyIndex] > 0) {
4101
+ // If no clear winners, distribute power equally for this strategy
4102
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4103
+ normalizedScores[choiceIndex][strategyIndex] =
4104
+ strategyTotals[strategyIndex] / choicesCount;
4105
+ }
4106
+ }
4107
+ }
4108
+ return normalizedScores;
4050
4109
  }
4051
4110
  // Calculates the total score (sum of all valid vote balances)
4052
4111
  getScoresTotal() {
@@ -2817,6 +2817,21 @@ var networks = {
2817
2817
  },
2818
2818
  start: 3525964,
2819
2819
  logo: "ipfs://bafkreidqkleori7pmilesz4t52iebebaqf3oflzmoz646qfuaznanb3sgm"
2820
+ },
2821
+ "4162": {
2822
+ key: "4162",
2823
+ name: "SX Rollup",
2824
+ shortName: "mainnet",
2825
+ chainId: 4162,
2826
+ network: "mainnet",
2827
+ multicall: "0x46997eb05F6A6A0bD5C140ABed09Cb02CA36b98c",
2828
+ rpc: [
2829
+ ],
2830
+ explorer: {
2831
+ url: "https://explorerl2.sx.technology"
2832
+ },
2833
+ start: 1847483,
2834
+ logo: "ipfs://bafkreibjkjx7evbw74e3sx65dtmcdr4g2rrbcorb2ivd6jz6yh6on6ozw4"
2820
2835
  },
2821
2836
  "4200": {
2822
2837
  key: "4200",
@@ -3966,36 +3981,46 @@ class CopelandVoting {
3966
3981
  const validVotes = this.getValidVotes();
3967
3982
  const choicesCount = this.proposal.choices.length;
3968
3983
  const pairwiseComparisons = Array.from({ length: choicesCount }, () => Array(choicesCount).fill(0));
3984
+ const totalVotingPower = this.getScoresTotal();
3969
3985
  // Calculate pairwise comparisons
3970
3986
  for (const vote of validVotes) {
3971
- for (let i = 0; i < vote.choice.length; i++) {
3972
- for (let j = i + 1; j < vote.choice.length; j++) {
3973
- const winner = vote.choice[i] - 1;
3974
- const loser = vote.choice[j] - 1;
3975
- pairwiseComparisons[winner][loser] += vote.balance;
3976
- pairwiseComparisons[loser][winner] -= vote.balance;
3987
+ for (let currentRank = 0; currentRank < vote.choice.length; currentRank++) {
3988
+ for (let nextRank = currentRank + 1; nextRank < vote.choice.length; nextRank++) {
3989
+ const preferredChoice = vote.choice[currentRank] - 1;
3990
+ const lowerChoice = vote.choice[nextRank] - 1;
3991
+ pairwiseComparisons[preferredChoice][lowerChoice] += vote.balance;
3992
+ pairwiseComparisons[lowerChoice][preferredChoice] -= vote.balance;
3977
3993
  }
3978
3994
  }
3979
3995
  }
3980
3996
  // Calculate Copeland scores
3981
3997
  const scores = Array(choicesCount).fill(0);
3982
- for (let i = 0; i < choicesCount; i++) {
3983
- for (let j = 0; j < choicesCount; j++) {
3984
- if (i !== j) {
3985
- if (pairwiseComparisons[i][j] > 0) {
3986
- scores[i]++;
3998
+ let totalCopelandScore = 0;
3999
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4000
+ for (let opponentIndex = 0; opponentIndex < choicesCount; opponentIndex++) {
4001
+ if (choiceIndex !== opponentIndex) {
4002
+ const comparison = pairwiseComparisons[choiceIndex][opponentIndex];
4003
+ if (comparison > 0) {
4004
+ scores[choiceIndex]++;
3987
4005
  }
3988
- else if (pairwiseComparisons[i][j] < 0) {
3989
- scores[j]++;
4006
+ else if (comparison < 0) {
4007
+ scores[opponentIndex]++;
3990
4008
  }
3991
4009
  else {
3992
- scores[i] += 0.5;
3993
- scores[j] += 0.5;
4010
+ scores[choiceIndex] += 0.5;
4011
+ scores[opponentIndex] += 0.5;
3994
4012
  }
3995
4013
  }
3996
4014
  }
3997
4015
  }
3998
- return scores;
4016
+ // Calculate total Copeland score for normalization
4017
+ totalCopelandScore = scores.reduce((sum, score) => sum + score, 0);
4018
+ // Normalize scores to distribute voting power
4019
+ if (totalCopelandScore > 0) {
4020
+ return scores.map((score) => (score / totalCopelandScore) * totalVotingPower);
4021
+ }
4022
+ // If no clear winners, distribute power equally
4023
+ return scores.map(() => totalVotingPower / choicesCount);
3999
4024
  }
4000
4025
  // Calculates the Copeland scores for each choice, broken down by strategy
4001
4026
  getScoresByStrategy() {
@@ -4003,40 +4028,74 @@ class CopelandVoting {
4003
4028
  const choicesCount = this.proposal.choices.length;
4004
4029
  const strategiesCount = this.strategies.length;
4005
4030
  const pairwiseComparisons = Array.from({ length: choicesCount }, () => Array.from({ length: choicesCount }, () => Array(strategiesCount).fill(0)));
4031
+ // Calculate total voting power per strategy
4032
+ const strategyTotals = Array(strategiesCount).fill(0);
4033
+ for (const vote of validVotes) {
4034
+ for (let i = 0; i < strategiesCount; i++) {
4035
+ strategyTotals[i] += vote.scores[i];
4036
+ }
4037
+ }
4006
4038
  // Calculate pairwise comparisons for each strategy
4007
4039
  for (const vote of validVotes) {
4008
- for (let i = 0; i < vote.choice.length; i++) {
4009
- for (let j = i + 1; j < vote.choice.length; j++) {
4010
- const winner = vote.choice[i] - 1;
4011
- const loser = vote.choice[j] - 1;
4012
- for (let s = 0; s < strategiesCount; s++) {
4013
- pairwiseComparisons[winner][loser][s] += vote.scores[s];
4014
- pairwiseComparisons[loser][winner][s] -= vote.scores[s];
4040
+ for (let currentRank = 0; currentRank < vote.choice.length; currentRank++) {
4041
+ for (let nextRank = currentRank + 1; nextRank < vote.choice.length; nextRank++) {
4042
+ const preferredChoice = vote.choice[currentRank] - 1;
4043
+ const lowerChoice = vote.choice[nextRank] - 1;
4044
+ for (let strategyIndex = 0; strategyIndex < strategiesCount; strategyIndex++) {
4045
+ pairwiseComparisons[preferredChoice][lowerChoice][strategyIndex] +=
4046
+ vote.scores[strategyIndex];
4047
+ pairwiseComparisons[lowerChoice][preferredChoice][strategyIndex] -=
4048
+ vote.scores[strategyIndex];
4015
4049
  }
4016
4050
  }
4017
4051
  }
4018
4052
  }
4019
4053
  // Calculate Copeland scores for each strategy
4020
4054
  const scores = Array.from({ length: choicesCount }, () => Array(strategiesCount).fill(0));
4021
- for (let i = 0; i < choicesCount; i++) {
4022
- for (let j = 0; j < choicesCount; j++) {
4023
- if (i !== j) {
4024
- for (let s = 0; s < strategiesCount; s++) {
4025
- if (pairwiseComparisons[i][j][s] > 0) {
4026
- scores[i][s]++;
4055
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4056
+ for (let opponentIndex = 0; opponentIndex < choicesCount; opponentIndex++) {
4057
+ if (choiceIndex !== opponentIndex) {
4058
+ for (let strategyIndex = 0; strategyIndex < strategiesCount; strategyIndex++) {
4059
+ const comparison = pairwiseComparisons[choiceIndex][opponentIndex][strategyIndex];
4060
+ if (comparison > 0) {
4061
+ scores[choiceIndex][strategyIndex]++;
4027
4062
  }
4028
- else if (pairwiseComparisons[i][j][s] < 0) {
4029
- scores[j][s]++;
4063
+ else if (comparison < 0) {
4064
+ scores[opponentIndex][strategyIndex]++;
4030
4065
  }
4031
4066
  else {
4032
- scores[i][s] += 0.5;
4033
- scores[j][s] += 0.5;
4067
+ scores[choiceIndex][strategyIndex] += 0.5;
4068
+ scores[opponentIndex][strategyIndex] += 0.5;
4034
4069
  }
4035
4070
  }
4036
4071
  }
4037
4072
  }
4038
4073
  }
4039
- return scores;
4074
+ // Normalize scores by strategy to distribute voting power
4075
+ const normalizedScores = Array.from({ length: choicesCount }, () => Array(strategiesCount).fill(0));
4076
+ for (let strategyIndex = 0; strategyIndex < strategiesCount; strategyIndex++) {
4077
+ // Calculate total Copeland score for this strategy
4078
+ let totalCopelandScore = 0;
4079
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4080
+ totalCopelandScore += scores[choiceIndex][strategyIndex];
4081
+ }
4082
+ // Normalize scores to distribute voting power for this strategy
4083
+ if (totalCopelandScore > 0) {
4084
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4085
+ normalizedScores[choiceIndex][strategyIndex] =
4086
+ (scores[choiceIndex][strategyIndex] / totalCopelandScore) *
4087
+ strategyTotals[strategyIndex];
4088
+ }
4089
+ }
4090
+ else if (strategyTotals[strategyIndex] > 0) {
4091
+ // If no clear winners, distribute power equally for this strategy
4092
+ for (let choiceIndex = 0; choiceIndex < choicesCount; choiceIndex++) {
4093
+ normalizedScores[choiceIndex][strategyIndex] =
4094
+ strategyTotals[strategyIndex] / choicesCount;
4095
+ }
4096
+ }
4097
+ }
4098
+ return normalizedScores;
4040
4099
  }
4041
4100
  // Calculates the total score (sum of all valid vote balances)
4042
4101
  getScoresTotal() {