endless-web3-sdk
v0.1.48
Published
endless web3 js sdk
Downloads
452
Readme
SDK for Endless Web Wallet
Install
npm install 'endless-ts-sdk'
npm install 'endless-web3-sdk'
Usage
import {
Network,
EntryFunctionABI,
TypeTagAddress,
TypeTagU64,
AccountAddress,
} from 'endless-ts-sdk'
import {
EndlessJsSdk,
UserResponseStatus,
EndlessSignAndSubmitTransactionInput
} 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.toBs58String())
}
}
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.toBs58String())
}
}
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 TypeTagU64() ]
}
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(MethodName.CONNECT, res => {
console.log('connect', res)
})
jssdk.on(MethodName.DISCONNECT, res => {
console.log('disconnect', res)
})
jssdk.onAccountChange((accountAddress) => {
console.log('onAccountChange', accountAddress)
if(accountAddress.account) {
setAccountAddress(accountAddress.account.toBs58String())
}
})