
Web3.Swift是一個迅速的庫,用於簽署交易並與以太坊網絡中的智能合約進行交互。
它允許您連接到Geth或Erigon以太坊節點(如鍊節點),以發送交易和讀取智能合約的值,而無需編寫您自己的協議實現。
Web3.Swift支持Swift Package Manager,支持iOS,MACOS,TVOS,WatchOS和Linux。
檢查下面的用法或查看存儲庫測試。
Swift已經寫了一些Web3庫。我們知道他們的優點和缺點,對於我們的用例,它們只是不起作用。
Web3.swift是考慮到模塊化,可移植性,速度和效率的。
好的,謝謝您的流行語。但是這實際上是什麼意思?
Web3.swift是模塊化的。如果您安裝/使用基本的Web3 SPM產品,則可以訪問最基本的功能,例如交易簽名和與HTTP RPC服務器進行交互。
如果要添加對IPC RPC或其他內容的支持,則可以簡單地創建一個依賴於Web3庫並實現此確切的功能。稍後會有更多信息。
如果您想對Web3調用使用PromiseKit擴展名,則可以使用提供的PromiseKit SPM產品或創建自己的PromiseKit SPM產品。
如果您想方便地解析Json Abis的以太坊智能合約,則可以使用提供的ABI解析SPM產品。
最後,如果您想在尚未提供的Web3.swift中添加功能,則不必等到它合併並以版本的顛簸發布。您可以在自己的應用程序中簡單擴展/更新功能,因為我們的API非常開放以進行更改。
例如,如果要添加Web3.swift尚未提供的Web3方法(我們只支持支持INFURA支持的方法),則只需要添加3行代碼(取決於方法的輸入和輸出參數)。添加IPC RPC支持僅是實現協議和回答請求。
就像您看到的那樣, Web3.swift的一切可能。
我們開始從事此項目的主要原因之一是因為我們想在不同平台上與Swift Package Manager一起使用它。
因此, Web3.swift可通過Swift Package Manager在iOS,MACOS,TVOS,WatchOS和Linux上獲得。
注意:對於SPM,我們僅測試iOS,MACOS和正式支持的Linux發行版(當前Ubuntu 16.04和20.04),但應與所有能夠編譯Swift Compiler,Foundation,Foundation和Glibc的所有小Endian系統兼容。
我們嘗試使該庫盡可能快地嘗試提供API,從而增加您的開發工作流程,以便您可以專注於構建出色的DAPP,而不必擔心實施細節。
我們所有的API都是線程安全的,設計用於高度並發應用。
Web3與Swift Package Manager V5(Swift 5及以上)兼容。只需將其添加到Package.swift中的依賴項。
dependencies: [
. package ( url : " https://github.com/Boilertalk/Web3.swift.git " , from : " 0.6.0 " )
]然後將其添加到您的目標依賴性中:
targets: [
. target (
name : " MyProject " ,
dependencies : [
. product ( name : " Web3 " , package : " Web3.swift " ) ,
. product ( name : " Web3PromiseKit " , package : " Web3.swift " ) ,
. product ( name : " Web3ContractABI " , package : " Web3.swift " ) ,
]
) ,
. testTarget (
name : " MyProjectTests " ,
dependencies : [ " MyProject " ] )
]注意:
Web3PromiseKit和Web3ContractABI是可選的,只需將它們放入目標依賴項(如果您想使用它們)。
安裝後,您可以在.swift文件中導入Web3 。
import Web3
// Optional
import Web3PromiseKit
import Web3ContractABI由於內部決策,我們停止支持以0.5.0版本開頭的SPM以外的任何軟件包經理。
為了詳細說明這一決定:使用Xcode 11和Swift 5.1,我們與Swift Package Manager達到了一個點,它慢慢開始使其他軟件包經理無關緊要。您已經可以使用Swift Package Manager加載XCode項目中的所有依賴項。
隨著更多更新,它變得更加普遍。可可蛋白魚類和迦太基維護者對其項目失去了興趣,並停止維護該項目。有許多未解決的問題,尤其是對於具有可可錄的圖書館開發人員而言,許多問題。
這麼多麻煩,沒有真正的收益。用戶已經可以將支持SPM的依賴項放入其Xcode項目中。那為什麼要打擾呢?
答案很簡單。有些仍然使用Xcode <11,一些庫開發人員在自己的豆莢/迦太基中取決於Web3。
這個決定很難,花了一些時間。但是,在看到最後一個版本非常穩定並已經用於許多生產應用程序之後,我們決定現在開始此舉。
Xcode 10已經超過2年了。大多數已經升級的項目都沒有比Web3的問題要大得多。
圖書館的所有者根據Web3.Swift也被鼓勵放棄Cocoapods和Carthage支持。
SPM是未來。對於所有使用它的可可錄和迦太基用戶,因為許多庫還沒有切換到SPM:您仍然可以將Web3.swift作為SPM產品添加到.xcworkspace或.xcodeproj中,並將所有其他依賴性保留在Cocoapopods/Carthage中。但仍然我們鼓勵您使用盡可能多的依賴關係切換到SPM。比晚早。請參閱下一節有關如何執行此操作的部分。
使用Xcode 11或更高版本(對於iOS,MACOS或其他Apple平台),您可以非常簡單地添加SPM軟件包。
在Xcode中,從下拉列表中選擇您的項目,選擇項目,而不是單個目標,在選項卡中選擇Swift Packages 。
然後,您可以單擊 +圖標並將其放入此存儲庫中(https://github.com/boilertalk/web3.swift)。
現在,您可以選擇所有產品,然後單擊下一步直到添加依賴關係。
就是這樣。如果您推動更改,即使您的CI也不會遇到任何問題。沒有過時的規格存儲庫的麻煩,某些怪異的鏈接器錯誤沒有有時/與某些依賴關係發生的問題。
如果您需要進一步的指導,請加入我們的電報小組,我們將為您提供幫助。 https://t.me/web3_swift
使用Web3.swift您可以在服務器上使用以太坊節點與以太坊進行通信。
您可以發送簽名的交易,讀取合同數據,呼叫合同功能等等。
所有可用方法的基類是Web3 。例如,您可以將其與HTTP提供商進行實例化:
let web3 = Web3 ( rpcURL : " https://mainnet.infura.io/<your_infura_id> " )所有web3_方法均可直接從Web3 struct獲得。 net_方法可在web3結構中的net結構下找到。 eth_方法可在web3結構中的eth結構下找到。
請參閱下面的示例
注意:要工作的示例,您需要首先導入Web3和PromiseKit
返回當前客戶端版本。
參數
沒有任何
返回
String - 當前客戶版本
firstly {
web3 . clientVersion ( )
} . done { version in
print ( version )
} . catch { error in
print ( " Error " )
} 返回當前網絡ID。
參數
沒有任何
返回
String - 當前網絡ID
firstly {
web3 . net . version ( )
} . done { version in
print ( version )
} . catch { error in
print ( " Error " )
} 返回當前連接到客戶端的同行數量。
參數
沒有任何
返回
EthereumQuantity - 連接的同齡人數量的巨大。
firstly {
web3 . net . peerCount ( )
} . done { ethereumQuantity in
print ( ethereumQuantity . quantity )
} . catch { error in
print ( " Error " )
} 創建新消息呼叫交易或為簽名交易創建合同。
參數
EthereumTransaction :簽名的交易返回
EthereumData ,32個字節 - 交易哈希或零哈希(如果尚未可用)
要發送一些ETH,您首先需要獲得發件人(NONCE)的當前交易計數,創建交易,簽名然後發送。
let privateKey = try ! EthereumPrivateKey ( hexPrivateKey : " 0xa26da69ed1df3ba4bb2a231d506b711eace012f1bd2571dfbfff9650b03375af " )
firstly {
web3 . eth . getTransactionCount ( address : privateKey . address , block : . latest )
} . then { nonce in
let tx = try EthereumTransaction (
nonce : nonce ,
gasPrice : EthereumQuantity ( quantity : 21 . gwei ) ,
gas : 21000 ,
to : EthereumAddress ( hex : " 0xC0866A1a0ed41e1aa75c932cA3c55fad847fd90D " , eip55 : true ) ,
value : EthereumQuantity ( quantity : 1 . eth )
)
return try tx . sign ( with : privateKey , chainId : 1 ) . promise
} . then { tx in
web3 . eth . sendRawTransaction ( transaction : tx )
} . done { hash in
print ( hash )
} . catch { error in
print ( error )
} firstly {
web3 . eth . getBlockTransactionCountByNumber ( block : . block ( 5397389 ) )
} . done { count in
print ( count ) // 88
} . catch { error in
print ( error )
} 有關更多示例,請閱讀我們的測試用例,即Web3結構或官方的Ethereum JSON RPC文檔。
我們提供了一個可選的模塊,用於與智能合約互動。要使用它,您必須將Web3ContractABI添加到Podfile(SPM)中的目標依賴項中。確保首先檢查安裝說明。
我們提供了兩個不同的選項,以在Swift中創建合同ABI接口。您要么手動定義功能和事件(或使用我們提供的界面之一,例如ERC20或ERC721)。或者,您就像在web3.js中一樣從JSON ABI代表中解析它們。
靜態合同是實施StaticContract類。他們提供了一套要從原始智能合約中使用的功能和事件。查看我們提供的靜態合同作為起點(ERC20或ERC721)。
我們的靜態ERC20接口稱為GenericERC20Contract ,ERC721合同稱為GenericERC721Contract 。兩者都可以分類以添加自定義合同的更多功能。
使用這些StaticContract類型,您可以像以下示例中創建和使用合同(我們在示例中再次使用PromiseKit)。
let web3 = Web3 ( rpcURL : " https://mainnet.infura.io/<your_infura_id> " )
let contractAddress = try EthereumAddress ( hex : " 0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0 " , eip55 : true )
let contract = web3 . eth . Contract ( type : GenericERC20Contract . self , address : contractAddress )
// Get balance of some address
firstly {
try contract . balanceOf ( address : EthereumAddress ( hex : " 0x3edB3b95DDe29580FFC04b46A68a31dD46106a4a " , eip55 : true ) ) . call ( )
} . done { outputs in
print ( outputs [ " _balance " ] as? BigUInt )
} . catch { error in
print ( error )
}
// Send some tokens to another address (locally signing the transaction)
let myPrivateKey = try EthereumPrivateKey ( hexPrivateKey : " ... " )
firstly {
web3 . eth . getTransactionCount ( address : myPrivateKey . address , block : . latest )
} . then { nonce in
try contract . transfer ( to : EthereumAddress ( hex : " 0x3edB3b95DDe29580FFC04b46A68a31dD46106a4a " , eip55 : true ) , value : 100000 ) . createTransaction (
nonce : nonce ,
gasPrice : EthereumQuantity ( quantity : 21 . gwei ) ,
maxFeePerGas : nil ,
maxPriorityFeePerGas : nil ,
gasLimit : 100000 ,
from : myPrivateKey . address ,
value : 0 ,
accessList : [ : ] ,
transactionType : . legacy
) ! . sign ( with : myPrivateKey ) . promise
} . then { tx in
web3 . eth . sendRawTransaction ( transaction : tx )
} . done { txHash in
print ( txHash )
} . catch { error in
print ( error )
}
// Send some tokens to another address (signing will be done by the node)
let myAddress = try EthereumAddress ( hex : " 0x1f04ef7263804fafb839f0d04e2b5a6a1a57dc60 " , eip55 : true )
firstly {
web3 . eth . getTransactionCount ( address : myAddress , block : . latest )
} . then { nonce in
try contract . transfer ( to : EthereumAddress ( hex : " 0x3edB3b95DDe29580FFC04b46A68a31dD46106a4a " , eip55 : true ) , value : 100000 ) . send (
nonce : nonce ,
gasPrice : EthereumQuantity ( quantity : 21 . gwei ) ,
maxFeePerGas : nil ,
maxPriorityFeePerGas : nil ,
gasLimit : 150000 ,
from : myAddress ,
value : 0 ,
accessList : [ : ] ,
transactionType : . legacy
)
} . done { txHash in
print ( txHash )
} . catch { error in
print ( error )
}通過創建自己的界面,您可以與任何智能合約進行交互!
如果您只能訪問智能合約的JSON ABI或不想創建靜態模板,則可以使用我們的動態合同API將JSON字符串解析為運行時的可用合同。請參見下面的示例。
let web3 = Web3 ( rpcURL : " https://mainnet.infura.io/<your_infura_id> " )
let contractAddress = try EthereumAddress ( hex : " 0x86fa049857e0209aa7d9e616f7eb3b3b78ecfdb0 " , eip55 : true )
let contractJsonABI = " <your contract ABI as a JSON string> " . data ( using : . utf8 ) !
// You can optionally pass an abiKey param if the actual abi is nested and not the top level element of the json
let contract = try web3 . eth . Contract ( json : contractJsonABI , abiKey : nil , address : contractAddress )
print ( contract . methods . count )
// Get balance of some address
firstly {
try contract [ " balanceOf " ] ! ( EthereumAddress ( hex : " 0x3edB3b95DDe29580FFC04b46A68a31dD46106a4a " , eip55 : true ) ) . call ( )
} . done { outputs in
print ( outputs [ " _balance " ] as? BigUInt )
} . catch { error in
print ( error )
}
// Send some tokens to another address (locally signing the transaction)
let myPrivateKey = try EthereumPrivateKey ( hexPrivateKey : " ... " )
guard let transaction = contract [ " transfer " ] ? ( EthereumAddress . testAddress , BigUInt ( 100000 ) ) . createTransaction (
nonce : 0 ,
gasPrice : EthereumQuantity ( quantity : 21 . gwei ) ,
maxFeePerGas : nil ,
maxPriorityFeePerGas : nil ,
gasLimit : 150000 ,
from : myPrivateKey . address ,
value : 0 ,
accessList : [ : ] ,
transactionType : . legacy
) ) else {
return
}
let signedTx = try transaction . sign ( with : myPrivateKey )
firstly {
web3 . eth . sendRawTransaction ( transaction : signedTx )
} . done { txHash in
print ( txHash )
} . catch { error in
print ( error )
}使用此API,您可以與以太坊網絡中的任何智能合約進行交互!
有關更多示例,包括創建合同(構造函數),請查看我們的測試。
如果從JSON解析ABI時遇到此錯誤,則可能是因為您的合同具有後備功能。要解決它,請刪除具有有關後備功能的信息的ABI片段。您應該刪除的零件可能看起來像:
{
"payable": false,
"stateMutability": "nonpayable",
"type": "fallback"
},
在我們達到1.0.0版本之前,我們的API會受到次要版本跳躍之間的破壞變化。這是為了確保我們可以專注於在進行繁重發展時提供最佳的實施,而不是試圖維護棄用的東西。
話雖如此,我們將嘗試最大程度地減少破裂變化。最肯定的是不會有很多。
Boilertalk的真棒傢伙⚗️
...還有更多來自社區的很棒的成員?
查看貢獻者列表中的完整列表。
Web3可根據MIT許可證獲得。有關更多信息,請參見許可證文件。