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

New Merkle Distribution for Dec 1st 23 #111

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
3,963 changes: 3,963 additions & 0 deletions distributions/2023-12-01/MerkleDist.json

Large diffs are not rendered by default.

658 changes: 658 additions & 0 deletions distributions/2023-12-01/MerkleInputBonusRewards.json

Large diffs are not rendered by default.

1,050 changes: 1,050 additions & 0 deletions distributions/2023-12-01/MerkleInputPreRewards.json

Large diffs are not rendered by default.

614 changes: 614 additions & 0 deletions distributions/2023-12-01/MerkleInputTbtcv2Rewards.json

Large diffs are not rendered by default.

1,070 changes: 1,070 additions & 0 deletions distributions/2023-12-01/MerkleInputTotalRewards.json

Large diffs are not rendered by default.

2,793 changes: 2,793 additions & 0 deletions distributions/2023-12-01/tBTCv2-rewards-details/1700625971-1701388800.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions distributions/distributions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"LatestCumulativeAmount": "650469442934220952184301762",
"LatestCumulativeAmount": "658532485099719945522079260",
"CumulativeAmountByDistribution": {
"2022-07-15": "124734992091552235627767774",
"2022-09-01": "183365680150625789032093616",
Expand All @@ -17,6 +17,7 @@
"2023-09-01": "560462938437958253216409831",
"2023-10-01": "593880963299388518452439316",
"2023-11-01": "629110104280766546988133843",
"2023-11-22": "650469442934220952184301762"
"2023-11-22": "650469442934220952184301762",
"2023-12-01": "658532485099719945522079260"
}
}
58 changes: 55 additions & 3 deletions src/scripts/gen_rewards_dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require("dotenv").config()
const fs = require("fs")
const shell = require("shelljs")
const ethers = require("ethers")
const Subgraph = require("./pre-rewards/subgraph.js")
const Rewards = require("./pre-rewards/rewards.js")
const MerkleDist = require("./merkle_dist/merkle_dist.js")
Expand All @@ -13,9 +14,9 @@ const MerkleDist = require("./merkle_dist/merkle_dist.js")
const bonusWeight = 0.0
const preWeight = 0.25
const tbtcv2Weight = 0.75
const startTime = new Date("2023-11-01T00:00:00+00:00").getTime() / 1000
const endTime = new Date("2023-11-22T04:06:11+00:00").getTime() / 1000
const lastDistribution = "2023-11-01"
const startTime = new Date("2023-11-22T04:06:11+00:00").getTime() / 1000
const endTime = new Date("2023-12-01T00:00:00+00:00").getTime() / 1000
const lastDistribution = "2023-11-22"

const etherscanApiKey = process.env.ETHERSCAN_TOKEN
const subgraphApiKey = process.env.SUBGRAPH_API_KEY
Expand Down Expand Up @@ -59,6 +60,29 @@ async function main() {
return
}

// Special case: Dec 1st 23 distribution. ######
// Nov 22nd 23, legacy stakes (i.e. T stakes that originally came from Keep
// and Nu staking contracts) were deactivated. There is a period in which
// these stakers can migrate to T and stake their tokens without losing the
// rewards associated a this migration period.
// This period goes beyond the date in which this distribution is made, so
// there is no way of knowing which stakes will comply with the migration
// requirement. So the rewards for elegible legacy stakes in this period will
// be released in the following distributions.

// More info can be found here:
// https://github.com/threshold-network/solidity-contracts/issues/141
// https://forum.threshold.network/t/transition-guide-for-legacy-stakers/719
// https://etherscan.io/tx/0x68ddee6b5651d5348a40555b0079b5066d05a63196e3832323afafae0095a656

// Block height in which legacy stakers were deactivated
const blockNumber = 18624792
const legacyStakes = await Subgraph.getLegacyStakes(
graphqlApi,
blockNumber - 1
)
// ########################

// Bonus rewards calculation
if (bonusWeight > 0) {
console.log("Calculating bonus rewards...")
Expand Down Expand Up @@ -90,6 +114,34 @@ async function main() {
)
}

// ### Special case: Dec 1st 23 distribution. #####
Object.keys(legacyStakes).map((legacyStake) => {
const legacyStakeAddress = ethers.utils.getAddress(legacyStake)
delete earnedPreRewards[legacyStakeAddress]
delete earnedTbtcv2Rewards[legacyStakeAddress]
})

const rewardsDetailsPath =
"distributions/2023-12-01/tBTCv2-rewards-details/1700625971-1701388800.json"
const rewardsDetails = JSON.parse(fs.readFileSync(rewardsDetailsPath))

const rewardsDetailsFiltered = rewardsDetails.filter((rewardDetail) => {
const rewardStakingProvider = Object.keys(rewardDetail)[0].toLowerCase()
const legacyStakesStakingProviders = Object.keys(legacyStakes)
return !legacyStakesStakingProviders.includes(rewardStakingProvider)
})

fs.writeFileSync(
rewardsDetailsPath,
JSON.stringify(rewardsDetailsFiltered, null, 4)
)
// ##################################

console.log("PRE rewards ###################")
console.log(earnedPreRewards)
console.log("tBTCv2 Rewards ##################")
console.log(earnedTbtcv2Rewards)

