From 881a6d7497ec5e57157d24c4f646bacf72c48705 Mon Sep 17 00:00:00 2001 From: minapham17 Date: Mon, 1 Jul 2024 13:37:11 +0700 Subject: [PATCH 1/2] feat: add Unizen staking contract Signed-off-by: minapham17 --- src/adapters/unizen-staking.adapter.ts | 48 ++++ src/component.ts | 2 + src/contracts/unizen-staking.json | 337 +++++++++++++++++++++++++ 3 files changed, 387 insertions(+) create mode 100644 src/adapters/unizen-staking.adapter.ts create mode 100644 src/contracts/unizen-staking.json diff --git a/src/adapters/unizen-staking.adapter.ts b/src/adapters/unizen-staking.adapter.ts new file mode 100644 index 0000000..0e91cf5 --- /dev/null +++ b/src/adapters/unizen-staking.adapter.ts @@ -0,0 +1,48 @@ +import {BindingScope, extensionFor, injectable} from '@loopback/core'; +import {BigNumber} from 'ethers'; +import {STAKING_ADAPTERS_EXTENSION_POINT} from '../keys.js'; +import {BaseStakingContractAdapter, StakingAsset} from '../staking.js'; +// Use the full path to import instead of `../types` +import {UnizenStaking__factory} from '../types/factories/UnizenStaking__factory.js'; + +@injectable( + { + scope: BindingScope.SINGLETON, // Mark the adapter as a singleton + }, + // Mark it as an extension to staking contracts service + extensionFor(STAKING_ADAPTERS_EXTENSION_POINT), +) +export class UnizenStakingContractAdapter extends BaseStakingContractAdapter { + /** + * The contract address + */ + contractAddress = '0x0Df016Fb18ef4195b2CF9d8623E236272ec52e14'; + + /** + * The chain the assets exist on + */ + chainId: number = 137; + + /** + * Assets that can be staked to this contract + */ + supportedAssets: StakingAsset[] = [ + { + name: 'pZCX', + asset: 'ERC20:0xDD75542611D57C4b6e68168B14C3591C539022eD', + }, + ]; + + /** + * Get staked token ids for the given owner + * @param owner - Owner address + * @returns + */ + getStakedTokenIds(owner: string): Promise { + const contract = UnizenStaking__factory.connect( + this.contractAddress, + this.provider, + ); + return contract['getUserStakes(address)'](owner); + } +} diff --git a/src/component.ts b/src/component.ts index 61976fb..f0ec0b9 100644 --- a/src/component.ts +++ b/src/component.ts @@ -73,6 +73,7 @@ import {SuperverseStakingContractAdapter} from './adapters/superverse-staking.ad import {SupremeKong2StakingContractAdapter} from './adapters/supreme-kong-2-staking.adapter.js'; import {SupremeKongStakingContractAdapter} from './adapters/supreme-kong-staking.adapter.js'; import {TtooStakingContractAdapter} from './adapters/ttoo-staking.adapter.js'; +import {UnizenStakingContractAdapter} from './adapters/unizen-staking.adapter.js'; import {UwULendStakingContractAdapter} from './adapters/uwulend-staking.adapter.js'; import {WarriorsofankhStakingContractAdapter} from './adapters/warriorsofankh-staking.adapter.js'; import {XenoStakingContractAdapter} from './adapters/xeno-mining.adapter.js'; @@ -144,6 +145,7 @@ export class StakingContractsComponent implements Component { RailgunBinanceGovernanceAdapter, RailgunEthereumGovernanceAdapter, RailgunPolygonGovernanceAdapter, + UnizenStakingContractAdapter, ]; constructor() {} } diff --git a/src/contracts/unizen-staking.json b/src/contracts/unizen-staking.json new file mode 100644 index 0000000..78f33c4 --- /dev/null +++ b/src/contracts/unizen-staking.json @@ -0,0 +1,337 @@ +[ + { + "inputs": [], + "name": "getActiveTokens", + "outputs": [ + { + "internalType": "address[]", + "name": "", + "type": "address[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_blocknumber", + "type": "uint256" + } + ], + "name": "getTVLs", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getTVLs", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getTokenWeights", + "outputs": [ + { + "internalType": "uint256[]", + "name": "weights", + "type": "uint256[]" + }, + { + "internalType": "uint256", + "name": "combinedWeight", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + } + ], + "name": "getUserData", + "outputs": [ + { + "internalType": "address[]", + "name": "", + "type": "address[]" + }, + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_blocknumber", + "type": "uint256" + } + ], + "name": "getUserStakes", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + } + ], + "name": "getUserStakes", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + }, + { + "internalType": "address", + "name": "_token", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_startBlock", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "_endBlock", + "type": "uint256" + }, + { + "internalType": "uint256[]", + "name": "_claimedBlocks", + "type": "uint256[]" + } + ], + "name": "getUserStakesSnapshots", + "outputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "startBlock", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "endBlock", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "stakedAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "startTVL", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "endTVL", + "type": "uint256" + } + ], + "internalType": "struct SharedDataTypes.StakeSnapshot[]", + "name": "snapshots", + "type": "tuple[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_precision", + "type": "uint256" + } + ], + "name": "getUserTVLShare", + "outputs": [ + { + "internalType": "uint256[]", + "name": "", + "type": "uint256[]" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_user", + "type": "address" + }, + { + "internalType": "address", + "name": "_token", + "type": "address" + } + ], + "name": "getUsersStakedAmountOfToken", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "stake", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_lpToken", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "stake", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "withdraw", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "_lpToken", + "type": "address" + }, + { + "internalType": "uint256", + "name": "_amount", + "type": "uint256" + } + ], + "name": "withdraw", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "nonpayable", + "type": "function" + } +] From 7f6296a32d3885e9c76067ee1343a5e07f110ce6 Mon Sep 17 00:00:00 2001 From: minapham17 Date: Tue, 2 Jul 2024 22:43:40 +0700 Subject: [PATCH 2/2] fix: fixed test Signed-off-by: minapham17 --- src/adapters/unizen-staking.adapter.ts | 29 ++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/adapters/unizen-staking.adapter.ts b/src/adapters/unizen-staking.adapter.ts index 0e91cf5..ca6b8f5 100644 --- a/src/adapters/unizen-staking.adapter.ts +++ b/src/adapters/unizen-staking.adapter.ts @@ -12,12 +12,14 @@ import {UnizenStaking__factory} from '../types/factories/UnizenStaking__factory. // Mark it as an extension to staking contracts service extensionFor(STAKING_ADAPTERS_EXTENSION_POINT), ) + export class UnizenStakingContractAdapter extends BaseStakingContractAdapter { /** * The contract address */ - contractAddress = '0x0Df016Fb18ef4195b2CF9d8623E236272ec52e14'; + contractAddress = '0x5655B12f1e74D1D1fc3F1048a89850a0149Aa5d4'; + PZCX_ADDRESS = '0xdd75542611d57c4b6e68168b14c3591c539022ed' /** * The chain the assets exist on */ @@ -29,7 +31,7 @@ export class UnizenStakingContractAdapter extends BaseStakingContractAdapter { supportedAssets: StakingAsset[] = [ { name: 'pZCX', - asset: 'ERC20:0xDD75542611D57C4b6e68168B14C3591C539022eD', + asset: `ERC20:${this.PZCX_ADDRESS}`, }, ]; @@ -38,11 +40,30 @@ export class UnizenStakingContractAdapter extends BaseStakingContractAdapter { * @param owner - Owner address * @returns */ - getStakedTokenIds(owner: string): Promise { + async getStakedTokenIds(owner: string): Promise { + const contract = UnizenStaking__factory.connect( + this.contractAddress, + this.provider, + ); + try { + const userStakes = await contract.callStatic['getUsersStakedAmountOfToken'](owner, this.PZCX_ADDRESS); + return [userStakes] + } catch (error) { + return [BigNumber.from(0)] + } + } + + async getStakedTokenBalance(owner: string, assetName: string): Promise { const contract = UnizenStaking__factory.connect( this.contractAddress, this.provider, ); - return contract['getUserStakes(address)'](owner); + try { + const userStakes = await contract.callStatic['getUsersStakedAmountOfToken'](owner, this.PZCX_ADDRESS); + return userStakes + } catch (error) { + return BigNumber.from(0) + } } } +