Skip to content

Commit

Permalink
tron withdraw reward
Browse files Browse the repository at this point in the history
  • Loading branch information
zyang committed Jun 15, 2020
1 parent 7172772 commit 3e0a2bc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crypto-coin-kit",
"version": "0.2.24",
"version": "0.2.25",
"description": "crypto coin kit for common coins",
"main": "./dist/index.js",
"scripts": {
Expand Down
35 changes: 25 additions & 10 deletions src/TRON/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
buildTransferTransaction,
buildTriggerSmartContract,
buildVote,
buildWithdrawBalance,
// @ts-ignore
} from '@tronscan/client/src/utils/transactionBuilder';
import assert from 'assert';
Expand Down Expand Up @@ -84,11 +85,7 @@ export class TRON implements Coin {
} else {
tx = this.buildTransferTx(txData);
}
const raw = tx.getRawData();
const rawBytes = raw.serializeBinary();
const hash = sha256(rawBytes);
const sig = await signProvider.sign(hash.toString('hex'));
return this.buildSignTxResult(sig, tx);
return this.signTxRawData(tx, signProvider);
};

public generateTransactionSync = (
Expand Down Expand Up @@ -126,11 +123,18 @@ export class TRON implements Coin {
public vote = async (voteData: VoteData, signProvider: SignProvider) => {
const {address, votes, latestBlock} = voteData;
const tx = this.refWithLatestBlock(buildVote(address, votes), latestBlock);
const raw = tx.getRawData();
const rawBytes = raw.serializeBinary();
const hash = sha256(rawBytes);
const sig = await signProvider.sign(hash.toString('hex'));
return this.buildSignTxResult(sig, tx);
return this.signTxRawData(tx, signProvider);
};

public withdrawReward = async (
{address, latestBlock}: {address: string; latestBlock: LatestBlock},
signProvider: SignProvider,
) => {
const tx = this.refWithLatestBlock(
buildWithdrawBalance(address),
latestBlock,
);
return this.signTxRawData(tx, signProvider);
};

private buildTransferTx = (txData: TxData) => {
Expand Down Expand Up @@ -226,4 +230,15 @@ export class TRON implements Coin {
.slice(0, 8);
return selectorStr + parameters;
};

private signTxRawData = async (
tx: Transaction,
signProvider: SignProvider,
) => {
const raw = tx.getRawData();
const rawBytes = raw.serializeBinary();
const hash = sha256(rawBytes);
const sig = await signProvider.sign(hash.toString('hex'));
return this.buildSignTxResult(sig, tx);
};
}
27 changes: 27 additions & 0 deletions src/__tests__/coins/TRON.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,31 @@ describe('coin.TRON', () => {
'0af7020a02fa692208a9fcd49cae8a7fc340f8aef3b0ab2e5adf02080412da020a30747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e566f74655769746e657373436f6e747261637412a5020a1541056b1d26c3422e391a50b497f6d1f5aa0efd7cf612190a15414ac0706a3d08c0416ad361878b05d6fc8d36863e100e12190a15412d7bdb9846499a2e5e6c5a7e6fb05731c83107c7100612190a15414a193c92cd631c1911b99ca964da8fd342f4cddd100312190a15414d1ef8673f916debb7e2515a8f3ecaf2611034aa100a12190a1541530f931037de0c369968a5cf622003d240ef96e2100a12190a15415863f6091b8e71766da808b1dd3159790f61de7d100512190a154178c842ee63b253f8f0d2955bbc582c661a078c9d100212190a1541c2de79fc11be35c35e5148bd2e45d0633f641ac8100312190a1541d25855804e4e65de904faf3ac74b0bdfc53fac76100a12190a1541e40302d6b5e889bfbd395ed884638d7f03ee3f87100112416dbac65bf7760f606a000a65a31ac68414fc1b1e4efa3f32fc23719d021d9cefa14d97e9c7de8143aa8e0af13b643e9c4aeb89a89932ca88a270c6242222c1db01',
);
});

// curl -X POST --data '{"transaction":"0a6a0a0234df22089f02fd52839049064098e9a8c6ab2e5a53080d124f0a34747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e576974686472617742616c616e6365436f6e747261637412170a1541056b1d26c3422e391a50b497f6d1f5aa0efd7cf61241e14a9cc04f3313d09ca585f85df90f98818c7c6e98d5bb9580327fe118db321a5867fd3adf53cbb60fcf33eee455bb906dd1950b3f25663727459ff2ce32957a00"}' https://apilist.tronscan.org/api/broadcast
// {"code":"SUCCESS","success":true,"message":"","transaction":""}
// https://tronscan.org/#/transaction/93bd3e6c951201b88bf41a2669fffecf8db9beedd3ba07f791da09b721866658
it('should sign withdrawReward', async () => {
const params = {
address: 'TATrhRLpi65bMe8WwKCYAPjRAAoc3QWgR3',
latestBlock: {
hash:
'd3049042bed284589f02fd528390490656d43acb33f1bb8e0d27e537d4808f70',
number: 20657375,
timestamp: 1592237859000,
},
privateKey:
'87a88aff2fd0ea09f9d63c379a821f317c0e170d9a557296807d1922f81a2850',
};
const tx = await tron.withdrawReward(
{
address: params.address,
latestBlock: params.latestBlock,
},
signWithPrivateKey(params.privateKey),
);
expect(tx.txHex).toBe(
'0a6a0a0234df22089f02fd52839049064098e9a8c6ab2e5a53080d124f0a34747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e576974686472617742616c616e6365436f6e747261637412170a1541056b1d26c3422e391a50b497f6d1f5aa0efd7cf61241e14a9cc04f3313d09ca585f85df90f98818c7c6e98d5bb9580327fe118db321a5867fd3adf53cbb60fcf33eee455bb906dd1950b3f25663727459ff2ce32957a00',
);
});
});

0 comments on commit 3e0a2bc

Please sign in to comment.