Javascript SDK of WoopChain protocol
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sdk/packages/harmony-staking/test/testSign.test.ts

519 lines
16 KiB

/**
* @packageDocumentation
* @module harmony-staking
* @ig
*/
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';
const http = new HttpProvider('http://mock.com');
// const ws = new WSProvider('ws://mock.com');
const msgHttp = new Messenger(http, ChainType.Harmony, ChainID.HmyLocal);
// const msgWs = new Messenger(ws, ChainType.Harmony, ChainID.HmyLocal);
const walletHttp = new Wallet(msgHttp);
5 years ago
import {
CreateValidator,
5 years ago
EditValidator,
Delegate,
Undelegate,
CollectRewards,
5 years ago
Directive,
StakingTransaction,
Description,
CommissionRate,
Decimal,
StakingFactory,
} from '../src';
5 years ago
import testTransactions from './transactions.json';
describe('test sign staking transaction', () => {
afterEach(() => {
fetch.resetMocks();
// jest.clearAllTimers();
});
it('should test sign create validator staking transaction', () => {
5 years ago
const testTx: any = testTransactions[0];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const desc: Description = new Description(
testTx.description.name,
testTx.description.identity,
testTx.description.website,
testTx.description.securityContact,
testTx.description.details,
);
const commissionRates: CommissionRate = new CommissionRate(
new Decimal(testTx.commissionRates.rate),
new Decimal(testTx.commissionRates.maxRate),
new Decimal(testTx.commissionRates.maxChangeRate),
5 years ago
);
const stakeMsg: CreateValidator = new CreateValidator(
testTx.validatorAddress,
5 years ago
desc,
commissionRates,
5 years ago
testTx.minSelfDelegation,
testTx.maxTotalDelegation,
testTx.slotPubKeys,
5 years ago
testTx.amount,
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveCreateValidator,
5 years ago
stakeMsg,
testTx.nonce,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign create validator staking transaction using factory', () => {
const testTx: any = testTransactions[0];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakingFactory = new StakingFactory(msgHttp);
const stakingTx = stakingFactory
.createValidator({
validatorAddress: testTx.validatorAddress,
description: {
...testTx.description,
},
commissionRate: {
...testTx.commissionRates,
},
minSelfDelegation: testTx.minSelfDelegation,
maxTotalDelegation: testTx.maxTotalDelegation,
slotPubKeys: testTx.slotPubKeys,
amount: testTx.amount,
})
.setTxParams({
nonce: testTx.nonce,
gasPrice: testTx.gasPrice,
gasLimit: testTx.gasLimit,
chainId: testTx.chainID,
signature: {
v: 0,
r: '',
s: '',
},
})
.build();
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
5 years ago
it('should test sign edit validator staking transaction', () => {
const testTx: any = testTransactions[1];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const desc: Description = new Description(
testTx.description.name,
testTx.description.identity,
testTx.description.website,
testTx.description.securityContact,
testTx.description.details,
);
const stakeMsg: EditValidator = new EditValidator(
testTx.validatorAddress,
5 years ago
desc,
new Decimal(testTx.commissionRate),
5 years ago
testTx.minSelfDelegation,
testTx.maxTotalDelegation,
testTx.slotKeyToRemove,
testTx.slotKeyToAdd,
5 years ago
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveEditValidator,
stakeMsg,
testTx.nonce,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign edit validator staking transaction using factory', () => {
const testTx: any = testTransactions[1];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakingFactory = new StakingFactory(msgHttp);
const stakingTx = stakingFactory
.editValidator({
validatorAddress: testTx.validatorAddress,
description: { ...testTx.description },
commissionRate: testTx.commissionRate,
minSelfDelegation: testTx.minSelfDelegation,
maxTotalDelegation: testTx.maxTotalDelegation,
slotKeyToRemove: testTx.slotKeyToRemove,
slotKeyToAdd: testTx.slotKeyToAdd,
})
.setTxParams({
nonce: testTx.nonce,
gasPrice: testTx.gasPrice,
gasLimit: testTx.gasLimit,
chainId: testTx.chainID,
signature: {
v: 0,
r: '',
s: '',
},
})
.build();
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign delegate staking transaction', () => {
const testTx: any = testTransactions[2];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakeMsg: Delegate = new Delegate(
testTx.delegatorAddress,
testTx.validatorAddress,
testTx.amount,
);
5 years ago
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveDelegate,
stakeMsg,
testTx.nonce,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign delegate staking transaction using factory', () => {
const testTx: any = testTransactions[2];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakingFactory = new StakingFactory(msgHttp);
const stakingTx = stakingFactory
.delegate({
delegatorAddress: testTx.delegatorAddress,
validatorAddress: testTx.validatorAddress,
amount: testTx.amount,
})
.setTxParams({
nonce: testTx.nonce,
gasPrice: testTx.gasPrice,
gasLimit: testTx.gasLimit,
chainId: testTx.chainID,
signature: {
v: 0,
r: '',
s: '',
},
})
.build();
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign undelegate staking transaction', () => {
5 years ago
const testTx: any = testTransactions[3];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakeMsg: Undelegate = new Undelegate(
5 years ago
testTx.delegatorAddress,
testTx.validatorAddress,
5 years ago
testTx.amount,
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveUndelegate,
5 years ago
stakeMsg,
testTx.nonce,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign undelegate staking transaction using factory', () => {
const testTx: any = testTransactions[3];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakingFactory = new StakingFactory(msgHttp);
const stakingTx = stakingFactory
.undelegate({
delegatorAddress: testTx.delegatorAddress,
validatorAddress: testTx.validatorAddress,
amount: testTx.amount,
})
.setTxParams({
nonce: testTx.nonce,
gasPrice: testTx.gasPrice,
gasLimit: testTx.gasLimit,
chainId: testTx.chainID,
signature: {
v: 0,
r: '',
s: '',
},
})
.build();
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign collect rewards staking transaction', () => {
5 years ago
const testTx: any = testTransactions[4];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakeMsg: CollectRewards = new CollectRewards(testTx.delegatorAddress);
5 years ago
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveCollectRewards,
5 years ago
stakeMsg,
testTx.nonce,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign collect rewards staking transaction using factory', () => {
const testTx: any = testTransactions[4];
const address = getAddressFromPrivateKey(testTx.privateKey);
expect(isValidAddress(address)).toEqual(true);
const stakingFactory = new StakingFactory(msgHttp);
const stakingTx = stakingFactory
.collectRewards({
delegatorAddress: testTx.delegatorAddress,
})
.setTxParams({
nonce: testTx.nonce,
gasPrice: testTx.gasPrice,
gasLimit: testTx.gasLimit,
chainId: testTx.chainID,
signature: {
v: 0,
r: '',
s: '',
},
})
.build();
const signed = stakingTx.rlpSign(testTx.privateKey);
expect(signed[1]).toEqual(testTx.encoded);
});
it('should test sign create validator staking transaction using wallet', async () => {
const testTx: any = testTransactions[0];
const responses = [
{
jsonrpc: '2.0',
id: 1,
result: '0x666666666666',
},
{
jsonrpc: '2.0',
id: 1,
result: testTx.nonce,
},
].map((res) => [JSON.stringify(res)] as [string]);
fetch.mockResponses(...responses);
const desc: Description = new Description(
testTx.description.name,
testTx.description.identity,
testTx.description.website,
testTx.description.securityContact,
testTx.description.details,
);
const commissionRates: CommissionRate = new CommissionRate(
new Decimal(testTx.commissionRates.rate),
new Decimal(testTx.commissionRates.maxRate),
new Decimal(testTx.commissionRates.maxChangeRate),
);
const stakeMsg: CreateValidator = new CreateValidator(
testTx.validatorAddress,
desc,
commissionRates,
testTx.minSelfDelegation,
testTx.maxTotalDelegation,
testTx.slotPubKeys,
testTx.amount,
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveCreateValidator,
stakeMsg,
0,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const account: any = walletHttp.addByPrivateKey(testTx.privateKey);
const signedStaking: StakingTransaction = await account.signStaking(stakingTx);
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded);
});
it('should test sign edit validator staking transaction using wallet', async () => {
const testTx: any = testTransactions[1];
const responses = [
{
jsonrpc: '2.0',
id: 1,
result: '0x666666666666',
},
{
jsonrpc: '2.0',
id: 1,
result: testTx.nonce,
},
].map((res) => [JSON.stringify(res)] as [string]);
fetch.mockResponses(...responses);
const desc: Description = new Description(
testTx.description.name,
testTx.description.identity,
testTx.description.website,
testTx.description.securityContact,
testTx.description.details,
);
const stakeMsg: EditValidator = new EditValidator(
testTx.validatorAddress,
desc,
new Decimal(testTx.commissionRate),
testTx.minSelfDelegation,
testTx.maxTotalDelegation,
testTx.slotKeyToRemove,
testTx.slotKeyToAdd,
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveEditValidator,
stakeMsg,
0,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const account: any = walletHttp.addByPrivateKey(testTx.privateKey);
const signedStaking: StakingTransaction = await account.signStaking(stakingTx);
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded);
});
it('should test sign delegate staking transaction using wallet', async () => {
const testTx: any = testTransactions[2];
const responses = [
{
jsonrpc: '2.0',
id: 1,
result: '0x666666666666',
},
{
jsonrpc: '2.0',
id: 1,
result: testTx.nonce,
},
].map((res) => [JSON.stringify(res)] as [string]);
fetch.mockResponses(...responses);
const stakeMsg: Delegate = new Delegate(
testTx.delegatorAddress,
testTx.validatorAddress,
testTx.amount,
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveDelegate,
stakeMsg,
0,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const account: any = walletHttp.addByPrivateKey(testTx.privateKey);
const signedStaking: StakingTransaction = await account.signStaking(stakingTx);
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded);
});
it('should test sign undelegate staking transaction using wallet', async () => {
const testTx: any = testTransactions[3];
const responses = [
{
jsonrpc: '2.0',
id: 1,
result: '0x666666666666',
},
{
jsonrpc: '2.0',
id: 1,
result: testTx.nonce,
},
].map((res) => [JSON.stringify(res)] as [string]);
fetch.mockResponses(...responses);
const stakeMsg: Undelegate = new Undelegate(
testTx.delegatorAddress,
testTx.validatorAddress,
testTx.amount,
);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveUndelegate,
stakeMsg,
0,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
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 collect rewards staking transaction using wallet', async () => {
const testTx: any = testTransactions[4];
const responses = [
{
jsonrpc: '2.0',
id: 1,
result: '0x666666666666',
},
{
jsonrpc: '2.0',
id: 1,
result: testTx.nonce,
},
].map((res) => [JSON.stringify(res)] as [string]);
fetch.mockResponses(...responses);
const stakeMsg: CollectRewards = new CollectRewards(testTx.delegatorAddress);
const stakingTx: StakingTransaction = new StakingTransaction(
Directive.DirectiveCollectRewards,
stakeMsg,
0,
testTx.gasPrice,
testTx.gasLimit,
testTx.chainID,
);
const account: any = walletHttp.addByPrivateKey(testTx.privateKey);
const signedStaking: StakingTransaction = await account.signStaking(stakingTx);
expect(signedStaking.getRawTransaction()).toEqual(testTx.encoded);
});
5 years ago
});