增加一个erc20转账的方法
This commit is contained in:
parent
668d41c804
commit
43ace4dae1
@ -13,7 +13,12 @@ var global =
|
|||||||
(typeof global !== 'undefined' && global) ||
|
(typeof global !== 'undefined' && global) ||
|
||||||
{}
|
{}
|
||||||
declare global {
|
declare global {
|
||||||
interface Window { jc: {wallet: JCWallet;} }
|
interface Window {
|
||||||
|
jc: {
|
||||||
|
wallet: JCWallet;
|
||||||
|
},
|
||||||
|
ethSigUtil: any
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import Web3 = require('../lib/web3.min');
|
import Web3 = require("../lib/web3.min");
|
||||||
import { abiERC20 } from '../abis/abiERC20';
|
import { abiERC20 } from "../abis/abiERC20";
|
||||||
|
|
||||||
export class ERC20Standard {
|
export class ERC20Standard {
|
||||||
private web3: Web3;
|
private web3: Web3;
|
||||||
@ -15,17 +15,25 @@ export class ERC20Standard {
|
|||||||
* @param selectedAddress - Current account public address.
|
* @param selectedAddress - Current account public address.
|
||||||
* @returns Promise resolving to BN object containing balance for current account on specific asset contract.
|
* @returns Promise resolving to BN object containing balance for current account on specific asset contract.
|
||||||
*/
|
*/
|
||||||
async getBalanceOf(address: string, selectedAddress: string): Promise<Web3.utils.BN> {
|
async getBalanceOf(
|
||||||
|
address: string,
|
||||||
|
selectedAddress: string
|
||||||
|
): Promise<Web3.utils.BN> {
|
||||||
const contract = new this.web3.eth.Contract(abiERC20, address);
|
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||||
return new Promise<Web3.utils.BN>((resolve, reject) => {
|
return new Promise<Web3.utils.BN>((resolve, reject) => {
|
||||||
contract.methods.balanceOf(selectedAddress).call({from: selectedAddress}, (error: Error, result: Web3.utils.BN) => {
|
contract.methods
|
||||||
|
.balanceOf(selectedAddress)
|
||||||
|
.call(
|
||||||
|
{ from: selectedAddress },
|
||||||
|
(error: Error, result: Web3.utils.BN) => {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve(result);
|
resolve(result);
|
||||||
});
|
}
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,7 +46,9 @@ export class ERC20Standard {
|
|||||||
async getTokenDecimals(address: string): Promise<string> {
|
async getTokenDecimals(address: string): Promise<string> {
|
||||||
const contract = new this.web3.eth.Contract(abiERC20, address);
|
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods.decimals().call((error: Error, result: Web3.utils.BN | string) => {
|
contract.methods
|
||||||
|
.decimals()
|
||||||
|
.call((error: Error, result: Web3.utils.BN | string) => {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
@ -58,7 +68,9 @@ export class ERC20Standard {
|
|||||||
async getTokenSymbol(address: string): Promise<string> {
|
async getTokenSymbol(address: string): Promise<string> {
|
||||||
const contract = new this.web3.eth.Contract(abiERC20, address);
|
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||||
return new Promise<string>((resolve, reject) => {
|
return new Promise<string>((resolve, reject) => {
|
||||||
contract.methods.symbol().call((error: Error, result: Web3.utils.BN | string) => {
|
contract.methods
|
||||||
|
.symbol()
|
||||||
|
.call((error: Error, result: Web3.utils.BN | string) => {
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (error) {
|
if (error) {
|
||||||
reject(error);
|
reject(error);
|
||||||
@ -113,7 +125,7 @@ export class ERC20Standard {
|
|||||||
*/
|
*/
|
||||||
async getDetails(
|
async getDetails(
|
||||||
address: string,
|
address: string,
|
||||||
userAddress?: string,
|
userAddress?: string
|
||||||
): Promise<{
|
): Promise<{
|
||||||
standard: string;
|
standard: string;
|
||||||
symbol: string | undefined;
|
symbol: string | undefined;
|
||||||
@ -132,7 +144,28 @@ export class ERC20Standard {
|
|||||||
decimals,
|
decimals,
|
||||||
symbol,
|
symbol,
|
||||||
balance,
|
balance,
|
||||||
standard: 'ERC20',
|
standard: "ERC20",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async transfer({
|
||||||
|
address,
|
||||||
|
from,
|
||||||
|
to,
|
||||||
|
amount,
|
||||||
|
gas,
|
||||||
|
}: {
|
||||||
|
address: string;
|
||||||
|
from: string;
|
||||||
|
to: string;
|
||||||
|
amount: number | string;
|
||||||
|
gas?: number;
|
||||||
|
}) {
|
||||||
|
const contract = new this.web3.eth.Contract(abiERC20, address);
|
||||||
|
amount = Web3.utils.toBN(Web3.utils.toWei(amount + ""));
|
||||||
|
return contract.methods.transfer(to, amount).send({
|
||||||
|
from,
|
||||||
|
gas: gas || 1000000,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
File diff suppressed because it is too large
Load Diff
@ -94,20 +94,29 @@ export default class WalletController extends cc.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async testERC20() {
|
async testERC20() {
|
||||||
let symbol = await this.wallet.erc20Standard.getTokenSymbol('0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc')
|
// let address = '0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc'
|
||||||
|
let address = '0xdb6D4bB22E2C12686Efff25a79EC78f9f078fe7D'
|
||||||
|
let symbol = await this.wallet.erc20Standard.getTokenSymbol(address)
|
||||||
console.log(symbol)
|
console.log(symbol)
|
||||||
let decimals = await this.wallet.erc20Standard.getTokenDecimals('0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc')
|
let decimals = await this.wallet.erc20Standard.getTokenDecimals(address)
|
||||||
console.log(decimals)
|
console.log(decimals)
|
||||||
let result3 = await this.wallet.erc20Standard
|
let result3 = await this.wallet.erc20Standard
|
||||||
.getBalanceOf('0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc',
|
.getBalanceOf(address,
|
||||||
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e')
|
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e')
|
||||||
console.log(`balance: ${renderFromTokenMinimalUnit(result3, decimals, 4)}`)
|
console.log(`balance: ${renderFromTokenMinimalUnit(result3, decimals, 4)}`)
|
||||||
console.log(`balance: ${renderFromTokenMinimalUnit('10000000000000', decimals, 6)}`)
|
|
||||||
console.log(`renderFromWei: ${renderFromWei('100000000000000')}`)
|
|
||||||
let resultAll = await this.wallet.erc20Standard.getDetails(
|
let resultAll = await this.wallet.erc20Standard.getDetails(
|
||||||
'0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc',
|
address,
|
||||||
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e'
|
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e'
|
||||||
)
|
)
|
||||||
console.log(resultAll)
|
console.log(resultAll)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async testSendMoney() {
|
||||||
|
let address = '0xdb6D4bB22E2C12686Efff25a79EC78f9f078fe7D'
|
||||||
|
let amount = 1
|
||||||
|
let from = '0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e'
|
||||||
|
let to = '0x50A8e60041A206AcaA5F844a1104896224be6F39'
|
||||||
|
let result = await this.wallet.erc20Standard.transfer({address, from, to, amount})
|
||||||
|
console.log(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user