Skip to content

Instantly share code, notes, and snippets.

@corck
Created June 11, 2019 09:34
Show Gist options
  • Save corck/d7f5be88cbf62db30dca24ffd8cb7a43 to your computer and use it in GitHub Desktop.
Save corck/d7f5be88cbf62db30dca24ffd8cb7a43 to your computer and use it in GitHub Desktop.
Neo dAPI example to invoke a method with verification
//= require babel/polyfill
const network = 'PrivateNet'
const scriptHash = '58e7c597e246a11177580fdda62e0881b8e7e3c0'
class Account {
constructor(address, name) {
this.address = address
this.name = name
}
}
neoAccount = new Account()
window.o3dapi.initPlugins([o3dapiNeo]);
window.o3dapi.NEO.getNetworks()
.then(() => {
networks => console.log(networks)
});
function connectAccount() {
o3dapi.NEO.getAccount()
.then((account) => {
const {
address,
label,
} = account;
neoAccount.address = address
neoAccount.name = label
console.log('Account address: ' + address);
console.log('Account label: ' + label);
$('#account').text(neoAccount.name);
$('#wallet-address').text(neoAccount.address);
$('#connect-account').toggleClass('d-none')
$('#account-info').toggleClass('d-none')
getBalance(neoAccount)
})
.catch(({ type, description, data }) => {
switch (type) {
case o3dapi.NEO.NO_PROVIDER:
console.log('No provider available.');
break;
case o3dapi.NEO.CONNECTION_DENIED:
console.log('The user rejected the request to connect with your dApp');
break;
}
});
}
function getBalance(account) {
o3dapi.NEO.getBalance({
params: {
address: account.address,
assets: ['NEO', 'GAS']
},
network: network,
})
.then((results) => {
Object.keys(results).forEach(address => {
const balances = results[address];
balances.forEach(balance => {
const { assetID, symbol, amount } = balance
console.log('Address: ' + address);
console.log('Asset ID: ' + assetID);
console.log('Asset symbol: ' + symbol);
console.log('Amount: ' + amount);
});
});
})
// .catch(({type, description, data}) => {
// switch(type) {
// case NO_PROVIDER:
// console.log('No provider available.');
// break;
// case CONNECTION_DENIED:
// console.log('The user rejected the request to connect with your dApp');
// break;
// }
// });
}
function tokenSale() {
let gas = 1
let neo = 1
o3dapi.NEO.invoke({
scriptHash: scriptHash,
operation: 'add',
arguments: [
{
type: 'string',
value: 'AXLk85FpcsSNCTeMAB6ovGm8dsc5G8gcDT'
},
{ type: 'number',
value: 1
}
],
attachedAssets: {
GAS: gas,
},
fee: '0.1',
network: network,
broadcastOverride: false,
triggerContractVerification: true
})
.catch(({type, description, data}) => {
console.log(type)
console.log(description)
console.log(data)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment