1.2.3 • Published 2 years ago
@emit-technology/emit-account-node-sdk v1.2.3
emit-account-node-sdk
emit-account-node-sdkis a web3 provider for dapps ,that's develop base on emit-core .
1. Install SDK and Web3
npm i @emit-technology/emit-account-node-sdk
npm i web3@npm:@emit-technology/web3
npm i web3-core-helpers@npm:@emit-technology/web3-core-helpers
npm i web3-utils@npm:@emit-technology/web3-utils
2. Import and initialize a web3 instance
import {AccountModel,ChainType} from '@emit-technology/emit-lib';
import EmitBox from '@emit-technology/emit-account-node-sdk';
import Web3 from 'web3';
const dapp = {
name: "DEMO",
url: "http://localhost:3000",
category: "web3",
contractAddress: ""
}
const network_emit = {nodeUrl: "https://node-emit-dev.bangs.network", chainId: "667", chainType: ChainType.EMIT}
const network_bsc = {nodeUrl: "https://node-bsc.bangs.network", chainId: "1", chainType: ChainType.BSC}
const network_eth = {nodeUrl: "https://node-bsc.bangs.network", chainId: "1", chainType: ChainType.ETH}
const emitBox = new EmitBox(dapp, network_emit);
const ethProvider = emitBox.newProvider({
dapp: dapp,
network: network_eth,
version: "1.0"
});
const bscProvider = emitBox.newProvider({
dapp: dapp,
network: network_bsc,
version: "1.0"
})
//if you single use ethereum network
const web3 = new Web3(ethProvider);
//if you are using multi-chain , and use web3[ChainType.ETH] instead of web3, eg: web3[ChainType.ETH].eth.getAccounts
/**
const web3 = {
[ChainType.ETH]: new Web3(ethProvider),
[ChainType.BSC]: new Web3(bscProvider)
}
*/
3.Verify everything works by calling a web3 method such as getAccounts:
web3.eth.getAccounts((err,accounts)=>{
console.log(err,accounts[0])
})
or
web3.eth.getAccounts().then(accounts=>{
console.log("accounts:",accounts[0]);
}).catch(e=>{
console.error("error",e)
})
or
emitBox.requestAccount().then((account:AccountModel)=>{
console.log("account:",account);
}).catch(e=>{
console.error("error",e)
})
4 sign Method
web3.eth.personal.sign("TEST DATA",account,"").then(value => {
console.log(value);
})
web3.eth.personal.ecRecover("TEST DATA",signature).then(value => {
console.log(value);
})
or
//Batch sign
emitBox.batchSignMsg([{
chain: ChainType.EMIT,
msg: {data:"0x64c2d1dab59dee2c7741acee3826dad50cdb7f6adb28a159b1bc6aa5374a2b24"}
}]).then((data:any)=>console.log(data))
5 Send transaction
web3 chain
getNonce = async (): Promise<any> => {
const from = ""//account address
return new Promise((resolve, reject) => {
web3.eth.getTransactionCount(from, "pending", (error, nonce) => {
if (error) {
reject(error)
} else {
resolve(nonce)
}
})
})
}
getGasPrice = async (): Promise<string> => {
const gasPrice = await web3.eth.getGasPrice()
return gasPrice;
}
estimateGas = async (txParams: any): Promise<any> => {
return new Promise((resolve, reject) => {
web3.eth.estimateGas(txParams, (err, ret) => {
if (err) {
reject(err)
} else {
resolve(ret);
}
})
})
}
async function send {
const txConfig = {
from: from,
to: receive,
value: data ? "0x0" : utils.toHex(utils.toValue(amount, decimal)),
nonce: await this.getNonce(),
data: data,
// common: config.chains[chain].common
/**
for dev env
{
baseChain: "mainnet",
customer: {
name: "mainnet",
networkId: 15,
chainId: 1337,
},
hardfork: "petersburg"
},
**/
}
txConfig["gas"] = await this.estimateGas(txConfig);
const result = await web3.eth.sendTransaction(txConfig)
}
emit core
async function send {
const prepareBlock = await emitBoxSdk.emitBox.emitDataNode.genPrepareBlock(
from,
data,
{
settles: [],
outs: [
{
target: receive,
factor: {
category: utils.token2Category(token),
value: utils.toValueHex(amount),
},
},
],
},
undefined
);
await emitBoxSdk.emitBox.emitDataNode.prepareBlock(prepareBlock);
}
1.2.0
2 years ago
1.2.3
2 years ago
1.2.2
2 years ago
1.2.1
2 years ago
1.1.1
2 years ago
1.1.0
2 years ago
1.1.9
2 years ago
1.1.8
2 years ago
1.1.7
2 years ago
1.1.6
2 years ago
1.1.5
2 years ago
1.1.4
2 years ago
1.1.3
2 years ago
1.1.2
2 years ago
1.0.9
2 years ago
1.0.8
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago