How to send TRX to a smart contract function in tronlink web - solidity

How can I send tron to a smart contract function in tronlink web.
I am trying the below method, but not working.
var contract = await tronweb.contract().at('{{env('CONTRACT_ADDRESS')}}')
await contract.Invest({{session('member_id')}}).address.send(amount);
it's calling the smart contract function but not sending tron.

callValue - Amount of SUN transferred to the contract with this transaction.
(1TRX = 1,000,000 SUN)
await contract.Invest({{session('member_id')}}).send({callValue: amount})
You can check tronweb documentation for send () parameters.

Related

How to interact with interfaces through HardHat?

how can I interact with interfaces through hardhat.
For example in brownie you can just call function from an interface like this:
def main():
weth = interface.IWeth("weth_address")
tx = weth.deposit({"from": account, "value": "value"})
and that's it.
Is there a way to do the same in hardhat? I've been siting on this problem for couple of hours and for the life of me I can't figure out how to do this.
If it's not possible how do I get weth through solidity.
Yes, you can do it with ethers.getContractAt.
import { ethers } from "hardhat";
...
// Assume `ISetup` is an interface of the contract.
const setup = await ethers.getContractAt("ISetup", contractAddress);
In hardhat, you can achieve it using the attachment.
Go to hardhat console: npx hardhat console
Get the contract factory: factory = await ethers.getContractFactory('YourContractName')
Attach to address weth = await factory.attach('weth_address')
Do your function call e.g:
signer = await ethers.getSigner();
await signer.sendTransaction({ to: tokenAddress, value: ethers.utils.parseEther("5.0") });

Etherjs Transfer BNB Return 0 and doesnt end to Receiver Address