// Add rewards earned to cumulative totals
try {
bonusRewards = JSON.parse(
Expand Down
66 changes: 66 additions & 0 deletions src/scripts/pre-rewards/subgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,69 @@ exports.getStakingHistory = async function (

return stakingHistory
}

/**
* For a specific block, return a list of stakes whose tokens were staked in Nu
* or Keep staking contracts. These staked tokens were registred as nuInT and
* keepInT in the Threshold staking contract.
* @param {string} gqlUrl Subgraph's GraphQL API URL
* @param {number} blockNumber Block at which legacy stakes were deactivated
* @returns {Object[]} Stakes info
*/
exports.getLegacyStakes = async function (gqlUrl, blockNumber) {
const legacyStakersQuery = gql`
query legacyStakersQuery($blockNumber: Int) {
accounts(
first: 1000
block: { number: $blockNumber }
where: {
stakes_: { or: [{ keepInTStake_gt: "0" }, { nuInTStake_gt: "0" }] }
}
) {
id
stakes {
id
keepInTStake
nuInTStake
tStake
totalStaked
}
}
}
`

const gqlClient = createClient({ url: gqlUrl, maskTypename: true })

const response = await gqlClient
.query(legacyStakersQuery, { blockNumber: blockNumber })
.toPromise()

if (response.error) {
console.error(`Error in getLegacyStakes: ${response.error.message}`)
return null
}

if (!response.data) {
console.error("No data found")
return null
}

const accounts = response.data["accounts"]

const stakes = {}
accounts.map((account) => {
account.stakes.map((stake) => {
if (stake.keepInTStake === "0" && stake.nuInTStake === "0") {
return
}
stakes[stake.id] = {
owner: account.id,
keepInTStake: stake.keepInTStake,
nuInTStake: stake.nuInTStake,
tStake: stake.tStake,
}
})
})

return stakes
}
66 changes: 35 additions & 31 deletions src/scripts/tbtcv2-rewards/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ export async function calculateRewards() {
currentBlockNumber
)

// Special case: Dec 1st 23 distribution - legacy stakes
const legacyEvents = await getLegacyEvents(provider)
// Special case: legacy stakes
// const legacyEvents = await getLegacyEvents(provider)

// For Dec 1st distribution, legacy stakes will not receive rewards.
// The corresponding rewards will be received in following distributions.
const legacyEvents: ethers.Event[] = []

const intervalLegacyEvents = legacyEvents.filter(
(event) =>
Expand Down Expand Up @@ -793,34 +797,34 @@ async function queryPrometheus(url: string, params: any): Promise<any> {

// Transaction in which legacy stakes were disabled:
// https://etherscan.io/tx/0x68ddee6b5651d5348a40555b0079b5066d05a63196e3832323afafae0095a656
async function getLegacyEvents(provider: ethers.providers.EtherscanProvider) {
const legacyTxHash =
"0x68ddee6b5651d5348a40555b0079b5066d05a63196e3832323afafae0095a656"
const eventSignature =
"0x0f0171fffaa54732b1f79a3164b315658061a1a51bf8c1010fbed908a8e333f9"
const legacyTxReceipt = await provider.getTransactionReceipt(legacyTxHash)

const legacyEvents = legacyTxReceipt.logs
.filter((log) => log.topics[0] === eventSignature)
.map((legacyEvent) => {
const parsedArgs = {
stakingProvider: ethers.utils.getAddress(
"0x" + legacyEvent.topics[1].slice(-40)
),
application: ethers.utils.getAddress(
"0x" + legacyEvent.topics[2].slice(-40)
),
fromAmount: ethers.BigNumber.from(legacyEvent.data.slice(0, 66)),
toAmount: ethers.BigNumber.from("0x" + legacyEvent.data.slice(66)),
}

return {
...legacyEvent,
args: parsedArgs,
} as unknown as ethers.Event
})

return legacyEvents
}
// async function getLegacyEvents(provider: ethers.providers.EtherscanProvider) {
// const legacyTxHash =
// "0x68ddee6b5651d5348a40555b0079b5066d05a63196e3832323afafae0095a656"
// const eventSignature =
// "0x0f0171fffaa54732b1f79a3164b315658061a1a51bf8c1010fbed908a8e333f9"
// const legacyTxReceipt = await provider.getTransactionReceipt(legacyTxHash)

// const legacyEvents = legacyTxReceipt.logs
// .filter((log) => log.topics[0] === eventSignature)
// .map((legacyEvent) => {
// const parsedArgs = {
// stakingProvider: ethers.utils.getAddress(
// "0x" + legacyEvent.topics[1].slice(-40)
// ),
// application: ethers.utils.getAddress(
// "0x" + legacyEvent.topics[2].slice(-40)
// ),
// fromAmount: ethers.BigNumber.from(legacyEvent.data.slice(0, 66)),
// toAmount: ethers.BigNumber.from("0x" + legacyEvent.data.slice(66)),
// }

// return {
// ...legacyEvent,
// args: parsedArgs,
// } as unknown as ethers.Event
// })

// return legacyEvents
// }

calculateRewards()