|
|
|
@ -1,10 +1,8 @@ |
|
|
|
|
import { getAddressFromPrivateKey } from '@harmony-js/crypto'; |
|
|
|
|
import { isValidAddress, ChainType, ChainID } from '@harmony-js/utils'; |
|
|
|
|
// tslint:disable-next-line: no-implicit-dependencies
|
|
|
|
|
import { Wallet } from '@harmony-js/account'; |
|
|
|
|
|
|
|
|
|
import { getAddressFromPrivateKey } from '@harmony-js/crypto'; |
|
|
|
|
import { HttpProvider, Messenger } from '@harmony-js/network'; |
|
|
|
|
|
|
|
|
|
import { ChainID, ChainType, isValidAddress } from '@harmony-js/utils'; |
|
|
|
|
// tslint:disable-next-line: no-implicit-dependencies
|
|
|
|
|
import fetch from 'jest-fetch-mock'; |
|
|
|
|
|
|
|
|
@ -17,11 +15,11 @@ const msgHttp = new Messenger(http, ChainType.Harmony, ChainID.HmyLocal); |
|
|
|
|
const walletHttp = new Wallet(msgHttp); |
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
NewValidator, |
|
|
|
|
CreateValidator, |
|
|
|
|
EditValidator, |
|
|
|
|
Delegate, |
|
|
|
|
Redelegate, |
|
|
|
|
Undelegate, |
|
|
|
|
CollectRewards, |
|
|
|
|
Directive, |
|
|
|
|
StakingTransaction, |
|
|
|
|
Description, |
|
|
|
@ -36,7 +34,7 @@ describe('test sign staking transaction', () => { |
|
|
|
|
fetch.resetMocks(); |
|
|
|
|
// jest.clearAllTimers();
|
|
|
|
|
}); |
|
|
|
|
it('should test sign new validator staking transaction', () => { |
|
|
|
|
it('should test sign create validator staking transaction', () => { |
|
|
|
|
const testTx: any = testTransactions[0]; |
|
|
|
|
const address = getAddressFromPrivateKey(testTx.privateKey); |
|
|
|
|
expect(isValidAddress(address)).toEqual(true); |
|
|
|
@ -47,21 +45,22 @@ describe('test sign staking transaction', () => { |
|
|
|
|
testTx.description.securityContact, |
|
|
|
|
testTx.description.details, |
|
|
|
|
); |
|
|
|
|
const commission: CommissionRate = new CommissionRate( |
|
|
|
|
new Decimal(testTx.commission.rate, 0), |
|
|
|
|
new Decimal(testTx.commission.maxRate, 0), |
|
|
|
|
new Decimal(testTx.commission.maxChangeRate, 0), |
|
|
|
|
const commissionRates: CommissionRate = new CommissionRate( |
|
|
|
|
new Decimal(testTx.commissionRates.rate), |
|
|
|
|
new Decimal(testTx.commissionRates.maxRate), |
|
|
|
|
new Decimal(testTx.commissionRates.maxChangeRate), |
|
|
|
|
); |
|
|
|
|
const stakeMsg: NewValidator = new NewValidator( |
|
|
|
|
const stakeMsg: CreateValidator = new CreateValidator( |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
desc, |
|
|
|
|
commission, |
|
|
|
|
commissionRates, |
|
|
|
|
testTx.minSelfDelegation, |
|
|
|
|
testTx.stakingAddress, |
|
|
|
|
testTx.pubKey, |
|
|
|
|
testTx.maxTotalDelegation, |
|
|
|
|
testTx.slotPubKeys, |
|
|
|
|
testTx.amount, |
|
|
|
|
); |
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveNewValidator, |
|
|
|
|
Directive.DirectiveCreateValidator, |
|
|
|
|
stakeMsg, |
|
|
|
|
testTx.nonce, |
|
|
|
|
testTx.gasPrice, |
|
|
|
@ -86,10 +85,13 @@ describe('test sign staking transaction', () => { |
|
|
|
|
testTx.description.details, |
|
|
|
|
); |
|
|
|
|
const stakeMsg: EditValidator = new EditValidator( |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
desc, |
|
|
|
|
testTx.stakingAddress, |
|
|
|
|
new Decimal(testTx.commissionRate, 0), |
|
|
|
|
new Decimal(testTx.commissionRate), |
|
|
|
|
testTx.minSelfDelegation, |
|
|
|
|
testTx.maxTotalDelegation, |
|
|
|
|
testTx.slotKeyToRemove, |
|
|
|
|
testTx.slotKeyToAdd, |
|
|
|
|
); |
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveEditValidator, |
|
|
|
@ -129,19 +131,18 @@ describe('test sign staking transaction', () => { |
|
|
|
|
const signed = stakingTx.rlpSign(testTx.privateKey); |
|
|
|
|
expect(signed[1]).toEqual(testTx.encoded); |
|
|
|
|
}); |
|
|
|
|
it('should test sign redelegate staking transaction', () => { |
|
|
|
|
it('should test sign undelegate staking transaction', () => { |
|
|
|
|
const testTx: any = testTransactions[3]; |
|
|
|
|
const address = getAddressFromPrivateKey(testTx.privateKey); |
|
|
|
|
expect(isValidAddress(address)).toEqual(true); |
|
|
|
|
const stakeMsg: Redelegate = new Redelegate( |
|
|
|
|
const stakeMsg: Undelegate = new Undelegate( |
|
|
|
|
testTx.delegatorAddress, |
|
|
|
|
testTx.validatorSrcAddress, |
|
|
|
|
testTx.validatorDstAddress, |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
testTx.amount, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveRedelegate, |
|
|
|
|
Directive.DirectiveUndelegate, |
|
|
|
|
stakeMsg, |
|
|
|
|
testTx.nonce, |
|
|
|
|
testTx.gasPrice, |
|
|
|
@ -154,18 +155,14 @@ describe('test sign staking transaction', () => { |
|
|
|
|
const signed = stakingTx.rlpSign(testTx.privateKey); |
|
|
|
|
expect(signed[1]).toEqual(testTx.encoded); |
|
|
|
|
}); |
|
|
|
|
it('should test sign undelegate staking transaction', () => { |
|
|
|
|
it('should test sign collect rewards staking transaction', () => { |
|
|
|
|
const testTx: any = testTransactions[4]; |
|
|
|
|
const address = getAddressFromPrivateKey(testTx.privateKey); |
|
|
|
|
expect(isValidAddress(address)).toEqual(true); |
|
|
|
|
const stakeMsg: Undelegate = new Undelegate( |
|
|
|
|
testTx.delegatorAddress, |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
testTx.amount, |
|
|
|
|
); |
|
|
|
|
const stakeMsg: CollectRewards = new CollectRewards(testTx.delegatorAddress); |
|
|
|
|
|
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveUndelegate, |
|
|
|
|
Directive.DirectiveCollectRewards, |
|
|
|
|
stakeMsg, |
|
|
|
|
testTx.nonce, |
|
|
|
|
testTx.gasPrice, |
|
|
|
@ -178,7 +175,7 @@ describe('test sign staking transaction', () => { |
|
|
|
|
const signed = stakingTx.rlpSign(testTx.privateKey); |
|
|
|
|
expect(signed[1]).toEqual(testTx.encoded); |
|
|
|
|
}); |
|
|
|
|
it('should test sign new validator staking transaction using wallet', async () => { |
|
|
|
|
it('should test sign create validator staking transaction using wallet', async () => { |
|
|
|
|
const testTx: any = testTransactions[0]; |
|
|
|
|
const responses = [ |
|
|
|
|
{ |
|
|
|
@ -202,21 +199,22 @@ describe('test sign staking transaction', () => { |
|
|
|
|
testTx.description.securityContact, |
|
|
|
|
testTx.description.details, |
|
|
|
|
); |
|
|
|
|
const commission: CommissionRate = new CommissionRate( |
|
|
|
|
new Decimal(testTx.commission.rate, 0), |
|
|
|
|
new Decimal(testTx.commission.maxRate, 0), |
|
|
|
|
new Decimal(testTx.commission.maxChangeRate, 0), |
|
|
|
|
const commissionRates: CommissionRate = new CommissionRate( |
|
|
|
|
new Decimal(testTx.commissionRates.rate), |
|
|
|
|
new Decimal(testTx.commissionRates.maxRate), |
|
|
|
|
new Decimal(testTx.commissionRates.maxChangeRate), |
|
|
|
|
); |
|
|
|
|
const stakeMsg: NewValidator = new NewValidator( |
|
|
|
|
const stakeMsg: CreateValidator = new CreateValidator( |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
desc, |
|
|
|
|
commission, |
|
|
|
|
commissionRates, |
|
|
|
|
testTx.minSelfDelegation, |
|
|
|
|
testTx.stakingAddress, |
|
|
|
|
testTx.pubKey, |
|
|
|
|
testTx.maxTotalDelegation, |
|
|
|
|
testTx.slotPubKeys, |
|
|
|
|
testTx.amount, |
|
|
|
|
); |
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveNewValidator, |
|
|
|
|
Directive.DirectiveCreateValidator, |
|
|
|
|
stakeMsg, |
|
|
|
|
0, |
|
|
|
|
testTx.gasPrice, |
|
|
|
@ -255,10 +253,13 @@ describe('test sign staking transaction', () => { |
|
|
|
|
testTx.description.details, |
|
|
|
|
); |
|
|
|
|
const stakeMsg: EditValidator = new EditValidator( |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
desc, |
|
|
|
|
testTx.stakingAddress, |
|
|
|
|
new Decimal(testTx.commissionRate, 0), |
|
|
|
|
new Decimal(testTx.commissionRate), |
|
|
|
|
testTx.minSelfDelegation, |
|
|
|
|
testTx.maxTotalDelegation, |
|
|
|
|
testTx.slotKeyToRemove, |
|
|
|
|
testTx.slotKeyToAdd, |
|
|
|
|
); |
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveEditValidator, |
|
|
|
@ -313,8 +314,9 @@ describe('test sign staking transaction', () => { |
|
|
|
|
const signedStaking: StakingTransaction = await account.signStaking(stakingTx); |
|
|
|
|
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded); |
|
|
|
|
}); |
|
|
|
|
it('should test sign redelegate staking transaction using wallet', async () => { |
|
|
|
|
it('should test sign undelegate staking transaction using wallet', async () => { |
|
|
|
|
const testTx: any = testTransactions[3]; |
|
|
|
|
|
|
|
|
|
const responses = [ |
|
|
|
|
{ |
|
|
|
|
jsonrpc: '2.0', |
|
|
|
@ -329,16 +331,14 @@ describe('test sign staking transaction', () => { |
|
|
|
|
].map((res) => [JSON.stringify(res)] as [string]); |
|
|
|
|
|
|
|
|
|
fetch.mockResponses(...responses); |
|
|
|
|
|
|
|
|
|
const stakeMsg: Redelegate = new Redelegate( |
|
|
|
|
const stakeMsg: Undelegate = new Undelegate( |
|
|
|
|
testTx.delegatorAddress, |
|
|
|
|
testTx.validatorSrcAddress, |
|
|
|
|
testTx.validatorDstAddress, |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
testTx.amount, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveRedelegate, |
|
|
|
|
Directive.DirectiveUndelegate, |
|
|
|
|
stakeMsg, |
|
|
|
|
0, |
|
|
|
|
testTx.gasPrice, |
|
|
|
@ -350,11 +350,13 @@ describe('test sign staking transaction', () => { |
|
|
|
|
); |
|
|
|
|
const account: any = walletHttp.addByPrivateKey(testTx.privateKey); |
|
|
|
|
const signedStaking: StakingTransaction = await account.signStaking(stakingTx); |
|
|
|
|
expect(signedStaking.getFromAddress()).toEqual(account.bech32Address); |
|
|
|
|
expect(signedStaking.getSignature()).toBeTruthy(); |
|
|
|
|
expect(signedStaking.getUnsignedRawTransaction()).toBeTruthy(); |
|
|
|
|
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded); |
|
|
|
|
}); |
|
|
|
|
it('should test sign undelegate staking transaction using wallet', async () => { |
|
|
|
|
it('should test sign collect rewards staking transaction using wallet', async () => { |
|
|
|
|
const testTx: any = testTransactions[4]; |
|
|
|
|
|
|
|
|
|
const responses = [ |
|
|
|
|
{ |
|
|
|
|
jsonrpc: '2.0', |
|
|
|
@ -369,14 +371,11 @@ describe('test sign staking transaction', () => { |
|
|
|
|
].map((res) => [JSON.stringify(res)] as [string]); |
|
|
|
|
|
|
|
|
|
fetch.mockResponses(...responses); |
|
|
|
|
const stakeMsg: Undelegate = new Undelegate( |
|
|
|
|
testTx.delegatorAddress, |
|
|
|
|
testTx.validatorAddress, |
|
|
|
|
testTx.amount, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const stakeMsg: CollectRewards = new CollectRewards(testTx.delegatorAddress); |
|
|
|
|
|
|
|
|
|
const stakingTx: StakingTransaction = new StakingTransaction( |
|
|
|
|
Directive.DirectiveUndelegate, |
|
|
|
|
Directive.DirectiveCollectRewards, |
|
|
|
|
stakeMsg, |
|
|
|
|
0, |
|
|
|
|
testTx.gasPrice, |
|
|
|
@ -388,9 +387,6 @@ describe('test sign staking transaction', () => { |
|
|
|
|
); |
|
|
|
|
const account: any = walletHttp.addByPrivateKey(testTx.privateKey); |
|
|
|
|
const signedStaking: StakingTransaction = await account.signStaking(stakingTx); |
|
|
|
|
expect(signedStaking.getFromAddress()).toEqual(account.bech32Address); |
|
|
|
|
expect(signedStaking.getSignature()).toBeTruthy(); |
|
|
|
|
expect(signedStaking.getUnsignedRawTransaction()).toBeTruthy(); |
|
|
|
|
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|