Skip to content
This repository has been archived by the owner on Jun 14, 2018. It is now read-only.

Setting global, contract level and instance level defaults

Tim Coulter edited this page Jun 24, 2015 · 3 revisions

Continuing with the MetaCoin example from the README:

# Set a default gasLimit for all transactions across all Pudding contracts.
# This is an example of global defaults.
Pudding.defaults({gasLimit: 3141592})

# Create two MetaCoin classes, one defaulting to transactions from "Jim" 
# and the other defaulting to transactions from "Tom". 
# These are examples of class-level defaults.
JimsCoin = Pudding.whisk(metaCoinABI, {from: "5b42bd01ff..."})
TomsCoin = Pudding.whisk(metaCoinABI, {from: "e1fd0d4a52..."})

jims = JimsCoin.at(contract_address)

# This transaction would have default transaction values of:
#   gasLimit: 3141592 (global default)
#   from: 5b42bd01ff... (class-level default)
jims.sendCoin("e1fd0d4a52...", 15).then (tx) ->
   # Do something...

# Maybe the contract requires Jim sends a value of 1000 wei with every 
# transaction. This is an example of instance-level defaults.
jims = JimsCoin.at(contract_address, {value: 1000})

# This transaction would have default transaction values of:
#   gasLimit: 3141592 (global default)
#   from: 5b42bd01ff... (class-level default)
#   value: 1000 (instance-level default)
jims.sendCoin("e1fd0d4a52...", 15).then (tx) ->
   # Do something...

# Finally, any default can be overridden by the transaction itself:
jims.sendCoin("3eb62dd932...", 1, {
  from: "abcde12345..."
  value: 5
  gasLimit: 100000
})