I have implemented swift Library in my Objective-C code.
Here is the code which I have implemented to send ERC20 tokens.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSError *error;
// Setting your node address
NSURL *nodeURL = [[NSURL alloc] initWithString: @"https://ropsten.infura.io/v3/key"];
W3Web3 *node = [[W3Web3 alloc] initWithUrl: nodeURL];
[W3Web3 setDefault: node];
W3Web3 *web3 = [W3Web3 default];
web3 = [web3 initWithInfura:W3NetworkId.ropsten];
//NetworkId.ropsten.objc
NSString *password = @"";
NSData *privateKey = fromHex(@"private key");
W3EthereumKeystoreV3 *keystore = [[W3EthereumKeystoreV3 alloc] initWithPrivateKey:privateKey password:password aesMode:@"aes-128-cbc" error:&error];
W3KeystoreManager *keystoreManager = [[W3KeystoreManager alloc] initWithEthereumKeystores:@[keystore]];
// Getting your address
//W3Address *yourAddress = [[keystore addresses] firstObject];
W3Address *yourAddress = keystore.addresses.firstObject;
NSLog(@"%@",[yourAddress description]);
// Adding keystore to the default provider
[web3 setKeystoreManager: keystoreManager];
// ERC20 abi. Contains information about its functions
NSString *erc20Abi = @"erc20Abi";
W3Address *contractAddress = [[W3Address alloc] initWithString:@"Contract address" type:W3AddressTypeNormal];
W3Contract *contract = [[W3Contract alloc] init:erc20Abi at:contractAddress error:&error];
NSLog(@"%@ %@",[error description], [contract description]);
// Setting sending options
// You can also customize gasPrice and gasLimit here
W3Options *options = [W3Options default];
W3UInt *gasPrice = [[W3UInt alloc] init: @"3" units: W3UnitsGwei];
W3UInt *maxGasPrice = [[W3UInt alloc] init: @"200000" units: W3UnitsGwei];
//W3UInt *nonce = []
options.from = yourAddress;
options.gasPrice = gasPrice;
options.gasLimit = maxGasPrice;
NSString *recipient = @"from address";
NSString *amount = @"1";
// Creating transaction
W3EthereumTransaction *transaction = [contract method:@"transfer" parameters:@[recipient, amount] extraData:nil options:options error:&error];
[transaction UNSAFE_setChainID:W3NetworkId.ropsten];
NSLog(@"%@ %@",error.localizedDescription, transaction.description);
// 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];
//W3TransactionSendingResult *result1 = [[web3 eth] sendRawTransaction:transaction error:&error];
NSLog(@"%@ %@", [result description], [error description]);
}
I get below Transaction details when I put log for W3EthereumTransaction
019-01-07 16:04:09.875093+0530 Test[685:127019] (null) Transaction
Nonce: 0
Gas price: 3000000000
Gas limit: 200000000000000
To: to 0X0X
Value: 0
Data: a9059cbb000000000000000000000000data0000000000000000000000000000000000000000000000000000000000000001
v: 1
r: 0
s: 0
Intrinsic chainID: Optional(ropsten)
Infered chainID: Optional(mainnet)
sender: nil
hash: Optional(32 bytes)
So here are my questions :
1) How to change Transaction Nonce?
2) To address is wrong in to address it shows contract address
3) Why Value is 0?
4) chainID I need to use ropsten not mainnet right now it shows both.
5) Why Seder is nil?
Thanks in advance,