feat(staking):add set function

staking
neeboo 5 years ago
parent bee8b4c4da
commit 04573f58ed
  1. 1
      packages/harmony-account/package.json
  2. 54
      packages/harmony-account/src/account.ts
  3. 9
      packages/harmony-account/tsconfig.json
  4. 3
      packages/harmony-staking/package.json
  5. 1
      packages/harmony-staking/src/index.ts
  6. 79
      packages/harmony-staking/src/stakingTransaction.ts
  7. 3
      packages/harmony-staking/test/tsconfig.json
  8. 1
      scripts/packagesTs.ts
  9. 1
      tsconfig.test.json

@ -21,6 +21,7 @@
"@harmony-js/core": "0.1.29",
"@harmony-js/crypto": "0.1.28",
"@harmony-js/network": "0.1.29",
"@harmony-js/staking": "^0.1.29",
"@harmony-js/transaction": "0.1.29",
"@harmony-js/utils": "0.1.28"
},

@ -19,6 +19,7 @@ import {
ChainType,
} from '@harmony-js/utils';
import { Transaction, RLPSign } from '@harmony-js/transaction';
import { StakingTransaction } from '@harmony-js/staking';
import { Messenger, RPCMethod } from '@harmony-js/network';
import { Shards } from './types';
import { defaultMessenger } from './utils';
@ -242,6 +243,59 @@ class Account {
return transaction;
}
}
async signStaking(
staking: StakingTransaction,
updateNonce: boolean = true,
encodeMode: string = 'rlp',
blockNumber: string = 'latest',
shardID: number = this.messenger.currentShard,
): Promise<StakingTransaction> {
if (!this.privateKey || !isPrivateKey(this.privateKey)) {
throw new Error(`${this.privateKey} is not found or not correct`);
}
if (updateNonce) {
// await this.updateBalances(blockNumber);
const txShardID = shardID;
const shardBalanceObject = await this.getShardBalance(
typeof txShardID === 'string' ? Number.parseInt(txShardID, 10) : txShardID,
blockNumber,
);
if (shardBalanceObject !== undefined) {
const shardNonce = shardBalanceObject.nonce;
staking.setFromAddress(
this.messenger.chainPrefix === ChainType.Harmony
? this.bech32Address
: this.checksumAddress || '0x',
);
staking.setNonce(shardNonce);
} else {
staking.setFromAddress(
this.messenger.chainPrefix === ChainType.Harmony
? this.bech32Address
: this.checksumAddress || '0x',
);
staking.setNonce(0);
}
}
if (encodeMode === 'rlp') {
const [signature, rawTransaction]: [Signature, string] = staking.rlpSign(this.privateKey);
staking.setRawTransaction(rawTransaction);
staking.setSignature(signature);
staking.setFromAddress(
this.messenger.chainPrefix === ChainType.Harmony
? this.bech32Address
: this.checksumAddress || '0x',
);
return staking;
} else {
// TODO: if we use other encode method, eg. protobuf, we should implement this
return staking;
}
}
setMessenger(messenger: Messenger) {
this.messenger = messenger;
}

@ -6,9 +6,10 @@
},
"include": ["src", "../../typings/**/*.d.ts"],
"references": [
{"path": "../harmony-crypto"},
{"path": "../harmony-utils"},
{"path": "../harmony-transaction"},
{"path": "../harmony-network"}
{ "path": "../harmony-crypto" },
{ "path": "../harmony-utils" },
{ "path": "../harmony-transaction" },
{ "path": "../harmony-staking" },
{ "path": "../harmony-network" }
]
}

@ -20,7 +20,8 @@
"dependencies": {
"@harmony-js/crypto": "0.1.28",
"@harmony-js/network": "0.1.29",
"@harmony-js/utils": "0.1.28"
"@harmony-js/utils": "0.1.28",
"text-encoding": "^0.7.0"
},
"gitHead": "775e2ba36924fcffd8d2c0b73587b549ed90bb81"
}

@ -0,0 +1 @@
export * from './stakingTransaction';

