I have a few ideas for smart contracts that I will be implementing using Ink! on Substrate.
Most of these ideas involve callers making deposits into the smart contract which will be held indefinitely, and then the caller may be able to withdraw at some future point, depending on other factors.
I have found an example that allows callers to withdraw from a smart contract:
https://github.com/paritytech/ink/blob/master/examples/contract-transfer/lib.rs
This hints at an approach to the caller making a deposit - the self.env().transferred_value() method suggests the caller can/has send/sent value.
I am struggling to find an example that deposits funds into a smart contract. Perhaps I am missing something fundamental here?
Ideally I want to avoid implementing a function that takes 2 addresses and transfers from one to the other (the contract already having & knowing its own address!), instead favouring the caller sending an amount, and it being deposited into the smart contract.
I think this may be possible with a smart contract method that takes no parameters but not certain and not at all clear on how the contract would then receive & hold the funds.
Looking for a concrete code example to show how this works in full, but also appreciate any comments to clarify or correct my (quite possibly incorrect) understanding.
Thanks in advance!
The method of your contract, that expects the payments should be marked with #[ink(message, payable)] for example like here. During the call of that method the user should specify that amount to send in the native currency(in the case of polkadot-js it is value field of payable method).
In the method, you can use let amount = Self::env().transferred_value(); to get a sent amount. If your transaction is not rejected your contract will hold amount tokens on its balance. It works the same way as in Solidity.
To get the current balance of the contract Self::env().balance(). To send a native currency Self::env().transfer(to, amount).
Maybe it will be useful to check that example.
Related
The problem is that I am trying to make free read-only calls to a smart contract on the Hedera network, but am encountering unexpected results. I have tried various methods, but am unable to successfully make the calls without incurring charges. I am looking for a solution or guidance on how to properly make these free read-only calls to the smart contract on Hedera.
//Create the transaction
const transaction = new ContractExecuteTransaction()
.setContractId(newContractId)
.setFunction("get_message")
I expected this get_message to not charge me HBAR since that function just returns a hardcoded string but I cant execute it for free like I want to. How do I do this?
If you're using the SDK, using ContractCallQuery() is better suited for read-only queries. See sample below:
// Query the contract to check changes in state variable
const contractQueryTx1 = new ContractCallQuery()
.setContractId(contractId)
.setGas(100000)
.setFunction("get_message";
const contractQuerySubmit1 = await contractQueryTx1.execute(client);
Note that the SDK still requires some small amount of gas.
There are a couple of other ways to do cost-free queries.
Use mirror nodes. These two tutorials can give you additional information on working with mirror nodes: https://hedera.com/blog/how-to-inspect-smart-contract-transactions-on-hedera-using-mirror-nodes and https://hedera.com/blog/how-to-look-up-transaction-history-on-hedera-using-mirror-nodes-back-to-the-basics
If you use Hashio (https://swirldslabs.com/hashio/) as a JSON-RPC relay, then you can use EVM tooling to deploy and interact with contracts on Hedera. Then you can simply call contracts the way you would in a chain like Ethereum. Here are some examples: https://github.com/hashgraph/hedera-json-rpc-relay/tree/main/tools
Modern AMM routers (eg UniswapV3, Trident) feature an abstract contract selfPermit.sol that allow users to submit their own ERC712 permit signature and call another contract function in a single transaction.
As per Uniswap documentation, "These functions are expected to be embedded in multicalls to allow EOAs to approve a contract and call a function that requires an approval in a single transaction.".
I cannot find any web3 reference implementation of how this work though. How to use Multicall to selfPermit + do action (eg swap) in a single transaction?
note: ERC712 signature process is clear, the question is focused on selfPermit w/ Multicall
Looking for a web3.js illustrative implementation.
I am trying to deploy this contract
https://github.com/juustesout/EtherRanger/blob/main/EtherRanger.sol
to BSC Testnet, and I get this error in return :
"revert
The transaction has been reverted to the initial state.
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information. "
this is an example of a failed transaction on BSC Testnet
https://testnet.bscscan.com/tx/0x5cbe0adcf6a522d89c974cadf70358dd9d79476988b4fb829fc1cc86c88aff0c
I tried forking multiple contracts from BSC, working tokens, but I always get the same error. I tried adding 'payable' to the constructor function, as other StackOverflow posts suggest, but no go. I have been reading any number of posts through Google Search, but I am getting nowhere.
The contracts compile flawless, no errors, no warnings, but they just won't deploy.
Could someone check if they can deploy this contract on BSC Testnet ? It may be a setting on my Remix or something, I am at a loss, I cannot tell where the error comes from.
I don’t know anything about this contract but I guess the hardcoded addresses for the IBEP20 and others are the problem. You need to deploy these contracts too and then pass their addresses to the interface addresses in your constructor.
But to connect to the BSC test network, you need to find the drop-down menu with a list of networks at the top of the MetaMask
https://domhtml.ru/en/sozdanie-sobstvennyh-tokenov-standarta-bep-20-v-seti-binance-smart-chain-bsc-pri-pomoshhi-remix-ide/
My contract needs LINK token in order to function.
I want to let users fund LINK tokens to the contract via a function on the contract, and then do some logic for the user based on their funding.
How can i make this possible within the contract?
I've tried to do calls like this.
LINK.balanceOf(walletaddress) does work, (It gets the link amount that's in the wallet).
However, this function below does not work for some reason.
It goes through and all, but with like empty data.
Metamask shows differently when i do the same call from their front-end button. (I assume it does the same as remix does)
https://testnet.bscscan.com/token/0x84b9B910527Ad5C03A9Ca831909E21e236EA7b06#readContract
Here is how i try to get my contracts approval.
function approveTransfer(uint256 amount) public returns(string memory) {
uint256 approveAmt = amount * 10**18;
LINK.approve(_msgSender(),approveAmt);
approvedAmount = approveAmt;
}
Okay, so i kept searching and searching and searching....
Until i found something amazing on their discord channel. (It's most likely written somewhere else too).
Harry | Chainlink
The LINK token is an ERC677 with transferAndCall functionality. So
depending on how your smart contract function call that generates the
random number is made, you can change it to be a 'transferAndCall'
function instead of one that just does the VRF request, with the idea
being that it will transfer enough LINK to fulfill the VRF request.
Then in your consuming contract that does the VRF request, you
implement the 'onTokenTransfer' function which simply calls your other
function that does the VRF request. The end result of this is that
when the user transfers LINK to the contract, it automatically does a
VRF request all in the 1 single transaction.
So instead of the user pressing a button which calls the function in
your consuming contract to do the VRF request, they press a button
which does a 'transferAndCall' function from the LINK token contract,
which in turn transfer LINK to your consuming contract and calls the
'onTokenTransfer' function in your consuming contract, which then
calls your function to do the VRF request, which will be successfully
fulfilled because it just received LINK for the request
See an implementation of this in my previous hackathon entry "Link Gas Station"
https://github.com/pappas999/Link-Gas-Station/blob/master/contracts/WeatherCheck.sol
https://github.com/pappas999/Link-Gas-Station/blob/master/src/relayer/relayer.js
So in short, this is possible because my contract have the
function onTokenTransfer(address from, uint256 amount, bytes memory data) public {
receivedTokenTransfer = true;
lastDepositer = from;
lastDepositerAmountInLink = amount / 10**18;
}
I can therefor instead of sending LINK to my own contract, i can send LINK to LINK's contract address, with the data payload transferAndCall , MycontractAddress, and the amount of LINK my contract should receive.
Upon this payment is sent, chainlink will send my contract the payment and call the function called onTokenTransfer (On my contract). :)))
Ho0pe this helps someone in the future.
Not possible from within your contract, unless the external contract explicitly allows it or contains a security flaw using deprecated tx.origin instead of msg.sender.
See the last paragraph and code snippet in this answer to see how it could be misused if it were possible.
When your contract executes LINK's function, msg.sender in the LINK contract is now your contract - not the user.
Both transfer() and approve() (in order to call transferFrom() later) functions rely on msg.sender, which you need to be the user. But it's not - it's your contract.
When your contract delegates a call to LINK's function, the state changes are stored in your contract - not in the LINK.
But you'd need to store the state changes in the LINK contract, so this is not an option either.
A client with a subscription "Large" (recurring payment).
I create a payment and offer object for doing that, and it works.
Now I want to update that subscription to "Small" (a different name and amount) but without updating the credit card.
The paymill flow for doing this is very documented very vague and it's uncertain what the process is.
Have you done anything like this with Paymill, I would be happy to hear about what calls you are doing.
I am using the .NET wrapper
To change an offer (plan) of a running subscription there are 3 types you can use.
Here is the documentation: https://developers.paymill.com/en-gb/subscription-v2-workflow/#update-sub-plan
In .NET they are exposed with the three methods, that are documented:
ChangeOfferKeepCaptureDateAndRefundAsync
ChangeOfferKeepCaptureDateNoRefundAsync
ChangeOfferChangeCaptureDateAndRefundAsync