-
Notifications
You must be signed in to change notification settings - Fork 2
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 #17 from Abracadabra-money/feature/magicusd0pp
- Loading branch information
Showing
7 changed files
with
165 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { AutomateSDK, TriggerType } from "@gelatonetwork/automate-sdk"; | ||
import hre from "hardhat"; | ||
import { DEVOPS_SAFE } from "../utils/constants"; | ||
|
||
const { ethers, w3f } = hre; | ||
|
||
const THIRTY_SECONDS = 30 * 1000; | ||
|
||
const main = async () => { | ||
const magicUsd0pp = w3f.get("magicusd0pp"); | ||
|
||
const [deployer] = await ethers.getSigners(); | ||
const chainId = (await ethers.provider.getNetwork()).chainId; | ||
|
||
const automate = new AutomateSDK(chainId, deployer); | ||
|
||
// Deploy Web3Function on IPFS | ||
console.log("Deploying Web3Function on IPFS..."); | ||
const cid = await magicUsd0pp.deploy(); | ||
console.log(`Web3Function IPFS CID: ${cid}`); | ||
|
||
const { tx } = await automate.prepareBatchExecTask( | ||
{ | ||
name: "MagicUSD0pp Off-Chain Distribution Claimer", | ||
web3FunctionHash: cid, | ||
trigger: { | ||
type: TriggerType.TIME, | ||
interval: THIRTY_SECONDS, | ||
}, | ||
web3FunctionArgs: { | ||
execAddress: "0x75cC0C0DDD2Ccafe6EC415bE686267588011E36A", | ||
usualApiEndpoint: "https://app.usual.money/api/rewards", | ||
magicUsd0ppAddress: "0x73075fD1522893D9dC922991542f98F08F2c1C99", | ||
}, | ||
}, | ||
{}, | ||
DEVOPS_SAFE, | ||
); | ||
console.log(`to: ${tx.to}`); | ||
console.log(tx.data); | ||
console.log("------------------"); | ||
console.log(); | ||
}; | ||
|
||
main() | ||
.then(() => { | ||
process.exit(); | ||
}) | ||
.catch((err) => { | ||
console.error("Error:", err.message); | ||
process.exit(1); | ||
}); |
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,93 @@ | ||
import { | ||
Web3Function, | ||
type Web3FunctionContext, | ||
Web3FunctionResult, | ||
} from "@gelatonetwork/web3-functions-sdk"; | ||
import ky from "ky"; | ||
import { type Address, type Hex, encodeFunctionData, parseAbi } from "viem"; | ||
import { createJsonRpcPublicClient } from "../../utils/viem"; | ||
|
||
const DISTRIBUTION_ABI = parseAbi([ | ||
"function getOffChainDistributionData() external view returns (uint256 timestamp, bytes32 merkleRoot)", | ||
"function getOffChainTokensClaimed(address account) external view returns (uint256)", | ||
"function claimOffChainDistribution(address account, uint256 amount, bytes32[] calldata proof) external", | ||
]); | ||
|
||
type MagicUsd0ppUserArgs = { | ||
execAddress: Address; | ||
usualApiEndpoint: string; | ||
magicUsd0ppAddress: Address; | ||
}; | ||
|
||
type OffChainDistribution = { | ||
blockHash: Hex; | ||
merkleRoot: Hex; | ||
timestamp: string; | ||
value: `${bigint}`; | ||
merkleProof: Array<Hex>; | ||
}; | ||
|
||
Web3Function.onRun( | ||
async ({ userArgs, multiChainProvider }: Web3FunctionContext) => { | ||
const { execAddress, usualApiEndpoint, magicUsd0ppAddress } = | ||
userArgs as MagicUsd0ppUserArgs; | ||
|
||
const client = createJsonRpcPublicClient(multiChainProvider.default()); | ||
const usualRewardsApi = ky.extend({ | ||
prefixUrl: new URL("rewards", new URL(usualApiEndpoint)), | ||
}); | ||
|
||
const [[, currentMerkleRoot], claimedTokens, distributions] = | ||
await Promise.all([ | ||
client.readContract({ | ||
abi: DISTRIBUTION_ABI, | ||
address: execAddress, | ||
functionName: "getOffChainDistributionData", | ||
}), | ||
client.readContract({ | ||
abi: DISTRIBUTION_ABI, | ||
address: execAddress, | ||
functionName: "getOffChainTokensClaimed", | ||
args: [magicUsd0ppAddress], | ||
}), | ||
usualRewardsApi | ||
.get(magicUsd0ppAddress) | ||
.json<Array<OffChainDistribution>>(), | ||
]); | ||
|
||
if (distributions.length === 0) { | ||
return { canExec: false, message: "No distributions" }; | ||
} | ||
|
||
const currentDistribution = distributions.find( | ||
({ merkleRoot }) => | ||
merkleRoot.toLowerCase() === currentMerkleRoot.toLowerCase(), | ||
); | ||
|
||
if (currentDistribution === undefined) { | ||
return { canExec: false, message: "No matching distribution" }; | ||
} | ||
|
||
if (claimedTokens === BigInt(currentDistribution.value)) { | ||
return { canExec: false, message: "Already claimed latest distribution" }; | ||
} | ||
|
||
return { | ||
canExec: true, | ||
callData: [ | ||
{ | ||
to: execAddress, | ||
data: encodeFunctionData({ | ||
abi: DISTRIBUTION_ABI, | ||
functionName: "claimOffChainDistribution", | ||
args: [ | ||
magicUsd0ppAddress, | ||
BigInt(currentDistribution.value), | ||
currentDistribution.merkleProof, | ||
], | ||
}), | ||
}, | ||
], | ||
}; | ||
}, | ||
); |
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,11 @@ | ||
{ | ||
"web3FunctionVersion": "2.0.0", | ||
"runtime": "js-1.0", | ||
"memory": 128, | ||
"timeout": 30, | ||
"userArgs": { | ||
"execAddress": "string", | ||
"usualApiEndpoint": "string", | ||
"magicUsd0ppAddress": "string" | ||
} | ||
} |
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 @@ | ||
{} |
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,5 @@ | ||
{ | ||
"execAddress": "0x75cC0C0DDD2Ccafe6EC415bE686267588011E36A", | ||
"usualApiEndpoint": "https://app.usual.money/api/rewards", | ||
"magicUsd0ppAddress": "0x73075fD1522893D9dC922991542f98F08F2c1C99" | ||
} |