endless-web3-sdk
v0.1.76
Published
endless web3 js sdk
Downloads
854
Readme
SDK for Endless Web Wallet
Install
npm install 'endless-ts-sdk'
npm install 'endless-web3-sdk'
Usage
import {
Network,
EntryFunctionABI,
TypeTagAddress,
TypeTagU128,
AccountAddress,
} from 'endless-ts-sdk'
import {
EndlessJsSdk,
UserResponseStatus,
EndlessSignAndSubmitTransactionInput,
EndLessSDKEvent
} from 'endless-web3-sdk'
const jssdk = new EndlessJsSdk({
network: Network.TESTNET,
})
const connectWalletHandler = async () => {
const connectRes = await jssdk.connect()
if(connectRes.status === UserResponseStatus.APPROVED) {
setAccountAddress(connectRes.args.account)
}
}
const disconnectWalletHandler = () => {
jssdk.disconnect().then(() => {
console.log('disconnect success')
})
}
const getAccountHandler = async () => {
const getAccountRes = await jssdk.getAccount()
if(getAccountRes.status === UserResponseStatus.APPROVED) {
console.log('getAccountRes =====>',getAccountRes.args.account)
}
}
const signMessageHandler = async () => {
if(!signMessage || !accountAddress){
return
}
const signMessageRes = await jssdk.signMessage({ message: signMessage })
if(signMessageRes.status === UserResponseStatus.APPROVED) {
setSignature(signMessageRes.args.signature.toString())
}
}
const signAndSubmitTrancactionHandler = async () => {
if(!accountAddress) {
alert('please connect wallet')
return
}
if(!toAccountAddress || !transferAmount) {
alert('please input toAccountAddress and transferAmount')
return
}
const transferEDSAbi: EntryFunctionABI = {
typeParameters: [],
parameters: [ new TypeTagAddress(), new TypeTagU128() ]
}
const transferData: EndlessSignAndSubmitTransactionInput = {
payload: {
function: '0x1::endless_account::transfer',
functionArguments: [
AccountAddress.fromBs58String(toAccountAddress),
BigInt(parseInt(transferAmount)*100000000),
],
abi: transferEDSAbi,
}
}
const transactionRes = await jssdk.signAndSubmitTransaction(transferData)
console.log('transactionRes =====>',transactionRes)
}
jssdk.on(EndLessSDKEvent.CONNECT, res => {
console.log('connect', res)
})
jssdk.on(EndLessSDKEvent.DISCONNECT, res => {
console.log('disconnect', res)
})
jssdk.on(EndLessSDKEvent.ACCOUNT_CHANGE, accountAddress => {
console.log('accountChange', accountAddress)
if(accountAddress.account) {
setAccountAddress(accountAddress.account.toBs58String())
}
})
jssdk.on(EndLessSDKEvent.NETWORK_CHANGE, networkInfo => {
console.log('networkInfo', networkInfo)
})
const openHandler = () => {}
jssdk.on(EndLessSDKEvent.OPEN, openHandler)
jssdk.off(EndLessSDKEvent.OPEN, openHandler)
const closeHandler = () => {}
jssdk.on(EndLessSDKEvent.CLOSE, closeHandler)
jssdk.off(EndLessSDKEvent.CLOSE, closeHandler)
// remove all event listener
jssdk.off(EndLessSDKEvent.CLOSE)
jssdk.changeNetwork({
network: Network.MAINNET
})