JAVASCRIPT
68
SEND TX WEB3 CONTRACT
Guest on 27th May 2022 04:21:47 PM
function pay() {
//WRONG NETWORK (SWAP TO FANTOM)
if(window.ethereum.chainId !== '0xFA'){
window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0xFA',
chainName: 'Fantom Opera',
nativeCurrency: {
name: 'Fantom',
symbol: 'FTM',
decimals: 18
},
rpcUrls: ['https://rpc.ftm.tools'],
blockExplorerUrls: ['https://ftmscan.com/']
}]
}).catch((error) => {
window.unityInstance.SendMessage('WalletInteraction', 'Status', 'failed To Connect');
window.unityInstance.SendMessage('WalletInteraction', 'Connection_Error');
});
return;
}
//IF FANTOM SEND TX
const web3 = new Web3(window.ethereum);
let tokenAddress = "0x04068DA6C83AFCFA0e13ba15A6696662335D5B75"; //USDC
let toAddress = "0x05167EaF383a185c5CFc4147773FD54B2bBa8936"; //MY WALLET ADDRESS3
let fromAddress = currentAccount;
// Use BigNumber
let decimals = 6;
let amount = 1;
let minABI = [
// transfer
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
{
"name": "",
"type": "bool"
}
],
"type": "function"
}
];
// Get ERC20 Token contract instance
let contract = new web3.eth.Contract(minABI, tokenAddress);
// calculate ERC20 token amount
let value = amount * Math.pow(10, decimals).toString();
// call transfer function
contract.methods.transfer(toAddress, value).send({ from: fromAddress })
.on('transactionHash', function (hash) {
console.log(hash);
});
}