Skip to content

Commit

Permalink
fix: use current P address for change and reward (#396)
Browse files Browse the repository at this point in the history
refactor: shared delegate/validate methods
  • Loading branch information
kanatliemre authored May 18, 2023
1 parent 6c8bf0f commit f5a51dc
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 236 deletions.
109 changes: 0 additions & 109 deletions src/helpers/wallet_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,115 +90,6 @@ class WalletHelper {
return txId
}

static async validate(
wallet: WalletType,
nodeID: string,
amt: BN,
start: Date,
end: Date,
delegationFee: number,
rewardAddress?: string,
utxos?: PlatformUTXO[]
): Promise<string> {
let utxoSet = wallet.getPlatformUTXOSet()

// If given custom UTXO set use that
if (utxos) {
utxoSet = new PlatformUTXOSet()
utxoSet.addArray(utxos)
}

// Sort utxos high to low
const sortedSet = sortUTxoSetP(utxoSet, false)

const pAddressStrings = wallet.getAllAddressesP()

const stakeAmount = amt

// If reward address isn't given use index 0 address
if (!rewardAddress) {
rewardAddress = wallet.getPlatformRewardAddress()
}

// For change address use first available on the platform chain
const changeAddress = wallet.getFirstAvailableAddressPlatform()

const stakeReturnAddr = wallet.getCurrentAddressPlatform()

// Convert dates to unix time
const startTime = new BN(Math.round(start.getTime() / 1000))
const endTime = new BN(Math.round(end.getTime() / 1000))

const unsignedTx = await pChain.buildAddValidatorTx(
sortedSet,
[stakeReturnAddr],
pAddressStrings, // from
[changeAddress], // change
nodeID,
startTime,
endTime,
stakeAmount,
[rewardAddress],
delegationFee
)

const tx = await wallet.signP(unsignedTx)
return issueP(tx)
}

static async delegate(
wallet: WalletType,
nodeID: string,
amt: BN,
start: Date,
end: Date,
rewardAddress?: string,
utxos?: PlatformUTXO[]
): Promise<string> {
let utxoSet = wallet.getPlatformUTXOSet()
const pAddressStrings = wallet.getAllAddressesP()

const stakeAmount = amt

// If given custom UTXO set use that
if (utxos) {
utxoSet = new PlatformUTXOSet()
utxoSet.addArray(utxos)
}

// Sort utxos high to low
const sortedSet = sortUTxoSetP(utxoSet, false)

// If reward address isn't given use index 0 address
if (!rewardAddress) {
rewardAddress = wallet.getPlatformRewardAddress()
}

const stakeReturnAddr = wallet.getPlatformRewardAddress()

// For change address use first available on the platform chain
const changeAddress = wallet.getFirstAvailableAddressPlatform()

// Convert dates to unix time
const startTime = new BN(Math.round(start.getTime() / 1000))
const endTime = new BN(Math.round(end.getTime() / 1000))

const unsignedTx = await pChain.buildAddDelegatorTx(
sortedSet,
[stakeReturnAddr],
pAddressStrings,
[changeAddress],
nodeID,
startTime,
endTime,
stakeAmount,
[rewardAddress] // reward address
)

const tx = await wallet.signP(unsignedTx)
return issueP(tx)
}

static async sendEth(
wallet: WalletType,
to: string,
Expand Down
21 changes: 5 additions & 16 deletions src/js/wallets/AbstractHdWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { UTXOSet as AVMUTXOSet } from 'avalanche/dist/apis/avm/utxos'
import HDKey from 'hdkey'
import { HdHelper } from '@/js/HdHelper'
import { UTXOSet as PlatformUTXOSet } from 'avalanche/dist/apis/platformvm/utxos'
import { buildCreateNftFamilyTx, buildMintNftTx, buildUnsignedTransaction } from '../TxHelper'
import { buildUnsignedTransaction } from '../TxHelper'
import { AbstractWallet } from '@/js/wallets/AbstractWallet'
import { updateFilterAddresses } from '../../providers'
import { digestMessage } from '@/helpers/helper'

// A base class other HD wallets are based on.
// Mnemonic Wallet and LedgerWallet uses this

/**
* A base class other HD wallets are based on.
* Mnemonic Wallet and LedgerWallet uses this
*/
abstract class AbstractHdWallet extends AbstractWallet {
chainId: string

Expand Down Expand Up @@ -68,10 +69,6 @@ abstract class AbstractHdWallet extends AbstractWallet {
this.utxoset = joined
}

getFirstAvailableAddressPlatform(): string {
return this.platformHelper.getFirstAvailableAddress()
}

updateFetchState() {
this.isFetchUtxos =
this.externalHelper.isFetchUtxo ||
Expand Down Expand Up @@ -167,10 +164,6 @@ abstract class AbstractHdWallet extends AbstractWallet {
return this.internalHelper.getCurrentAddress()
}

getChangeAddressPlatform() {
return this.platformHelper.getCurrentAddress()
}

getChangePath(chainId?: ChainAlias): string {
switch (chainId) {
case 'P':
Expand Down Expand Up @@ -203,10 +196,6 @@ abstract class AbstractHdWallet extends AbstractWallet {
}
}

getPlatformRewardAddress(): string {
return this.platformHelper.getCurrentAddress()
}

getCurrentAddressPlatform(): string {
return this.platformHelper.getCurrentAddress()
}
Expand Down
143 changes: 142 additions & 1 deletion src/js/wallets/AbstractWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ import {
UnsignedTx as PlatformUnsignedTx,
} from 'avalanche/dist/apis/platformvm/tx'
import { Tx as AVMTx, UnsignedTx as AVMUnsignedTx } from 'avalanche/dist/apis/avm/tx'
import { AvmImportChainType } from '@/js/wallets/types'
import { AvmImportChainType, WalletType } from '@/js/wallets/types'
import { issueC, issueP, issueX } from '@/helpers/issueTx'
import { sortUTxoSetP } from '@/helpers/sortUTXOs'
import { getStakeForAddresses } from '@/helpers/utxo_helper'
import glacier from '@/js/Glacier/Glacier'
import { isMainnetNetworkID } from '@/store/modules/network/isMainnetNetworkID'
import { isTestnetNetworkID } from '@/store/modules/network/isTestnetNetworkID'
import { web3 } from '@/evm'
import { UTXO as PlatformUTXO } from 'avalanche/dist/apis/platformvm/utxos'
const uniqid = require('uniqid')

abstract class AbstractWallet {
Expand Down Expand Up @@ -74,6 +75,20 @@ abstract class AbstractWallet {
this.isFetchUtxos = false
}

/**
* Get change address to use with P Chain transactions. Uses current address.
*/
getChangeAddressPlatform() {
return this.getCurrentAddressPlatform()
}

/**
* Address to use for staking rewards. Uses current P chain address.
*/
getPlatformRewardAddress() {
return this.getCurrentAddressPlatform()
}

async evmGetAtomicUTXOs(sourceChain: ExportChainsC) {
const addrs = [this.getEvmAddressBech()]
return await UtxoHelper.evmGetAtomicUTXOs(addrs, sourceChain)
Expand Down Expand Up @@ -368,5 +383,131 @@ abstract class AbstractWallet {
const tx = await this.signX(unsignedTx)
return this.issueX(tx)
}

/**
* Create and issue an AddValidatorTx
* @param nodeID Node ID to add as a valdiator
* @param amt Stake amount in nAVAX
* @param start Stake Start Date
* @param end Stake End Date
* @param delegationFee
* @param rewardAddress Address which will receive the rewards
* @param utxos UTXOs to use for the transaction
*/
async validate(
nodeID: string,
amt: BN,
start: Date,
end: Date,
delegationFee: number,
rewardAddress?: string,
utxos?: PlatformUTXO[]
): Promise<string> {
let utxoSet = this.getPlatformUTXOSet()

// If given custom UTXO set use that
if (utxos) {
utxoSet = new PlatformUTXOSet()
utxoSet.addArray(utxos)
}

// Sort utxos high to low
const sortedSet = sortUTxoSetP(utxoSet, false)

const pAddressStrings = this.getAllAddressesP()

const stakeAmount = amt

// If reward address isn't given use index 0 address
if (!rewardAddress) {
rewardAddress = this.getPlatformRewardAddress()
}

// For change address use first available on the platform chain
const changeAddress = this.getChangeAddressPlatform()

const stakeReturnAddr = this.getCurrentAddressPlatform()

// Convert dates to unix time
const startTime = new BN(Math.round(start.getTime() / 1000))
const endTime = new BN(Math.round(end.getTime() / 1000))

const unsignedTx = await pChain.buildAddValidatorTx(
sortedSet,
[stakeReturnAddr],
pAddressStrings, // from
[changeAddress], // change
nodeID,
startTime,
endTime,
stakeAmount,
[rewardAddress],
delegationFee
)

const tx = await this.signP(unsignedTx)
return issueP(tx)
}

/**
* Create and issue an AddDelegatorTx
* @param nodeID
* @param amt
* @param start
* @param end
* @param rewardAddress
* @param utxos
*/
async delegate(
nodeID: string,
amt: BN,
start: Date,
end: Date,
rewardAddress?: string,
utxos?: PlatformUTXO[]
): Promise<string> {
let utxoSet = this.getPlatformUTXOSet()
const pAddressStrings = this.getAllAddressesP()

const stakeAmount = amt

// If given custom UTXO set use that
if (utxos) {
utxoSet = new PlatformUTXOSet()
utxoSet.addArray(utxos)
}

// Sort utxos high to low
const sortedSet = sortUTxoSetP(utxoSet, false)

// If reward address isn't given use index 0 address
if (!rewardAddress) {
rewardAddress = this.getPlatformRewardAddress()
}

const stakeReturnAddr = this.getPlatformRewardAddress()

// For change address use first available on the platform chain
const changeAddress = this.getChangeAddressPlatform()

// Convert dates to unix time
const startTime = new BN(Math.round(start.getTime() / 1000))
const endTime = new BN(Math.round(end.getTime() / 1000))

const unsignedTx = await pChain.buildAddDelegatorTx(
sortedSet,
[stakeReturnAddr],
pAddressStrings,
[changeAddress],
nodeID,
startTime,
endTime,
stakeAmount,
[rewardAddress] // reward address
)

const tx = await this.signP(unsignedTx)
return issueP(tx)
}
}
export { AbstractWallet }
32 changes: 0 additions & 32 deletions src/js/wallets/LedgerWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,38 +920,6 @@ class LedgerWallet extends AbstractHdWallet implements AvaWalletCore {
return await WalletHelper.issueBatchTx(this, orders, addr, memo)
}

async delegate(
nodeID: string,
amt: BN,
start: Date,
end: Date,
rewardAddress?: string,
utxos?: PlatformUTXO[]
): Promise<string> {
return await WalletHelper.delegate(this, nodeID, amt, start, end, rewardAddress, utxos)
}

async validate(
nodeID: string,
amt: BN,
start: Date,
end: Date,
delegationFee: number,
rewardAddress?: string,
utxos?: PlatformUTXO[]
): Promise<string> {
return await WalletHelper.validate(
this,
nodeID,
amt,
start,
end,
delegationFee,
rewardAddress,
utxos
)
}

async signHashByExternalIndex(index: number, hash: BufferAvax) {
const pathStr = `0/${index}`
const addressPath = bippath.fromString(pathStr, false)
Expand Down
Loading

0 comments on commit f5a51dc

Please sign in to comment.