I am using Web3.js with Node JS. I need to get the transaction details for a smart contract under BNB. I developed my codes, but no matter what I did, I couldn't get it to work. Can you help me? Where am I doing wrong?
.env
bnbProvider = https://bsc-dataseed1.binance.org:443
web3crypto.js
const Web3 = require('web3')
const thisWeb3 = new Web3(new Web3.providers.HttpProvider(process.env.bnbProvider))
const minimumABI = [ABI OBJECT]
const tb = new thisWeb3.eth.Contract(minimumABI, process.env.contractId)
my function
exports.checkTransaction = async transactionId => {
const transactionData = await thisWeb3.eth.getTransaction(transactionId)
console.log(transactionData)
return transactionData
}
and Error
Error: Returned error: invalid argument 0: hex string has length 40, want 64 for common.Hash
Related
I am trying to sign a build & sign transaction then create a hex to broadcast the transaction on TRON Network.
I have successfully done this but when i broadcast this transaction i am getting "TRON TAPOS_ERROR" error.
I have searched and got the reason that i have to include last block number and hash.
check this : https://github.com/tronprotocol/java-tron/issues/857
But I don't know how to do this.
I have tried this code :
const CryptoUtils = require("#tronscan/client/src/utils/crypto");
const TransactionUtils = require("#tronscan/client/src/utils/transactionBuilder");
async function transferContractTx() {
const fromAddress = "FROM_ADDRESS";
const toAddress = "TO_ADDRESS";
const privateKey = "MY_PRIVATE_KEY";
const token = "TRX";
const amount = 1000000;
let transaction = TransactionUtils.buildTransferTransaction(token, fromAddress, toAddress, amount);
console.log(JSON.stringify(transaction));
let signedTransaction = CryptoUtils.signTransaction(privateKey, transaction);
console.log(signedTransaction);
}
transferContractTx();
The below code throws "Transaction Oversize" error
const contractBytecode = fs.readFileSync("TestToken_sol_TestToken.bin");
const fileCreateTx = new FileCreateTransaction()
.setContents(contractBytecode)
.setKeys([operatorKey])
.freezeWith(client);
const fileCreateSign = await fileCreateTx.sign(operatorKey);
const fileCreateSubmit = await fileCreateSign.execute(client);
const fileCreateRx = await fileCreateSubmit.getReceipt(client);
const bytecodeFileId = fileCreateRx.fileId;
console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);
But trying out solution provided here, I ran into this new error "ContractCreateFlow is not a constructor" :
const contractInstantiateTx = new ContractCreateFlow()
.setGas(100000)
.setBytecode(contractBytecode)
const contractInstantiateSubmit = await contractInstantiateTx.execute(client);
const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(client);
const contractId = contractInstantiateRx.contractId;
const contractAddress = contractId.toSolidityAddress();
console.log(`- The smart contract ID is: ${contractId} \n`);
console.log(`- The smart contract ID in Solidity format is: ${contractAddress} \n`);
Please see this answer: Getting error "Transaction Oversize" while creating a smart contract in Hedera blockchain
You may need to import the ContractCreateFlow() module into your program from the SDK. Check the SDK version you're using as ContractCreateFlow() was introduced in v2.14 of the JS SDK.
As an alternative, see this example if you wish to use FileCreate, FileAppend, and Contract create modules:
//Create a file on Hedera and store the hex-encoded bytecode
const fileCreateTx = await new FileCreateTransaction().setKeys([adminKey]).execute(client);
const fileCreateRx = await fileCreateTx.getReceipt(client);
const bytecodeFileId = fileCreateRx.fileId;
console.log(`- The smart contract bytecode file ID is: ${bytecodeFileId}`);
// Append contents to the file
const fileAppendTx = await new FileAppendTransaction()
.setFileId(bytecodeFileId)
.setContents(bytecode)
.setMaxChunks(10)
.setMaxTransactionFee(new Hbar(2))
.execute(client);
await fileAppendTx.getReceipt(client);
console.log(`- Content added`);
console.log(`\nSTEP 2 - Create contract`);
const contractCreateTx = await new ContractCreateTransaction()
.setAdminKey(adminKey)
.setBytecodeFileId(bytecodeFileId)
.setGas(100000)
.execute(client);
const contractCreateRx = await contractCreateTx.getReceipt(client);
const contractId = contractCreateRx.contractId.toString();
console.log(`- Contract created ${contractId}`);
If you don't use ContractCreateFlow(), you first create an empty file, then append content (by chunks), and then create the contract - as shown by the code above.
My bin file size is 18kb only. I also get a solution to use IPFS but don't know how to use it. If there is any reference for using IPFS then share me plz. :
Error: PrecheckStatusError: transaction 0.0.34898094#1653658245.135892060 failed precheck with status TRANSACTION_OVERSIZE
Here is my code :
const {
AccountId,
PrivateKey,
Client,
FileCreateTransaction,
ContractCreateTransaction,
ContractFunctionParameters,
ContractExecuteTransaction,
ContractCallQuery,
Hbar
} = require("#hashgraph/sdk");
const fs = require("fs");
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
async function main() {
// Import the compiled contract bytecode
const contractBytecode = fs.readFileSync("first_contract_sol_ABC_TOKEN.bin");
// Create a file on Hedera and store the bytecode
const fileCreateTx = new FileCreateTransaction().setContents(contractBytecode).setKeys([operatorKey]).setMaxTransactionFee(new Hbar(1))
.freezeWith(client);
const fileCreateSign = await fileCreateTx.sign(operatorKey);
console.log(Date.now() / 1000);
const fileCreateSubmit = await fileCreateSign.execute(client);
const fileCreateRx = await fileCreateSubmit.getReceipt(client);
const bytecodeFileId = fileCreateRx.fileId;
console.log(`- The bytecode file ID is: ${bytecodeFileId} \n`);
// Instantiate the smart contract
const contractInstantiateTx = new ContractCreateTransaction()
.setBytecodeFileId(bytecodeFileId)
.setGas(100000)
.setConstructorParameters(
new ContractFunctionParameters().addString("Alice").addUint256(111111)
);
const contractInstantiateSubmit = await contractInstantiateTx.execute(client);
const contractInstantiateRx = await contractInstantiateSubmit.getReceipt(
client
);
const contractId = contractInstantiateRx.contractId;
const contractAddress = contractId.toSolidityAddress();
console.log(`- The smart contract ID is: ${contractId} \n`);
console.log(`- Smart contract ID in Solidity format: ${contractAddress} \n`);
}
main();
You are hitting the TRANSACTION_OVERSIZE error because Hedera transactions have a 6kb size limit including all signatures.
If the compiled bytecode for your contract is relatively large, then you will need to create a file on Hedera, then append the contents in multiple chunks, and then create the contract.
The SDK now handles those 3 steps for you when you use ContractCreateFlow().Here's an example:
const contractCreate = new ContractCreateFlow()
.setGas(100000)
.setBytecode(bytecode);
//Sign the transaction with the client operator key and submit to a Hedera network
const txResponse = contractCreate.execute(client);
//Get the receipt of the transaction
const receipt = (await txResponse).getReceipt(client);
I'm trying to purchase a fixed price listed NFT from a specific collection using the opensea-js sdk, however when passing through a valid order into seaport.fulfillOrder() I am just getting undefined as the returned value.
My assumption is that the web3 provider is incorrectly set up for the account looking to make the purchase but I have been stuck on this for hours and haven't made any progress. Anyone had any luck using this method to successfully purchase NFTs on Opensea through Node.js?
I'm testing on the Rinkeby testnet.
Here's the code I use to fulfill the order, the order object is valid and retrieved using seaport.api.getOrder()
const order = await seaport.api.getOrder({
asset_contract_address: NFT_CONTRACT_ADDRESS,
maker: undefined,
taker: '0x0000000000000000000000000000000000000000',
owner: undefined,
side: OrderSide.OrderSide.Sell,
bundled: false,
token_id: token_id,
sale_kind: 0
});
const accountAddress = '0x12....';
const transactionHash = await seaport.fulfillOrder({
accountAddress,
order,
}).catch(err => console.log(err));
console.log("tx hash: " + transactionHash);
Here's the code I'm using to initialise web3 and seaport:
const provider = new Web3.providers.HttpProvider(walletAPIUrl);
const web3 = new Web3(provider);
const account = web3.eth.accounts.privateKeyToAccount('0x' + PRIVATE_KEY);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
const seaport = new OpenSeaPort(
provider,
{
networkName: Network.Rinkeby,
},
(arg) => console.log(arg)
);
Below is the error message I'm seeing when running the function:
I am trying to create a business network which demonstrates nondeterministic behaviour by requesting a random number from a URL (https://random-number-api.mybluemix.net/random).
However, when I invoke the transaction from, the following error appears:
TypeError: request.get is not a function
In the documentation (https://hyperledger.github.io/composer/latest/integrating/call-out), it is mentioned that request is global and can be used directly.
To test the URL,
curl -X GET https://random-number-api.mybluemix.net/random
I am using Composer 0.18.2.
The following is the code. The whole business network definition is found here: https://github.com/caveman7/nondeterministic
async function createStock(request) {
const factory = getFactory();
const namespace = 'org.nondeterministic';
const stock = factory.newResource(namespace, 'Stock', request.stockId);
const priceAsStr = await request.get({uri: 'https://random-number-api.mybluemix.net/random'});
stock.price = parseInt(priceAsStr);
//stock.price = getPrice();
console.log('#debug - Stock Price Generated is: USD' + stock.price);
const ar = await getAssetRegistry(namespace + '.Stock')
await ar.add(stock);
}
const getPrice = () => {
const price = Math.floor((Math.random() * 10000) + 1);
return price;
}
probably because you're providing request as an object to the TP. As shown above or https://github.com/caveman7/nondeterministic/blob/master/lib/script.js#L22
Errors as its 'not' got a method called get?. It should be async function createStock(createStock ) to match the parameter name in the transaction decorator (tracing the github script file from the above link) - and then you should be able to access request.get() as you would expect (since request.get came in in 0.18.1 IIRC).