chore(account):add shardID to current Account

crossShard
neeboo 5 years ago
parent 7f136d0fd3
commit 94a633213d
  1. 38
      packages/harmony-account/src/account.ts
  2. 2
      packages/harmony-contract/src/contract.ts
  3. 26
      packages/harmony-contract/src/methods/method.ts
  4. 1
      packages/harmony-contract/src/utils/options.ts
  5. 4
      packages/harmony-network/src/messenger/messenger.ts

@ -41,6 +41,7 @@ class Account {
address?: string;
balance?: string = '0';
nonce?: number = 0;
shardID: number;
shards: Shards;
messenger: Messenger;
encrypted: boolean = false;
@ -74,8 +75,9 @@ class Account {
} else {
this._import(key);
}
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(0, {
this.shards.set(this.shardID, {
address: `${this.bech32Address}${AddressSuffix}0`,
balance: this.balance || '0',
nonce: this.nonce || 0,
@ -117,17 +119,19 @@ class Account {
async getBalance(blockNumber: string = 'latest'): Promise<object> {
try {
if (this.messenger) {
const balance = await this.messenger.send(RPCMethod.GetBalance, [
this.address,
blockNumber,
this.messenger.currentShard,
]);
const balance = await this.messenger.send(
RPCMethod.GetBalance,
[this.address, blockNumber],
this.messenger.chainPrefix,
this.messenger.currentShard || 0,
);
const nonce = await this.messenger.send(RPCMethod.GetTransactionCount, [
this.address,
blockNumber,
this.messenger.currentShard,
]);
const nonce = await this.messenger.send(
RPCMethod.GetTransactionCount,
[this.address, blockNumber],
this.messenger.chainPrefix,
this.messenger.currentShard || 0,
);
if (balance.isError()) {
throw balance.error.message;
}
@ -137,12 +141,14 @@ class Account {
this.balance = hexToNumber(balance.result);
this.nonce = Number.parseInt(hexToNumber(nonce.result), 10);
this.shardID = this.messenger.currentShard || 0;
} else {
throw new Error('No Messenger found');
}
return {
balance: this.balance,
nonce: this.nonce,
shardID: this.shardID,
};
} catch (error) {
throw error;
@ -163,8 +169,11 @@ class Account {
await this.shards.set(name === val.shardID ? name : val.shardID, balanceObject);
}
} else {
const shard0 = await this.getShardBalance(0, blockNumber);
this.shards.set(0, shard0);
const currentShard = await this.getShardBalance(
this.messenger.currentShard || 0,
blockNumber,
);
this.shards.set(this.messenger.currentShard || 0, currentShard);
}
}
@ -281,8 +290,9 @@ class Account {
this.privateKey = add0xToString(key);
this.publicKey = getPubkeyFromPrivateKey(this.privateKey);
this.address = getAddressFromPrivateKey(this.privateKey);
this.shardID = this.messenger.currentShard || 0;
this.shards = new Map();
this.shards.set(0, {
this.shards.set(this.shardID, {
address: `${this.bech32Address}${AddressSuffix}0`,
balance: this.balance || '0',
nonce: this.nonce || 0,

@ -20,6 +20,7 @@ export class Contract {
wallet: Wallet | any;
transaction?: Transaction;
status: ContractStatus;
shardID: number;
constructor(
abi: any = [],
@ -33,6 +34,7 @@ export class Contract {
this.abiModel = abiMapper(abi, this.abiCoder);
this.options = options;
this.address = this.options.address || address;
this.shardID = this.options.shardID || 0;
this.wallet = wallet;
this.methods = {};
this.events = {};

@ -55,13 +55,18 @@ export class ContractMethod {
}
async call(options: any, blockNumber: any = 'latest') {
try {
const shardID =
options !== undefined && options.shardID !== undefined
? options.shardID
: this.contract.shardID;
const nonce =
this.wallet.signer || (options !== undefined && options.from)
? getResultForData(
await this.wallet.messenger.send(RPCMethod.GetTransactionCount, [
this.wallet.signer ? this.wallet.signer.address : options.from,
blockNumber,
]),
await this.wallet.messenger.send(
RPCMethod.GetTransactionCount,
[this.wallet.signer ? this.wallet.signer.address : options.from, blockNumber],
shardID,
),
)
: '0x0';
@ -126,7 +131,12 @@ export class ContractMethod {
}
}
const result = await this.wallet.messenger.send(RPCMethod.Call, [sendPayload, blockNumber]);
const result = await (<Wallet>this.wallet).messenger.send(
RPCMethod.Call,
[sendPayload, blockNumber],
(<Wallet>this.wallet).messenger.chainPrefix,
shardID,
);
this.callPayload = sendPayload;
this.callResponse = result;
if (result.isError()) {
@ -145,7 +155,7 @@ export class ContractMethod {
async estimateGas() {
try {
const result = getResultForData(
await this.wallet.messenger.send(RPCMethod.EstimateGas, [
await (<Wallet>this.wallet).messenger.send(RPCMethod.EstimateGas, [
{
to: this.transaction.txParams.to,
data: this.transaction.txParams.data,
@ -217,7 +227,7 @@ export class ContractMethod {
id,
20,
1000,
this.transaction.txParams.shardID,
this.transaction ? this.transaction.txParams.shardID : this.contract.shardID,
);
if (result.receipt && result.txStatus === TxStatus.CONFIRMED) {
@ -252,7 +262,7 @@ export class ContractMethod {
data: this.encodeABI(),
};
const result = new TransactionFactory(this.wallet.messenger).newTx(txObject);
const result = new TransactionFactory((<Wallet>this.wallet).messenger).newTx(txObject);
return result;
} else {

@ -1,5 +1,6 @@
export interface ContractOptions {
data?: string;
shardID?: number;
address?: string;
defaultAccount?: string;
defaultBlock?: string;

@ -75,6 +75,10 @@ class Messenger extends HarmonyCore {
get currentShard(): number | undefined {
return this.getCurrentShardID();
}
get shardCount(): number {
return this.shardProviders.size;
}
/**
* @function send
* @memberof Messenger.prototype

Loading…
Cancel
Save