@ -1,3 +1,5 @@
// tslint:disable: max-classes-per-file
import {
encode,
arrayify,
@ -12,7 +14,7 @@ import {
BN,
} from '@harmony-js/crypto';
import { TextEncoder } from 'text-encoding';
import { Unit, numberToHex } from '@harmony-js/utils'; //add0xToString
import { Unit, numberToHex } from '@harmony-js/utils';
export class StakingSettings {
public static PRECISION = 18;
@ -33,9 +35,10 @@ export class StakingTransaction {
private gasLimit: number | string;
private gasPrice: number | string;
private chainId: number;
// private rawTransaction: string;
// private unsignedRawTransaction: string;
// private signature: Signature;
private rawTransaction: string;
private unsignedRawTransaction: string;
private signature: Signature;
private from: string;
constructor(
directive: Directive,
@ -53,22 +56,26 @@ export class StakingTransaction {
this.nonce = nonce;
this.gasLimit = gasLimit;
this.gasPrice = gasPrice;
// this.rawTransaction = '0x';
// this.unsignedRawTransaction = '0x';
// this.signature = {
// r: r,
// s: s,
// recoveryParam: 0,
// v: v,
// };
this.rawTransaction = '0x';
this.unsignedRawTransaction = '0x';
this.signature = {
r,
s,
recoveryParam: 0,
v,
};
this.chainId = chainID;
this.from = '0x';
}
encode(): [string, any[]] {
const raw: Array<string | Uint8Array | Array<string | Uint8Array>> = [];
// TODO: temporary hack for converting 0x00 to 0x
if (!this.directive) raw.push('0x');
else raw.push(hexlify(this.directive));
if (!this.directive) {
raw.push('0x');
} else {
raw.push(hexlify(this.directive));
}
raw.push(this.stakeMsg.encode());
raw.push(hexlify(this.nonce));
raw.push(hexlify(this.gasPrice));
@ -102,6 +109,39 @@ export class StakingTransaction {
return encode(raw);
}
setUnsigned(unSigned: string) {
this.unsignedRawTransaction = unSigned;
}
setRawTransaction(rawTransaction: string) {
this.rawTransaction = rawTransaction;
}
setSignature(signature: Signature) {
this.signature = {
r: signature.r,
s: signature.s,
v: signature.v,
recoveryParam: signature.recoveryParam,
};
}
setNonce(nonce: number) {
this.nonce = nonce;
}
setFromAddress(address: string) {
this.from = address;
}
getUnsignedRawTransaction() {
return this.unsignedRawTransaction;
}
getRawTransaction() {
return this.rawTransaction;
}
getSignature() {
return this.signature;
}
getFromAddress() {
return this.from;
}
}
export class Description {
@ -127,7 +167,7 @@ export class Description {
encode(): any[] {
const raw: Array<string | Uint8Array> = [];
let enc = new TextEncoder();
const enc = new TextEncoder();
raw.push(enc.encode(this.name));
raw.push(enc.encode(this.identity));
raw.push(enc.encode(this.website));
@ -145,9 +185,10 @@ export class Decimal {
`too much precision: ${precision}, should be less than ${StakingSettings.PRECISION}`,
);
}
let zerosToAdd = StakingSettings.PRECISION - precision;
let multiplier = Math.pow(10, zerosToAdd);
this.value = new Unit((value * multiplier).toString()).asWei().toWei(); //(value * multiplier).toString();
const zerosToAdd = StakingSettings.PRECISION - precision;
const multiplier = Math.pow(10, zerosToAdd);
// (value * multiplier).toString();
this.value = new Unit((value * multiplier).toString()).asWei().toWei();
}
encode(): any[] {
@ -172,7 +213,7 @@ export class CommissionRate {
raw.push(this.rate.encode());
raw.push(this.maxRate.encode());
raw.push(this.maxChangeRate.encode());
//console.log(decode(encode(raw)));
// console.log(decode(encode(raw)));
return raw;
}
}

@ -0,0 +1,3 @@
{
"extends": "../tsconfig.test.json"
}

@ -4,6 +4,7 @@ const packages = [
'harmony-account',
'harmony-network',
'harmony-transaction',
'harmony-staking',
'harmony-contract',
'harmony-core',
];

@ -9,6 +9,7 @@
{ "path": "packages/harmony-utils" },
{ "path": "packages/harmony-network" },
{ "path": "packages/harmony-transaction" },
{ "path": "packages/harmony-staking" },
{ "path": "packages/harmony-contract" }
]
}

Loading…
Cancel
Save