Skip to content

Commit

Permalink
Merge pull request #34 from vechainfoundation/24-support-clauses
Browse files Browse the repository at this point in the history
Add clauses builder implementation
  • Loading branch information
vanja-vechain authored Jul 24, 2023
2 parents 3671149 + 520af21 commit 31e4ed1
Show file tree
Hide file tree
Showing 10 changed files with 460 additions and 23 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/common-unit-sonar.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ jobs:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: yarn install
run: |
yarn install
yarn build
- name: Unit tests and coverage
run: yarn workspace @vechain/hardhat-vechain test-coverage
run: |
docker-compose -f packages/vechain/test/docker-compose.yaml up -d
yarn workspace @vechain/hardhat-vechain test-coverage
- name: SonarCloud Scan
uses: sonarsource/sonarcloud-github-action@master
Expand Down
5 changes: 4 additions & 1 deletion packages/ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ const modified = (provider: ConnexProviderWrapper) => {
}
return defaultSigner;
}
modifiedProvider.getNetwork = () => {
return provider.getVechainNetwork();
}

return modifiedProvider
return modifiedProvider;
}

extendEnvironment(hre => {
Expand Down
6 changes: 4 additions & 2 deletions packages/vechain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@
"prepack": "yarn build",
"clean": "git clean -dfX",
"test": "cross-env TESTING=1 yarn node --experimental-vm-modules --no-warnings $(yarn bin jest) --runInBand --forceExit",
"test-coverage": "cross-env TESTING=1 yarn node --experimental-vm-modules --no-warnings $(yarn bin jest) --runInBand --detectOpenHandles --coverage"
"test-coverage": "cross-env TESTING=1 yarn node --experimental-vm-modules --no-warnings $(yarn bin jest) --runInBand --forceExit --coverage"
},
"dependencies": {
"@vechain/connex-driver": "^2.0.12",
"@vechain/connex-framework": "^2.0.12",
"@vechain/web3-providers-connex": "^1.0.0",
"debug": "^4.3.4",
"ethers": "^5.7.2",
"thor-devkit": "^2.0.6"
"thor-devkit": "^2.0.6",
"pino": "8.14.1",
"pino-pretty": "9.4.0"
},
"devDependencies": {
"@types/debug": "^4.1.7",
Expand Down
17 changes: 16 additions & 1 deletion packages/vechain/src/ConnexProviderWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@ import { randomBytes } from "crypto";
import { TransactionRequest } from "@ethersproject/abstract-provider"
import { VechainHardhatPluginError } from "./error";
import { Transaction } from "thor-devkit";
import { Network } from "@ethersproject/networks";

export class ConnexProviderWrapper extends EventEmitter implements EthereumProvider {
private _provider: Promise<Provider>;
private _wallet: SimpleWallet;
private _verbose: boolean;
private _log: debug.Debugger;

constructor(networkConfig: NetworkConfig, verbose: boolean) {
private readonly _networkConfig: NetworkConfig;
private readonly _networkName: string;

constructor(networkConfig: NetworkConfig, verbose: boolean, networkName: string) {
super();
this._networkConfig = networkConfig;
this._networkName = networkName;
this._log = debug("hardhat:vechain:provider");
this._verbose = verbose;
this._wallet = createWallet(networkConfig);
Expand All @@ -40,6 +46,15 @@ export class ConnexProviderWrapper extends EventEmitter implements EthereumProvi
});
}

public async getVechainNetwork(): Promise<Network> {
const provider = await this._provider;
return {
name: this._networkName,
chainId: this._networkConfig.chainId!!,
_defaultProvider: (providers: any, options?: any) => provider
}
}

public async sign(transaction: Deferrable<TransactionRequest>) {
let key: Wallet.Key | undefined = undefined;
const from = await transaction.from;
Expand Down
54 changes: 54 additions & 0 deletions packages/vechain/src/clausesBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {BaseContract} from "ethers";
import {VechainHardhatPluginError} from "./error";
import logger from 'pino';

export interface Clause {
args: any[],
abi: string,
method: string
}

export class ClausesBuilder {
private readonly clauses: Clause[] = [];
private readonly contract: BaseContract;

private log = logger({
transport: {
target: 'pino-pretty'
}
});

constructor(contract: BaseContract) {
this.contract = contract;
}

public withClause(clause: Clause): ClausesBuilder {
this.clauses.push(clause);
return this;
}


public async send(): Promise<{
txid: string
signer: string
}> {
const net = await this.contract.provider.getNetwork();
if (!net || !net['_defaultProvider']) {
this.log.error('Error while getting default provider for network');
throw new VechainHardhatPluginError('vechain hardhat plugin requires vechain network for clauses operation')
}
const provider = net._defaultProvider(null, null);
if (!provider) {
this.log.error('Error while getting default provider for network');
throw new VechainHardhatPluginError('vechain hardhat plugin requires vechain provider for clauses operation')
}
const processedClauses = this.clauses.map(clause => {
const abi = JSON.parse(clause.abi);
const functionAbi = abi.find((func: any) => func.type === 'function' && func.name === clause.method);
const method = provider.connex.thor.account(this.contract.address).method(functionAbi);
return method.asClause(clause.args);
});
const signedTx = provider.connex.vendor.sign('tx', processedClauses);
return signedTx.request();
}
}
2 changes: 1 addition & 1 deletion packages/vechain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extendEnvironment(hre => {
if (!hre.network.name.includes("vechain")) {
return;
}
hre.vechain = lazyObject(() => new ConnexProviderWrapper(hre.network.config, hre.hardhatArguments.verbose));
hre.vechain = lazyObject(() => new ConnexProviderWrapper(hre.network.config, hre.hardhatArguments.verbose, hre.network.name));
hre.network.provider = hre.vechain;
});

Expand Down
46 changes: 31 additions & 15 deletions packages/vechain/test/ConnexProviderWrapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {HttpNetworkConfig, JsonRpcResponse} from "hardhat/types";
import { ConnexProviderWrapper } from "../src/ConnexProviderWrapper";
import { Driver } from "@vechain/connex-driver";
import * as assert from "assert";
import {createProvider} from "../src/helpers/createProvider";
import { createProvider } from "../src/helpers/createProvider";
import { createWallet } from "../src/helpers/createWallet";
import { ConnexProviderWrapper } from "../src/ConnexProviderWrapper";

describe('index tests', () => {
const thorSoloUrl = "http://127.0.0.1:8669";
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
expect(connexProviderWrapper).toBeDefined();
});

Expand All @@ -44,13 +44,13 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
expect(await connexProviderWrapper.disableDelegate()).toBeNull()
});

it('create ConnexProviderWrapper and expect connection error', async () => {

const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
try {
await connexProviderWrapper.disableDelegate();
} catch (e: any) {
Expand All @@ -65,15 +65,15 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
expect(connexProviderWrapper.setProvider(provider)).toBe(void 0);
});

it('create ConnexProviderWrapper and set provider error', async () => {

const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
try {
Expand All @@ -90,7 +90,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
connexProviderWrapper.setProvider(provider);
Expand All @@ -108,7 +108,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
connexProviderWrapper.setProvider(provider);
Expand All @@ -127,7 +127,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
try {
await connexProviderWrapper.send('testMethod', ['1']);
} catch (e: any) {
Expand All @@ -142,7 +142,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
connexProviderWrapper.setProvider(provider);
Expand Down Expand Up @@ -174,7 +174,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
connexProviderWrapper.setProvider(provider);
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('index tests', () => {
id: ''
}
});
const connexProviderWrapper = new ConnexProviderWrapper(config, true);
const connexProviderWrapper = new ConnexProviderWrapper(config, true, 'vechain');
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
connexProviderWrapper.setProvider(provider);
Expand Down Expand Up @@ -243,7 +243,7 @@ describe('index tests', () => {

it("fails with key not included in provider", async () => {
const tx = { from: "" }
const connexProviderWrapper = new ConnexProviderWrapper(config, false)
const connexProviderWrapper = new ConnexProviderWrapper(config, false, 'vechain')
expect(connexProviderWrapper.sign(tx)).rejects.toThrow('transaction.from: "" is not included in wallet');
});

Expand All @@ -254,7 +254,7 @@ describe('index tests', () => {
jest.spyOn(resolved, 'request').mockResolvedValue(dummyResponse);
});

const connexProviderWrapper = new ConnexProviderWrapper(config, false);
const connexProviderWrapper = new ConnexProviderWrapper(config, false, 'vechain');
connexProviderWrapper.setProvider(provider);

const tx = {
Expand All @@ -265,6 +265,22 @@ describe('index tests', () => {
};
expect(await connexProviderWrapper.sign(tx)).toMatch(/0x([0-9]|[a-f]|[A-F])+/);
});

it("get vechain network", async () => {
const wallet = createWallet(config);
const provider = createProvider(config, wallet);
provider.then(resolved => {
jest.spyOn(resolved, 'request').mockResolvedValue(dummyResponse);
});

const connexProviderWrapper = new ConnexProviderWrapper(config, false, 'vechain');
connexProviderWrapper.setProvider(provider);

const network = await connexProviderWrapper.getVechainNetwork()

expect(network.name).toEqual('vechain');
expect(network.chainId).toEqual(1);
});
})

});
Loading

0 comments on commit 31e4ed1

Please sign in to comment.