Skip to content

Commit

Permalink
feat: 🎸 contract payment
Browse files Browse the repository at this point in the history
  • Loading branch information
SmilingXinyi committed Feb 25, 2021
1 parent e9c2e13 commit 4a0f121
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
13 changes: 8 additions & 5 deletions src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ export default class Contract {
contractName: string,
bin: string,
abi: string,
// @ts-ignore
lang: string,
initArgs: any
) {
Expand Down Expand Up @@ -265,7 +264,8 @@ export default class Contract {
contractName: string,
methodName: string,
moduleName: string,
args: any
args: any,
amount: string
): ContractRequesttModel[] {

const newArgs = {
Expand All @@ -285,7 +285,8 @@ export default class Contract {
module_name: moduleName,
method_name: methodName,
contract_name: contractName,
args: newArgs
args: newArgs,
amount
}];

return invokeRequests;
Expand All @@ -295,7 +296,8 @@ export default class Contract {
contractName: string,
methodName: string,
moduleName: string,
args: any
args: any,
amount: string
): ContractRequesttModel[] {

const newArgs = {
Expand All @@ -318,7 +320,8 @@ export default class Contract {
module_name: moduleName,
method_name: methodName,
contract_name: contractName,
args: newArgs
args: newArgs,
amount
}];

return invokeRequests;
Expand Down
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ export default class XuperSDK implements XuperSDKInterface {

async invoke(
invokeRequests: ContractRequesttModel[],
amount = '0',
account?: AccountModel,
): Promise<any> {

Expand Down Expand Up @@ -269,9 +270,9 @@ export default class XuperSDK implements XuperSDKInterface {
const gasUsed = preExecWithUtxos.response.gas_used || 0;

const tx = await this.transactionInstance.makeTransaction(acc, {
amount: '0',
amount,
fee: gasUsed.toString(),
to: ''
to: amount !== '0' ? invokeRequests[invokeRequests.length - 1].contract_name! : ''
}, authRequires, preExecWithUtxos);

return {
Expand Down Expand Up @@ -481,13 +482,15 @@ export default class XuperSDK implements XuperSDKInterface {
methodName: string,
moduleName: string,
args: any,
amount = '0',
account?: AccountModel
): Promise<any> {
const invokeRequests = this.contractInstance.invokeContract(
contractName,
methodName,
moduleName,
args
args,
amount
);

if (!account) {
Expand All @@ -498,21 +501,23 @@ export default class XuperSDK implements XuperSDKInterface {
throw Errors.ACCOUNT_NOT_EXIST;
}

return this.invoke(invokeRequests, account)
return this.invoke(invokeRequests, amount, account);
}

async invokeSolidityContarct(
contractName: string,
methodName: string,
moduleName: string,
args: any,
amount = '0',
account?: AccountModel
): Promise<any> {
const invokeRequests = this.contractInstance.invokeSolidityContract(
contractName,
methodName,
moduleName,
args
args,
amount
);

if (!account) {
Expand All @@ -523,7 +528,7 @@ export default class XuperSDK implements XuperSDKInterface {
throw Errors.ACCOUNT_NOT_EXIST;
}

return this.invoke(invokeRequests, account)
return this.invoke(invokeRequests, amount, account)
}

transactionIdToHex(t: Required<string>): string {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/endorsement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const plugin = (args: any) => ({
sign: async (checkTx: TransactionModel, tx: TransactionModel): Promise<TransactionModel> => {
const obj = {
bcname: chain,
tx: convert(tx)
// @ts-ignore
tx: convert(tx, ['jsonEncoded'])
};

const body = {
Expand Down
47 changes: 27 additions & 20 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ export default class Transaction {
}

encodeDataForDigestHash(tx: TransactionModel, include_signs: boolean) {
const newtx: TransactionModel = JSON.parse(JSON.stringify(tx));

let str = '';

tx.txInputs.forEach(
newtx.txInputs.forEach(
(txInput: TXInput) => {
if (txInput.refTxid) {
str += jsonEncode(txInput.refTxid);
Expand All @@ -162,18 +164,18 @@ export default class Transaction {
}
);

str += jsonEncode(convert(tx.txOutputs));
str += jsonEncode(convert(newtx.txOutputs));

if (tx.desc && tx.desc.length > 0) {
str += jsonEncode(tx.desc);
if (newtx.desc && newtx.desc.length > 0) {
str += jsonEncode(newtx.desc);
}

str += jsonEncode(tx.nonce);
str += jsonEncode(tx.timestamp);
str += jsonEncode(tx.version);
str += jsonEncode(newtx.nonce);
str += jsonEncode(newtx.timestamp);
str += jsonEncode(newtx.version);

if (tx.txInputsExt && tx.txInputsExt.length) {
tx.txInputsExt.forEach(inputExt => {
if (newtx.txInputsExt && newtx.txInputsExt.length) {
newtx.txInputsExt.forEach(inputExt => {
str += jsonEncode(inputExt.bucket);
if (inputExt.key) {
str += jsonEncode(inputExt.key);
Expand All @@ -189,8 +191,8 @@ export default class Transaction {
});
}

if (tx.txOutputsExt && tx.txOutputsExt.length) {
tx.txOutputsExt.forEach(outputExt => {
if (newtx.txOutputsExt && newtx.txOutputsExt.length) {
newtx.txOutputsExt.forEach(outputExt => {
str += jsonEncode(outputExt.bucket);
if (outputExt.key) {
str += jsonEncode(outputExt.key);
Expand All @@ -201,31 +203,36 @@ export default class Transaction {
});
}

if (tx.contractRequests && tx.contractRequests.length > 0) {
if (newtx.contractRequests && newtx.contractRequests.length > 0) {
// @ts-ignore
tx.contractRequests = tx.contractRequests.map((req: any) => {
newtx.contractRequests = newtx.contractRequests.map((req: any) => {
if (req.args) {
req.args = JSON.parse(stringify(req.args));
Object.keys(req.args).map((key: string) => {
if (req.args[key] === "") {
req.args[key] = null
}
});
}
return req;
});
}

// @ts-ignore
str += jsonEncode(convert(tx.contractRequests, ['jsonEncoded']));
str += jsonEncode(convert(newtx.contractRequests, ['jsonEncoded']));

str += jsonEncode(tx.initiator);
str += jsonEncode(newtx.initiator);

str += jsonEncode(tx.authRequire && tx.authRequire.length > 0 ? tx.authRequire : null);
str += jsonEncode(newtx.authRequire && newtx.authRequire.length > 0 ? newtx.authRequire : null);

if (include_signs) {
str += jsonEncode(tx.initiatorSigns);
str += jsonEncode(tx.authRequireSigns);
str += jsonEncode(newtx.initiatorSigns);
str += jsonEncode(newtx.authRequireSigns);
}

str += jsonEncode(tx.coinbase);
str += jsonEncode(newtx.coinbase);

str += jsonEncode(tx.autogen);
str += jsonEncode(newtx.autogen);

const te = new TextEncoder();
const bytes = te.encode(str);
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export type ContractRequesttModel = {
contract_name?: string;
method_name: string;
args: any;
amount?: string;
}

export type AuthModel = {
Expand Down

0 comments on commit 4a0f121

Please sign in to comment.