Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix contract bugs #682

4 changes: 2 additions & 2 deletions Sources/Core/EthereumABI/ABIElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ extension ABI.Element.Function {

/// Encode parameters of a given contract method
/// - Parameter parameters: Parameters to pass to Ethereum contract
/// - Returns: Encoded data
/// - Returns: Encoded data
public func encodeParameters(_ parameters: [AnyObject]) -> Data? {
guard parameters.count == inputs.count,
let data = ABIEncoder.encode(types: inputs, values: parameters) else { return nil }
Expand Down Expand Up @@ -325,7 +325,7 @@ extension ABI.Element.Function {
// set a flag to detect the request succeeded
}

if returnArray.isEmpty {
if returnArray.isEmpty && !outputs.isEmpty {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I'm annoying with this but can we please make a test here. 😄

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will work on this

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wonder, does it resolved yet, @pharms-eth?

return nil
}

Expand Down
44 changes: 27 additions & 17 deletions Sources/web3swift/Operations/WriteOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,35 @@ import Core
public class WriteOperation: ReadOperation {

// FIXME: Rewrite this to CodableTransaction
/// Sends raw transaction for write operation.
/// Sends signed or unsigned transaction for write operation.
/// - Parameters:
/// - password: Password for private key.
/// - policies: Custom policies for how to resolve (optional). Default is auto.
public func writeToChain(password: String, policies: Policies = .auto) async throws -> TransactionSendingResult {
/// - password: Password for the private key in the keystore manager attached to the provider
/// you set to `web3` passed in the initializer.
/// - policies: Determining the behaviour of how transaction attributes like gas limit and
/// nonce are resolved. Default value is ``Policies/auto``.
/// - sendRaw: If set to `true` transaction will be signed and sent using `eth_sendRawTransaction`.
/// Otherwise, no signing attempts will take place and the `eth_sendTransaction` RPC will be used instead.
/// Default value is `true`.
public func writeToChain(password: String, policies: Policies = .auto, sendRaw: Bool = true) async throws -> TransactionSendingResult {
try await policyResolver.resolveAll(for: &transaction, with: policies)
if let attachedKeystoreManager = self.web3.provider.attachedKeystoreManager {
do {
try Web3Signer.signTX(transaction: &transaction,
keystore: attachedKeystoreManager,
account: transaction.from ?? transaction.sender ?? EthereumAddress.contractDeploymentAddress(),
password: password)
} catch {
throw Web3Error.inputError(desc: "Failed to locally sign a transaction")
}
guard let transactionData = transaction.encode(for: .transaction) else { throw Web3Error.dataError }
return try await web3.eth.send(raw: transactionData)

guard sendRaw else {
return try await web3.eth.send(transaction)
}

guard let attachedKeystoreManager = web3.provider.attachedKeystoreManager else {
throw Web3Error.inputError(desc: "Failed to locally sign a transaction. Web3 provider doesn't have keystore attached.")
}

do {
try Web3Signer.signTX(transaction: &transaction,
keystore: attachedKeystoreManager,
account: transaction.from ?? transaction.sender ?? EthereumAddress.contractDeploymentAddress(),
password: password)
} catch {
throw Web3Error.inputError(desc: "Failed to locally sign a transaction. \(error.localizedDescription)")
}
// MARK: Sending Data flow
return try await web3.eth.send(transaction)
guard let transactionData = transaction.encode(for: .transaction) else { throw Web3Error.dataError }
return try await web3.eth.send(raw: transactionData)
}
}
10 changes: 5 additions & 5 deletions Tests/web3swiftTests/localTests/AdvancedABIv2Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AdvancedABIv2Tests: LocalTestCase {
deployTx.transaction.from = allAddresses[0]
// MARK: Sending Data flow
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = result.hash.stripHexPrefix()

Thread.sleep(forTimeInterval: 1.0)
Expand Down Expand Up @@ -61,7 +61,7 @@ class AdvancedABIv2Tests: LocalTestCase {
let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = result.hash.stripHexPrefix()

Thread.sleep(forTimeInterval: 1.0)
Expand Down Expand Up @@ -97,7 +97,7 @@ class AdvancedABIv2Tests: LocalTestCase {
let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = result.hash.stripHexPrefix()

Thread.sleep(forTimeInterval: 1.0)
Expand Down Expand Up @@ -132,7 +132,7 @@ class AdvancedABIv2Tests: LocalTestCase {
let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = result.hash.stripHexPrefix()

Thread.sleep(forTimeInterval: 1.0)
Expand Down Expand Up @@ -168,7 +168,7 @@ class AdvancedABIv2Tests: LocalTestCase {
let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = result.hash.stripHexPrefix()

Thread.sleep(forTimeInterval: 1.0)
Expand Down
4 changes: 2 additions & 2 deletions Tests/web3swiftTests/localTests/BasicLocalNodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class BasicLocalNodeTests: LocalTestCase {
let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = result.hash.stripHexPrefix()

while true {
Expand Down Expand Up @@ -60,7 +60,7 @@ class BasicLocalNodeTests: LocalTestCase {
print("Balance before to: " + balanceBeforeTo.description)
print("Balance before from: " + balanceBeforeFrom.description)

let result = try await sendTx.writeToChain(password: "web3swift")
let result = try await sendTx.writeToChain(password: "web3swift", sendRaw: false)
let txHash = Data.fromHex(result.hash.stripHexPrefix())!

Thread.sleep(forTimeInterval: 1.0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/web3swiftTests/localTests/ERC20ClassTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ERC20ClassTests: LocalTestCase {

func testERC20tokenBalanceAndAllowance() async throws {
let (web3, _, receipt, _) = try await TestHelpers.localDeployERC20()
let erc20token = ERC20.init(web3: web3, provider: web3.provider, address: receipt.contractAddress!)
let erc20token = ERC20(web3: web3, provider: web3.provider, address: receipt.contractAddress!)

let userAddress = EthereumAddress("0xe22b8979739D724343bd002F9f432F5990879901")!

Expand Down
2 changes: 1 addition & 1 deletion Tests/web3swiftTests/localTests/LocalTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LocalTestCase: XCTestCase {
writeTX.transaction.value = value
let policies = Policies(gasLimitPolicy: .manual(78423), gasPricePolicy: .manual(20000000000))
for _ in block..<25 {
_ = try! await writeTX.writeToChain(password: "", policies: policies)
_ = try! await writeTX.writeToChain(password: "", policies: policies, sendRaw: false)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PersonalSignatureTests: XCTestCase {
let allAddresses = try await web3.eth.ownedAccounts()
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let deployResult = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let deployResult = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = Data.fromHex(deployResult.hash.stripHexPrefix())!

Thread.sleep(forTimeInterval: 1.0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/web3swiftTests/localTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TestHelpers {
parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = Data.fromHex(result.hash.stripHexPrefix())!

Thread.sleep(forTimeInterval: 1.0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/web3swiftTests/localTests/TransactionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ class TransactionsTests: XCTestCase {
writeTX.transaction.from = from
writeTX.transaction.value = value!
let policies = Policies(gasLimitPolicy: .manual(78423))
let result = try await writeTX.writeToChain(password: "", policies: policies)
let result = try await writeTX.writeToChain(password: "", policies: policies, sendRaw: false)
let txHash = Data.fromHex(result.hash.stripHexPrefix())!
print("Transaction with hash ", txHash)

Expand Down
4 changes: 2 additions & 2 deletions Tests/web3swiftTests/localTests/UserCases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class UserCases: XCTestCase {
let allAddresses = try await web3.eth.ownedAccounts()
let contract = web3.contract(Web3.Utils.estimateGasTestABI, at: nil, abiVersion: 2)!

let parameters = [] as [AnyObject]
let parameters = [AnyObject]()
let deployTx = contract.prepareDeploy(bytecode: bytecode, parameters: parameters)!
deployTx.transaction.from = allAddresses[0]
let policies = Policies(gasLimitPolicy: .manual(3000000))
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies)
let result = try await deployTx.writeToChain(password: "web3swift", policies: policies, sendRaw: false)
let txHash = Data.fromHex(result.hash.stripHexPrefix())!

Thread.sleep(forTimeInterval: 1.0)
Expand Down