1. Connect to Endless Wallet
Call the connect method in SKD to obtain the current address of the wallet.
Copy import { UserResponseStatus } from '@endlesslab/endless-web3-sdk';
const connectRes = await jssdk.connect();
if (connectRes.status === UserResponseStatus.APPROVED) {
// connected, you can get the wallet address
// The wallet address is Base58 string
console.log('wallet address: ', connectRes.args.account);
}else{
// connect failed
}
2. GetAccount method
The getAccount method needs to be called after the wallet initialization is completed.
Call the getAccount method to obtain the current user wallet address of the endless wallet.
Copy import { EndLessSDKEvent } from '@endlesslab/endless-web3-sdk';
jssdk.on(EndLessSDKEvent.INIT, async () => {
const getAccountRes = await jssdk.getAccount();
if (getAccountRes.status === UserResponseStatus.APPROVED) {
// The wallet address is Base58 string
console.log('wallet address: ', connectRes.args.account);
}else{
// The user is not connected to the dapp or the current wallet has not been created.
}
});
3. Disconnect method
Call the disconnect method in SKD to disconnect the dapp from the wallet.
Copy const disconnectRes = await jssdk.disconnect();
if (disconnectRes.status === UserResponseStatus.APPROVED) {
// disconnected
}else{
// disconnect failed
}
4. Signature message method
Call the signMessage method in SKD to sign the message and return the signed result.
Copy const signMessageRes = await jssdk.signMessage({ message: signMessage });
if (signMessageRes.status === UserResponseStatus.APPROVED) {
// signed successfully
console.log(signMessageRes.args.signature.toString());
}else{
// sign failed
}
5. Sending a Transaction method
Call the signMessage method in SKD, Sign the message and send it to the Endless blockchain. After success, return the transaction hash.
The following code example demonstrates how to use the API to sign transactions and send them to the endless blockchain.
Copy import { EndlessSignAndSubmitTransactionInput } from '@endlesslab/endless-web3-sdk';
const transferData: EndlessSignAndSubmitTransactionInput = {
payload: {
function: '0x1::endless_account::transfer',
functionArguments: [
AccountAddress.fromBs58String(toAccountAddress),
BigInt(transferAmount),
],
},
};
const transactionRes = await jssdk.signAndSubmitTransaction(transferData);
if (transactionRes.status === UserResponseStatus.APPROVED) {
console.log('transactionRes =====>', transactionRes);
}
6. Signature transaction method
The following code example demonstrates how to use the API to sign transactions.
Copy import { Endless, Network, EndlessConfig } from '@endlesslab/endless-ts-sdk';
import {
EndlessSignAndSubmitTransactionInput,
EndlessWalletTransactionType
} from '@endlesslab/endless-web3-sdk';
const config = new EndlessConfig({
network: Network.TESTNET
})
const endless = new Endless(config)
const transferData: EndlessSignAndSubmitTransactionInput = {
payload: {
function: '0x1::endless_account::transfer',
functionArguments: [
AccountAddress.fromBs58String(toAccountAddress),
BigInt(transferAmount),
],
},
};
const txn = await endless.transaction.build.simple({
sender: accountAddress,
data: transferData.payload
});
// EndlessWalletTransactionType.SIMPLE | EndlessWalletTransactionType.MULTI_AGENT
const transactionRes = await jssdk.signTransaction(txn, EndlessWalletTransactionType.SIMPLE);
if (transactionRes.status === UserResponseStatus.APPROVED) {
console.log('transactionRes =====>', transactionRes);
}
7. Open view method
The following code example demonstrates how to use the API to open the Endless Wallet window.
Copy import { EndLessSDKEvent } from '@endlesslab/endless-web3-sdk';
jssdk.on(EndLessSDKEvent.INIT, async () => {
await jssdk.open();
});
8. Add event listening method
This method is used to add an endless wallet event listening method.
Copy import { EndLessSDKEvent } from '@endlesslab/endless-web3-sdk';
jssdk.on(EndLessSDKEvent.INIT, async () => {
console.log('init success');
});
9. Uninstall event listening method
This method is used to uninstall the Endless Wallet event listening method.
Copy import { EndLessSDKEvent } from '@endlesslab/endless-web3-sdk';
jssdk.off(EndLessSDKEvent.INIT, async () => {
console.log('init success');
});
10. Transaction of sending EDS coins with script payload
Copy import { Endless, Network, EndlessConfig } from '@endlesslab/endless-ts-sdk';
import {
EndlessSignAndSubmitTransactionInput,
EndlessWalletTransactionType
} from '@endlesslab/endless-web3-sdk';
const config = new EndlessConfig({
network: Network.TESTNET
})
const endless = new Endless(config)
// byte code of the script of calling `endless_account::transfer_coins(...)`
const transferData: EndlessSignAndSubmitTransactionInput = {
payload: {
bytecode: 'a11ceb0b0600000007010004020406030a0604100205120e07202d084d2000000001010207010001000300010108000204060c05040b00010900000109000f656e646c6573735f6163636f756e74066f626a656374064f626a6563740e7472616e736665725f636f696e73000000000000000000000000000000000000000000000000000000000000000101080001060b000b010b020b03380002',
functionArguments: [
AccountAddress.fromBs58String(toAccountAddress),
new U128(parseFloat(transferAmount) * 100000000),
AccountAddress.fromBs58String('ENDLESSsssssssssssssssssssssssssssssssssssss'),
],
typeArguments: ['0x1::fungible_asset::Metadata'],
}
};
const txn = await endless.transaction.build.simple({
sender: fromAccountAddress,
data: transferData.payload
});
// EndlessWalletTransactionType.SIMPLE | EndlessWalletTransactionType.MULTI_AGENT
const transactionRes = await jssdk.signTransaction(txn, EndlessWalletTransactionType.SIMPLE);
if (transactionRes.status === UserResponseStatus.APPROVED) {
console.log('transactionRes =====>', transactionRes);
}