From a1db40e88e6f98ef6a2a932b948a52c08095e53f Mon Sep 17 00:00:00 2001 From: shima78 Date: Mon, 9 Jan 2023 11:02:01 +0330 Subject: [PATCH] avax page --- src/app.ts | 5 +- src/data/avax.json | 0 src/pages/avax.scss | 0 src/pages/avax.ts | 117 +++++++++++++++++++++++++++++++++++++++ src/pages/avax.xml | 120 ++++++++++++++++++++++++++++++++++++++++ src/pages/dashboard.xml | 2 +- 6 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 src/data/avax.json create mode 100644 src/pages/avax.scss create mode 100644 src/pages/avax.xml diff --git a/src/app.ts b/src/app.ts index 6a705bd..7c8e1c0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -6,8 +6,11 @@ import { Router } from "@web/core/router"; import { Dashboard } from "@web/pages/dashboard"; import { CscStakingPage } from "@web/pages/csc"; +import { AvaxStaking } from "@web/pages/avax"; import { MaticStaking } from "@web/pages/polygon"; import { FantomStaking } from "@web/pages/fantom"; + + const ROOT_TEMPLATE = xml`
@@ -54,7 +57,7 @@ export class Root extends Component { this.routeType = 'hash'; this.state = useState(new RootState()); this.routes = [ - Dashboard, CscStakingPage, MaticStaking, FantomStaking + Dashboard, CscStakingPage,AvaxStaking, MaticStaking, FantomStaking, ]; } } diff --git a/src/data/avax.json b/src/data/avax.json new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/avax.scss b/src/pages/avax.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/avax.ts b/src/pages/avax.ts index 7daaa76..2eeeb96 100644 --- a/src/pages/avax.ts +++ b/src/pages/avax.ts @@ -37,3 +37,120 @@ How many AVAX you are staking The address to send any rewards to */ + +import { Component, xml, useState, reactive, useRef, } from "@odoo/owl"; +import { useContract, useWallet, switchChain } from "@web/core/wallet"; +import {OPage} from "@web/components/page"; + +import avaxABI from "@web/data/avax.json" +import "./avax.xml"; +import "./avax.scss"; + + +const genzAdderss = "0x5a1b57f87b59e093d332c945c66b602843099f97"; +const testAddress = "0xaa3a160e91f63f1db959640e0a7b8911b6bd5b95"; +const avaxContract = "0xFC00FACE00000000000000000000000000000000"; +const weiRate = 1000000000000000000; +const testnetChainId = "0xfa2"; +const mainnetChainId = "0xfa"; + + + +export class AvaxStaking extends OPage { + static title = "avax"; + static logo = "./img/avax.svg"; + static route = '/staking/avax'; + static template = 'pages.avax'; + + wallet = useWallet(); + state = useState({ + staking: false, + unstaking: false, + value: 0, + }); + + + /** + * Stake the amount value from the input + * + * @returns promise | undefinde + */ + async stakeit() { + if (this.state.staking || !this._checkChainId()) { + await switchChain(mainnetChainId, 'avax Opera', 'https://rpcapi.avax.network/') + return; + } + this.state.staking = true; + let contract = useContract(this.wallet.chainId, avaxContract, avaxABI); + var amount = this.state.value*weiRate; + var val = "0x" + amount.toString(16) + return contract.methods + .delegate( this.getVerifierAddress()) + .send({ + from: this.wallet.account,val + + }).catch(ex => { + alert("Fail to performe the stake action"); + console.error(ex); + }).finally(() => this.state.staking = false); + } + + /** + * Unstake all m from the default Validator + * + * @returns promise to performe + */ + unstakeit() { + if (this.state.unstaking || !this._checkChainId()) { + // wait for end + return; + } + this.state.unstaking = true; + let contract = useContract(this.wallet.chainId, avaxContract, avaxABI); + contract.handleRevert = true + contract.methods + .unstake(this.getVerifierAddress()) + .send({ + from: this.wallet.account + }).catch(ex => { + alert("Fail to unstake!!"); + }).finally(async() => { + + this.state.unstaking = false; + }); + this.withdrawStaking() + } + withdrawStaking(){ + let contract = useContract(this.wallet.chainId, avaxContract, avaxABI); + return contract.methods + .withdrawStaking(this.getVerifierAddress()) + .send({ + from: this.wallet.account + }).catch(ex => { + alert("Fail to unstake!!",ex); + }); + } + + _checkChainId() { + if (this.wallet.chainId !== mainnetChainId && this.wallet.chainId !== testnetChainId) { + alert("Unsupported Chain ID :" + this.wallet.chainId); + return false; + } + return true; + } + + getVerifierAddress() { + if (this.wallet.chainId === mainnetChainId) { + return genzAdderss; + } + return testAddress; + } +} + + + + + + + + diff --git a/src/pages/avax.xml b/src/pages/avax.xml new file mode 100644 index 0000000..5543931 --- /dev/null +++ b/src/pages/avax.xml @@ -0,0 +1,120 @@ + + + +
+
+ +
+ + +
+
+
\ No newline at end of file diff --git a/src/pages/dashboard.xml b/src/pages/dashboard.xml index 96ede76..1784d5f 100644 --- a/src/pages/dashboard.xml +++ b/src/pages/dashboard.xml @@ -81,7 +81,7 @@

- Stake avax + Stake avax