Skip to content

Commit

Permalink
Merge pull request #101 from thepower/feat/execute_call
Browse files Browse the repository at this point in the history
feat/execute call
  • Loading branch information
jackkru69 authored Nov 1, 2023
2 parents 26a8383 + 8322d42 commit 2d4d5c6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/tssdk/src/helpers/network.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export enum ChainAction {
GET_WALLET = 'GET_WALLET',
GET_NODE_SETTINGS = 'GET_NODE_SETTINGS',
CREATE_TRANSACTION = 'CREATE_TRANSACTION',
EXECUTE_CALL = 'EXECUTE_CALL',
GET_MY_CHAIN = 'GET_MY_CHAIN',
GET_SC_CODE = 'GET_SC_CODE',
GET_SC_STATE = 'GET_SC_STATE',
Expand Down
36 changes: 35 additions & 1 deletion packages/tssdk/src/libs/network/network.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import axios from 'axios';
import createHash from 'create-hash';
import Debug from 'debug';
import { defaultAbiCoder as AbiCoder } from '@ethersproject/abi';
import { encodeFunction, getAbiInputsOutputsType } from '../../helpers/abi.helper';
import { config as cfg } from '../../config/chain.config';
import { ChainGlobalConfig, ChainNode } from '../../typings';
import { queueNodes, transformNodeList, transformResponse } from '../../helpers/network.helper';
Expand Down Expand Up @@ -258,7 +260,7 @@ export class NetworkApi {
private incrementNodeIndex = async () => {
this.nodeIndex += 1;
if (this.nodeIndex >= this.currentNodes.length || this.currentNodes[this.nodeIndex].time === cfg.maxNodeResponseTime) {
this.currentNodes = await queueNodes(this.currentNodes);
this.currentNodes = await queueNodes(this.currentNodes, 5000);
this.nodeIndex = 0;

if (this.nodeIndex >= this.currentNodes.length || this.currentNodes[this.nodeIndex].time === cfg.maxNodeResponseTime) {
Expand Down Expand Up @@ -335,6 +337,32 @@ export class NetworkApi {
return this.askBlockchainTo(ChainAction.CREATE_TRANSACTION, { data });
}

public async executeCall(address: string, method: string, args: any[], abi: any) {
// TODO move to evmContract.scGet
const io = getAbiInputsOutputsType(abi, method);

const encodedFunction = encodeFunction(method, args, io.inputs);

const data = { call: '0x0', args: [`0x${encodedFunction}`], to: `0x${address}` };

const response = await this.askBlockchainTo(ChainAction.EXECUTE_CALL, { data });

if (response.result !== 'return') throw new Error(`${response.result}: ${response?.signature}`);

const results = AbiCoder.decode(io.outputs, response.bin);

let returnValue: any = results;

if (io.outputNames.length === results.length) {
returnValue = results.reduce((aggr, item, key) => {
aggr[io.outputNames[key]] = item;
return aggr;
}, {});
}

return results.length === 1 ? results[0] : returnValue;
}

public async getTransactionStatus(txId: string) {
return this.askBlockchainTo(ChainAction.GET_TRANSACTION_STATUS, { txId });
}
Expand Down Expand Up @@ -374,6 +402,12 @@ export class NetworkApi {
requestParams.data = parameters.data;
break;

case ChainAction.EXECUTE_CALL:
actionUrl = '/execute/call';
requestParams.method = 'post';
requestParams.data = parameters.data;
break;

case ChainAction.GET_TRANSACTION_STATUS:
actionUrl = '/tx/status';
requestParams.url = parameters.txId;
Expand Down
8 changes: 1 addition & 7 deletions packages/tssdk/src/libs/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,15 @@ const getRegisterTxBody = async (
referrer: string,
powDifficulty = 16,
) => {
/**
* @todo move to env/const
*/
const clid = `power_wallet_${process.env.GIT_HEAD_HASH}`;

let body = {
k: KIND_REGISTER,
t: timestamp,
nonce: '',
h: createHash('sha256').update(publicKey).digest(),
e: { clid },
e: { },
};

if (referrer) {
// @ts-ignore
body = { ...body, e: { ...body.e, referrer } };
}

Expand Down

0 comments on commit 2d4d5c6

Please sign in to comment.