Skip to content

Commit a56ccf0

Browse files
committed
Add YuKirasawa 07.16 notes
1 parent 48ab57b commit a56ccf0

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

YuKirasawa_WICL1st.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,56 @@ npx web3curl -v 'web3://w3url.eth'
133133

134134
### 07.16
135135

136-
XXX
136+
- 今日学习时间:1 h
137+
- 学习内容小结:进一步了解 web3 url 访问合约的接口
138+
139+
当合约没有专门实现 `web3://` 协议的接口时,`web3://` 协议的路径表示了对合约方法的调用,这种模式称为 auto mode. Auto mode 中路径的格式为
140+
141+
```
142+
/<methodName>/<methodArg1>/<methodArg2>/...[?returns=(<type1>,<type2>,...)]
143+
```
144+
145+
例如 `web3://0x4e1f41613c9084fdb9e34e11fae9412427480e56/tokenHTML/9352` 表示访问 `0x4e1f41613c9084fdb9e34e11fae9412427480e56` 合约的 `tokenHTML` 方法,参数为 `9352`. 使用 web3curl 也可以看出解析该地址的过程
146+
147+
```
148+
* Resolve mode: auto
149+
*
150+
* Path parsing...
151+
* Contract call mode: method
152+
* Method name: tokenHTML
153+
* Method arguments types: [{"type":"uint256"}]
154+
* Method arguments values: ["0x2488"]
155+
* Contract return processing: decodeABIEncodedBytes
156+
* Contract return processing: decodeABIEncodedBytes: MIME type: (not set)
157+
```
158+
159+
对于合约方法的参数,可以使用 `[<type>!]<value>` 的方式指定参数类型,比如 `web3://0x36f379400de6c6bcdf4408b282f8b685c56adc60/getIdToSVG/uint8!42` 表示参数的类型为 `uint8`,值为 42。使用 web3curl 可以看出参数类型确实是 `uint8`
160+
161+
```
162+
* Resolve mode: auto
163+
*
164+
* Path parsing...
165+
* Contract call mode: method
166+
* Method name: getIdToSVG
167+
* Method arguments types: [{"type":"uint8"}]
168+
* Method arguments values: ["0x2a"]
169+
* Contract return processing: decodeABIEncodedBytes
170+
* Contract return processing: decodeABIEncodedBytes: MIME type: (not set)
171+
```
172+
173+
默认情况下,合约方法的返回值会被解析为 bytes,如果合约方法使用了其他类型的返回值,则需要设置 `?returns=` 字段,例如 `web3://0xA5aFC9fE76a28fB12C60954Ed6e2e5f8ceF64Ff2/levelAndTile/2/50?returns=(uint256,uint256)`
174+
175+
合约也可以专门实现 `web3://` 协议的接口,通过 `resolveMode` 方法来声明
176+
177+
```
178+
function resolveMode() external pure returns (bytes32) {
179+
return "manual";
180+
}
181+
```
182+
183+
这种模式就是 manual mode.
184+
185+
在 Manual mode 中,路径信息会传入合约的 `fallback(bytes)` 方法,从而允许合约使用自定义的方式处理请求。这种模式和 web2 的 url 路由是一致的。
137186

138187
### 07.17
139188

0 commit comments

Comments
 (0)