Hi im new to smart contract. Im trying to code a simple transfer using etherjs and nextjs. Basically below are my code
import {ethers} from 'ethers';
const ContractAddr = '0xB8c77482e45F1F44dE1745F52C74426C631bDD52'; /* BNB Contract address */
const ContractABI =[{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"}]
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
let numberOfTokens = ethers.utils.parseUnits('0.2', 18)
const Contract = new ethers.Contract(ContractAddr,ContractABI,signer)
const receiver = '0x8CA65A3Ce90d31a88FAa747c695183C2aefe537f';
Contract.transfer(receiver, numberOfTokens).then((transferResult) => {
console.log(transferResult)
alert("sent token")
}).catch((error) => {
console.error('error',error);
});
on my metamask, it pops up as i wanted as follow :
however when i check the transaction details at the BSCscan Testnet, its showing 0 Value an the receiver is the Contract Address instead of the receiver.
https://testnet.bscscan.com/tx/0x3278d9668391eee98aadf83e8ae58a0b999f2e9c3e5f47a21f504e1c04b24814
Help. Been stuck for a week searching high and low for similiar issue at Stackoverflow :(
You're mixing together two different approaches - sending ERC-20 tokens and sending native currency of the network.
On the Ethereum networks, BNB is an ERC-20 token (link - mind the 0xB8c7... address that you're using in your code).
However, on the BSC networks, BNB is the native currency. So you transfer it using native transactions - the same way as you would transfer ETH on Ethereum.
Assuming your code uses BSC provider:
const provider = new ethers.providers.Web3Provider(window.ethereum);
const params = [{
from: senderAddress,
to: '0x8CA65A3Ce90d31a88FAa747c695183C2aefe537f',
value: ethers.utils.parseUnits('0.2', 18),
}];
const transactionResult = await provider.send('eth_sendTransaction', params);

How to get balanceOf of tron smart contract

I am using tronlink chrome extension and trying to call balanceOf method of a smart contract. I am very new to smart contract. Unable to find any solution. Please check my code:
let contractDetail = await window.tronWeb.trx.getContract('TG7DLMkJPYeG4QTZ8Qfgk9Mu7ePM5SQpbN');
let contract = await window.tronWeb.contract(contractDetail.abi.entrys, 'TG7DLMkJPYeG4QTZ8Qfgk9Mu7ePM5SQpbN');
balance = contract.balanceOf.call('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
console.log(balance)
//Error: Uncaught TypeError: Cannot read property 'call' of undefined
balanceOf requires one argument.
'balanceOf(address)'
Instead of
contract.balanceOf.call('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');
You should pass in the address into balanceOf
contract.balanceOf('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS').call();
Use tronWeb.trx.getBalance(CONTRACT_ADDRESS);
Your code should look like
balance = await tronWeb.trx.getBalance('TNkJRejobNuZhV2LiwfGQ7wPNiLtcbDueS');

Deploy a smart contract with constructor in Truffle

I want to deploy my contract which contain a constructor (2 account address and a value P), in Remix, I put manually the address of both account and P value, but in truffle, I edited manually the 2_deploy_contracts.js file as follow:
Contract:
constructor(address payable _account1, address payable _account2, uint _P) public {account1 = _account1;account2 = _account2; P = _P;}
2_deploy_contracts.js:
var contract = artifacts.require("contract");
module.exports = function(deployer) {
deployer.deploy(contract, account1, account2, P);};
Thanks in advance for help.
You have to declare and initialize these parameters :
var account1='0x...';
var account2='0x...';
var P=...;
deployer.deploy(contract, account1, account2, P);
so i was facing the similar problem. And there are few things to be noted.
In your contract, you should declare the contracts, constructer something like this:
Here Coin and Reward are my contracts.
constructor(Coin _coinContractsAddress, Reward _rewardContractAddress){
coin_ = _coinContractsAddress;
reward_ = _rewardContractAddress;
}
Now this was for the contract.
Changes in Deployment file should be made like this:
deploy-contracts.js
module.exports = async function() {
await deployer.deploy(Coin);
const coin_ = await Coin.deployed();
await deployer.deploy(Reward);
const reward_ = await Reward.deployed();
//Now we are going to deploy these contracts, address in the 3rd Contract address named Bank.
await deployer.deploy(Bank, coin_.address, reward_.address);
//Point to note is we are deploying the Bank contract and in its constructor at same time we are passing the coin_.address which is the deployed contract address and reward_.address.
}
Now run truffle migrations and it will be successfully deployed. truffle migrate --reset

Use a contract at a specific address

In migration file i'm deploying one contract and for that contract i'm passing another contract's address as a constructor parameter
var jbkContract = artifacts.require("./JBK.sol");
var payBContract = artifacts.require("./payback.sol");
var contract_address = '0xB525F2F0046fA37f21EaF1F0619B3de7c1094324';
module.exports = async function(deployer) {
let ins = await jbkContract.at(contract_address);
await deployer.deploy(payBContract,ins,{"from":"0x69A9CAEc73e4378801266dFc796d92aFC98013f6"});
};
For this i'm getting this error
"payback" -- invalid address (arg="_jbkContract", coderType="address", value="[object Object]").
Earlier i was deploying JBKContract and then using that address i was deploying the payback contract and that was working fine.
var jbkContract = artifacts.require("./JBK.sol");
var payBContract = artifacts.require("./payback.sol");
module.exports = async function(deployer) {
deployer.deploy(jbkContract,1000000,"JBK","JBK",{"from":"0x69A9CAEc73e4374401266dFc796d92aFC98013f6"}).then(
function() {
return deployer.deploy(payBContract,jbkContract.address,{"from":"0x69A9CAEc73e4374401266dFc796d92aFC98013f6"});
}
)
};
But i don't want to deploy JBK contract only once and use that contract's address to deploy Payback everytime.How to do that?
I see 2 issues here.
1.0xB525F2F0046fA37f21EaF1F0619B3de7c1094324 is not an address with valid checksum. Address with valid checksum is 0xB525f2f0046Fa37F21Eaf1F0619b3DE7C1094324. This might not be a problem in Truffle though.
2.In the first chunk of code in this line
await deployer.deploy(payBContract, ins, {"from":"0x69A9CAEc73e4378801266dFc796d92aFC98013f6"});
you are passing the contract instance as a parameter instead of contract address. Correct code might be
await deployer.deploy(payBContract, ins.address, {"from":"0x69A9CAEc73e4378801266dFc796d92aFC98013f6"});