Skip to content

Instantly share code, notes, and snippets.

@v57
Created January 2, 2019 11:57
Show Gist options
  • Save v57/a7a6244ce68a9d3ea74a2f9e93394d4d to your computer and use it in GitHub Desktop.
Save v57/a7a6244ce68a9d3ea74a2f9e93394d4d to your computer and use it in GitHub Desktop.
NSError *error;
// Setting your node address
NSURL *nodeURL = [[NSURL alloc] initWithString: @"your node address"];
W3Web3 *node = [[W3Web3 alloc] initWithUrl: nodeURL];
[W3Web3 setDefault: node];
W3Web3 *web3 = [W3Web3 default];
// Setting your mnemonics
W3Mnemonics *mnemonics = [[W3Mnemonics alloc] init: @"fruit wave dwarf banana earth journey tattoo true farm silk olive fence fruit" language:W3BIP39LanguageEnglish error: &error];
// Creating keystore that contains private key. Private key will be encrypted using password.
// So for every transaction you need to enter account's password
NSString *password = @"Some password that uses to encrypt your keystore";
W3BIP32Keystore *keystore = [[W3BIP32Keystore alloc] initWithMnemonics:mnemonics password: password prefixPath:@"m/44'/60'/0'/0" error: &error];
W3KeystoreManager *keystoreManager = [[W3KeystoreManager alloc] initWithBip32Keystores:@[keystore]];
// Getting your address
W3Address *yourAddress = [[keystore addresses] firstObject];
// Adding keystore to the default provider
[web3 setKeystoreManager: keystoreManager];
// ERC20 abi. Contains information about its functions
NSString *erc20Abi = @"[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"version\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"},{\"name\":\"_extraData\",\"type\":\"bytes\"}],\"name\":\"approveAndCall\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"remaining\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[{\"name\":\"_initialAmount\",\"type\":\"uint256\"},{\"name\":\"_tokenName\",\"type\":\"string\"},{\"name\":\"_decimalUnits\",\"type\":\"uint8\"},{\"name\":\"_tokenSymbol\",\"type\":\"string\"}],\"type\":\"constructor\"},{\"payable\":false,\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"_owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"_spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},]";
// Creating contract
W3Address *contractAddress = [[W3Address alloc] initWithString:@"0x45245bc59219eeaaf6cd3f382e078a461ff9de7b" type:W3AddressTypeNormal];
W3Contract *contract = [[W3Contract alloc] init:erc20Abi at:contractAddress error:&error];
W3UInt *amount = [[W3UInt alloc] init: @"100" units: W3UnitsEth];
// Setting sending options
// You can also customize gasPrice and gasLimit here
W3Options *options = [W3Options default];
options.from = yourAddress;
W3Address *recipient = [[W3Address alloc] initWithString:@"0x6a6a0b4aaa60E97386F94c5414522159b45DEdE8" type:W3AddressTypeNormal];
// Creating transaction
W3EthereumTransaction *transaction = [contract method:@"transfer" parameters:@[recipient, amount] extraData:nil options:options error:&error];
// Signing transaction
[[web3 wallet] signWithTransaction:transaction account:yourAddress password:password error:&error];
// Sending transaction
W3TransactionSendingResult *result = [[web3 eth] sendTransaction:transaction options:options password:@"password" error:&error];
NSLog(@"%lu", (unsigned long)result.hash);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment