Skip to content

Commit

Permalink
some
Browse files Browse the repository at this point in the history
  • Loading branch information
lovelycs committed Feb 21, 2019
1 parent 24bef57 commit f42e5c1
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/Wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ class Account {
}

async callContract({
toAddress, jsonInterface, params, tokenId, amount
toAddress, abi, params, methodName, tokenId, amount
}) {
const _callContractBlock = await this._client.buildinTxBlock.callContract({
accountAddress: this.address,
toAddress, jsonInterface, params, tokenId, amount
toAddress, abi, params, methodName, tokenId, amount
});
return this._client.buildinLedger.sendRawTx(_callContractBlock, this.privateKey);
}
Expand Down
3 changes: 1 addition & 2 deletions src/client/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ export default class ledger {
let _accountBlock = signAccountBlock(accountBlock, privateKey);

try {
let _result = await this._client.tx.sendRawTx(_accountBlock);
return _result;
return await this._client.tx.sendRawTx(_accountBlock);
} catch(err) {
let _err = err;
_err.accountBlock = _accountBlock;
Expand Down
11 changes: 6 additions & 5 deletions src/client/txBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RPCresponse, SBPregBlock, block8, block7, revokeVotingBlock, quotaBlock
import { checkParams, validNodeName } from "utils/tools";
import { formatAccountBlock, validReqAccountBlock } from "utils/builtin";
import { getAccountBlock as _getAccountBlock, getSendTxBlock, getReceiveTxBlock } from 'utils/accountBlock';
import { encodeFunctionCall } from "utils/abi";
import { encodeFunctionCall, encodeParameters } from "utils/abi";
import { isArray } from 'utils/encoder';

import client from '.';
Expand Down Expand Up @@ -318,7 +318,7 @@ export default class tx {
}

async createContract({
accountAddress, tokenId, amount, fee, hexCode, abi, height, prevHash, snapshotHash
accountAddress, tokenId, amount, fee, hexCode, abi, params, height, prevHash, snapshotHash
}: createContractBlock, requestType = 'async') {
let err = checkParams({ hexCode, abi, tokenId, amount, fee}, ['hexCode', 'abi', 'tokenId', 'amount', 'fee']);
if (err) {
Expand All @@ -342,21 +342,22 @@ export default class tx {
inputs.forEach(({ type }) => {
types.push(type);
});
data += encodeParameters(types, params);
}

block.data = Buffer.from(data, 'hex').toString('base64');
return block;
}

async callContract({
accountAddress, toAddress, tokenId, amount, jsonInterface, params=[], height, prevHash, snapshotHash
accountAddress, toAddress, tokenId, amount, abi, methodName, params=[], height, prevHash, snapshotHash
}: callContractBlock, requestType = 'async') {
let err = checkParams({ toAddress, jsonInterface, tokenId }, ['toAddress', 'jsonInterface', 'tokenId']);
let err = checkParams({ toAddress, abi, tokenId }, ['toAddress', 'abi', 'tokenId']);
if (err) {
return Promise.reject(err);
}

let data = encodeFunctionCall(jsonInterface, params);
let data = encodeFunctionCall(abi, params, methodName);
return this[`${requestType}AccountBlock`]({
blockType: 2,
accountAddress,
Expand Down
3 changes: 2 additions & 1 deletion src/const/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ export declare type createContractBlock = {
export declare type callContractBlock = {
accountAddress: Address,
toAddress: Address,
jsonInterface: object,
abi: object,
methodName?: string,
tokenId: TokenId,
amount: BigInt,
params?: [],
Expand Down
10 changes: 6 additions & 4 deletions src/utils/abi/encodeFunction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isArray, isObject, blake2bHex } from 'utils/encoder';
import { validType } from './inputsType';

function encodeFunction(jsonFunction, mehtodName?) {
export function encodeFunction(jsonFunction, mehtodName?) {
let isArr = isArray(jsonFunction);
let _jsonFunction = isArr ? getFunction(jsonFunction, mehtodName) : jsonFunction;
let result = jsonFunctionToString(_jsonFunction);
Expand Down Expand Up @@ -32,7 +32,11 @@ function jsonFunctionToString(jsonFunction) {
return jsonFunction.name + '(' + types.join(',') + ')';
}

function getFunction(jsonFunction, mehtodName?) {
export function getFunction(jsonFunction, mehtodName?) {
if (!isArray(jsonFunction) && isObject(jsonFunction)) {
return jsonFunction;
}

if (jsonFunction.length !== 1 && !mehtodName) {
throw '[Error] Param(s) missing, methodName.';
}
Expand All @@ -47,5 +51,3 @@ function getFunction(jsonFunction, mehtodName?) {
}
}
}

export default encodeFunction;
13 changes: 9 additions & 4 deletions src/utils/abi/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArray } from 'utils/encoder';
import encodeFunction from './encodeFunction';
import { encodeFunction, getFunction } from './encodeFunction';
import { encodeParameter as _encodeParameter, encodeParameters as _encodeParameters, decodeParameter as _decodeParameter, decodeParameters as _decodeParameters } from './coder';


Expand All @@ -12,13 +12,18 @@ export function encodeParameter(type, param) {
export const encodeParameters = _encodeParameters
export const decodeParameter = _decodeParameter
export const decodeParameters = _decodeParameters
export function encodeFunctionCall(jsonInterface, params) {
let inputs = jsonInterface.inputs;
export function encodeFunctionCall(jsonInterface, params, methodName?) {
if (!methodName && isArray(jsonInterface)) {
throw 'No methodName, jsonInterface should be jsonObject.'
}

let func = getFunction(jsonInterface, methodName);
let inputs = func.inputs;
let types = [];
inputs.forEach(({ type }) => {
types.push(type);
});
return encodeFunctionSignature(jsonInterface) + encodeParameters(types, params)
return encodeFunctionSignature(func) + encodeParameters(types, params)
}


Expand Down
23 changes: 23 additions & 0 deletions test/utils/abi.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,5 +469,28 @@ describe('utils/tools', function () {
}]
}, ['2345675643', 'Hello!%']);
assert.equal('96173f6c000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000', result);

let result1 = abi.encodeFunctionCall([{
name: 'myMethod',
type: 'function',
inputs: [{
type: 'uint256',
name: 'myNumber'
},{
type: 'string',
name: 'myString'
}]
}, {
name: 'myethod',
type: 'function',
inputs: [{
type: 'uint256',
name: 'myNumber'
},{
type: 'string',
name: 'myString'
}]
}], ['2345675643', 'Hello!%'], 'myMethod');
assert.equal('96173f6c000000000000000000000000000000000000000000000000000000008bd02b7b0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000748656c6c6f212500000000000000000000000000000000000000000000000000', result1);
});
});

0 comments on commit f42e5c1

Please sign in to comment.