The addresses from which the transaction takes place are not recorded in the Postgres database, though transaction hash is recorded. I have 2 functions :
- Update hash transaction API
2.Update account transaction API
Both are pretty similar but # 1 is working, #2 - not.
views.py
def update_ipfstrans(request, id_transaction):
index = IndexInfo.objects.all()[0]
languages = Language.objects.all()
item = IPFS.objects.get(id=id_transaction)
if request.method == 'POST':
form = UpdateIPFSTransaction(instance=item, data=request.POST or None)
if form.is_valid():
form.save()
return redirect('w3:update_ipfstrans', id_transaction=id_transaction)
form = UpdateTextTransactionForm(instance=item)
w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/27709d11030e4a8f8a3066732c9e6b90"))
gasprice = w3.toWei(item.gas_price, 'gwei')
s = item.text.encode('utf-8')
data = str(s.hex())
gas = item.gas
return render(request, 'w3/update_ipfstrans.html', context={'form': form, 'gas': gas,
'gasprice': gasprice,
'data': data, 'index': index, 'languages': languages,
'id_transaction': id_transaction})
class UpdateAccountIPFS(APIView):
def post(self, request, *args, **kwargs):
account = IPFS.objects.get(id=request.data.get('id'))
account.account = request.data.get('account')
account.save()
return Response(account.user_wallet_address, account)
models.py
class IPFS(models.Model):
objects = None
file = models.FileField(null=True, blank=True)
user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE)
result_hash = models.CharField(max_length=250, null=True, blank=True)
account = models.ForeignKey(AccountMetamask, null=True, blank=True, on_delete=models.CASCADE)
to_account = models.CharField(max_length=250, null=True, blank=True)
gas = models.IntegerField(default=0, null=True, blank=True)
gas_price = models.IntegerField(default=0, null=True, blank=True)
text = models.CharField(max_length=250, null=True, blank=True)
hash_ipfs = models.CharField(max_length=250, null=True, blank=True)
js
sendEthButton.addEventListener('click', () => {
ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: localStorage.getItem('myAccount'),
to: toaccount,
data: data,
gasPrice: "0x" + Number(gasprice).toString(16),
gas: Number(gas).toString(16),
},
],
})
.then((txHash) => {
console.log(txHash);
console.log('result');
let xhr = new XMLHttpRequest();
xhr.open("POST", "http://127.0.0.1:8000/w3/update/ipfs/hash/transaction/");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = () => console.log(xhr.responseText);
let data = JSON.stringify({
"id": id_transaction,
"result_hash": txHash,
});
xhr.send(data);
})
.catch((error) => console.error);
});
ethereumButton.addEventListener('click', () => {
getAccount();
let xhr = new XMLHttpRequest();
xhr.open("POST", "http://127.0.0.1:8000/w3/update/account/ipfs/");
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = () => console.log(xhr.responseText);
let data = JSON.stringify({
"id": id_transaction,
"account": localStorage.getItem('myAccount'),
});
xhr.send(data);
});
async function getAccount() {
accounts = await ethereum.request({ method: 'eth_requestAccounts' });
}
</script>
Has anyone else experienced this and knows how to help?