1. 首先引入web3.js最新版本 ,再引入 jquery 遇到合約有ABI文件.個(gè)人習(xí)慣單獨(dú)保存ABI文件.通過jquery遠(yuǎn)程讀取.
實(shí)例化合約后直接在鏈上與合約交互
$.getJSON('ABI文件.json',function(result){
let MyContract = new web3.eth.Contract(result,'TOKEN合約地址');
let data = MyContract.methods.transfer('收幣人地址',web3.utils.toWei('轉(zhuǎn)賬金額','ether')).encodeABI();
const transactionParameters = {
// nonce: '0x00', // ignored by MetaMask
gasPrice: web3.utils.toHex(web3.utils.toWei('5','gwei')),
// gas: '0x2710', // customizable by user during MetaMask confirmation.
to: 'TOKEN合約地址', // Required except during contract publications.
from: ethereum.selectedAddress, // must match user's active address.
// value: web3.utils.toHex('12'), // Only required to send ether to the recipient from the initiating external account.
data: data, // Optional, but used for defining smart contract creation and interaction.
chainId: web3.utils.toHex('56'), // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
};
ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
})
.then(function(result){
//result是生成的交易hash值,用于在鏈上查看交易狀態(tài)信息的索引
successMessage(JSON.stringify(result))
})
.catch(function(reason){
if(reason.code == 4001){
errorMessage(reason.message);
}else{
console.log(reason);
}
});
});
通過按鈕什么的觸發(fā)后就可以通過代碼轉(zhuǎn)賬了.
?2. 無ABI與合約交互,因?yàn)橛械暮霞s閉源了,所以我們直接手工構(gòu)造data參數(shù)內(nèi)容,免去了實(shí)例化合約的步驟
let data = web3.eth.abi.encodeFunctionCall({
name: 'rent',
type: 'function',
inputs: [{
type: 'uint256',
name: 'orderId'
},{
type: 'address',
name: 'nftAddress'
},{
type: 'address',
name: 'tokenOwner'
},{
type: 'uint256[]',
name: 'rentInfo'
},{
type: 'address[]',
name: 'inviters'
},{
type: 'address[]',
name: 'agents'
},{
type: 'uint256',
name: 'deadline'
},{
type: 'bytes',
name: 'signature'
}]
}, [order_id.toString(), nft_address,token_owner,[token_id,expire_at,web3.utils.toBN(price)],[],[],deadline,'0x'+signature]);
const transactionParameters = {
// nonce: '0x00', // ignored by MetaMask
gasPrice: web3.utils.toHex(web3.utils.toWei('5','gwei')),
// gas: '0x2710', // customizable by user during MetaMask confirmation.
to: '合約地址', // Required except during contract publications.
from: ethereum.selectedAddress, // must match user's active address.
// value: '0x00', // Only required to send ether to the recipient from the initiating external account.
data: data, // Optional, but used for defining smart contract creation and interaction.
// chainId: '有需要就用web.utils.toHex("NETWORK十進(jìn)制ID")', // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
};
ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
})
.then(function(result){
//result是生成的交易hash值,用于在鏈上查看交易狀態(tài)信息的索引
successMessage(JSON.stringify(result))
})
.catch(function(reason){
console.log('eth_sendTransaction info');
console.log(reason);
});
3. ERC20標(biāo)準(zhǔn)代幣合約一般默認(rèn)實(shí)現(xiàn)了transfer / mint /?transfer 等等標(biāo)準(zhǔn)方法,參數(shù)也一樣,直接測(cè)試調(diào)用就行了.文章來源:http://www.zghlxwxcb.cn/news/detail-598052.html
4. 區(qū)塊鏈原生代幣轉(zhuǎn)賬更加簡單文章來源地址http://www.zghlxwxcb.cn/news/detail-598052.html
const transactionParameters = {
// nonce: '0x00', // ignored by MetaMask
gasPrice: web3.utils.toHex(web3.utils.toWei('5','gwei')),
// gas: '0x2710', // customizable by user during MetaMask confirmation.
to: '收款人錢包地址', // Required except during contract publications.
from: ethereum.selectedAddress, // must match user's active address.
value: web3.utils.toHex(web3.utils.toWei('轉(zhuǎn)賬數(shù)量','ether')), // Only required to send ether to the recipient from the initiating external account.
chainId: web3.utils.toHex('56'), // Used to prevent transaction reuse across blockchains. Auto-filled by MetaMask.
};
ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
})
.then(function(result){
successMessage(JSON.stringify(result))
})
.catch(function(reason){
if(reason.code == 4001){
errorMessage(reason.message);
}else{
console.log(reason);
}
});
到了這里,關(guān)于通過metamask與合約交互(發(fā)送ERC20 TOKEN/原生TOKEN/方法調(diào)用) javascrpt實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!