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