|
1 | 1 | import {Eth} from '../index';
|
2 |
| -import {Buffer} from 'buffer'; |
3 |
| -import {ContractExecuteTransaction} from "@hashgraph/sdk"; |
| 2 | +import { |
| 3 | + ContractExecuteTransaction, |
| 4 | + Client, AccountId |
| 5 | +} from "@hashgraph/sdk"; |
| 6 | +var cache = require('js-cache'); |
4 | 7 |
|
5 | 8 | export class EthImpl implements Eth {
|
| 9 | + |
6 | 10 | // FIXME
|
7 | 11 | feeHistory() {
|
8 | 12 | const blockNum = "0x" + Date.now()
|
@@ -121,17 +125,69 @@ export class EthImpl implements Eth {
|
121 | 125 | return 0x1;
|
122 | 126 | }
|
123 | 127 |
|
124 |
| - sendRawTransaction(transaction: string): string { |
125 |
| - if (transaction.startsWith("0x")) { |
126 |
| - transaction = transaction.substring(2); |
| 128 | + async sendRawTransaction(transaction: string): Promise<string> { |
| 129 | + let client; |
| 130 | + |
| 131 | + try { |
| 132 | + client = Client.fromConfig( |
| 133 | + { |
| 134 | + network: |
| 135 | + { |
| 136 | + '34.70.108.154:50211': new AccountId(3) |
| 137 | + }, |
| 138 | + operator: { |
| 139 | + accountId: '0.0.2', |
| 140 | + privateKey: '302e020100300506032b65700422042091132178e72057a1d7528025956fe39b0b847f200ab59b2fdd367017f3087137'} |
| 141 | + } |
| 142 | + ); |
| 143 | + } catch (error) { |
| 144 | + console.log(error); |
| 145 | + throw new Error( |
| 146 | + "Environment variables HEDERA_NETWORK, OPERATOR_ID, and OPERATOR_KEY are required." |
| 147 | + ); |
127 | 148 | }
|
128 | 149 |
|
129 |
| - var data = Buffer.from(transaction, 'hex'); |
130 | 150 |
|
131 |
| - var txRequest : ContractExecuteTransaction = |
132 |
| - new ContractExecuteTransaction() |
133 |
| - .populateFromForeignTransaction(data); |
| 151 | + var txRequest : ContractExecuteTransaction | null = null; |
| 152 | + |
| 153 | + txRequest = new ContractExecuteTransaction(); |
| 154 | + |
| 155 | + txRequest = txRequest.populateFromForeignTransaction(transaction); |
| 156 | + |
| 157 | + console.log(txRequest); |
| 158 | + |
| 159 | + console.log("ProtoBuf"); |
| 160 | + |
| 161 | + var contractExecuteResponse = null; |
| 162 | + |
| 163 | + if (client instanceof Client) { |
| 164 | + try { |
| 165 | + contractExecuteResponse = await txRequest.execute(client); |
| 166 | + } catch (e) { |
| 167 | + console.log(e); |
| 168 | + throw e; |
| 169 | + } |
| 170 | + } else { |
| 171 | + throw new Error( |
| 172 | + "txRequest was not a ContractExecute Transaction or the Client was invalid" |
| 173 | + ); |
| 174 | + } |
| 175 | + cache.set(contractExecuteResponse.transactionHash, contractExecuteResponse.transactionId); |
| 176 | + |
| 177 | + console.log(contractExecuteResponse); |
| 178 | + |
| 179 | + try { |
| 180 | + const contractReceipt = await contractExecuteResponse.getReceipt(client); |
| 181 | + |
| 182 | + console.log(contractReceipt); |
| 183 | + |
| 184 | + const contractRecord = await contractExecuteResponse.getRecord(client); |
| 185 | + |
| 186 | + console.log(contractRecord); |
| 187 | + } catch (e) { |
| 188 | + console.log(e); |
| 189 | + } |
134 | 190 |
|
135 |
| - return data.toString(); |
| 191 | + return Buffer.from(contractExecuteResponse.transactionHash).toString('hex'); |
136 | 192 | }
|
137 | 193 | }
|
0 commit comments