Recover BNB sent to self deployed contract - bots

I was trying out creating BOT, so created and deployed a contract in BSC from meatamask. I sent some BNB to that contract to check but it didnt work. How can I get the BNB back? Help is appreciated as I am very new to this

Unless you have a custom function in the contract that allows you to withdraw the funds, they are forever locked in the contract address.
pragma solidity ^0.8;
contract MyContract {
receive() external payable {}
function withdraw() external {
// transfer this contract's whole BNB balance to the `0x123` address
payable(address(0x123)).transfer(address(this).balance);
}
}

Related

Erc20 token and buying

with my own 0x363.. wallet address
I am generating erc20 token and when I issue this erc20 contract a contract address is generated (0x966D...). that is, I have one wallet address and one coin address.
E.g:
1 mytoken = 1 ethereum
E.g:
If a user buys my token with metamask, the user will pay ethereum.
And where is 1 ethereum. Why is this ethereum not uploaded to my admin account (0x363..). this ethereum goes to erc20 (0x966D ..) as far as I understand. How can I get this ethereum to my admin account?
How can I get this ethereum to my admin account?
Based on the context of the question (the ETH is paid to the token contract - not to a DEX pair contract), I'm assuming you have a custom buy function.
You can expand this buy function to use the transfer() method of address payable to transfer the just received ETH to your admin address.
pragma solidity ^0.8;
contract MyToken {
address admin = address(0x123);
function buy() external payable {
// transfers the whole ETH `value` sent to the `buy()` function
// to the `admin` address
payable(admin).transfer(msg.value);
// ... rest of your code
}
}
Contract bytecode is immutable (with some edge case exceptions), so in order to perform the change, you'll need to deploy a new contract - can't expand the already deployed contract.

How to send transaction from metamask wallet to backend node js

i am building simple dapp application where i want to verify something and then only make contract interaction but right now i am struggling to put a middleware which will act like user will make txn through metamask and then this txn or something will go to backend server on any language probably node js , and i will do some checking and if all good then Send it to block chain.
Any suggestion?
Right now its all in react Frontend and metamask browser extension.. and i can not make client side code restricted
And i can not ask for private key even.
Not possible by design. A transaction needs to be signed by the sender's private key. So unless the users are willing to give you their private key (so that you could sign the transaction for them on the backend), you'll need to change your approach.
If you need to allow interaction with the contract only to users authorized by your app, the contract needs to hold the list of authorized addresses. And the list can be updated by your app (that holds the private key to the owner address). Example:
pragma solidity ^0.8;
contract MyContract {
address public owner = address(0x123);
mapping(address => bool) public isAuthorized;
function setAuthorized(address _address, bool _isAuthorized) external {
require(msg.sender == owner, 'Only the contract owner can set authorized addresses');
isAuthorized[_address] = _isAuthorized;
}
function foo() external {
require(isAuthorized[msg.sender], 'Only authorized addresses can execute this function');
// ...
}
}

BEP-20 Token Transaction on NodeJs

Hi I'm just confused that how to transact BEP-20 Token(e.g: Binance-Peg BUSD-T). I have simply transact bnb in Binance Smart Chain with this code:
const tx = await web3.eth.accounts.signTransaction({
to: '0xB1455f4721b32390f4b65F86D2Cd50e74FaD7A99',
value: '500000000000000',
gas: 2000000
}, 'SENDER_PRIVATE_KEY');
const transaction = await web3.eth.sendSignedTransaction(tx.rawTransaction);
And it work perfectly fine. But I just do absolutely anything to transact a token, for example I used web3.eth.Contract(abi, contract_addr) and then
await contract.methods.transfer(toAddress, '500000000000000000').send({
from: '0xF9FF794700224fc9a1D6a30eb2A90d11eA1D82D1'
});
or with ethereumjs-tx package and ..., but none of them transact the token. I just need a sample code example or a well documented blog to tell me what should I do. Can anyone help me with that ?!!
In order to use the .send({from: ...}) method, you need to
Have the from account unlocked on your provider.
OR
Add its private key to the web3 account wallet (docs)
Ulocked provider account
This approach is mostly used on local providers (e.g. Ganache) that fund and unlock some accounts by default.
Keeping an unlocked account on a production provider is unwise, because anyone who queries the provider can send transactions.
Web3 account wallet
You need to pass the private key that generates the from address.
web3.eth.accounts.wallet.add(privateKey);
And then you can use the .send({from: ...}) method
await contract.methods.transfer(toAddress, '500000000000000000').send({
from: '0xF9FF794700224fc9a1D6a30eb2A90d11eA1D82D1'
});

