StarkNet
Get the injected object
Dapp can access the injected object in two ways, namely
js
const provider = window.starknet_bitkeep; //It is recommended to use this method
const provider = window.starknet;
const provider = window.starknet_bitkeep; //It is recommended to use this method
const provider = window.starknet;
Inject object properties and methods
name
- string:Wallet name, value is'Bitget Wallet'.icon
- string:wallet iconversion
- string:versionchainId
- string:Only supports mainnet, value is SN_MAINisConnected
- boolean::Is the current wallet connectedselectedAddress
- string:The wallet address currently selected by the useraccount
- Account:Access the account object, which is inherited from the Account of starknet.js. For specific properties and methods on the instance, please refer to starknet.jsprovider
- Provider:To access the provider object, use RpcProvider of starknet.js. For specific properties and methods on the instance, please refer to starknet.jsenable
- () => [string]:Used to connect to the wallet. After a successful call, the BitgetWallet wallet connection page will be evoked. The user can decide whether to connect to the current DApp. If the user agrees, a single-item array of the selected address will be returned.on
- (event, callback) => void:Add event listeneraccountsChanged
Event: This event is triggered when the user switches accounts and returns an array of new addresses; when the connection is disconnected, an empty array is returned.
off
- (event, callback) => void:Remove event listenerdisconnect
- Disconnect wallet
Connected to BitgetWallet
parameters
options
- object:OptionalstarknetVersion
-v4
|v5
:defaultv5
js
const [public_address] = await provider.enable();
//const [public_address] = await provider.enable({ starknetVersion: 'v4' });
const account = provider.account;
// account = {
// address: "0x04a6f...52f4d801c84",
// cairoVersion: "0",
// sdkVersion: "v5",
// ...
// }
provider.on("accountsChanged", (event) => {
// cb(event)...
});
const [public_address] = await provider.enable();
//const [public_address] = await provider.enable({ starknetVersion: 'v4' });
const account = provider.account;
// account = {
// address: "0x04a6f...52f4d801c84",
// cairoVersion: "0",
// sdkVersion: "v5",
// ...
// }
provider.on("accountsChanged", (event) => {
// cb(event)...
});
Add token
parameters
type
- string:wallet_watchAsset
params
- object:Token Infos
js
const res = await provider.request({
type: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address: '0x0A4E1BdFA75292A98C15870AeF24bd94BFFe0Bd4',
symbol: 'FOTA',
name: 'FOTA'
}
}
})
const res = await provider.request({
type: 'wallet_watchAsset',
params: {
type: 'ERC20',
options: {
address: '0x0A4E1BdFA75292A98C15870AeF24bd94BFFe0Bd4',
symbol: 'FOTA',
name: 'FOTA'
}
}
})
sign
parameters
typedData
- object:The object to be signed, refer to EIP-712
return value
signature
- string[]:The result of the signature, including two items
js
const typedData = {
domain: {
name: "Starknet demo app",
version: "1",
chainId: 'SN_MAIN',
},
types: {
StarkNetDomain: [
{ name: "name", type: "felt" },
{ name: "version", type: "felt" },
{ name: "chainId", type: "felt" },
],
Message: [{ name: "message", type: "felt" }],
},
message: {
message: 'sign message test',
},
primaryType: "Message",
};
const [r, s] = await provider.signMessage(typedData);
const typedData = {
domain: {
name: "Starknet demo app",
version: "1",
chainId: 'SN_MAIN',
},
types: {
StarkNetDomain: [
{ name: "name", type: "felt" },
{ name: "version", type: "felt" },
{ name: "chainId", type: "felt" },
],
Message: [{ name: "message", type: "felt" }],
},
message: {
message: 'sign message test',
},
primaryType: "Message",
};
const [r, s] = await provider.signMessage(typedData);
Contract call
Perform one or more calls. If there is only one call, transactions
is an object with the properties described below. If there are multiple calls, an array of objects.
parameters
transactions
- object:contractAddress
- string:The address of the contractentrypoint
- string:The entry point of the contractcalldata
- array:call datasignature
- array:signature
abi
- The contract’s abi, optional
return value
result
- objecttransaction_hash
- string:Transaction hash
js
// Take StarkGate: ETH Token as an example
const transactions = {
contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
entrypoint: 'transfer',
calldata: CallData.compile({
recipient: '0x05b98d6ccbb660c3ca3c699f8dc0d2c8f58c539feac4fe2d57def7d2fa7312d1',
amount: cairo.uint256(1000000000n)
})
}
// "transactions": {
// "contractAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
// "entrypoint": "transfer",
// "calldata": [
// "2589407029262891725723779965976037245771646239489440786683818579381309346513",
// "1000000000",
// "0"
// ]
// },
const res = await provider.execute(transactions);
// Take StarkGate: ETH Token as an example
const transactions = {
contractAddress: '0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7',
entrypoint: 'transfer',
calldata: CallData.compile({
recipient: '0x05b98d6ccbb660c3ca3c699f8dc0d2c8f58c539feac4fe2d57def7d2fa7312d1',
amount: cairo.uint256(1000000000n)
})
}
// "transactions": {
// "contractAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
// "entrypoint": "transfer",
// "calldata": [
// "2589407029262891725723779965976037245771646239489440786683818579381309346513",
// "1000000000",
// "0"
// ]
// },
const res = await provider.execute(transactions);
For other properties and methods on starknet.account and starknet.provider, see starknet.js