Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
refactor(contract): use blockchain pkg (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
Noel committed Oct 18, 2021
1 parent 5142b56 commit a4aeae1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 27 deletions.
35 changes: 9 additions & 26 deletions packages/zilliqa-js-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
toChecksumAddress,
} from '@zilliqa-js/crypto';
import { BN, validation } from '@zilliqa-js/util';
import { Blockchain } from '@zilliqa-js/blockchain';

import { Contracts } from './factory';
import {
Expand All @@ -44,6 +45,7 @@ export class Contract {
factory: Contracts;
provider: Provider;
signer: Wallet;
blockchain: Blockchain;

init: Init;
abi?: ABI;
Expand All @@ -65,6 +67,8 @@ export class Contract {
this.factory = factory;
this.provider = factory.provider;
this.signer = factory.signer;
this.blockchain = new Blockchain(factory.provider, factory.signer);

// assume that we are accessing an existing contract
if (address) {
this.abi = abi;
Expand Down Expand Up @@ -351,8 +355,6 @@ export class Contract {
}
}

// FIXME: Link to @zilliqa-js/blockchain package (reuse code)

async getState(): Promise<State> {
if (this.status !== ContractStatus.Deployed) {
return Promise.resolve([]);
Expand All @@ -362,24 +364,11 @@ export class Contract {
throw new Error('Cannot get state of uninitialised contract');
}

const response = await this.provider.send(
RPCMethod.GetSmartContractState,
this.address.replace('0x', '').toLowerCase(),
);
const response = await this.blockchain.getSmartContractState(this.address);

return response.result;
}

// FIXME: Link to @zilliqa-js/blockchain package (reuse code)

/**
* getSubState
*
* @param { string } variableName - variable name within the state
* @param { string[] } indices - (optional) If the variable is of map type, you can specify an index (or indices)
* @returns {Promise<RPCResponse<any, string>>}
*/

async getSubState(variableName: string, indices?: string[]): Promise<State> {
if (this.status !== ContractStatus.Deployed) {
return Promise.resolve([]);
Expand All @@ -393,18 +382,15 @@ export class Contract {
throw new Error('Variable name required');
}

const response = await this.provider.send(
RPCMethod.GetSmartContractSubState,
this.address.replace('0x', '').toLowerCase(),
const response = await this.blockchain.getSmartContractSubState(
this.address,
variableName,
indices === undefined ? [] : indices,
indices,
);

return response.result;
}

// FIXME: Link to @zilliqa-js/blockchain package (reuse code)

async getInit(): Promise<State> {
if (this.status !== ContractStatus.Deployed) {
return Promise.resolve([]);
Expand All @@ -414,10 +400,7 @@ export class Contract {
throw new Error('Cannot get state of uninitialised contract');
}

const response = await this.provider.send(
RPCMethod.GetSmartContractInit,
this.address.replace('0x', '').toLowerCase(),
);
const response = await this.blockchain.getSmartContractInit(this.address);

return response.result;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/zilliqa-js-contract/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"references": [
{ "path": "../zilliqa-js-account" },
{ "path": "../zilliqa-js-core" },
{ "path": "../zilliqa-js-util" }
{ "path": "../zilliqa-js-util" },
{ "path": "../zilliqa-js-blockchain" }
]
}

0 comments on commit a4aeae1

Please sign in to comment.