Magic Extension Icon Blockchain
Installation
npm i magic-sdk @magic-sdk/extension-icon
Setup
Setup IconExtension with magic-sdk
import { Magic } from 'magic-sdk';
import { IconExtension } from '@magic-sdk/extension-icon';
const magic = new Magic('YOUR_API_KEY', {
extensions: [
new IconExtension({
rpcUrl: 'icon rpc url'
})
]
});
const magic = new Magic('YOUR_API_KEY', {
extensions: {
icon: new IconExtension({
rpcUrl: 'icon rpc url'
})
}
});
Magic SDK
See the developer documentation to learn how you can master the Magic SDK in a matter of minutes.
Usage
Get account
Using getAccount function to get Icon public address for current user.
const publicAddress = await magic.icon.getAccount();
console.log('icon public address', publicAddress);
Send Transaction
Build the transaction object using builder class.
const txObj = new IconBuilder.IcxTransactionBuilder()
.from("hxe4837d3b902dcaf9abe529f5584489c28d8b4621")
.to("hxe4837d3b902dcaf9abe529f5584489c28d8b4621")
.value(IconAmount.of(0.5, IconAmount.Unit.ICX).toLoop())
.stepLimit(IconConverter.toBigNumber(100000))
.nid(IconConverter.toBigNumber(3))
.nonce(IconConverter.toBigNumber(1))
.version(IconConverter.toBigNumber(3))
.timestamp((new Date()).getTime() * 1000)
.build();
const txObj = new IconBuilder.MessageTransactionBuilder()
.from('hxe4837d3b902dcaf9abe529f5584489c28d8b4621')
.to('hxe4837d3b902dcaf9abe529f5584489c28d8b4621')
.stepLimit(IconConverter.toBigNumber(1000000))
.nid(IconConverter.toBigNumber(3))
.nonce(IconConverter.toBigNumber(1))
.version(IconConverter.toBigNumber(3))
.timestamp((new Date()).getTime() * 1000)
.data(IconConverter.fromUtf8('Hello'))
.build();
const txObj = new DeployTransactionBuilder()
.from('hxe4837d3b902dcaf9abe529f5584489c28d8b4621')
.to('cx0000000000000000000000000000000000000000')
.stepLimit(IconConverter.toBigNumber(2100000000))
.nid(IconConverter.toBigNumber(3))
.nonce(IconConverter.toBigNumber(1))
.version(IconConverter.toBigNumber(3))
.timestamp((new Date()).getTime() * 1000)
.contentType('application/zip')
.content(`0x${content}`)
.params({
initialSupply: IconConverter.toHex('100000000000'),
decimals: IconConverter.toHex(18),
name: 'StandardToken',
symbol: 'ST',
})
.build();
const txObj = new IconBuilder.CallTransactionBuilder()
.from('hxe4837d3b902dcaf9abe529f5584489c28d8b4621')
.to('cxd1602bde1d4b2b4facc8673c661c5e59e6ac20b4')
.stepLimit(IconConverter.toBigNumber('2000000'))
.nid(IconConverter.toBigNumber('3'))
.nonce(IconConverter.toBigNumber('1'))
.version(IconConverter.toBigNumber('3'))
.timestamp((new Date()).getTime() * 1000)
.method('hello')
.params({})
By passing txObj instance to magic.icon.sendTransaction()
method, it will automatically sign the transaction with current user and
generate transaction object including signature, then send to ICON node.
const txhash = await magic.icon.sendTransaction(txObj);
console.log('transaction result', txhash);
window.open(`https://bicon.tracker.solidwallet.io/transaction/${txhash}`)