perun-network / erdstall-ts-sdk

TypeScript client SDK to interact with Erdstall.
Apache License 2.0
5 stars 2 forks source link

Usage Documentation and helper functions #144

Open DragonDev1906 opened 2 years ago

DragonDev1906 commented 2 years ago

While informative, the current README.md does not explain much about using the SDK. https://github.com/perun-network/erdstall-ts-sdk/blob/main/src/e2e/sdk_actions.ts does explain most of the things below, but it is not linked from the README.md and not in an easy-to-find place, thus I have not found it while looking how to implement an ETH deposit.

async function balanceOf(erdstall, address, token) {
        let accountData = await erdstall.getAccount(address)
        let amount = accountData.values.values.get(token.toString())
        if (amount === undefined)
                return 0
        else
                return amount.value
}

A different point, but it might be useful to (in addition to client.on and client.once) have the ability to just delay execution until an event happened, instead of having to use a callback with client.once: let receipt = await client.waitForEvent("receipt"). As shown in the example below this is possible but not as nice to use/must be implemented if needed. Maybe even add an optional callback that is called to see if the given event value/object is what the user is waiting for. This would make chaining multiple calls (e.g. an offChainTransfer and getAccount calls) using async/await easier. (it could also be that waiting for the receipt is not needed here, I'm not sure)

// await erdstallEvent(erdstall, "receipt")
async function erdstallEvent(erdstall, name) {
        return new Promise((resolve, reject) => {
                try {
                        erdstall.once(name, x => {
                                resolve(x)
                        })
                } catch (error) {
                        reject(error)
                }
        })
}

// Example usage
async function run(erdstall) {
        await deposit(erdstall, ETH_TOKEN, BigInt(100))
        await printBalanceOf(erdstall, address, ETH_TOKEN, "WEI")
        await printBalanceOf(erdstall, address2, ETH_TOKEN, "WEI")

        // Make an off-chain transaction, waiting for offChainTransferTo (erdstall.transferTo) to finish, i.e. getting the receipt does not wait long enough for the upcomming printBalanceOf calls to get the new balances. Though waiting on any receipt probably isn't a good solution.
        await offChainTransferTo(erdstall, address2, ETH_TOKEN, 50)
        await erdstallEvent(erdstall, "receipt")

        // Print balances again
        await printBalanceOf(erdstall, address, ETH_TOKEN, "WEI")
        await printBalanceOf(erdstall, address2, ETH_TOKEN, "WEI")
}

async function offChainTransferTo(erdstall, toAddress, token, amount) {
        const assets = new Assets({
                token: token,
                asset: new Amount(amount)
        })
        const receipt = await (erdstall.transferTo(assets, toAddress)).receipt
        return receipt
}
RmbRT commented 1 year ago

Await events via await client.next("event-name", [timeout]).