Application fees error in Stripe Terminal (Direct charge)

I want to collect application fees in direct charge concept with Stripe Terminal but Am getting error" Can only apply an application_fee_amount when the PaymentIntent is attempting a direct payment (using an OAuth key or Stripe-Account header)". when i use destination charge its work fine but i want to use direct charge. below code is for destination code.Please help me how i can get application fee in direct charge.
public void paymentIntent() {
PaymentIntentParameters params = new PaymentIntentParameters.Builder()
.setAmount(usdamount)
.setCurrency("usd")
.setApplicationFeeAmount(usdapplicationfee)
.setDescription("Order#" + orderref)
.setMetadata(initialMetadata)
.setOnBehalfOf(accountid)
.setTransferDataDestination(accountid)
.build();
Terminal.getInstance().createPaymentIntent(params, new PaymentIntentCallback() {
#Override
public void onSuccess(PaymentIntent paymentIntent) {
collecetpayment(paymentIntent);
}
#Override
public void onFailure(TerminalException exception) {
accounterror(exception.getErrorMessage(), exception.getMessage());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
finish();
}
});
}
So it's a little complex, because for direct charges to succeed the PaymentIntent,
Reader, Location, and Connection must exist on the connected account. That means that you must pass the Stripe-Account header when creating these objects.
If you use direct charges, all Terminal API objects belong to connected accounts.
Here's the flow to create a direct charge with application fee with Terminal:
Your server creates the PaymentIntent on the connected account (by passing the Stripe-Account header) & sets the application fee.
(p400 only) Your server creates a Location on the connected account
(p400 only) Your server registers a Reader to that Location (passing the Stripe-Account header)
Your server creates a Connection token on the connected account
Note that steps 2 & 3 only apply to the Verifone reader—the bluetooth-based reader will register itself and your app's current location when integrated with the Stripe Terminal SDK.
After initializing Terminal with the Connection from step 4, you can pass the PaymentIntent from Step1 to Terminal.instance().collectPaymentMethod(), .processPayment(). Because the Connection and PaymentIntent are both attached to the connected account, the Terminal SDK will be able to process the direct charges.
There doesn't seem to be an example of this in the Stripe docs at the moment, so if you get stuck I'd recommend asking on their IRC channel (#stripe on freenode) or emailing Stripe support.

Stripe connect share card source not working

I am saving customers and their sources on my platform, and trying to create charges for my connected accounts. I am able to successfully create destination charges, but I'm having trouble with creating direct charges.
I've tried creating a token per: https://stripe.com/docs/connect/shared-customers
If I create a token using just the customer the example, the error is:
'You provided a customer without specifying a source. The default source of the customer is a source and cannot be shared from existing customers.'
Even though the documentation says that you need "The card or bank account ID for that customer, if you want to charge a specific card or bank account rather than the default".
I cannot find a parameter that lets me specify a source as well or instead of a customer.
I've tried sharing the customer's chosen source with the connected account per: https://stripe.com/docs/sources/connect#shared-card-sources
which results in the error:
'Sending credit card numbers directly to the Stripe API is generally unsafe. We suggest you use test tokens that map to the test card you are using, see https://stripe.com/docs/testing.'
I tried attaching a test token (tok_visa) to my customer, and creating a charge with that, but that had the same result.
Every time I have some success I end up with the error about sending numbers being unsafe even though I'm only ever sending Stripe's provided token or source ID's.
I've tried this:
const newToken = await stripe.tokens.create({
customer: customerId,
}, {
stripe_account: stripeAccountId,
}).catch((e) => {
console.log(e);
});
and this:
const newToken = await stripe.sources.create({
customer: customerId,
usage: 'single_use',
original_source: sourceId,
}, {
stripe_account: stripeAccountId,
}).catch((e) => {
console.log(e);
});
The only success I've had is creating the direct charge with a test token (tok_visa), completely bypassing the sharing of tokens/sources. No other combination seems to work. But that leaves me at a loss as to how to get a real shared token when I need it.
I found out I should be using sources, not tokens. And it turns out I was accidentally using the whole newToken as the source for the charge. It's working now that I'm passing the newToken.id to the charge.

Resources